SteamWar/SpigotCore
Archiviert
13
0

Fix REntityServer click listener #230

Zusammengeführt
Lixfel hat 4 Commits von REntityCallback nach master 2023-02-23 17:02:28 +01:00 zusammengeführt
Nur Änderungen aus Commit ad9b2465ee werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -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<Player> 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<Integer> useEntityTarget = Reflection.getField(useEntity, int.class, 0);
private static final Reflection.FieldAccessor<Object> useEntityAction = (Reflection.FieldAccessor<Object>) 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<Object, Integer> 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<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;
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;
};