From c032f55d2e140f4654815d3c21c8f5c4d2fc1154 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 10 Feb 2023 16:20:43 +0100 Subject: [PATCH 1/3] Fix REntityServer click listener --- .../src/de/steamwar/entity/REntityServer.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java index ecc59c7..df18398 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java +++ b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java @@ -47,8 +47,17 @@ public class REntityServer implements Listener { private static final HashSet emptyPlayers = new HashSet<>(0); private static final Class useEntity = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity"); + private static final Class useEntityEnumAction = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity$EnumEntityUseAction"); 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 static final Reflection.FieldAccessor useEntityAction = (Reflection.FieldAccessor) Reflection.getField(useEntity, useEntityEnumAction, 0); + private static final Reflection.MethodInvoker useEntityGetAction; + static { + if (useEntityEnumAction.isEnum()) { + useEntityGetAction = null; + } else { + useEntityGetAction = Reflection.getMethod(useEntity, "a"); + } + } private final ConcurrentHashMap entityMap = new ConcurrentHashMap<>(); private final HashMap> entities = new HashMap<>(); @@ -64,7 +73,15 @@ public class REntityServer implements Listener { REntity entity = entityMap.get(target); if (entity == null) return packet; - EntityAction action = useEntityAction.get(packet) == 1 ? EntityAction.ATTACK : EntityAction.INTERACT; + Object entityAction = useEntityAction.get(packet); + int entityActionOrdinal; + if (useEntityEnumAction.isEnum()) { + entityActionOrdinal = ((Enum) entityAction).ordinal(); + } else { + entityActionOrdinal = ((Enum) useEntityGetAction.invoke(entityAction)).ordinal(); + } + + EntityAction action = entityActionOrdinal == 1 ? EntityAction.ATTACK : EntityAction.INTERACT; Bukkit.getScheduler().runTask(Core.getInstance(), () -> { callback.onAction(player, entity, action); }); From 833e6f58184880317e5ad4c338f96af8ee17030e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 23 Feb 2023 11:03:36 +0100 Subject: [PATCH 2/3] Fix stuff from merge --- SpigotCore_Main/src/de/steamwar/entity/REntityServer.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java index df18398..f40e3a9 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java +++ b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java @@ -22,6 +22,7 @@ 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; @@ -93,8 +94,11 @@ public class REntityServer implements Listener { } public void setCallback(EntityActionListener callback) { + boolean uninitialized = this.callback == null; this.callback = callback; - TinyProtocol.instance.addFilter(useEntity, filter); + + if(uninitialized) + TinyProtocol.instance.addFilter(useEntity, filter); } public void addPlayer(Player player) { @@ -261,7 +265,7 @@ public class REntityServer implements Listener { } private int viewRadius(Player player) { - return player.getClientViewDistance() / 2; + return FlatteningWrapper.impl.getViewDistance(player) / 2; } private long entityToId(REntity entity) { From ad9b2465ee7140696b56eb1710b2205e2b032642 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 23 Feb 2023 16:59:36 +0100 Subject: [PATCH 3/3] Simplify REntityFilter --- .../src/de/steamwar/entity/REntityServer.java | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java index f40e3a9..7c60ad4 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java +++ b/SpigotCore_Main/src/de/steamwar/entity/REntityServer.java @@ -40,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,15 +49,16 @@ public class REntityServer implements Listener { private static final HashSet emptyPlayers = new HashSet<>(0); private static final Class useEntity = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity"); - private static final Class useEntityEnumAction = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseEntity$EnumEntityUseAction"); private static final Reflection.FieldAccessor useEntityTarget = Reflection.getField(useEntity, int.class, 0); - private static final Reflection.FieldAccessor useEntityAction = (Reflection.FieldAccessor) Reflection.getField(useEntity, useEntityEnumAction, 0); - private static final Reflection.MethodInvoker useEntityGetAction; + 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 getEntityAction; static { - if (useEntityEnumAction.isEnum()) { - useEntityGetAction = null; + if(Core.getVersion() > 15) { + Reflection.MethodInvoker useEntityGetAction = Reflection.getMethod(useEntity, "a"); + getEntityAction = value -> ((Enum) useEntityGetAction.invoke(value)).ordinal(); } else { - useEntityGetAction = Reflection.getMethod(useEntity, "a"); + getEntityAction = value -> ((Enum) value).ordinal(); } } @@ -68,24 +70,13 @@ public class REntityServer implements Listener { private EntityActionListener callback = null; - private BiFunction 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 filter = (player, packet) -> { + REntity entity = entityMap.get(useEntityTarget.get(packet)); + if (entity == null) + return packet; - Object entityAction = useEntityAction.get(packet); - int entityActionOrdinal; - if (useEntityEnumAction.isEnum()) { - entityActionOrdinal = ((Enum) entityAction).ordinal(); - } else { - entityActionOrdinal = ((Enum) useEntityGetAction.invoke(entityAction)).ordinal(); - } - - EntityAction action = entityActionOrdinal == 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; };