SteamWar/SpigotCore
Archiviert
13
0

Merge pull request 'Fix REntityServer click listener' (#230) from REntityCallback into master
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Reviewed-on: #230
Reviewed-by: Lixfel <lixfel@steamwar.de>
Dieser Commit ist enthalten in:
Lixfel 2023-02-23 17:02:26 +01:00
Commit ff47c6ea0d

Datei anzeigen

@ -23,6 +23,7 @@ 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;
@ -39,6 +40,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
public class REntityServer implements Listener {
@ -48,7 +50,17 @@ public class REntityServer implements Listener {
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 static final Class<?> useEntityEnumAction = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity$EnumEntityUseAction");
private static final Reflection.FieldAccessor<?> useEntityAction = Reflection.getField(useEntity, useEntityEnumAction, 0);
private static final Function<Object, Integer> getEntityAction;
static {
if(Core.getVersion() > 15) {
Reflection.MethodInvoker useEntityGetAction = Reflection.getMethod(useEntity, "a");
getEntityAction = value -> ((Enum<?>) useEntityGetAction.invoke(value)).ordinal();
} else {
getEntityAction = value -> ((Enum<?>) value).ordinal();
}
}
private final ConcurrentHashMap<Integer, REntity> entityMap = new ConcurrentHashMap<>();
private final HashMap<Long, HashSet<REntity>> entities = new HashMap<>();
@ -58,16 +70,13 @@ public class REntityServer implements Listener {
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;
private final BiFunction<Player, Object, Object> filter = (player, packet) -> {
REntity entity = entityMap.get(useEntityTarget.get(packet));
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);
});*/
EntityAction action = getEntityAction.apply(useEntityAction.get(packet)) == 1 ? EntityAction.ATTACK : EntityAction.INTERACT;
Bukkit.getScheduler().runTask(Core.getInstance(), () -> callback.onAction(player, entity, action));
return null;
};