From 895a4ea9379d584e138b380c5567a723c616de77 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Fri, 21 Apr 2023 14:35:39 +0100 Subject: [PATCH] fix potential injection fail, more refactoring, some documentation --- .../github/znetworkw/znpcservers/npc/NPC.java | 36 ++--- .../znetworkw/znpcservers/npc/NPCType.java | 10 +- .../npc/function/GlowFunction.java | 10 +- .../znpcservers/npc/hologram/Hologram.java | 18 +-- .../znpcservers/npc/packet/Packet.java | 32 ++--- .../znpcservers/npc/packet/PacketV16.java | 4 +- .../znpcservers/npc/packet/PacketV17.java | 6 +- .../znpcservers/npc/packet/PacketV18.java | 4 +- .../znpcservers/npc/packet/PacketV19.java | 6 +- .../znpcservers/npc/packet/PacketV8.java | 22 ++-- .../znpcservers/npc/packet/PacketV9.java | 14 +- ...ClassCache.java => EnumPropertyCache.java} | 7 +- .../reflection/ReflectionBuilder.java | 2 +- ...lection.java => ReflectionLazyLoader.java} | 22 ++-- .../reflection/ReflectionPackage.java | 11 ++ ...{ReflectionCache.java => Reflections.java} | 124 +++++++++--------- .../reflection/types/ClassReflection.java | 6 +- .../types/ConstructorReflection.java | 8 +- .../reflection/types/EnumReflection.java | 12 +- .../reflection/types/FieldReflection.java | 12 +- .../reflection/types/MethodReflection.java | 10 +- .../znetworkw/znpcservers/user/ZUser.java | 20 +-- .../znetworkw/znpcservers/utility/Utils.java | 4 +- 23 files changed, 212 insertions(+), 188 deletions(-) rename src/main/java/io/github/znetworkw/znpcservers/reflection/{ClassCache.java => EnumPropertyCache.java} (81%) rename src/main/java/io/github/znetworkw/znpcservers/reflection/{BaseReflection.java => ReflectionLazyLoader.java} (66%) rename src/main/java/io/github/znetworkw/znpcservers/reflection/{ReflectionCache.java => Reflections.java} (73%) diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java b/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java index a4232af..c7d44e5 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java @@ -5,7 +5,7 @@ import com.mojang.authlib.GameProfile; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; import io.github.znetworkw.znpcservers.UnexpectedCallException; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.npc.conversation.ConversationModel; import io.github.znetworkw.znpcservers.npc.hologram.Hologram; import io.github.znetworkw.znpcservers.npc.packet.PacketCache; @@ -137,8 +137,8 @@ public class NPC { if (updateTime) this.lastMove = System.nanoTime(); this.npcPojo.setLocation(new ZLocation(location = new Location(location.getWorld(), location.getBlockX() + 0.5D, location.getY(), location.getBlockZ() + 0.5D, location.getYaw(), location.getPitch()))); } - ReflectionCache.SET_LOCATION_METHOD.get().invoke(this.nmsEntity, location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); - Object npcTeleportPacket = ReflectionCache.PACKET_PLAY_OUT_ENTITY_TELEPORT_CONSTRUCTOR.get().newInstance(this.nmsEntity); + Reflections.SET_LOCATION_METHOD.get().invoke(this.nmsEntity, location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); + Object npcTeleportPacket = Reflections.PACKET_PLAY_OUT_ENTITY_TELEPORT_CONSTRUCTOR.get().newInstance(this.nmsEntity); this.viewers.forEach(player -> Utils.sendPackets(player, npcTeleportPacket)); this.hologram.setLocation(location, this.npcPojo.getNpcType().getHoloHeight()); } catch (ReflectiveOperationException operationException) { @@ -157,13 +157,13 @@ public class NPC { public void setSecondLayerSkin() { try { - Object dataWatcherObject = ReflectionCache.GET_DATA_WATCHER_METHOD.get().invoke(this.nmsEntity); + Object dataWatcherObject = Reflections.GET_DATA_WATCHER_METHOD.get().invoke(this.nmsEntity); if (Utils.versionNewer(9)) { - ReflectionCache.SET_DATA_WATCHER_METHOD.get().invoke(dataWatcherObject, - ReflectionCache.DATA_WATCHER_OBJECT_CONSTRUCTOR.get() - .newInstance(this.npcSkin.getLayerIndex(), ReflectionCache.DATA_WATCHER_REGISTER_FIELD.get()), (byte) 127); + Reflections.SET_DATA_WATCHER_METHOD.get().invoke(dataWatcherObject, + Reflections.DATA_WATCHER_OBJECT_CONSTRUCTOR.get() + .newInstance(this.npcSkin.getLayerIndex(), Reflections.DATA_WATCHER_REGISTER_FIELD.get()), (byte) 127); } else { - ReflectionCache.WATCH_DATA_WATCHER_METHOD.get().invoke(dataWatcherObject, 10, (byte) 127); + Reflections.WATCH_DATA_WATCHER_METHOD.get().invoke(dataWatcherObject, 10, (byte) 127); } } catch (ReflectiveOperationException operationException) { throw new UnexpectedCallException(operationException); @@ -173,24 +173,24 @@ public class NPC { public synchronized void changeType(NPCType npcType) { deleteViewers(); try { - Object nmsWorld = ReflectionCache.GET_HANDLE_WORLD_METHOD.get().invoke(getLocation().getWorld()); + Object nmsWorld = Reflections.GET_HANDLE_WORLD_METHOD.get().invoke(getLocation().getWorld()); boolean isPlayer = (npcType == NPCType.PLAYER); this.nmsEntity = isPlayer ? this.packets.getProxyInstance().getPlayerPacket(nmsWorld, this.gameProfile) : (Utils.versionNewer(14) ? npcType.getConstructor().newInstance(npcType.getNmsEntityType(), nmsWorld) : npcType.getConstructor().newInstance(nmsWorld)); - this.bukkitEntity = ReflectionCache.GET_BUKKIT_ENTITY_METHOD.get().invoke(this.nmsEntity); - this.uuid = (UUID) ReflectionCache.GET_UNIQUE_ID_METHOD.get().invoke(this.nmsEntity, new Object[0]); + this.bukkitEntity = Reflections.GET_BUKKIT_ENTITY_METHOD.get().invoke(this.nmsEntity); + this.uuid = (UUID) Reflections.GET_UNIQUE_ID_METHOD.get().invoke(this.nmsEntity, new Object[0]); if (isPlayer) { try { - this.tabConstructor = ReflectionCache.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(ReflectionCache.ADD_PLAYER_FIELD.get(), Collections.singletonList(this.nmsEntity)); + this.tabConstructor = Reflections.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(Reflections.ADD_PLAYER_FIELD.get(), Collections.singletonList(this.nmsEntity)); } catch (Throwable e) { - this.tabConstructor = ReflectionCache.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(ReflectionCache.ADD_PLAYER_FIELD.get(), this.nmsEntity); - this.updateTabConstructor = ReflectionCache.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(ReflectionCache.UPDATE_LISTED_FIELD.get(), this.nmsEntity); + this.tabConstructor = Reflections.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(Reflections.ADD_PLAYER_FIELD.get(), this.nmsEntity); + this.updateTabConstructor = Reflections.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(Reflections.UPDATE_LISTED_FIELD.get(), this.nmsEntity); } setSecondLayerSkin(); } this.npcPojo.setNpcType(npcType); setLocation(getLocation(), false); this.packets.flushCache("spawnPacket", "removeTab"); - this.entityID = (Integer) ReflectionCache.GET_ENTITY_ID.get().invoke(this.nmsEntity, new Object[0]); + this.entityID = (Integer) Reflections.GET_ENTITY_ID.get().invoke(this.nmsEntity, new Object[0]); FunctionFactory.findFunctionsForNpc(this).forEach(function -> function.resolve(this)); getPackets().getProxyInstance().update(this.packets); this.hologram.createHologram(); @@ -248,8 +248,8 @@ public class NPC { if (this.lastMove > 1L && lastMoveNanos < 1000000000L) return; Location direction = rotation ? location : this.npcPojo.getLocation().bukkitLocation().clone().setDirection(location.clone().subtract(this.npcPojo.getLocation().bukkitLocation().clone()).toVector()); try { - Object lookPacket = ReflectionCache.PACKET_PLAY_OUT_ENTITY_LOOK_CONSTRUCTOR.get().newInstance(this.entityID, (byte) (int) (direction.getYaw() * 256.0F / 360.0F), (byte) (int) (direction.getPitch() * 256.0F / 360.0F), true); - Object headRotationPacket = ReflectionCache.PACKET_PLAY_OUT_ENTITY_HEAD_ROTATION_CONSTRUCTOR.get().newInstance(this.nmsEntity, (byte) (int) (direction.getYaw() * 256.0F / 360.0F)); + Object lookPacket = Reflections.PACKET_PLAY_OUT_ENTITY_LOOK_CONSTRUCTOR.get().newInstance(this.entityID, (byte) (int) (direction.getYaw() * 256.0F / 360.0F), (byte) (int) (direction.getPitch() * 256.0F / 360.0F), true); + Object headRotationPacket = Reflections.PACKET_PLAY_OUT_ENTITY_HEAD_ROTATION_CONSTRUCTOR.get().newInstance(this.nmsEntity, (byte) (int) (direction.getYaw() * 256.0F / 360.0F)); if (player != null) Utils.sendPackets(player, lookPacket, headRotationPacket); else this.viewers.forEach(players -> Utils.sendPackets(players, headRotationPacket)); } catch (ReflectiveOperationException operationException) { @@ -275,7 +275,7 @@ public class NPC { public void updateProfile(PropertyMap propertyMap) { if (this.npcPojo.getNpcType() != NPCType.PLAYER) return; try { - Object gameProfileObj = ReflectionCache.GET_PROFILE_METHOD.get().invoke(this.nmsEntity); + Object gameProfileObj = Reflections.GET_PROFILE_METHOD.get().invoke(this.nmsEntity); Utils.setValue(gameProfileObj, "name", this.gameProfile.getName()); Utils.setValue(gameProfileObj, "id", this.gameProfile.getId()); Utils.setValue(gameProfileObj, "properties", propertyMap); diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/NPCType.java b/src/main/java/io/github/znetworkw/znpcservers/npc/NPCType.java index d15c14f..99a1f80 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/NPCType.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/NPCType.java @@ -1,7 +1,7 @@ package io.github.znetworkw.znpcservers.npc; import io.github.znetworkw.znpcservers.UnexpectedCallException; -import io.github.znetworkw.znpcservers.reflection.ClassCache; +import io.github.znetworkw.znpcservers.reflection.EnumPropertyCache; import io.github.znetworkw.znpcservers.utility.Utils; import org.bukkit.entity.EntityType; @@ -11,7 +11,7 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.Optional; -import static io.github.znetworkw.znpcservers.reflection.ReflectionCache.*; +import static io.github.znetworkw.znpcservers.reflection.Reflections.*; @SuppressWarnings("unused") public enum NPCType { @@ -112,12 +112,12 @@ public enum NPCType { return this.customizationLoader; } - public static Object[] arrayToPrimitive(String[] strings, Method method) { + public static Object[] collectArguments(String[] strings, Method method) { Class[] methodParameterTypes = method.getParameterTypes(); Object[] newArray = new Object[methodParameterTypes.length]; for (int i = 0; i < methodParameterTypes.length; ++i) { TypeProperty typeProperty = TypeProperty.forType(methodParameterTypes[i]); - newArray[i] = typeProperty != null ? typeProperty.getFunction().apply(strings[i]) : ClassCache.find(strings[i], methodParameterTypes[i]); + newArray[i] = typeProperty != null ? typeProperty.getFunction().apply(strings[i]) : EnumPropertyCache.find(strings[i], methodParameterTypes[i]); } return newArray; } @@ -128,7 +128,7 @@ public enum NPCType { } try { Method method = this.customizationLoader.getMethods().get(name); - method.invoke(npc.getBukkitEntity(), NPCType.arrayToPrimitive(values, method)); + method.invoke(npc.getBukkitEntity(), NPCType.collectArguments(values, method)); npc.updateMetadata(npc.getViewers()); } catch (IllegalAccessException | InvocationTargetException e) { diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/function/GlowFunction.java b/src/main/java/io/github/znetworkw/znpcservers/npc/function/GlowFunction.java index 662f8ac..6bbac8f 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/function/GlowFunction.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/function/GlowFunction.java @@ -1,6 +1,6 @@ package io.github.znetworkw.znpcservers.npc.function; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.npc.FunctionContext; import io.github.znetworkw.znpcservers.npc.FunctionFactory; import io.github.znetworkw.znpcservers.npc.NPC; @@ -16,15 +16,15 @@ public class GlowFunction extends NPCFunction { throw new IllegalStateException("invalid context type, " + functionContext.getClass().getSimpleName() + ", expected ContextWithValue."); String glowColorName = ((FunctionContext.ContextWithValue) functionContext).getValue(); try { - Object glowColor = ReflectionCache.ENUM_CHAT_FORMAT_FIND.get().invoke(null, ( + Object glowColor = Reflections.ENUM_CHAT_FORMAT_FIND.get().invoke(null, ( glowColorName == null || glowColorName.length() == 0) ? "WHITE" : glowColorName); if (glowColor == null) return NPCFunction.ResultType.FAIL; npc.getNpcPojo().setGlowName(glowColorName); npc.setGlowColor(glowColor); - ReflectionCache.SET_DATA_WATCHER_METHOD.get().invoke(ReflectionCache.GET_DATA_WATCHER_METHOD - .get().invoke(npc.getNmsEntity()), ReflectionCache.DATA_WATCHER_OBJECT_CONSTRUCTOR - .get().newInstance(0, ReflectionCache.DATA_WATCHER_REGISTER_FIELD + Reflections.SET_DATA_WATCHER_METHOD.get().invoke(Reflections.GET_DATA_WATCHER_METHOD + .get().invoke(npc.getNmsEntity()), Reflections.DATA_WATCHER_OBJECT_CONSTRUCTOR + .get().newInstance(0, Reflections.DATA_WATCHER_REGISTER_FIELD .get()), (byte) (!FunctionFactory.isTrue(npc, this) ? 64 : 0)); npc.getPackets().getProxyInstance().update(npc.getPackets()); npc.deleteViewers(); diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/hologram/Hologram.java b/src/main/java/io/github/znetworkw/znpcservers/npc/hologram/Hologram.java index 09d6938..663f3ad 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/hologram/Hologram.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/hologram/Hologram.java @@ -1,7 +1,7 @@ package io.github.znetworkw.znpcservers.npc.hologram; import io.github.znetworkw.znpcservers.UnexpectedCallException; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.configuration.Configuration; import io.github.znetworkw.znpcservers.configuration.ConfigurationConstants; import io.github.znetworkw.znpcservers.configuration.ConfigurationValue; @@ -37,14 +37,14 @@ public class Hologram { Location location = this.npc.getLocation(); for (String line : this.npc.getNpcPojo().getHologramLines()) { boolean visible = !line.equalsIgnoreCase("%space%"); - Object armorStand = ReflectionCache.ENTITY_CONSTRUCTOR.get().newInstance(ReflectionCache.GET_HANDLE_WORLD_METHOD.get().invoke(location.getWorld()), + Object armorStand = Reflections.ENTITY_CONSTRUCTOR.get().newInstance(Reflections.GET_HANDLE_WORLD_METHOD.get().invoke(location.getWorld()), location.getX(), location.getY() - 0.15D + y, location.getZ()); if (visible) { - ReflectionCache.SET_CUSTOM_NAME_VISIBLE_METHOD.get().invoke(armorStand, true); + Reflections.SET_CUSTOM_NAME_VISIBLE_METHOD.get().invoke(armorStand, true); updateLine(line, armorStand, null); } - ReflectionCache.SET_INVISIBLE_METHOD.get().invoke(armorStand, true); - this.hologramLines.add(new HologramLine(line.replace(ConfigurationConstants.SPACE_SYMBOL, " "), armorStand, (Integer) ReflectionCache.GET_ENTITY_ID + Reflections.SET_INVISIBLE_METHOD.get().invoke(armorStand, true); + this.hologramLines.add(new HologramLine(line.replace(ConfigurationConstants.SPACE_SYMBOL, " "), armorStand, (Integer) Reflections.GET_ENTITY_ID .get().invoke(armorStand))); y += LINE_SPACING; } @@ -92,7 +92,7 @@ public class Hologram { public void updateLocation() { this.hologramLines.forEach(hologramLine -> { try { - Object packet = ReflectionCache.PACKET_PLAY_OUT_ENTITY_TELEPORT_CONSTRUCTOR.get().newInstance(hologramLine.armorStand); + Object packet = Reflections.PACKET_PLAY_OUT_ENTITY_TELEPORT_CONSTRUCTOR.get().newInstance(hologramLine.armorStand); this.npc.getViewers().forEach(player -> Utils.sendPackets(player, packet)); } catch (ReflectiveOperationException operationException) { @@ -106,7 +106,7 @@ public class Hologram { try { double y = this.npc.getNpcPojo().getHologramHeight(); for (HologramLine hologramLine : this.hologramLines) { - ReflectionCache.SET_LOCATION_METHOD.get().invoke(hologramLine.armorStand, location.getX(), location.getY() - 0.15 + y, location.getZ(), location.getYaw(), location.getPitch()); + Reflections.SET_LOCATION_METHOD.get().invoke(hologramLine.armorStand, location.getX(), location.getY() - 0.15 + y, location.getZ(), location.getYaw(), location.getPitch()); y += LINE_SPACING; } this.updateLocation(); @@ -118,9 +118,9 @@ public class Hologram { private void updateLine(String line, Object armorStand, @Nullable ZUser user) throws InvocationTargetException, IllegalAccessException { if (NEW_METHOD) { - ReflectionCache.SET_CUSTOM_NAME_NEW_METHOD.get().invoke(armorStand, ReflectionCache.CRAFT_CHAT_MESSAGE_METHOD.get().invoke(null, LineReplacer.makeAll(user, line))); + Reflections.SET_CUSTOM_NAME_NEW_METHOD.get().invoke(armorStand, Reflections.CRAFT_CHAT_MESSAGE_METHOD.get().invoke(null, LineReplacer.makeAll(user, line))); } else { - ReflectionCache.SET_CUSTOM_NAME_OLD_METHOD.get().invoke(armorStand, LineReplacer.makeAll(user, line)); + Reflections.SET_CUSTOM_NAME_OLD_METHOD.get().invoke(armorStand, LineReplacer.makeAll(user, line)); } } diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/Packet.java b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/Packet.java index 2f5e197..4eea9f7 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/Packet.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/Packet.java @@ -2,7 +2,7 @@ package io.github.znetworkw.znpcservers.npc.packet; import com.google.common.collect.ImmutableList; import com.mojang.authlib.GameProfile; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.npc.FunctionFactory; import io.github.znetworkw.znpcservers.npc.ItemSlot; import io.github.znetworkw.znpcservers.npc.NPC; @@ -35,22 +35,22 @@ public interface Packet { @SuppressWarnings("SuspiciousTernaryOperatorInVarargsCall") @PacketValue(keyName = "destroyPacket", valueType = ValueType.ARGUMENTS) default Object getDestroyPacket(int entityId) throws ReflectiveOperationException { - return ReflectionCache.PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR.get().newInstance(ReflectionCache.PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR.get().getParameterTypes()[0].isArray() ? new int[] {entityId} : entityId); + return Reflections.PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR.get().newInstance(Reflections.PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR.get().getParameterTypes()[0].isArray() ? new int[] {entityId} : entityId); } @PacketValue(keyName = "enumSlot", valueType = ValueType.ARGUMENTS) default Object getItemSlot(int slot) { - return ReflectionCache.ENUM_ITEM_SLOT.getEnumConstants()[slot]; + return Reflections.ENUM_ITEM_SLOT.getEnumConstants()[slot]; } @PacketValue(keyName = "removeTab") default Object getTabRemovePacket(Object nmsEntity) throws ReflectiveOperationException { try { - return ReflectionCache.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(ReflectionCache.REMOVE_PLAYER_FIELD.get(), Collections.singletonList(nmsEntity)); + return Reflections.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(Reflections.REMOVE_PLAYER_FIELD.get(), Collections.singletonList(nmsEntity)); } catch (Throwable throwable) { - boolean useOldMethod = (ReflectionCache.PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CLASS != null); - if (useOldMethod) return ReflectionCache.PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CONSTRUCTOR.get().newInstance(Collections.singletonList(ReflectionCache.GET_UNIQUE_ID_METHOD.get().invoke(nmsEntity))); - return ReflectionCache.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(ReflectionCache.REMOVE_PLAYER_FIELD.get(), nmsEntity); + boolean useOldMethod = (Reflections.PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CLASS != null); + if (useOldMethod) return Reflections.PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CONSTRUCTOR.get().newInstance(Collections.singletonList(Reflections.GET_UNIQUE_ID_METHOD.get().invoke(nmsEntity))); + return Reflections.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(Reflections.REMOVE_PLAYER_FIELD.get(), nmsEntity); } } @@ -63,29 +63,29 @@ public interface Packet { ImmutableList.Builder builder = ImmutableList.builder(); boolean isVersion17 = (Utils.BUKKIT_VERSION > 16); boolean isVersion9 = (Utils.BUKKIT_VERSION > 8); - Object scoreboardTeamPacket = isVersion17 ? ReflectionCache.SCOREBOARD_TEAM_CONSTRUCTOR.get().newInstance(null, npc.getGameProfile().getName()) : ReflectionCache.PACKET_PLAY_OUT_SCOREBOARD_TEAM_CONSTRUCTOR_OLD.get().newInstance(); + Object scoreboardTeamPacket = isVersion17 ? Reflections.SCOREBOARD_TEAM_CONSTRUCTOR.get().newInstance(null, npc.getGameProfile().getName()) : Reflections.PACKET_PLAY_OUT_SCOREBOARD_TEAM_CONSTRUCTOR_OLD.get().newInstance(); if (!isVersion17) { Utils.setValue(scoreboardTeamPacket, "a", npc.getGameProfile().getName()); Utils.setValue(scoreboardTeamPacket, isVersion9 ? "i" : "h", 1); } - builder.add(isVersion17 ? ReflectionCache.PACKET_PLAY_OUT_SCOREBOARD_TEAM_CREATE_V1.get().invoke(null, scoreboardTeamPacket) : scoreboardTeamPacket); + builder.add(isVersion17 ? Reflections.PACKET_PLAY_OUT_SCOREBOARD_TEAM_CREATE_V1.get().invoke(null, scoreboardTeamPacket) : scoreboardTeamPacket); if (isVersion17) { - scoreboardTeamPacket = ReflectionCache.SCOREBOARD_TEAM_CONSTRUCTOR.get().newInstance(null, npc.getGameProfile().getName()); + scoreboardTeamPacket = Reflections.SCOREBOARD_TEAM_CONSTRUCTOR.get().newInstance(null, npc.getGameProfile().getName()); if (Utils.BUKKIT_VERSION > 17) { Utils.setValue(scoreboardTeamPacket, "d", npc.getGameProfile().getName()); - ReflectionUtils.findFieldForClassAndSet(scoreboardTeamPacket, ReflectionCache.ENUM_TAG_VISIBILITY, ReflectionCache.ENUM_TAG_VISIBILITY_NEVER_FIELD.get()); - Utils.setValue(scoreboardTeamPacket, "m", ReflectionCache.ENUM_CHAT_FORMAT_FIND.get().invoke(null, "DARK_GRAY")); + ReflectionUtils.findFieldForClassAndSet(scoreboardTeamPacket, Reflections.ENUM_TAG_VISIBILITY, Reflections.ENUM_TAG_VISIBILITY_NEVER_FIELD.get()); + Utils.setValue(scoreboardTeamPacket, "m", Reflections.ENUM_CHAT_FORMAT_FIND.get().invoke(null, "DARK_GRAY")); } else { Utils.setValue(scoreboardTeamPacket, "e", npc.getGameProfile().getName()); - Utils.setValue(scoreboardTeamPacket, "l", ReflectionCache.ENUM_TAG_VISIBILITY_NEVER_FIELD.get()); + Utils.setValue(scoreboardTeamPacket, "l", Reflections.ENUM_TAG_VISIBILITY_NEVER_FIELD.get()); } } else { - scoreboardTeamPacket = ReflectionCache.PACKET_PLAY_OUT_SCOREBOARD_TEAM_CONSTRUCTOR_OLD.get().newInstance(); + scoreboardTeamPacket = Reflections.PACKET_PLAY_OUT_SCOREBOARD_TEAM_CONSTRUCTOR_OLD.get().newInstance(); Utils.setValue(scoreboardTeamPacket, "a", npc.getGameProfile().getName()); Utils.setValue(scoreboardTeamPacket, "e", "never"); Utils.setValue(scoreboardTeamPacket, isVersion9 ? "i" : "h", 0); } - Collection collection = isVersion17 ? (Collection) ReflectionCache.SCOREBOARD_PLAYER_LIST.get().invoke(scoreboardTeamPacket) : (Collection) Utils.getValue(scoreboardTeamPacket, isVersion9 ? "h" : "g"); + Collection collection = isVersion17 ? (Collection) Reflections.SCOREBOARD_PLAYER_LIST.get().invoke(scoreboardTeamPacket) : (Collection) Utils.getValue(scoreboardTeamPacket, isVersion9 ? "h" : "g"); if (npc.getNpcPojo().getNpcType() == NPCType.PLAYER) { collection.add(npc.getGameProfile().getName()); } else { @@ -93,7 +93,7 @@ public interface Packet { } if (allowGlowColor() && FunctionFactory.isTrue(npc, "glow")) updateGlowPacket(npc, scoreboardTeamPacket); - builder.add(isVersion17 ? ReflectionCache.PACKET_PLAY_OUT_SCOREBOARD_TEAM_CREATE.get().invoke(null, scoreboardTeamPacket, Boolean.TRUE) : scoreboardTeamPacket); + builder.add(isVersion17 ? Reflections.PACKET_PLAY_OUT_SCOREBOARD_TEAM_CREATE.get().invoke(null, scoreboardTeamPacket, Boolean.TRUE) : scoreboardTeamPacket); return builder.build(); } diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV16.java b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV16.java index eda01e7..c3b6bec 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV16.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV16.java @@ -3,7 +3,7 @@ package io.github.znetworkw.znpcservers.npc.packet; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.mojang.datafixers.util.Pair; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.npc.ItemSlot; import io.github.znetworkw.znpcservers.npc.NPC; import org.bukkit.inventory.ItemStack; @@ -22,6 +22,6 @@ public class PacketV16 extends PacketV9 { pairs.add(new Pair<>(getItemSlot(entry .getKey().getSlot()), convertItemStack(npc.getEntityID(), entry.getKey(), entry.getValue()))); - return ImmutableList.of(ReflectionCache.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_V1.get().newInstance(npc.getEntityID(), pairs)); + return ImmutableList.of(Reflections.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_V1.get().newInstance(npc.getEntityID(), pairs)); } } diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV17.java b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV17.java index 0b0fb92..4faf681 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV17.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV17.java @@ -1,7 +1,7 @@ package io.github.znetworkw.znpcservers.npc.packet; import com.mojang.authlib.GameProfile; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.npc.NPC; import io.github.znetworkw.znpcservers.utility.Utils; import org.bukkit.Bukkit; @@ -12,11 +12,11 @@ public class PacketV17 extends PacketV16 { } public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException { - return ReflectionCache.PLAYER_CONSTRUCTOR_NEW.get().newInstance(ReflectionCache.GET_SERVER_METHOD.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile); + return Reflections.PLAYER_CONSTRUCTOR_NEW.get().newInstance(Reflections.GET_SERVER_METHOD.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile); } public void updateGlowPacket(NPC npc, Object packet) throws ReflectiveOperationException { - Utils.setValue(packet, "n", ReflectionCache.ENUM_CHAT_FORMAT_FIND.get().invoke(null, npc.getNpcPojo().getGlowName())); + Utils.setValue(packet, "n", Reflections.ENUM_CHAT_FORMAT_FIND.get().invoke(null, npc.getNpcPojo().getGlowName())); } public Object getClickType(Object interactPacket) { diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV18.java b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV18.java index 00cf7b2..a5abba0 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV18.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV18.java @@ -1,6 +1,6 @@ package io.github.znetworkw.znpcservers.npc.packet; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.npc.NPC; import io.github.znetworkw.znpcservers.utility.Utils; @@ -10,6 +10,6 @@ public class PacketV18 extends PacketV17 { } public void updateGlowPacket(NPC npc, Object packet) throws ReflectiveOperationException { - Utils.setValue(packet, "m", ReflectionCache.ENUM_CHAT_FORMAT_FIND.get().invoke(null, npc.getNpcPojo().getGlowName())); + Utils.setValue(packet, "m", Reflections.ENUM_CHAT_FORMAT_FIND.get().invoke(null, npc.getNpcPojo().getGlowName())); } } diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV19.java b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV19.java index 6d5592a..db096dd 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV19.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV19.java @@ -1,7 +1,7 @@ package io.github.znetworkw.znpcservers.npc.packet; import com.mojang.authlib.GameProfile; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import org.bukkit.Bukkit; public class PacketV19 extends PacketV18 { @@ -11,9 +11,9 @@ public class PacketV19 extends PacketV18 { public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException { try { - return ReflectionCache.PLAYER_CONSTRUCTOR_NEW_1.get(true).newInstance(ReflectionCache.GET_SERVER_METHOD.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile, null); + return Reflections.PLAYER_CONSTRUCTOR_NEW_1.get(true).newInstance(Reflections.GET_SERVER_METHOD.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile, null); } catch (Throwable e) { - return ReflectionCache.PLAYER_CONSTRUCTOR_NEW_2.get().newInstance(ReflectionCache.GET_SERVER_METHOD.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile); + return Reflections.PLAYER_CONSTRUCTOR_NEW_2.get().newInstance(Reflections.GET_SERVER_METHOD.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile); } } } diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV8.java b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV8.java index b874497..51efbc0 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV8.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV8.java @@ -2,7 +2,7 @@ package io.github.znetworkw.znpcservers.npc.packet; import com.google.common.collect.ImmutableList; import com.mojang.authlib.GameProfile; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.npc.ItemSlot; import io.github.znetworkw.znpcservers.npc.NPC; import io.github.znetworkw.znpcservers.utility.Utils; @@ -18,18 +18,18 @@ public class PacketV8 implements Packet { } public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException { - Constructor constructor = (Utils.BUKKIT_VERSION > 13) ? ReflectionCache.PLAYER_INTERACT_MANAGER_NEW_CONSTRUCTOR.get() : ReflectionCache.PLAYER_INTERACT_MANAGER_OLD_CONSTRUCTOR.get(); - return ReflectionCache.PLAYER_CONSTRUCTOR_OLD.get().newInstance(ReflectionCache.GET_SERVER_METHOD + Constructor constructor = (Utils.BUKKIT_VERSION > 13) ? Reflections.PLAYER_INTERACT_MANAGER_NEW_CONSTRUCTOR.get() : Reflections.PLAYER_INTERACT_MANAGER_OLD_CONSTRUCTOR.get(); + return Reflections.PLAYER_CONSTRUCTOR_OLD.get().newInstance(Reflections.GET_SERVER_METHOD .get().invoke(Bukkit.getServer()), nmsWorld, gameProfile, constructor.newInstance(nmsWorld)); } public Object getSpawnPacket(Object nmsEntity, boolean isPlayer) throws ReflectiveOperationException { - return isPlayer ? ReflectionCache.PACKET_PLAY_OUT_NAMED_ENTITY_CONSTRUCTOR.get().newInstance(nmsEntity) : ReflectionCache.PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR.get().newInstance(nmsEntity); + return isPlayer ? Reflections.PACKET_PLAY_OUT_NAMED_ENTITY_CONSTRUCTOR.get().newInstance(nmsEntity) : Reflections.PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR.get().newInstance(nmsEntity); } public Object convertItemStack(int entityId, ItemSlot itemSlot, ItemStack itemStack) throws ReflectiveOperationException { - return ReflectionCache.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_OLD.get().newInstance(entityId, - itemSlot.getSlotOld(), ReflectionCache.AS_NMS_COPY_METHOD.get().invoke(ReflectionCache.CRAFT_ITEM_STACK_CLASS, itemStack)); + return Reflections.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_OLD.get().newInstance(entityId, + itemSlot.getSlotOld(), Reflections.AS_NMS_COPY_METHOD.get().invoke(Reflections.CRAFT_ITEM_STACK_CLASS, itemStack)); } public Object getClickType(Object interactPacket) throws ReflectiveOperationException { @@ -37,22 +37,22 @@ public class PacketV8 implements Packet { } public Object getMetadataPacket(int entityId, Object nmsEntity) throws ReflectiveOperationException { - Object dataWatcher = ReflectionCache.GET_DATA_WATCHER_METHOD.get().invoke(nmsEntity); + Object dataWatcher = Reflections.GET_DATA_WATCHER_METHOD.get().invoke(nmsEntity); try { - return ReflectionCache.PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR.get(true).newInstance(entityId, dataWatcher, true); + return Reflections.PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR.get(true).newInstance(entityId, dataWatcher, true); } catch (Exception e2) { - return ReflectionCache.PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR_V1.get().newInstance(entityId, ReflectionCache.GET_DATAWATCHER_B_LIST.get().invoke(dataWatcher)); + return Reflections.PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR_V1.get().newInstance(entityId, Reflections.GET_DATAWATCHER_B_LIST.get().invoke(dataWatcher)); } } public Object getHologramSpawnPacket(Object armorStand) throws ReflectiveOperationException { - return ReflectionCache.PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR.get().newInstance(armorStand); + return Reflections.PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR.get().newInstance(armorStand); } public ImmutableList getEquipPackets(NPC npc) throws ReflectiveOperationException { ImmutableList.Builder builder = ImmutableList.builder(); for (Map.Entry stackEntry : npc.getNpcPojo().getNpcEquip().entrySet()) { - builder.add(ReflectionCache.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_OLD.get().newInstance(npc.getEntityID(), stackEntry.getKey().getSlotOld(), + builder.add(Reflections.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_OLD.get().newInstance(npc.getEntityID(), stackEntry.getKey().getSlotOld(), convertItemStack(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue()))); } return builder.build(); diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV9.java b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV9.java index 3fdc219..04963c9 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV9.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/packet/PacketV9.java @@ -1,7 +1,7 @@ package io.github.znetworkw.znpcservers.npc.packet; import com.google.common.collect.ImmutableList; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.npc.ItemSlot; import io.github.znetworkw.znpcservers.npc.NPC; import io.github.znetworkw.znpcservers.utility.Utils; @@ -15,13 +15,13 @@ public class PacketV9 extends PacketV8 { } public Object convertItemStack(int entityId, ItemSlot itemSlot, ItemStack itemStack) throws ReflectiveOperationException { - return ReflectionCache.AS_NMS_COPY_METHOD.get().invoke(ReflectionCache.CRAFT_ITEM_STACK_CLASS, itemStack); + return Reflections.AS_NMS_COPY_METHOD.get().invoke(Reflections.CRAFT_ITEM_STACK_CLASS, itemStack); } public ImmutableList getEquipPackets(NPC npc) throws ReflectiveOperationException { ImmutableList.Builder builder = ImmutableList.builder(); for (Map.Entry stackEntry : npc.getNpcPojo().getNpcEquip().entrySet()) { - builder.add(ReflectionCache.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_NEWEST_OLD.get().newInstance(npc.getEntityID(), + builder.add(Reflections.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_NEWEST_OLD.get().newInstance(npc.getEntityID(), getItemSlot(stackEntry.getKey().getSlot()), convertItemStack(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue()))); } @@ -29,12 +29,12 @@ public class PacketV9 extends PacketV8 { } public void updateGlowPacket(NPC npc, Object packet) throws ReflectiveOperationException { - Object enumChatString = ReflectionCache.ENUM_CHAT_TO_STRING_METHOD.get().invoke(npc.getGlowColor()); + Object enumChatString = Reflections.ENUM_CHAT_TO_STRING_METHOD.get().invoke(npc.getGlowColor()); if (Utils.BUKKIT_VERSION > 12) { - Utils.setValue(packet, npc.getGlowColor(), ReflectionCache.ENUM_CHAT_CLASS); - Utils.setValue(packet, "c", ReflectionCache.I_CHAT_BASE_COMPONENT_A_CONSTRUCTOR.get().newInstance(enumChatString)); + Utils.setValue(packet, npc.getGlowColor(), Reflections.ENUM_CHAT_CLASS); + Utils.setValue(packet, "c", Reflections.I_CHAT_BASE_COMPONENT_A_CONSTRUCTOR.get().newInstance(enumChatString)); } else { - Utils.setValue(packet, "g", ReflectionCache.GET_ENUM_CHAT_ID_METHOD.get().invoke(npc.getGlowColor())); + Utils.setValue(packet, "g", Reflections.GET_ENUM_CHAT_ID_METHOD.get().invoke(npc.getGlowColor())); Utils.setValue(packet, "c", enumChatString); } } diff --git a/src/main/java/io/github/znetworkw/znpcservers/reflection/ClassCache.java b/src/main/java/io/github/znetworkw/znpcservers/reflection/EnumPropertyCache.java similarity index 81% rename from src/main/java/io/github/znetworkw/znpcservers/reflection/ClassCache.java rename to src/main/java/io/github/znetworkw/znpcservers/reflection/EnumPropertyCache.java index 0695e75..2907ba3 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/reflection/ClassCache.java +++ b/src/main/java/io/github/znetworkw/znpcservers/reflection/EnumPropertyCache.java @@ -4,7 +4,12 @@ import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -public class ClassCache { +/** + * Gonalez decided to make every single enum we load be stored in this map + * to make NPC property application easier however this is terribly inefficient + * for memory. The entire NPC property system will most likely need a rewrite. + */ +public class EnumPropertyCache { protected static final ConcurrentMap CACHE = new ConcurrentHashMap<>(); public static Object find(String name, Class objectClass) { diff --git a/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionBuilder.java b/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionBuilder.java index caf53d7..d486bba 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionBuilder.java +++ b/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionBuilder.java @@ -68,7 +68,7 @@ public class ReflectionBuilder { return ImmutableList.copyOf(this.parameterTypes); } - public ImmutableList getClassName() { + public ImmutableList getClassNames() { return ImmutableList.copyOf(this.className); } diff --git a/src/main/java/io/github/znetworkw/znpcservers/reflection/BaseReflection.java b/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionLazyLoader.java similarity index 66% rename from src/main/java/io/github/znetworkw/znpcservers/reflection/BaseReflection.java rename to src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionLazyLoader.java index b7c998a..26b2f07 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/reflection/BaseReflection.java +++ b/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionLazyLoader.java @@ -5,24 +5,24 @@ import lol.pyr.znpcsplus.ZNPCsPlus; import java.util.List; -public abstract class BaseReflection { - protected final List className; +public abstract class ReflectionLazyLoader { + protected final List possibleClassNames; - protected Class BUILDER_CLASS; + protected Class reflectionClass; private T cached; private boolean loaded = false; - protected BaseReflection(ReflectionBuilder builder) { - this(builder.getClassName()); + protected ReflectionLazyLoader(ReflectionBuilder builder) { + this(builder.getClassNames()); } - protected BaseReflection(List className) { - this.className = className; - for (String classes : className) { + protected ReflectionLazyLoader(List possibleClassNames) { + this.possibleClassNames = possibleClassNames; + for (String classes : possibleClassNames) { try { - this.BUILDER_CLASS = Class.forName(classes); + this.reflectionClass = Class.forName(classes); break; } catch (ClassNotFoundException ignored) { } @@ -36,13 +36,13 @@ public abstract class BaseReflection { public T get(boolean missAllowed) { if (this.loaded) return this.cached; try { - if (this.BUILDER_CLASS == null) throw new ClassNotFoundException("No class found: " + className); + if (this.reflectionClass == null) throw new ClassNotFoundException("No class found: " + possibleClassNames); T eval = (this.cached != null) ? this.cached : (this.cached = load()); if (eval == null) throw new NullPointerException(); } catch (Throwable throwable) { if (!missAllowed) { warn(getClass().getSimpleName() + " get failed!"); - warn("Class Names: " + className); + warn("Class Names: " + possibleClassNames); warn("Loader Type: " + getClass().getCanonicalName()); warn("Bukkit Version: " + Utils.BUKKIT_VERSION + " (" + Utils.getBukkitPackage() + ")"); warn("Exception:"); diff --git a/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionPackage.java b/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionPackage.java index 0b684c9..b7a3df1 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionPackage.java +++ b/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionPackage.java @@ -6,10 +6,20 @@ import java.util.Arrays; import java.util.Objects; import java.util.stream.Collectors; +/** + * A class containing all of the packages of the server jar that we import classes from. + * Every line has a check for the "flattened" variable due to the fact that server jars + * pre-1.17 had all of their classes "flattened" into one package. + */ public class ReflectionPackage { private static final boolean flattened = !Utils.versionNewer(17); public static final String BUKKIT = "org.bukkit.craftbukkit." + Utils.getBukkitPackage(); + + /** + * Check if the classes are flattened, if so we need to add the version string into the + * package string which is another quirk of the old server jars. + */ public static final String MINECRAFT = join("net.minecraft", flattened ? "server." + Utils.getBukkitPackage() : ""); public static final String NETWORK = flattened ? MINECRAFT : join(MINECRAFT, "network"); @@ -26,6 +36,7 @@ public class ReflectionPackage { public static final String SERVER_NETWORK = flattened ? MINECRAFT : join(MINECRAFT, "server.network"); public static final String SERVER = flattened ? MINECRAFT : join(MINECRAFT, "server"); + // Simple method that joins all the non-null & non-empty arguments with a dot and returns the result public static String join(String... parts) { return Arrays.stream(parts) .filter(Objects::nonNull) diff --git a/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionCache.java b/src/main/java/io/github/znetworkw/znpcservers/reflection/Reflections.java similarity index 73% rename from src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionCache.java rename to src/main/java/io/github/znetworkw/znpcservers/reflection/Reflections.java index d9dacf0..dc67b01 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/reflection/ReflectionCache.java +++ b/src/main/java/io/github/znetworkw/znpcservers/reflection/Reflections.java @@ -13,7 +13,11 @@ import java.util.Collection; import java.util.List; import java.util.UUID; -public final class ReflectionCache { +/** + * Class containing all of the lazy-loaded reflections that the plugin uses to access + * inaccessible things from the server jar like packets, raw entity classes, etc. + */ +public final class Reflections { public static final Class PACKET_PLAY_IN_USE_ENTITY_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayInUseEntity")).get(); @@ -288,112 +292,112 @@ public final class ReflectionCache { public static final Class PROFILE_PUBLIC_KEY_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.WORLD_ENTITY_PLAYER) .withClassName("ProfilePublicKey")).get(!Utils.versionNewer(19)); - public static final BaseReflection> SCOREBOARD_TEAM_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> SCOREBOARD_TEAM_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(SCOREBOARD_TEAM_CLASS) .withParameterTypes(SCOREBOARD_CLASS, String.class)); - public static final BaseReflection> PLAYER_CONSTRUCTOR_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PLAYER_CONSTRUCTOR_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_PLAYER_CLASS) .withParameterTypes(MINECRAFT_SERVER_CLASS, WORLD_SERVER_CLASS, GameProfile.class, PLAYER_INTERACT_MANAGER_CLASS)); - public static final BaseReflection> PLAYER_CONSTRUCTOR_NEW = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PLAYER_CONSTRUCTOR_NEW = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_PLAYER_CLASS) .withParameterTypes(MINECRAFT_SERVER_CLASS, WORLD_SERVER_CLASS, GameProfile.class)); - public static final BaseReflection> PLAYER_CONSTRUCTOR_NEW_1 = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PLAYER_CONSTRUCTOR_NEW_1 = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_PLAYER_CLASS) .withParameterTypes(MINECRAFT_SERVER_CLASS, WORLD_SERVER_CLASS, GameProfile.class, PROFILE_PUBLIC_KEY_CLASS)); - public static final BaseReflection> PLAYER_CONSTRUCTOR_NEW_2 = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PLAYER_CONSTRUCTOR_NEW_2 = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_PLAYER_CLASS) .withParameterTypes(MINECRAFT_SERVER_CLASS, WORLD_SERVER_CLASS, GameProfile.class)); - public static final BaseReflection> PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(PACKET_PLAY_OUT_PLAYER_INFO_CLASS) .withParameterTypes(ENUM_PLAYER_INFO_CLASS, (Utils.BUKKIT_VERSION > 16) ? Collection.class : Iterable.class).withParameterTypes(ENUM_PLAYER_INFO_CLASS, ENTITY_PLAYER_CLASS)); - public static final BaseReflection> PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CLASS) .withParameterTypes(List.class)); - public static final BaseReflection> PACKET_PLAY_OUT_ENTITY_LOOK_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_ENTITY_LOOK_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutEntity$PacketPlayOutEntityLook") .withParameterTypes(int.class, byte.class, byte.class, boolean.class)); - public static final BaseReflection> PACKET_PLAY_OUT_ENTITY_HEAD_ROTATION_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_ENTITY_HEAD_ROTATION_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutEntityHeadRotation") .withParameterTypes(ENTITY_CLASS, byte.class)); - public static final BaseReflection> PACKET_PLAY_OUT_ENTITY_TELEPORT_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_ENTITY_TELEPORT_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutEntityTeleport") .withParameterTypes(ENTITY_CLASS)); - public static final BaseReflection> PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutEntityMetadata") .withParameterTypes(int.class, DATA_WATCHER_CLASS, boolean.class)); - public static final BaseReflection> PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR_V1 = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR_V1 = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutEntityMetadata") .withParameterTypes(int.class, List.class)); - public static final BaseReflection> PACKET_PLAY_OUT_NAMED_ENTITY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_NAMED_ENTITY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutNamedEntitySpawn") .withParameterTypes(ENTITY_HUMAN_CLASS)); - public static final BaseReflection> PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(PACKET_PLAY_OUT_ENTITY_DESTROY_CLASS) .withParameterTypes(int.class).withParameterTypes(int[].class)); - public static final BaseReflection> PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutSpawnEntity") .withClassName("PacketPlayOutSpawnEntityLiving") .withParameterTypes(ENTITY_LIVING).withParameterTypes(ENTITY_CLASS)); - public static final BaseReflection> PLAYER_INTERACT_MANAGER_OLD_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PLAYER_INTERACT_MANAGER_OLD_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PlayerInteractManager") .withParameterTypes(WORLD_CLASS)); - public static final BaseReflection> PLAYER_INTERACT_MANAGER_NEW_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PLAYER_INTERACT_MANAGER_NEW_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PlayerInteractManager") .withParameterTypes(WORLD_SERVER_CLASS)); - public static final BaseReflection> PACKET_PLAY_OUT_SCOREBOARD_TEAM_CONSTRUCTOR_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_SCOREBOARD_TEAM_CONSTRUCTOR_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(PACKET_PLAY_OUT_SCOREBOARD_TEAM_CLASS)); - public static final BaseReflection> PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutEntityEquipment") .withParameterTypes(int.class, int.class, ITEM_STACK_CLASS)); - public static final BaseReflection> PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_NEWEST_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_NEWEST_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutEntityEquipment") .withParameterTypes(int.class, ENUM_ITEM_SLOT, ITEM_STACK_CLASS)); - public static final BaseReflection> PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_V1 = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_V1 = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutEntityEquipment") .withParameterTypes(int.class, List.class)); - public static final BaseReflection> I_CHAT_BASE_COMPONENT_A_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.CHAT) + public static final ReflectionLazyLoader> I_CHAT_BASE_COMPONENT_A_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.CHAT) .withClassName("ChatComponentText") .withParameterTypes(String.class)); - public static final BaseReflection> ENTITY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> ENTITY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_ARMOR_STAND_CLASS) .withParameterTypes(WORLD_CLASS, double.class, double.class, double.class)); - public static final BaseReflection> DATA_WATCHER_OBJECT_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader> DATA_WATCHER_OBJECT_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(DATA_WATCHER_OBJECT) .withParameterTypes(int.class, DATA_WATCHER_SERIALIZER)); - public static final BaseReflection AS_NMS_COPY_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT) + public static final ReflectionLazyLoader AS_NMS_COPY_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT) .withClassName("inventory.CraftItemStack") .withMethodName("asNMSCopy") .withParameterTypes(ItemStack.class)); - public static final BaseReflection GET_PROFILE_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.ENTITY) + public static final ReflectionLazyLoader GET_PROFILE_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.ENTITY) .withClassName(ENTITY_HUMAN_CLASS) .withExpectResult(GameProfile.class)); - public static final BaseReflection GET_ENTITY_ID = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader GET_ENTITY_ID = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_CLASS) .withMethodName("getId") .withMethodName("ae") @@ -401,63 +405,63 @@ public final class ReflectionCache { .withMethodName("af") .withExpectResult(int.class)); - public static final BaseReflection GET_HANDLE_PLAYER_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT) + public static final ReflectionLazyLoader GET_HANDLE_PLAYER_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT) .withClassName("entity.CraftPlayer").withClassName("entity.CraftHumanEntity") .withMethodName("getHandle")); - public static final BaseReflection GET_HANDLE_WORLD_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT) + public static final ReflectionLazyLoader GET_HANDLE_WORLD_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT) .withClassName("CraftWorld") .withMethodName("getHandle")); - public static final BaseReflection GET_SERVER_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT) + public static final ReflectionLazyLoader GET_SERVER_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT) .withClassName("CraftServer") .withMethodName("getServer")); - public static final BaseReflection SEND_PACKET_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader SEND_PACKET_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(PLAYER_CONNECTION_CLASS) .withMethodName("sendPacket").withMethodName("a") .withParameterTypes(PACKET_CLASS)); - public static final BaseReflection SET_CUSTOM_NAME_OLD_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader SET_CUSTOM_NAME_OLD_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_CLASS) .withMethodName("setCustomName") .withParameterTypes(String.class)); - public static final BaseReflection SET_CUSTOM_NAME_NEW_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader SET_CUSTOM_NAME_NEW_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_CLASS) .withMethodName("setCustomName") .withMethodName("a") .withMethodName("b") .withParameterTypes(I_CHAT_BASE_COMPONENT).withExpectResult(void.class)); - public static final BaseReflection SET_CUSTOM_NAME_VISIBLE_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader SET_CUSTOM_NAME_VISIBLE_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_CLASS) .withMethodName("setCustomNameVisible") .withMethodName("n") .withParameterTypes(boolean.class)); - public static final BaseReflection SET_INVISIBLE_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader SET_INVISIBLE_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_ARMOR_STAND_CLASS) .withMethodName("setInvisible").withMethodName("j") .withParameterTypes(boolean.class)); - public static final BaseReflection SET_LOCATION_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader SET_LOCATION_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_CLASS) .withMethodName("setPositionRotation") .withMethodName("a") .withParameterTypes(double.class, double.class, double.class, float.class, float.class)); - public static final BaseReflection SET_DATA_WATCHER_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader SET_DATA_WATCHER_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(DATA_WATCHER_CLASS) .withMethodName("set").withMethodName("b") .withParameterTypes(DATA_WATCHER_OBJECT, Object.class)); - public static final BaseReflection WATCH_DATA_WATCHER_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader WATCH_DATA_WATCHER_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(DATA_WATCHER_CLASS) .withMethodName("watch") .withParameterTypes(int.class, Object.class)); - public static final BaseReflection GET_DATA_WATCHER_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader GET_DATA_WATCHER_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_CLASS) .withMethodName("getDataWatcher") .withMethodName("ai") @@ -465,91 +469,91 @@ public final class ReflectionCache { .withMethodName("aj") .withExpectResult(DATA_WATCHER_CLASS)); - public static final BaseReflection GET_BUKKIT_ENTITY_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader GET_BUKKIT_ENTITY_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENTITY_CLASS) .withMethodName("getBukkitEntity")); - public static final BaseReflection GET_ENUM_CHAT_ID_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader GET_ENUM_CHAT_ID_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENUM_CHAT_CLASS) .withMethodName("b")); - public static final BaseReflection ENUM_CHAT_TO_STRING_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader ENUM_CHAT_TO_STRING_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENUM_CHAT_CLASS) .withExpectResult(String.class) .withMethodName("toString")); - public static final BaseReflection ENTITY_TYPES_A_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.ENTITY) + public static final ReflectionLazyLoader ENTITY_TYPES_A_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.ENTITY) .withClassName(ENTITY_TYPES_CLASS) .withMethodName("a") .withParameterTypes(String.class)); - public static final BaseReflection PACKET_PLAY_OUT_SCOREBOARD_TEAM_CREATE_V1 = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader PACKET_PLAY_OUT_SCOREBOARD_TEAM_CREATE_V1 = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(PACKET_PLAY_OUT_SCOREBOARD_TEAM_CLASS) .withMethodName("a") .withParameterTypes(SCOREBOARD_TEAM_CLASS)); - public static final BaseReflection PACKET_PLAY_OUT_SCOREBOARD_TEAM_CREATE = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader PACKET_PLAY_OUT_SCOREBOARD_TEAM_CREATE = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(PACKET_PLAY_OUT_SCOREBOARD_TEAM_CLASS) .withMethodName("a") .withParameterTypes(SCOREBOARD_TEAM_CLASS, boolean.class)); - public static final BaseReflection SCOREBOARD_PLAYER_LIST = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader SCOREBOARD_PLAYER_LIST = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(SCOREBOARD_TEAM_CLASS) .withMethodName("getPlayerNameSet").withMethodName("g")); - public static final BaseReflection ENUM_CHAT_FORMAT_FIND = new MethodReflection(new ReflectionBuilder(ReflectionPackage.MINECRAFT) + public static final ReflectionLazyLoader ENUM_CHAT_FORMAT_FIND = new MethodReflection(new ReflectionBuilder(ReflectionPackage.MINECRAFT) .withClassName(ENUM_CHAT_CLASS) .withParameterTypes(String.class).withExpectResult(ENUM_CHAT_CLASS)); - public static final BaseReflection CRAFT_CHAT_MESSAGE_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT) + public static final ReflectionLazyLoader CRAFT_CHAT_MESSAGE_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT) .withClassName(CRAFT_CHAT_MESSAGE_CLASS) .withMethodName("fromStringOrNull") .withParameterTypes(String.class)); - public static final BaseReflection GET_UNIQUE_ID_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.MINECRAFT) + public static final ReflectionLazyLoader GET_UNIQUE_ID_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.MINECRAFT) .withClassName(ENTITY_CLASS) .withExpectResult(UUID.class)); - public static final BaseReflection GET_DATAWATCHER_B_LIST = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader GET_DATAWATCHER_B_LIST = new MethodReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withMethodName("c") .withClassName(DATA_WATCHER_CLASS)); - public static final BaseReflection PLAYER_CONNECTION_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.SERVER_LEVEL) + public static final ReflectionLazyLoader PLAYER_CONNECTION_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.SERVER_LEVEL) .withClassName(ENTITY_PLAYER_CLASS) .withFieldName((Utils.BUKKIT_VERSION > 16) ? "b" : "playerConnection")); - public static final BaseReflection NETWORK_MANAGER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader NETWORK_MANAGER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(PLAYER_CONNECTION_CLASS) .withFieldName((Utils.BUKKIT_VERSION > 16) ? "a" : "networkManager") .withExpectResult(NETWORK_MANAGER_CLASS)); - public static final BaseReflection CHANNEL_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.SERVER_NETWORK) + public static final ReflectionLazyLoader CHANNEL_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.SERVER_NETWORK) .withClassName(NETWORK_MANAGER_CLASS) .withExpectResult(Channel.class)); - public static final BaseReflection PACKET_IN_USE_ENTITY_ID_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader PACKET_IN_USE_ENTITY_ID_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayInUseEntity") .withFieldName("a")); - public static final BaseReflection ADD_PLAYER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader ADD_PLAYER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutPlayerInfo$EnumPlayerInfoAction") .withClassName("ClientboundPlayerInfoUpdatePacket$a") .withFieldName((Utils.BUKKIT_VERSION > 16) ? "a" : "ADD_PLAYER")).asValueField(); - public static final BaseReflection UPDATE_LISTED_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader UPDATE_LISTED_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("ClientboundPlayerInfoUpdatePacket$a") .withFieldName("d")).asValueField(); - public static final BaseReflection REMOVE_PLAYER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader REMOVE_PLAYER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName("PacketPlayOutPlayerInfo$EnumPlayerInfoAction") .withClassName("ClientboundPlayerInfoUpdatePacket$a") .withFieldName((Utils.BUKKIT_VERSION > 16) ? "e" : "REMOVE_PLAYER")).asValueField(); - public static final BaseReflection DATA_WATCHER_REGISTER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader DATA_WATCHER_REGISTER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(DATA_WATCHER_REGISTRY) .withFieldName("a")).asValueField(); - public static final BaseReflection ENUM_TAG_VISIBILITY_NEVER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) + public static final ReflectionLazyLoader ENUM_TAG_VISIBILITY_NEVER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) .withClassName(ENUM_TAG_VISIBILITY) .withFieldName("b")).asValueField(); } diff --git a/src/main/java/io/github/znetworkw/znpcservers/reflection/types/ClassReflection.java b/src/main/java/io/github/znetworkw/znpcservers/reflection/types/ClassReflection.java index 5e898ba..6b06b9b 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/reflection/types/ClassReflection.java +++ b/src/main/java/io/github/znetworkw/znpcservers/reflection/types/ClassReflection.java @@ -1,14 +1,14 @@ package io.github.znetworkw.znpcservers.reflection.types; -import io.github.znetworkw.znpcservers.reflection.BaseReflection; +import io.github.znetworkw.znpcservers.reflection.ReflectionLazyLoader; import io.github.znetworkw.znpcservers.reflection.ReflectionBuilder; -public class ClassReflection extends BaseReflection> { +public class ClassReflection extends ReflectionLazyLoader> { public ClassReflection(ReflectionBuilder reflectionBuilder) { super(reflectionBuilder); } protected Class load() { - return this.BUILDER_CLASS; + return this.reflectionClass; } } \ No newline at end of file diff --git a/src/main/java/io/github/znetworkw/znpcservers/reflection/types/ConstructorReflection.java b/src/main/java/io/github/znetworkw/znpcservers/reflection/types/ConstructorReflection.java index 5208cb5..250bc35 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/reflection/types/ConstructorReflection.java +++ b/src/main/java/io/github/znetworkw/znpcservers/reflection/types/ConstructorReflection.java @@ -2,12 +2,12 @@ package io.github.znetworkw.znpcservers.reflection.types; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import io.github.znetworkw.znpcservers.reflection.BaseReflection; +import io.github.znetworkw.znpcservers.reflection.ReflectionLazyLoader; import io.github.znetworkw.znpcservers.reflection.ReflectionBuilder; import java.lang.reflect.Constructor; -public class ConstructorReflection extends BaseReflection> { +public class ConstructorReflection extends ReflectionLazyLoader> { private final ImmutableList[]> parameterTypes; public ConstructorReflection(ReflectionBuilder reflectionBuilder) { @@ -19,9 +19,9 @@ public class ConstructorReflection extends BaseReflection> { Constructor constructor = null; if (Iterables.size(parameterTypes) > 1) { for (Class[] keyParameters : parameterTypes) try { - constructor = this.BUILDER_CLASS.getDeclaredConstructor(keyParameters); + constructor = this.reflectionClass.getDeclaredConstructor(keyParameters); } catch (NoSuchMethodException ignored) {} - } else constructor = (Iterables.size(parameterTypes) > 0) ? this.BUILDER_CLASS.getDeclaredConstructor(Iterables.get(parameterTypes, 0)) : this.BUILDER_CLASS.getDeclaredConstructor(); + } else constructor = (Iterables.size(parameterTypes) > 0) ? this.reflectionClass.getDeclaredConstructor(Iterables.get(parameterTypes, 0)) : this.reflectionClass.getDeclaredConstructor(); if (constructor != null) constructor.setAccessible(true); return constructor; } diff --git a/src/main/java/io/github/znetworkw/znpcservers/reflection/types/EnumReflection.java b/src/main/java/io/github/znetworkw/znpcservers/reflection/types/EnumReflection.java index 1b63512..ddf4cbf 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/reflection/types/EnumReflection.java +++ b/src/main/java/io/github/znetworkw/znpcservers/reflection/types/EnumReflection.java @@ -1,17 +1,17 @@ package io.github.znetworkw.znpcservers.reflection.types; -import io.github.znetworkw.znpcservers.reflection.BaseReflection; -import io.github.znetworkw.znpcservers.reflection.ClassCache; +import io.github.znetworkw.znpcservers.reflection.ReflectionLazyLoader; +import io.github.znetworkw.znpcservers.reflection.EnumPropertyCache; import io.github.znetworkw.znpcservers.reflection.ReflectionBuilder; -public class EnumReflection extends BaseReflection[]> { +public class EnumReflection extends ReflectionLazyLoader[]> { public EnumReflection(ReflectionBuilder reflectionBuilder) { super(reflectionBuilder); } protected Enum[] load() { - Enum[] arrayOfEnum = (Enum[]) this.BUILDER_CLASS.getEnumConstants(); - for (Enum enumConstant : arrayOfEnum) ClassCache.register(enumConstant.name(), enumConstant, this.BUILDER_CLASS); - return arrayOfEnum; + Enum[] enums = (Enum[]) this.reflectionClass.getEnumConstants(); + for (Enum enumConstant : enums) EnumPropertyCache.register(enumConstant.name(), enumConstant, this.reflectionClass); + return enums; } } \ No newline at end of file diff --git a/src/main/java/io/github/znetworkw/znpcservers/reflection/types/FieldReflection.java b/src/main/java/io/github/znetworkw/znpcservers/reflection/types/FieldReflection.java index b4cce94..e22f842 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/reflection/types/FieldReflection.java +++ b/src/main/java/io/github/znetworkw/znpcservers/reflection/types/FieldReflection.java @@ -1,12 +1,12 @@ package io.github.znetworkw.znpcservers.reflection.types; -import io.github.znetworkw.znpcservers.reflection.BaseReflection; +import io.github.znetworkw.znpcservers.reflection.ReflectionLazyLoader; import io.github.znetworkw.znpcservers.reflection.ReflectionBuilder; import java.lang.reflect.Field; import java.util.List; -public class FieldReflection extends BaseReflection { +public class FieldReflection extends ReflectionLazyLoader { private final String fieldName; private final Class expectType; @@ -18,22 +18,22 @@ public class FieldReflection extends BaseReflection { protected Field load() throws NoSuchFieldException { if (expectType != null) - for (Field field1 : this.BUILDER_CLASS.getDeclaredFields()) { + for (Field field1 : this.reflectionClass.getDeclaredFields()) { if (field1.getType() == expectType) { field1.setAccessible(true); return field1; } } - Field field = this.BUILDER_CLASS.getDeclaredField(fieldName); + Field field = this.reflectionClass.getDeclaredField(fieldName); field.setAccessible(true); return field; } public AsValueField asValueField() { - return new AsValueField(this, className); + return new AsValueField(this, possibleClassNames); } - private static class AsValueField extends BaseReflection { + private static class AsValueField extends ReflectionLazyLoader { private final FieldReflection fieldReflection; public AsValueField(FieldReflection fieldReflection, List className) { diff --git a/src/main/java/io/github/znetworkw/znpcservers/reflection/types/MethodReflection.java b/src/main/java/io/github/znetworkw/znpcservers/reflection/types/MethodReflection.java index b0e4788..50410ef 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/reflection/types/MethodReflection.java +++ b/src/main/java/io/github/znetworkw/znpcservers/reflection/types/MethodReflection.java @@ -2,12 +2,12 @@ package io.github.znetworkw.znpcservers.reflection.types; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import io.github.znetworkw.znpcservers.reflection.BaseReflection; +import io.github.znetworkw.znpcservers.reflection.ReflectionLazyLoader; import io.github.znetworkw.znpcservers.reflection.ReflectionBuilder; import java.lang.reflect.Method; -public class MethodReflection extends BaseReflection { +public class MethodReflection extends ReflectionLazyLoader { private final ImmutableList methods; private final ImmutableList[]> parameterTypes; private final Class expectType; @@ -22,11 +22,11 @@ public class MethodReflection extends BaseReflection { protected Method load() { Method methodThis = null; boolean hasExpectedType = (expectType != null); - if (methods.isEmpty() && hasExpectedType) for (Method method : this.BUILDER_CLASS.getDeclaredMethods()) if (method.getReturnType() == expectType) return method; + if (methods.isEmpty() && hasExpectedType) for (Method method : this.reflectionClass.getDeclaredMethods()) if (method.getReturnType() == expectType) return method; for (String methodName : methods) try { Method maybeGet; - if (!Iterables.isEmpty(parameterTypes)) maybeGet = this.BUILDER_CLASS.getDeclaredMethod(methodName, Iterables.get(parameterTypes, 0)); - else maybeGet = this.BUILDER_CLASS.getDeclaredMethod(methodName); + if (!Iterables.isEmpty(parameterTypes)) maybeGet = this.reflectionClass.getDeclaredMethod(methodName, Iterables.get(parameterTypes, 0)); + else maybeGet = this.reflectionClass.getDeclaredMethod(methodName); if (expectType != null && expectType != maybeGet.getReturnType()) continue; maybeGet.setAccessible(true); methodThis = maybeGet; diff --git a/src/main/java/io/github/znetworkw/znpcservers/user/ZUser.java b/src/main/java/io/github/znetworkw/znpcservers/user/ZUser.java index 8b3a203..3bc8dcd 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/user/ZUser.java +++ b/src/main/java/io/github/znetworkw/znpcservers/user/ZUser.java @@ -1,7 +1,7 @@ package io.github.znetworkw.znpcservers.user; import com.mojang.authlib.GameProfile; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.npc.NPC; import io.github.znetworkw.znpcservers.npc.NPCAction; import io.github.znetworkw.znpcservers.npc.event.ClickType; @@ -41,9 +41,9 @@ public class ZUser { this.lastClicked = new HashMap<>(); this.eventServices = new ArrayList<>(); try { - Object playerHandle = ReflectionCache.GET_HANDLE_PLAYER_METHOD.get().invoke(toPlayer()); - this.gameProfile = (GameProfile) ReflectionCache.GET_PROFILE_METHOD.get().invoke(playerHandle, new Object[0]); - this.playerConnection = ReflectionCache.PLAYER_CONNECTION_FIELD.get().get(playerHandle); + Object playerHandle = Reflections.GET_HANDLE_PLAYER_METHOD.get().invoke(toPlayer()); + this.gameProfile = (GameProfile) Reflections.GET_PROFILE_METHOD.get().invoke(playerHandle, new Object[0]); + this.playerConnection = Reflections.PLAYER_CONNECTION_FIELD.get().get(playerHandle); } catch (IllegalAccessException | InvocationTargetException e) { throw new IllegalStateException("can't create user for player " + uuid.toString(), e.getCause()); } @@ -62,7 +62,11 @@ public class ZUser { public void run() { Exception ex = user.tryRegisterChannel(); Player player = user.toPlayer(); - if (player == null || !player.isOnline() || ex == null) { + if (player == null) { + tries--; + return; + } + else if (!player.isOnline() || ex == null) { cancel(); return; } @@ -78,7 +82,7 @@ public class ZUser { private Exception tryRegisterChannel() { try { - Channel channel = (Channel) ReflectionCache.CHANNEL_FIELD.get().get(ReflectionCache.NETWORK_MANAGER_FIELD.get().get(this.playerConnection)); + Channel channel = (Channel) Reflections.CHANNEL_FIELD.get().get(Reflections.NETWORK_MANAGER_FIELD.get().get(this.playerConnection)); if (channel.pipeline().names().contains("npc_interact")) channel.pipeline().remove("npc_interact"); channel.pipeline().addAfter("decoder", "npc_interact", new ZNPCSocketDecoder()); return null; @@ -138,11 +142,11 @@ public class ZUser { class ZNPCSocketDecoder extends MessageToMessageDecoder { protected void decode(ChannelHandlerContext channelHandlerContext, Object packet, List out) throws Exception { out.add(packet); - if (packet.getClass() == ReflectionCache.PACKET_PLAY_IN_USE_ENTITY_CLASS) { + if (packet.getClass() == Reflections.PACKET_PLAY_IN_USE_ENTITY_CLASS) { long lastInteractNanos = System.nanoTime() - ZUser.this.lastInteract; if (ZUser.this.lastInteract != 0L && lastInteractNanos < 1000000000L) return; - int entityId = ReflectionCache.PACKET_IN_USE_ENTITY_ID_FIELD.get().getInt(packet); + int entityId = Reflections.PACKET_IN_USE_ENTITY_ID_FIELD.get().getInt(packet); NPC npc = NPC.all().stream().filter(npc1 -> (npc1.getEntityID() == entityId)).findFirst().orElse(null); if (npc == null) return; diff --git a/src/main/java/io/github/znetworkw/znpcservers/utility/Utils.java b/src/main/java/io/github/znetworkw/znpcservers/utility/Utils.java index acc8bd3..b0a65b6 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/utility/Utils.java +++ b/src/main/java/io/github/znetworkw/znpcservers/utility/Utils.java @@ -1,6 +1,6 @@ package io.github.znetworkw.znpcservers.utility; -import io.github.znetworkw.znpcservers.reflection.ReflectionCache; +import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.configuration.ConfigurationConstants; import io.github.znetworkw.znpcservers.user.ZUser; import me.clip.placeholderapi.PlaceholderAPI; @@ -76,7 +76,7 @@ public final class Utils { try { for (Object packet : packets) { if (packet != null) - ReflectionCache.SEND_PACKET_METHOD.get().invoke(user.getPlayerConnection(), packet); + Reflections.SEND_PACKET_METHOD.get().invoke(user.getPlayerConnection(), packet); } } catch (IllegalAccessException | java.lang.reflect.InvocationTargetException e) { e.printStackTrace();