From 1010a18e71e37f9f05861c9dfc3082a78bd0cd2a Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Sun, 10 Sep 2023 15:26:47 +0530 Subject: [PATCH] partial implementation of target npc property --- .../commands/property/PropertySetCommand.java | 4 +++ .../entity/properties/TargetNpcProperty.java | 29 +++++++++++++++++++ .../TargetNpcPropertySerializer.java | 21 ++++++++++++++ .../znpcsplus/npc/NpcTypeRegistryImpl.java | 2 +- 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/TargetNpcProperty.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/TargetNpcPropertySerializer.java diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertySetCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertySetCommand.java index d8e7a21..3ea0074 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertySetCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertySetCommand.java @@ -113,6 +113,10 @@ public class PropertySetCommand implements CommandHandler { valueName = String.valueOf(value); } } + else if (type == NpcEntryImpl.class) { + value = context.parse(type); + valueName = value == null ? "NONE" : ((NpcEntryImpl) value).getId(); + } else { value = context.parse(type); valueName = String.valueOf(value); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/TargetNpcProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/TargetNpcProperty.java new file mode 100644 index 0000000..fbe4beb --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/TargetNpcProperty.java @@ -0,0 +1,29 @@ +package lol.pyr.znpcsplus.entity.properties; + +import com.github.retrooper.packetevents.protocol.entity.data.EntityData; +import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; +import lol.pyr.znpcsplus.entity.EntityPropertyImpl; +import lol.pyr.znpcsplus.entity.PacketEntity; +import lol.pyr.znpcsplus.npc.NpcEntryImpl; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class TargetNpcProperty extends EntityPropertyImpl { + private final int index; + + public TargetNpcProperty(String name, int index, NpcEntryImpl defaultValue) { + super(name, defaultValue, NpcEntryImpl.class); + this.index = index; + } + + @Override + public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { + NpcEntryImpl value = entity.getProperty(this); + if (value == null) return; + if (value.getNpc().getEntity().getEntityId() == entity.getEntityId()) return; + if (value.getNpc().isVisibleTo(player)) { + properties.put(index, newEntityData(index, EntityDataTypes.INT, value.getNpc().getEntity().getEntityId())); + } + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/TargetNpcPropertySerializer.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/TargetNpcPropertySerializer.java new file mode 100644 index 0000000..7cd3654 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/TargetNpcPropertySerializer.java @@ -0,0 +1,21 @@ +package lol.pyr.znpcsplus.entity.serializers; + +import lol.pyr.znpcsplus.entity.PropertySerializer; +import lol.pyr.znpcsplus.npc.NpcEntryImpl; + +public class TargetNpcPropertySerializer implements PropertySerializer { + @Override + public String serialize(NpcEntryImpl property) { + return property.getId(); + } + + @Override + public NpcEntryImpl deserialize(String property) { + return null; // TODO: find a way to do this + } + + @Override + public Class getTypeClass() { + return NpcEntryImpl.class; + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java index bcb0856..509e246 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -329,7 +329,7 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { register(builder(p, "frog", EntityTypes.FROG) .setHologramOffset(-1.475) - .addProperties("frog_variant")); + .addProperties("frog_variant", "frog_target_npc")); register(builder(p, "tadpole", EntityTypes.TADPOLE) .setHologramOffset(-1.675));