|
|
|
@ -19,9 +19,11 @@
|
|
|
|
|
|
|
|
|
|
package de.steamwar.entity;
|
|
|
|
|
|
|
|
|
|
import com.comphenix.tinyprotocol.Reflection;
|
|
|
|
|
import com.comphenix.tinyprotocol.TinyProtocol;
|
|
|
|
|
import de.steamwar.core.Core;
|
|
|
|
|
import de.steamwar.core.FlatteningWrapper;
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
import org.bukkit.Location;
|
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
@ -34,7 +36,9 @@ import org.bukkit.event.player.PlayerQuitEvent;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
import java.util.function.BiConsumer;
|
|
|
|
|
import java.util.function.BiFunction;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -43,15 +47,43 @@ public class REntityServer implements Listener {
|
|
|
|
|
private static final HashSet<REntity> emptyEntities = new HashSet<>(0);
|
|
|
|
|
private static final HashSet<Player> emptyPlayers = new HashSet<>(0);
|
|
|
|
|
|
|
|
|
|
private static final Class<?> useEntity = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity");
|
|
|
|
|
private static final Reflection.FieldAccessor<Integer> useEntityTarget = Reflection.getField(useEntity, int.class, 0);
|
|
|
|
|
private static final Reflection.FieldAccessor<Integer> useEntityAction = Reflection.getField(useEntity, int.class, 1);
|
|
|
|
|
|
|
|
|
|
private final ConcurrentHashMap<Integer, REntity> entityMap = new ConcurrentHashMap<>();
|
|
|
|
|
private final HashMap<Long, HashSet<REntity>> entities = new HashMap<>();
|
|
|
|
|
private final HashMap<Long, Set<Player>> players = new HashMap<>();
|
|
|
|
|
private final HashMap<Player, Location> lastLocation = new HashMap<>();
|
|
|
|
|
private final HashMap<Player, Integer> viewDistance = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
private EntityActionListener callback = null;
|
|
|
|
|
|
|
|
|
|
private BiFunction<Player, Object, Object> filter = (player, packet) -> {
|
|
|
|
|
if (callback == null) return packet;
|
|
|
|
|
int target = useEntityTarget.get(packet);
|
|
|
|
|
REntity entity = entityMap.get(target);
|
|
|
|
|
if (entity == null) return packet;
|
|
|
|
|
|
|
|
|
|
EntityAction action = useEntityAction.get(packet) == 1 ? EntityAction.ATTACK : EntityAction.INTERACT;
|
|
|
|
|
Bukkit.getScheduler().runTask(Core.getInstance(), () -> {
|
|
|
|
|
callback.onAction(player, entity, action);
|
|
|
|
|
});
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public REntityServer() {
|
|
|
|
|
Core.getInstance().getServer().getPluginManager().registerEvents(this, Core.getInstance());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCallback(EntityActionListener callback) {
|
|
|
|
|
boolean uninitialized = this.callback == null;
|
|
|
|
|
this.callback = callback;
|
|
|
|
|
|
|
|
|
|
if(uninitialized)
|
|
|
|
|
TinyProtocol.instance.addFilter(useEntity, filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addPlayer(Player player) {
|
|
|
|
|
Location location = player.getLocation();
|
|
|
|
|
lastLocation.put(player, location);
|
|
|
|
@ -65,6 +97,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);
|
|
|
|
|
}
|
|
|
|
@ -72,6 +105,7 @@ public class REntityServer implements Listener {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addEntity(REntity entity) {
|
|
|
|
|
entityMap.put(entity.entityId, entity);
|
|
|
|
|
addEntityToChunk(entity);
|
|
|
|
|
entity.spawn(packet -> updateEntity(entity, packet));
|
|
|
|
|
}
|
|
|
|
@ -105,6 +139,7 @@ public class REntityServer implements Listener {
|
|
|
|
|
void removeEntity(REntity entity) {
|
|
|
|
|
entity.despawn(packet -> updateEntity(entity, packet));
|
|
|
|
|
removeEntityFromChunk(entity);
|
|
|
|
|
entityMap.remove(entity.entityId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addEntityToChunk(REntity entity) {
|
|
|
|
@ -227,4 +262,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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|