From f933f17e4516ed2c6e4d5414e166f28d9386b529 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Thu, 14 Dec 2023 11:49:49 +0100 Subject: [PATCH] stop using deprecated entity data types for components --- .../main/java/lol/pyr/znpcsplus/ZNpcsPlus.java | 2 +- .../entity/EntityPropertyRegistryImpl.java | 5 +++-- .../entity/properties/DinnerboneProperty.java | 6 ++---- .../znpcsplus/entity/properties/NameProperty.java | 15 ++++++++------- .../entity/properties/RabbitTypeProperty.java | 6 ++---- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 21383c2..8264383 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -125,7 +125,7 @@ public class ZNpcsPlus { MojangSkinCache skinCache = new MojangSkinCache(configManager); EntityPropertyRegistryImpl propertyRegistry = new EntityPropertyRegistryImpl(skinCache, configManager); PacketFactory packetFactory = setupPacketFactory(scheduler, propertyRegistry, configManager); - propertyRegistry.registerTypes(packetFactory); + propertyRegistry.registerTypes(packetFactory, textSerializer); ActionRegistry actionRegistry = new ActionRegistry(); NpcTypeRegistryImpl typeRegistry = new NpcTypeRegistryImpl(); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java index fddc533..9818194 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -21,6 +21,7 @@ import lol.pyr.znpcsplus.entity.serializers.*; import lol.pyr.znpcsplus.packets.PacketFactory; import lol.pyr.znpcsplus.skin.cache.MojangSkinCache; import lol.pyr.znpcsplus.util.*; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.Color; import org.bukkit.DyeColor; @@ -106,7 +107,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { */ } - public void registerTypes(PacketFactory packetFactory) { + public void registerTypes(PacketFactory packetFactory, LegacyComponentSerializer textSerializer) { ServerVersion ver = PacketEvents.getAPI().getServerManager().getVersion(); boolean legacyBooleans = ver.isOlderThan(ServerVersion.V_1_9); boolean legacyNames = ver.isOlderThan(ServerVersion.V_1_9); @@ -119,7 +120,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { register(new EquipmentProperty(packetFactory, "hand", EquipmentSlot.MAIN_HAND)); register(new EquipmentProperty(packetFactory, "offhand", EquipmentSlot.OFF_HAND)); - register(new NameProperty(legacyNames, optionalComponents)); + register(new NameProperty(textSerializer, legacyNames, optionalComponents)); register(new DummyProperty<>("display_name", String.class)); register(new DinnerboneProperty(legacyNames, optionalComponents)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DinnerboneProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DinnerboneProperty.java index a729c63..e8b81a6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DinnerboneProperty.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DinnerboneProperty.java @@ -19,11 +19,9 @@ public class DinnerboneProperty extends EntityPropertyImpl { public DinnerboneProperty(boolean legacy, boolean optional) { super("dinnerbone", false, Boolean.class); Component name = Component.text("Dinnerbone"); - String serialized = legacy ? - AdventureSerializer.getLegacyGsonSerializer().serialize(name) : - AdventureSerializer.getGsonSerializer().serialize(name); + Object serialized = legacy ? AdventureSerializer.getLegacyGsonSerializer().serialize(name) : name; this.serialized = optional ? Optional.of(serialized) : serialized; - this.type = optional ? EntityDataTypes.OPTIONAL_COMPONENT : EntityDataTypes.STRING; + this.type = optional ? EntityDataTypes.OPTIONAL_ADV_COMPONENT : EntityDataTypes.STRING; } @Override diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/NameProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/NameProperty.java index e3b7230..426101f 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/NameProperty.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/NameProperty.java @@ -7,17 +7,20 @@ import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.util.PapiUtil; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.entity.Player; import java.util.Map; import java.util.Optional; public class NameProperty extends EntityPropertyImpl { + private final LegacyComponentSerializer legacySerializer; private final boolean legacy; private final boolean optional; - public NameProperty(boolean legacy, boolean optional) { + public NameProperty(LegacyComponentSerializer legacySerializer, boolean legacy, boolean optional) { super("name", null, Component.class); + this.legacySerializer = legacySerializer; this.legacy = legacy; this.optional = optional; @@ -27,12 +30,10 @@ public class NameProperty extends EntityPropertyImpl { public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { Component value = entity.getProperty(this); if (value != null) { - String serialized = legacy ? - AdventureSerializer.getLegacyGsonSerializer().serialize(value) : - AdventureSerializer.getGsonSerializer().serialize(value); - serialized = PapiUtil.set(player, serialized); - if (optional) properties.put(2, newEntityData(2, EntityDataTypes.OPTIONAL_COMPONENT, Optional.of(serialized))); - else properties.put(2, newEntityData(2, EntityDataTypes.STRING, serialized)); + value = PapiUtil.set(legacySerializer, player, value); + Object serialized = legacy ? AdventureSerializer.getLegacyGsonSerializer().serialize(value) : value; + if (optional) properties.put(2, new EntityData(2, EntityDataTypes.OPTIONAL_ADV_COMPONENT, Optional.of(serialized))); + else properties.put(2, new EntityData(2, EntityDataTypes.STRING, serialized)); } if (legacy) properties.put(3, newEntityData(3, EntityDataTypes.BYTE, (byte) (value != null ? 1 : 0))); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/RabbitTypeProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/RabbitTypeProperty.java index 3a7532a..c67bd36 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/RabbitTypeProperty.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/RabbitTypeProperty.java @@ -24,11 +24,9 @@ public class RabbitTypeProperty extends EntityPropertyImpl { this.index = index; this.legacyBooleans = legacyBooleans; Component name = Component.text("Toast"); - String serialized = legacyNames ? - AdventureSerializer.getLegacyGsonSerializer().serialize(name) : - AdventureSerializer.getGsonSerializer().serialize(name); + Object serialized = legacyNames ? AdventureSerializer.getLegacyGsonSerializer().serialize(name) : name; this.serialized = optional ? Optional.of(serialized) : serialized; - this.type = optional ? EntityDataTypes.OPTIONAL_COMPONENT : EntityDataTypes.STRING; + this.type = optional ? EntityDataTypes.OPTIONAL_ADV_COMPONENT : EntityDataTypes.STRING; } @Override