Merge branch 'alexdev032.X' into 2.X

This commit is contained in:
Pyrbu 2024-02-16 17:46:57 +01:00
commit 828a8a3465
4 changed files with 38 additions and 2 deletions

@ -1,5 +1,7 @@
package lol.pyr.znpcsplus.api.entity; package lol.pyr.znpcsplus.api.entity;
import org.bukkit.inventory.ItemStack;
import java.util.Set; import java.util.Set;
/** /**
@ -32,10 +34,19 @@ public interface PropertyHolder {
*/ */
<T> void setProperty(EntityProperty<T> key, T value); <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 * 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(); Set<EntityProperty<?>> getAppliedProperties();
} }

@ -10,6 +10,7 @@ import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.reflection.Reflections; import lol.pyr.znpcsplus.reflection.Reflections;
import lol.pyr.znpcsplus.util.NpcLocation; import lol.pyr.znpcsplus.util.NpcLocation;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.Collection; import java.util.Collection;
import java.util.Set; import java.util.Set;
@ -97,6 +98,11 @@ public class PacketEntity implements PropertyHolder {
properties.setProperty(key, value); properties.setProperty(key, value);
} }
@Override
public void setItemProperty(EntityProperty<?> key, ItemStack value) {
properties.setItemProperty(key, value);
}
@Override @Override
public Set<EntityProperty<?>> getAppliedProperties() { public Set<EntityProperty<?>> getAppliedProperties() {
return properties.getAppliedProperties(); return properties.getAppliedProperties();

@ -7,6 +7,7 @@ import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.packets.PacketFactory; import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.util.NpcLocation; import lol.pyr.znpcsplus.util.NpcLocation;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; 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"); 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 @Override
public Set<EntityProperty<?>> getAppliedProperties() { public Set<EntityProperty<?>> getAppliedProperties() {
return properties; return properties;

@ -1,6 +1,7 @@
package lol.pyr.znpcsplus.npc; package lol.pyr.znpcsplus.npc;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import lol.pyr.znpcsplus.api.entity.EntityProperty; import lol.pyr.znpcsplus.api.entity.EntityProperty;
import lol.pyr.znpcsplus.api.npc.Npc; import lol.pyr.znpcsplus.api.npc.Npc;
import lol.pyr.znpcsplus.api.npc.NpcType; import lol.pyr.znpcsplus.api.npc.NpcType;
@ -18,6 +19,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -146,9 +148,20 @@ public class NpcImpl extends Viewable implements Npc {
return propertyMap.containsKey((EntityPropertyImpl<?>) key); return propertyMap.containsKey((EntityPropertyImpl<?>) key);
} }
@SuppressWarnings("unchecked")
@Override @Override
public <T> void setProperty(EntityProperty<T> key, T value) { 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) { public <T> void setProperty(EntityPropertyImpl<T> key, T value) {