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 0e75e4b..003a336 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java @@ -176,7 +176,7 @@ public class NPC { try { Object nmsWorld = Reflections.GET_HANDLE_WORLD_METHOD.get().invoke(getLocation().getWorld()); boolean isPlayer = (npcType == NPCType.PLAYER); - this.nmsEntity = isPlayer ? this.packets.getNms().getPlayerPacket(nmsWorld, this.gameProfile) : (Utils.versionNewer(14) ? npcType.getConstructor().newInstance(npcType.getNmsEntityType(), nmsWorld) : npcType.getConstructor().newInstance(nmsWorld)); + this.nmsEntity = isPlayer ? this.packets.getNms().createPlayer(nmsWorld, this.gameProfile) : (Utils.versionNewer(14) ? npcType.getConstructor().newInstance(npcType.getNmsEntityType(), nmsWorld) : npcType.getConstructor().newInstance(nmsWorld)); 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) { @@ -215,13 +215,13 @@ public class NPC { if (FunctionFactory.isTrue(this, "mirror")) updateProfile(user.getGameProfile().getProperties()); Utils.sendPackets(user, this.tabConstructor, this.updateTabConstructor); } - Utils.sendPackets(user, this.packets.getNms().getSpawnPacket(this.nmsEntity, npcIsPlayer)); + Utils.sendPackets(user, this.packets.getNms().createSpawnPacket(this.nmsEntity, npcIsPlayer)); if (FunctionFactory.isTrue(this, "holo")) this.hologram.spawn(user); updateMetadata(Collections.singleton(user)); sendEquipPackets(user); lookAt(user, getLocation(), true); if (npcIsPlayer) { - Object removeTabPacket = this.packets.getNms().getTabRemovePacket(this.nmsEntity); + Object removeTabPacket = this.packets.getNms().createTabRemovePacket(this.nmsEntity); ZNPCsPlus.SCHEDULER.scheduleSyncDelayedTask(() -> Utils.sendPackets(user, removeTabPacket, this.updateTabConstructor), 60); } } catch (ReflectiveOperationException operationException) { @@ -238,9 +238,9 @@ public class NPC { private void handleDelete(ZUser user) { try { - if (this.npcPojo.getNpcType() == NPCType.PLAYER) this.packets.getNms().getTabRemovePacket(this.nmsEntity); + if (this.npcPojo.getNpcType() == NPCType.PLAYER) this.packets.getNms().createTabRemovePacket(this.nmsEntity); this.hologram.delete(user); - Utils.sendPackets(user, this.packets.getNms().getDestroyPacket(this.entityID)); + Utils.sendPackets(user, this.packets.getNms().createEntityDestroyPacket(this.entityID)); } catch (ReflectiveOperationException operationException) { throw new UnexpectedCallException(operationException); } @@ -267,7 +267,7 @@ public class NPC { protected void updateMetadata(Iterable users) { try { - Object metaData = this.packets.getNms().getMetadataPacket(this.entityID, this.nmsEntity); + Object metaData = this.packets.getNms().createMetadataPacket(this.entityID, this.nmsEntity); for (ZUser user : users) Utils.sendPackets(user, metaData); } catch (ReflectiveOperationException operationException) { operationException.getCause().printStackTrace(); @@ -290,7 +290,7 @@ public class NPC { public void sendEquipPackets(ZUser zUser) { if (this.npcPojo.getNpcEquip().isEmpty()) return; try { - ImmutableList equipPackets = this.packets.getNms().getEquipPackets(this); + ImmutableList equipPackets = this.packets.getNms().createEquipmentPacket(this); equipPackets.forEach(o -> Utils.sendPackets(zUser, o)); } catch (ReflectiveOperationException operationException) { throw new UnexpectedCallException(operationException.getCause()); 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 df96570..8a12a60 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 @@ -35,7 +35,7 @@ public class GlowFunction extends NPCFunction { } protected boolean allow(NPC npc) { - return npc.getPackets().getNms().allowGlowColor(); + return npc.getPackets().getNms().allowsGlowColor(); } public NPCFunction.ResultType resolve(NPC npc) { 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 aba678b..9b07682 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 @@ -46,7 +46,7 @@ public class Hologram { y += LINE_SPACING; } setLocation(location, 0.0D); - this.npc.getPackets().flushCache("getHologramSpawnPacket"); + this.npc.getPackets().flushCache("createArmorStandSpawnPacket"); this.npc.getViewers().forEach(this::spawn); } catch (ReflectiveOperationException operationException) { throw new UnexpectedCallException(operationException); @@ -56,7 +56,7 @@ public class Hologram { public void spawn(ZUser user) { this.hologramLines.forEach(hologramLine -> { try { - Object entityPlayerPacketSpawn = this.npc.getPackets().getNms().getHologramSpawnPacket(hologramLine.armorStand); + Object entityPlayerPacketSpawn = this.npc.getPackets().getNms().createArmorStandSpawnPacket(hologramLine.armorStand); Utils.sendPackets(user, entityPlayerPacketSpawn); } catch (ReflectiveOperationException operationException) { delete(user); @@ -67,7 +67,7 @@ public class Hologram { public void delete(ZUser user) { this.hologramLines.forEach(hologramLine -> { try { - Utils.sendPackets(user, this.npc.getPackets().getNms().getDestroyPacket(hologramLine.id)); + Utils.sendPackets(user, this.npc.getPackets().getNms().createEntityDestroyPacket(hologramLine.id)); } catch (ReflectiveOperationException operationException) { throw new UnexpectedCallException(operationException); } @@ -78,7 +78,7 @@ public class Hologram { for (HologramLine hologramLine : this.hologramLines) { try { updateLine(hologramLine.line, hologramLine.armorStand, user); - Object metaData = this.npc.getPackets().getNms().getMetadataPacket(hologramLine.id, hologramLine.armorStand); + Object metaData = this.npc.getPackets().getNms().createMetadataPacket(hologramLine.id, hologramLine.armorStand); Utils.sendPackets(user, metaData); } catch (ReflectiveOperationException operationException) { throw new UnexpectedCallException(operationException); diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMS.java b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMS.java index 7f7bdd7..36b6248 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMS.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMS.java @@ -18,23 +18,21 @@ public interface NMS { int version(); @PacketValue(keyName = "playerPacket") - Object getPlayerPacket(Object paramObject, GameProfile paramGameProfile) throws ReflectiveOperationException; + Object createPlayer(Object paramObject, GameProfile paramGameProfile) throws ReflectiveOperationException; @PacketValue(keyName = "spawnPacket") - Object getSpawnPacket(Object paramObject, boolean paramBoolean) throws ReflectiveOperationException; + Object createSpawnPacket(Object paramObject, boolean paramBoolean) throws ReflectiveOperationException; - Object convertItemStack(int paramInt, ItemSlot paramItemSlot, ItemStack paramItemStack) throws ReflectiveOperationException; + Object createEntityEquipmentPacket(int paramInt, ItemSlot paramItemSlot, ItemStack paramItemStack) throws ReflectiveOperationException; - Object getClickType(Object paramObject) throws ReflectiveOperationException; - - Object getMetadataPacket(int paramInt, Object paramObject) throws ReflectiveOperationException; + Object createMetadataPacket(int paramInt, Object paramObject) throws ReflectiveOperationException; @PacketValue(keyName = "hologramSpawnPacket", valueType = ValueType.ARGUMENTS) - Object getHologramSpawnPacket(Object paramObject) throws ReflectiveOperationException; + Object createArmorStandSpawnPacket(Object paramObject) throws ReflectiveOperationException; @SuppressWarnings("SuspiciousTernaryOperatorInVarargsCall") @PacketValue(keyName = "destroyPacket", valueType = ValueType.ARGUMENTS) - default Object getDestroyPacket(int entityId) throws ReflectiveOperationException { + default Object createEntityDestroyPacket(int entityId) throws ReflectiveOperationException { 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); } @@ -44,7 +42,7 @@ public interface NMS { } @PacketValue(keyName = "removeTab") - default Object getTabRemovePacket(Object nmsEntity) throws ReflectiveOperationException { + default Object createTabRemovePacket(Object nmsEntity) throws ReflectiveOperationException { try { return Reflections.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(Reflections.REMOVE_PLAYER_FIELD.get(), Collections.singletonList(nmsEntity)); } catch (Throwable throwable) { @@ -55,7 +53,7 @@ public interface NMS { } @PacketValue(keyName = "equipPackets") - ImmutableList getEquipPackets(NPC paramNPC) throws ReflectiveOperationException; + ImmutableList createEquipmentPacket(NPC paramNPC) throws ReflectiveOperationException; @SuppressWarnings("unchecked") @PacketValue(keyName = "scoreboardPackets") @@ -91,15 +89,15 @@ public interface NMS { } else { collection.add(npc.getUUID().toString()); } - if (allowGlowColor() && FunctionFactory.isTrue(npc, "glow")) - updateGlowPacket(npc, scoreboardTeamPacket); + if (allowsGlowColor() && FunctionFactory.isTrue(npc, "glow")) + updateGlow(npc, scoreboardTeamPacket); builder.add(isVersion17 ? Reflections.PACKET_PLAY_OUT_SCOREBOARD_TEAM_CREATE.get().invoke(null, scoreboardTeamPacket, Boolean.TRUE) : scoreboardTeamPacket); return builder.build(); } - void updateGlowPacket(NPC paramNPC, Object paramObject) throws ReflectiveOperationException; + void updateGlow(NPC paramNPC, Object paramObject) throws ReflectiveOperationException; - boolean allowGlowColor(); + boolean allowsGlowColor(); default void update(PacketCache packetCache) throws ReflectiveOperationException { packetCache.flushCache("scoreboardPackets"); diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV16.java b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV16.java index a1662a0..5934172 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV16.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV16.java @@ -16,12 +16,12 @@ public class NMSV16 extends NMSV9 { return 16; } - public ImmutableList getEquipPackets(NPC npc) throws ReflectiveOperationException { + public ImmutableList createEquipmentPacket(NPC npc) throws ReflectiveOperationException { List> pairs = Lists.newArrayListWithCapacity((ItemSlot.values()).length); for (Map.Entry entry : npc.getNpcPojo().getNpcEquip().entrySet()) pairs.add(new Pair<>(getItemSlot(entry .getKey().getSlot()), - convertItemStack(npc.getEntityID(), entry.getKey(), entry.getValue()))); + createEntityEquipmentPacket(npc.getEntityID(), entry.getKey(), entry.getValue()))); 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/nms/NMSV17.java b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV17.java index 27b902f..46d25ec 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV17.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV17.java @@ -11,15 +11,11 @@ public class NMSV17 extends NMSV16 { return 17; } - public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException { + public Object createPlayer(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException { 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 { + public void updateGlow(NPC npc, Object packet) throws ReflectiveOperationException { Utils.setValue(packet, "n", Reflections.ENUM_CHAT_FORMAT_FIND.get().invoke(null, npc.getNpcPojo().getGlowName())); } - - public Object getClickType(Object interactPacket) { - return "INTERACT"; - } } diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV18.java b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV18.java index 8a45d78..076c792 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV18.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV18.java @@ -9,7 +9,7 @@ public class NMSV18 extends NMSV17 { return 18; } - public void updateGlowPacket(NPC npc, Object packet) throws ReflectiveOperationException { + public void updateGlow(NPC npc, Object packet) throws ReflectiveOperationException { 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/nms/NMSV19.java b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV19.java index 4323348..fde6f66 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV19.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV19.java @@ -9,7 +9,7 @@ public class NMSV19 extends NMSV18 { return 19; } - public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException { + public Object createPlayer(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException { try { return Reflections.PLAYER_CONSTRUCTOR_NEW_1.get().newInstance(Reflections.GET_SERVER_METHOD.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile, null); } catch (Throwable e) { diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV8.java b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV8.java index dce9679..fe73f56 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV8.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV8.java @@ -17,26 +17,22 @@ public class NMSV8 implements NMS { return 8; } - public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException { + public Object createPlayer(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException { 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 { + public Object createSpawnPacket(Object nmsEntity, boolean isPlayer) throws ReflectiveOperationException { 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 { + public Object createEntityEquipmentPacket(int entityId, ItemSlot itemSlot, ItemStack itemStack) throws ReflectiveOperationException { 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 { - return Utils.getValue(interactPacket, "action"); - } - - public Object getMetadataPacket(int entityId, Object nmsEntity) throws ReflectiveOperationException { + public Object createMetadataPacket(int entityId, Object nmsEntity) throws ReflectiveOperationException { Object dataWatcher = Reflections.GET_DATA_WATCHER_METHOD.get().invoke(nmsEntity); try { return Reflections.PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR.get().newInstance(entityId, dataWatcher, true); @@ -45,24 +41,24 @@ public class NMSV8 implements NMS { } } - public Object getHologramSpawnPacket(Object armorStand) throws ReflectiveOperationException { + public Object createArmorStandSpawnPacket(Object armorStand) throws ReflectiveOperationException { return Reflections.PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR.get().newInstance(armorStand); } - public ImmutableList getEquipPackets(NPC npc) throws ReflectiveOperationException { + public ImmutableList createEquipmentPacket(NPC npc) throws ReflectiveOperationException { ImmutableList.Builder builder = ImmutableList.builder(); for (Map.Entry stackEntry : npc.getNpcPojo().getNpcEquip().entrySet()) { builder.add(Reflections.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_OLD.get().newInstance(npc.getEntityID(), stackEntry.getKey().getSlotOld(), - convertItemStack(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue()))); + createEntityEquipmentPacket(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue()))); } return builder.build(); } - public void updateGlowPacket(NPC npc, Object packet) throws ReflectiveOperationException { + public void updateGlow(NPC npc, Object packet) throws ReflectiveOperationException { throw new IllegalStateException("Glow color is not supported for 1.8 version."); } - public boolean allowGlowColor() { + public boolean allowsGlowColor() { return false; } } diff --git a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV9.java b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV9.java index 2188645..376818b 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV9.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/nms/NMSV9.java @@ -14,21 +14,21 @@ public class NMSV9 extends NMSV8 { return 9; } - public Object convertItemStack(int entityId, ItemSlot itemSlot, ItemStack itemStack) throws ReflectiveOperationException { + public Object createEntityEquipmentPacket(int entityId, ItemSlot itemSlot, ItemStack itemStack) throws ReflectiveOperationException { return Reflections.AS_NMS_COPY_METHOD.get().invoke(Reflections.CRAFT_ITEM_STACK_CLASS, itemStack); } - public ImmutableList getEquipPackets(NPC npc) throws ReflectiveOperationException { + public ImmutableList createEquipmentPacket(NPC npc) throws ReflectiveOperationException { ImmutableList.Builder builder = ImmutableList.builder(); for (Map.Entry stackEntry : npc.getNpcPojo().getNpcEquip().entrySet()) { 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()))); + createEntityEquipmentPacket(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue()))); } return builder.build(); } - public void updateGlowPacket(NPC npc, Object packet) throws ReflectiveOperationException { + public void updateGlow(NPC npc, Object packet) throws ReflectiveOperationException { Object enumChatString = Reflections.ENUM_CHAT_TO_STRING_METHOD.get().invoke(npc.getGlowColor()); if (Utils.BUKKIT_VERSION > 12) { Utils.setValue(packet, npc.getGlowColor(), Reflections.ENUM_CHAT_CLASS); @@ -39,7 +39,7 @@ public class NMSV9 extends NMSV8 { } } - public boolean allowGlowColor() { + public boolean allowsGlowColor() { return true; } }