From cf34ddc32eec0a4e2f022c2c72bbc635997721c2 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Mon, 24 Apr 2023 14:18:49 +0100 Subject: [PATCH 1/2] filter out null equipment (fixes #25) --- src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 cb4cc41..7faea9d 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java @@ -244,7 +244,7 @@ public class NPC { PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerSpawnPlayer(entityID, this.gameProfile.getId(), SpigotConversionUtil.fromBukkitLocation(location.toBukkitLocation()))); PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerEntityMetadata(entityID, - List.of(new EntityData(17, EntityDataTypes.BYTE, Byte.MAX_VALUE)))); + List.of(new EntityData(17, EntityDataTypes.BYTE, Byte.MAX_VALUE)))); // 17 only works on ~1.19, this will need to be fixed in the entity rewrite PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerEntityHeadLook(entityID, location.getYaw())); }); } @@ -337,6 +337,8 @@ public class NPC { public void sendEquipPackets(ZUser zUser) { if (this.npcPojo.getNpcEquip().isEmpty()) return; List equipment = npcPojo.getNpcEquip().entrySet().stream() + .filter(entry -> Objects.nonNull(entry.getKey())) + .filter(entry -> Objects.nonNull(entry.getValue())) .map(entry -> new Equipment(entry.getKey(), SpigotConversionUtil.fromBukkitItemStack(entry.getValue()))) .toList(); From df668a8585dfe2741596b8ace1c266fc2635f3ab Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Mon, 24 Apr 2023 14:27:45 +0100 Subject: [PATCH 2/2] refactor a day keeps the doctor away --- .../github/znetworkw/znpcservers/npc/NPC.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) 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 7faea9d..076a28b 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java +++ b/src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java @@ -58,8 +58,7 @@ public class NPC { this.npcName = NamingType.DEFAULT.resolve(this); this.npcSkin = NPCSkin.forValues(npcModel.getSkin(), npcModel.getSignature()); this.uuid = npcModel.getUuid(); - if (load) - onLoad(); + if (load) onLoad(); } public NPC(NPCModel npcModel) { @@ -72,8 +71,7 @@ public class NPC { public static void unregister(int id) { NPC npc = find(id); - if (npc == null) - throw new IllegalStateException("can't find npc with id " + id); + if (npc == null) throw new IllegalStateException("can't find npc with id " + id); NPC_MAP.remove(id); npc.deleteViewers(); } @@ -97,8 +95,7 @@ public class NPC { updateProfile(this.gameProfile.getProperties()); setLocation(getNpcPojo().getLocation().toBukkitLocation(), false); this.hologram.createHologram(); - if (this.npcPojo.getPathName() != null) - setPath(NPCPath.AbstractTypeWriter.find(this.npcPojo.getPathName())); + if (this.npcPojo.getPathName() != null) setPath(NPCPath.AbstractTypeWriter.find(this.npcPojo.getPathName())); this.npcPojo.getCustomizationMap().forEach((key, value) -> this.npcPojo.getNpcType().updateCustomization(this, key, value)); } NPC_MAP.put(getNpcPojo().getId(), this); @@ -180,12 +177,9 @@ public class NPC { public void setSecondLayerSkin() { try { Object dataWatcherObject = Reflections.GET_DATA_WATCHER_METHOD.get().invoke(this.nmsEntity); - if (Utils.versionNewer(9)) { - Reflections.SET_DATA_WATCHER_METHOD.get().invoke(dataWatcherObject, Reflections.DATA_WATCHER_OBJECT_CONSTRUCTOR.get() + if (Utils.versionNewer(9)) 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 { - Reflections.WATCH_DATA_WATCHER_METHOD.get().invoke(dataWatcherObject, 10, (byte) 127); - } + else Reflections.WATCH_DATA_WATCHER_METHOD.get().invoke(dataWatcherObject, 10, (byte) 127); } catch (ReflectiveOperationException operationException) { throw new UnexpectedCallException(operationException); } @@ -325,10 +319,10 @@ public class NPC { public void updateProfile(PropertyMap propertyMap) { if (this.npcPojo.getNpcType() != NPCType.PLAYER) return; try { - 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); + Object gameProfile = Reflections.GET_PROFILE_METHOD.get().invoke(this.nmsEntity); + Utils.setValue(gameProfile, "name", this.gameProfile.getName()); + Utils.setValue(gameProfile, "id", this.gameProfile.getId()); + Utils.setValue(gameProfile, "properties", propertyMap); } catch (ReflectiveOperationException operationException) { throw new UnexpectedCallException(operationException); }