From 740ebc0c1df756181c8e433b6f713d2bf13adb95 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 3 Feb 2023 17:59:58 +0100 Subject: [PATCH] Add click callback for REntityServer --- .../src/de/steamwar/entity/REntityServer.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java index 70a5fee..5359b4a 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java +++ b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java @@ -19,8 +19,10 @@ package de.steamwar.entity; +import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.core.Core; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -33,7 +35,9 @@ import org.bukkit.event.player.PlayerQuitEvent; import java.util.HashMap; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; +import java.util.function.BiFunction; import java.util.function.Consumer; @@ -42,13 +46,41 @@ public class REntityServer implements Listener { private static final HashSet emptyEntities = new HashSet<>(0); private static final HashSet emptyPlayers = new HashSet<>(0); + private static final Class useEntity = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity"); + private static final Reflection.FieldAccessor useEntityTarget = Reflection.getField(useEntity, int.class, 0); + private static final Reflection.FieldAccessor useEntityAction = Reflection.getField(useEntity, int.class, 1); + + private final HashMap entityMap = new HashMap<>(); private final HashMap> entities = new HashMap<>(); private final HashMap> players = new HashMap<>(); private final HashMap lastLocation = new HashMap<>(); private final HashMap viewDistance = new HashMap<>(); + private EntityActionListener callback = null; + + private BiFunction filter = (player, packet) -> { + if (callback == null) return packet; + int target = useEntityTarget.get(packet); + if (!entityMap.containsKey(target)) return packet; + + REntity entity = entityMap.get(target); + int action = useEntityAction.get(packet); + if (action == 2) action = 0; + int finalAction = action; + + Bukkit.getScheduler().runTaskLater(Core.getInstance(), () -> { + callback.onAction(player, entity, finalAction == 0 ? EntityAction.INTERACT : EntityAction.ATTACK); + }, 0); + return null; + }; + public REntityServer() { Core.getInstance().getServer().getPluginManager().registerEvents(this, Core.getInstance()); + TinyProtocol.instance.addFilter(useEntity, filter); + } + + public void setCallback(EntityActionListener callback) { + this.callback = callback; } public void addPlayer(Player player) { @@ -64,6 +96,7 @@ public class REntityServer implements Listener { } public void close() { + TinyProtocol.instance.removeFilter(useEntity, filter); for(Player player : lastLocation.keySet().toArray(new Player[0])) { removePlayer(player); } @@ -71,6 +104,7 @@ public class REntityServer implements Listener { } void addEntity(REntity entity) { + entityMap.put(entity.entityId, entity); addEntityToChunk(entity); entity.spawn(packet -> updateEntity(entity, packet)); } @@ -104,6 +138,7 @@ public class REntityServer implements Listener { void removeEntity(REntity entity) { entity.despawn(packet -> updateEntity(entity, packet)); removeEntityFromChunk(entity); + entityMap.put(entity.entityId, entity); } private void addEntityToChunk(REntity entity) { @@ -226,4 +261,13 @@ public class REntityServer implements Listener { private long chunkToId(int x, int z) { return ((long) x << 32) + z; } + + public enum EntityAction { + INTERACT, + ATTACK, + } + + public interface EntityActionListener { + void onAction(Player player, REntity entity, EntityAction action); + } }