fix itemstack properties in the api

This commit is contained in:
Pyrbu 2024-02-16 17:29:44 +01:00
parent 00de9ef636
commit c4bb24c888
4 changed files with 37 additions and 9 deletions

@ -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 {
*/
<T> void setProperty(EntityProperty<T> 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<EntityProperty<?>> getAppliedProperties();
}

@ -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<EntityProperty<?>> getAppliedProperties() {
return properties.getAppliedProperties();

@ -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<M> 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<EntityProperty<?>> getAppliedProperties() {
return properties;

@ -148,21 +148,26 @@ public class NpcImpl extends Viewable implements Npc {
return propertyMap.containsKey((EntityPropertyImpl<?>) key);
}
@SuppressWarnings("unchecked")
@Override
public <T> void setProperty(EntityProperty<T> key, T value) {
setProperty((EntityPropertyImpl<T>) 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<T>) key, (T) val);
}
@SuppressWarnings("unchecked")
@Override
public void setItemProperty(EntityProperty<?> key, ItemStack value) {
setProperty((EntityPropertyImpl<com.github.retrooper.packetevents.protocol.item.ItemStack>) key, SpigotConversionUtil.fromBukkitItemStack(value));
}
public <T> void setProperty(EntityPropertyImpl<T> 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);
}