From 551b9892400f6fc7fa9524a10a5abd5bcf536743 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Mon, 7 Aug 2023 22:23:30 +0200 Subject: [PATCH] implement cat & tamed property --- .../lol/pyr/znpcsplus/util/CatVariant.java | 32 +++++++------------ .../entity/EntityPropertyRegistryImpl.java | 20 ++++++++++++ .../properties/EncodedIntegerProperty.java | 16 +++++++--- .../znpcsplus/metadata/MetadataFactory.java | 8 ----- .../metadata/V1_14MetadataFactory.java | 17 ---------- .../metadata/V1_15MetadataFactory.java | 17 ---------- .../metadata/V1_17MetadataFactory.java | 22 ------------- .../metadata/V1_8MetadataFactory.java | 21 ------------ .../znpcsplus/npc/NpcTypeRegistryImpl.java | 5 +-- 9 files changed, 46 insertions(+), 112 deletions(-) diff --git a/api/src/main/java/lol/pyr/znpcsplus/util/CatVariant.java b/api/src/main/java/lol/pyr/znpcsplus/util/CatVariant.java index 843036f..9aaa7f4 100644 --- a/api/src/main/java/lol/pyr/znpcsplus/util/CatVariant.java +++ b/api/src/main/java/lol/pyr/znpcsplus/util/CatVariant.java @@ -1,25 +1,15 @@ package lol.pyr.znpcsplus.util; public enum CatVariant { - TABBY(0), - BLACK(1), - RED(2), - SIAMESE(3), - BRITISH_SHORTHAIR(4), - CALICO(5), - PERSIAN(6), - RAGDOLL(7), - WHITE(8), - JELLIE(9), - ALL_BLACK(10); - - private final int id; - - CatVariant(int id) { - this.id = id; - } - - public int getId() { - return id; - } + TABBY, + BLACK, + RED, + SIAMESE, + BRITISH_SHORTHAIR, + CALICO, + PERSIAN, + RAGDOLL, + WHITE, + JELLIE, + ALL_BLACK } 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 2ce417a..338133a 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -2,6 +2,7 @@ package lol.pyr.znpcsplus.entity; import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.manager.server.ServerVersion; +import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.player.EquipmentSlot; import lol.pyr.znpcsplus.api.entity.EntityProperty; import lol.pyr.znpcsplus.api.entity.EntityPropertyRegistry; @@ -210,6 +211,15 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { linkProperties("glow", "fire", "invisible"); register(new BooleanProperty("silent", 4, false, legacyBooleans)); + final int tamedIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) tamedIndex = 17; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) tamedIndex = 16; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) tamedIndex = 15; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) tamedIndex = 13; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_9)) tamedIndex = 12; + else tamedIndex = 16; + register(new BitsetProperty("tamed", tamedIndex, 0x04)); + int potionIndex; if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) potionIndex = 10; else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) potionIndex = 9; @@ -276,6 +286,16 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { if (!ver.isNewerThanOrEquals(ServerVersion.V_1_14)) return; + // Cat + int catIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) catIndex = 19; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) catIndex = 18; + else catIndex = 17; + register(new EncodedIntegerProperty<>("cat_variant", CatVariant.BLACK, catIndex++, Enum::ordinal, EntityDataTypes.CAT_VARIANT)); + register(new BooleanProperty("cat_laying", catIndex++, false, legacyBooleans)); + register(new BooleanProperty("cat_relaxed", catIndex++, false, legacyBooleans)); + register(new EncodedIntegerProperty<>("cat_collar", DyeColor.RED, catIndex, Enum::ordinal)); + // Fox int foxIndex; if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) foxIndex = 17; diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/EncodedIntegerProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/EncodedIntegerProperty.java index a860e09..d089547 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/EncodedIntegerProperty.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/EncodedIntegerProperty.java @@ -1,6 +1,7 @@ package lol.pyr.znpcsplus.entity.properties; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; +import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.entity.PacketEntity; @@ -9,29 +10,36 @@ import org.bukkit.entity.Player; import java.util.Map; public class EncodedIntegerProperty extends EntityPropertyImpl { + private final EntityDataType type; private final IntegerDecoder decoder; private final int index; - protected EncodedIntegerProperty(String name, T defaultValue, Class clazz, int index, IntegerDecoder decoder) { + protected EncodedIntegerProperty(String name, T defaultValue, Class clazz, int index, IntegerDecoder decoder, EntityDataType type) { super(name, defaultValue, clazz); this.decoder = decoder; this.index = index; + this.type = type; } @SuppressWarnings("unchecked") public EncodedIntegerProperty(String name, T defaultValue, int index, IntegerDecoder decoder) { - this(name, defaultValue, (Class) defaultValue.getClass(), index, decoder); + this(name, defaultValue, (Class) defaultValue.getClass(), index, decoder, EntityDataTypes.INT); + } + + @SuppressWarnings("unchecked") + public EncodedIntegerProperty(String name, T defaultValue, int index, IntegerDecoder decoder, EntityDataType type) { + this(name, defaultValue, (Class) defaultValue.getClass(), index, decoder, type); } public EncodedIntegerProperty(String name, Class clazz, int index, IntegerDecoder decoder) { - this(name, null, clazz, index, decoder); + this(name, null, clazz, index, decoder, EntityDataTypes.INT); } @Override public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { T value = entity.getProperty(this); if (value == null) return; - properties.put(index, newEntityData(index, EntityDataTypes.INT, decoder.decode(value))); + properties.put(index, newEntityData(index, type, decoder.decode(value))); } public interface IntegerDecoder { diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java index 20d4d69..d23a75a 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java @@ -2,10 +2,8 @@ package lol.pyr.znpcsplus.metadata; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose; -import lol.pyr.znpcsplus.util.CatVariant; import lol.pyr.znpcsplus.util.CreeperState; import lol.pyr.znpcsplus.util.ParrotVariant; -import org.bukkit.DyeColor; /** * 1.8 ... @@ -38,12 +36,6 @@ public interface MetadataFactory { // Blaze EntityData blazeOnFire(boolean onFire); - // Cat - EntityData catVariant(CatVariant variant); - EntityData catLying(boolean lying); - EntityData catTamed(boolean tamed); - EntityData catCollarColor(DyeColor collarColor); - // Creeper EntityData creeperState(CreeperState state); EntityData creeperCharged(boolean charged); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_14MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_14MetadataFactory.java index 4f9bd17..eebf156 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_14MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_14MetadataFactory.java @@ -4,10 +4,8 @@ import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose; import com.github.retrooper.packetevents.protocol.entity.villager.VillagerData; -import lol.pyr.znpcsplus.util.CatVariant; import lol.pyr.znpcsplus.util.CreeperState; import lol.pyr.znpcsplus.util.ParrotVariant; -import org.bukkit.DyeColor; @Deprecated public class V1_14MetadataFactory extends V1_13MetadataFactory { @@ -37,21 +35,6 @@ public class V1_14MetadataFactory extends V1_13MetadataFactory { return newEntityData(14, EntityDataTypes.BYTE, (byte) (onFire ? 0x01 : 0)); } - @Override - public EntityData catVariant(CatVariant variant) { - return newEntityData(17, EntityDataTypes.CAT_VARIANT, variant.getId()); - } - - @Override - public EntityData catLying(boolean lying) { - throw new UnsupportedOperationException("The cat lying entity data isn't supported on this version"); - } - - @Override - public EntityData catCollarColor(DyeColor collarColor) { - return newEntityData(20, EntityDataTypes.INT, collarColor.ordinal()); - } - @Override public EntityData creeperState(CreeperState state) { return newEntityData(14, EntityDataTypes.INT, state.getState()); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_15MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_15MetadataFactory.java index 9a79da7..5a3f1b3 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_15MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_15MetadataFactory.java @@ -3,10 +3,8 @@ package lol.pyr.znpcsplus.metadata; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.entity.villager.VillagerData; -import lol.pyr.znpcsplus.util.CatVariant; import lol.pyr.znpcsplus.util.CreeperState; import lol.pyr.znpcsplus.util.ParrotVariant; -import org.bukkit.DyeColor; @Deprecated public class V1_15MetadataFactory extends V1_14MetadataFactory { @@ -25,21 +23,6 @@ public class V1_15MetadataFactory extends V1_14MetadataFactory { return newEntityData(15, EntityDataTypes.BYTE, (byte) (onFire ? 0x01 : 0)); } - @Override - public EntityData catVariant(CatVariant variant) { - return newEntityData(18, EntityDataTypes.CAT_VARIANT, variant.getId()); - } - - @Override - public EntityData catLying(boolean lying) { - return newEntityData(19, EntityDataTypes.BOOLEAN, lying); - } - - @Override - public EntityData catCollarColor(DyeColor collarColor) { - return newEntityData(21, EntityDataTypes.INT, collarColor.ordinal()); - } - @Override public EntityData creeperState(CreeperState state) { return newEntityData(15, EntityDataTypes.INT, state.getState()); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_17MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_17MetadataFactory.java index ae25c11..3e0da54 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_17MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_17MetadataFactory.java @@ -3,10 +3,8 @@ package lol.pyr.znpcsplus.metadata; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.entity.villager.VillagerData; -import lol.pyr.znpcsplus.util.CatVariant; import lol.pyr.znpcsplus.util.CreeperState; import lol.pyr.znpcsplus.util.ParrotVariant; -import org.bukkit.DyeColor; @Deprecated public class V1_17MetadataFactory extends V1_16MetadataFactory { @@ -41,26 +39,6 @@ public class V1_17MetadataFactory extends V1_16MetadataFactory { return newEntityData(16, EntityDataTypes.BYTE, (byte) (onFire ? 0x01 : 0)); } - @Override - public EntityData catVariant(CatVariant variant) { - return newEntityData(19, EntityDataTypes.CAT_VARIANT, variant.getId()); - } - - @Override - public EntityData catLying(boolean lying) { - return newEntityData(20, EntityDataTypes.BOOLEAN, lying); - } - - @Override - public EntityData catTamed(boolean tamed) { - return newEntityData(17, EntityDataTypes.BYTE, (byte) (tamed ? 0x04 : 0)); - } - - @Override - public EntityData catCollarColor(DyeColor collarColor) { - return newEntityData(22, EntityDataTypes.INT, collarColor.ordinal()); - } - @Override public EntityData creeperState(CreeperState state) { return newEntityData(16, EntityDataTypes.INT, state.getState()); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java index 2e5b777..1bde7bb 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java @@ -5,7 +5,6 @@ import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose; import lol.pyr.znpcsplus.util.*; -import org.bukkit.DyeColor; @Deprecated public class V1_8MetadataFactory implements MetadataFactory { @@ -45,26 +44,6 @@ public class V1_8MetadataFactory implements MetadataFactory { return newEntityData(16, EntityDataTypes.BYTE, (byte) (onFire ? 1 : 0)); } - @Override - public EntityData catVariant(CatVariant variant) { - throw new UnsupportedOperationException("The cat variant entity data isn't supported on this version"); - } - - @Override - public EntityData catLying(boolean lying) { - throw new UnsupportedOperationException("The cat lying entity data isn't supported on this version"); - } - - @Override - public EntityData catTamed(boolean tamed) { - throw new UnsupportedOperationException("The cat tamed entity data isn't supported on this version"); - } - - @Override - public EntityData catCollarColor(DyeColor collarColor) { - throw new UnsupportedOperationException("The cat collar color entity data isn't supported on this version"); - } - @Override public EntityData creeperState(CreeperState state) { return newEntityData(16, EntityDataTypes.BYTE, (byte) state.getState()); 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 89cc48f..9e66516 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -143,7 +143,8 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { .setHologramOffset(1.525)); register(builder(p, "wolf", EntityTypes.WOLF) - .setHologramOffset(-1.125)); + .setHologramOffset(-1.125) + .addProperties("tamed")); register(builder(p, "zombie", EntityTypes.ZOMBIE) .setHologramOffset(-0.025) @@ -250,7 +251,7 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { register(builder(p, "cat", EntityTypes.CAT) .setHologramOffset(-1.275) - .addProperties("cat_variant", "cat_lying", "cat_collar_color")); + .addProperties("cat_variant", "cat_laying", "cat_relaxed", "cat_collar", "tamed")); register(builder(p, "fox", EntityTypes.FOX) .setHologramOffset(-1.275)