diff --git a/api/src/main/java/lol/pyr/znpcsplus/api/entity/PropertyHolder.java b/api/src/main/java/lol/pyr/znpcsplus/api/entity/PropertyHolder.java index cd10b37..65c34a1 100644 --- a/api/src/main/java/lol/pyr/znpcsplus/api/entity/PropertyHolder.java +++ b/api/src/main/java/lol/pyr/znpcsplus/api/entity/PropertyHolder.java @@ -1,5 +1,7 @@ package lol.pyr.znpcsplus.api.entity; +import org.bukkit.inventory.ItemStack; + import java.util.Set; /** @@ -32,10 +34,19 @@ public interface PropertyHolder { */ void setProperty(EntityProperty key, T value); + /** + * Weird fix which is sadly required in order to not decrease performance + * when using item properties, read https://github.com/Pyrbu/ZNPCsPlus/pull/129#issuecomment-1948777764 + * + * @param key Unique key representing a property + * @param value The value to assign to the property key on this holder + */ + void setItemProperty(EntityProperty key, ItemStack value); + /** * Method used to get a set of all of the property keys that this holder has a value for * - * @return List of property keys + * @return Set of property keys */ Set> getAppliedProperties(); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/PacketEntity.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/PacketEntity.java index a827221..e198779 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/PacketEntity.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/PacketEntity.java @@ -10,6 +10,7 @@ import lol.pyr.znpcsplus.packets.PacketFactory; import lol.pyr.znpcsplus.reflection.Reflections; import lol.pyr.znpcsplus.util.NpcLocation; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import java.util.Collection; import java.util.Set; @@ -97,6 +98,11 @@ public class PacketEntity implements PropertyHolder { properties.setProperty(key, value); } + @Override + public void setItemProperty(EntityProperty key, ItemStack value) { + properties.setItemProperty(key, value); + } + @Override public Set> getAppliedProperties() { return properties.getAppliedProperties(); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramLine.java b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramLine.java index 9bb695f..f2652f0 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramLine.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramLine.java @@ -7,6 +7,7 @@ import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.packets.PacketFactory; import lol.pyr.znpcsplus.util.NpcLocation; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import java.util.Collection; import java.util.HashSet; @@ -70,6 +71,11 @@ public class HologramLine implements PropertyHolder { throw new UnsupportedOperationException("Can't set properties on a hologram line"); } + @Override + public void setItemProperty(EntityProperty key, ItemStack value) { + throw new UnsupportedOperationException("Can't set properties on a hologram line"); + } + @Override public Set> getAppliedProperties() { return properties; diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java index 3e421e6..3b17eee 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java @@ -148,21 +148,26 @@ public class NpcImpl extends Viewable implements Npc { return propertyMap.containsKey((EntityPropertyImpl) key); } + @SuppressWarnings("unchecked") @Override public void setProperty(EntityProperty key, T value) { - setProperty((EntityPropertyImpl) key, value); + // See https://github.com/Pyrbu/ZNPCsPlus/pull/129#issuecomment-1948777764 + Object val = value; + if (val instanceof ItemStack) val = SpigotConversionUtil.fromBukkitItemStack((ItemStack) val); + + setProperty((EntityPropertyImpl) key, (T) val); + } + + @SuppressWarnings("unchecked") + @Override + public void setItemProperty(EntityProperty key, ItemStack value) { + setProperty((EntityPropertyImpl) key, SpigotConversionUtil.fromBukkitItemStack(value)); } public void setProperty(EntityPropertyImpl key, T value) { if (key == null) return; if (value == null || value.equals(key.getDefaultValue())) propertyMap.remove(key); - if (value instanceof ItemStack) { - ItemStack item = (ItemStack) value; - com.github.retrooper.packetevents.protocol.item.ItemStack packetItem = SpigotConversionUtil.fromBukkitItemStack(item); - propertyMap.put(key, packetItem); - } else { - propertyMap.put(key, value); - } + else propertyMap.put(key, value); UNSAFE_refreshProperty(key); }