From c11093c39267cc75516ea73e753f4653504cb889 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Sat, 5 Aug 2023 21:50:07 +0200 Subject: [PATCH] implement armor stand properties --- .../entity/EntityPropertyRegistryImpl.java | 15 ++++++-- .../entity/properties/EffectsProperty.java | 25 ------------- .../properties/SimpleBitsetProperty.java | 35 +++++++++++++++++++ 3 files changed, 48 insertions(+), 27 deletions(-) delete mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/EffectsProperty.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SimpleBitsetProperty.java 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 b707c49..3fbe883 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -241,11 +241,22 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { register(new DummyProperty<>("skin", SkinDescriptor.class, false)); register(new GlowProperty(packetFactory)); - register(new EffectsProperty("fire", 0x01)); - register(new EffectsProperty("invisible", 0x20)); + register(new SimpleBitsetProperty("fire", 0, 0x01)); + register(new SimpleBitsetProperty("invisible", 0, 0x20)); linkProperties("glow", "fire", "invisible"); register(new SimpleBooleanProperty("silent", 4, false, ver.isOlderThan(ServerVersion.V_1_9))); + + int armorStandIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) armorStandIndex = 15; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) armorStandIndex = 14; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) armorStandIndex = 13; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) armorStandIndex = 11; + else armorStandIndex = 10; + register(new SimpleBitsetProperty("small", armorStandIndex, 0x01)); + register(new SimpleBitsetProperty("arms", armorStandIndex, 0x04)); + register(new SimpleBitsetProperty("base_plate", armorStandIndex, 0x08, true)); + linkProperties("small", "arms", "base_plate"); } private void registerSerializer(PropertySerializer serializer) { diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/EffectsProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/EffectsProperty.java deleted file mode 100644 index e031ad7..0000000 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/EffectsProperty.java +++ /dev/null @@ -1,25 +0,0 @@ -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 org.bukkit.entity.Player; - -import java.util.Map; - -public class EffectsProperty extends EntityPropertyImpl { - private final int bitmask; - - public EffectsProperty(String name, int bitmask) { - super(name, false, Boolean.class); - this.bitmask = bitmask; - } - - @Override - public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { - EntityData oldData = properties.get(0); - byte oldValue = oldData == null ? 0 : (byte) oldData.getValue(); - properties.put(0, newEntityData(0, EntityDataTypes.BYTE, (byte) (oldValue | (entity.getProperty(this) ? bitmask : 0)))); - } -} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SimpleBitsetProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SimpleBitsetProperty.java new file mode 100644 index 0000000..e533992 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SimpleBitsetProperty.java @@ -0,0 +1,35 @@ +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 org.bukkit.entity.Player; + +import java.util.Map; + +public class SimpleBitsetProperty extends EntityPropertyImpl { + private final int index; + private final int bitmask; + private final boolean inverted; + + public SimpleBitsetProperty(String name, int index, int bitmask, boolean inverted) { + super(name, !inverted, Boolean.class); + this.index = index; + this.bitmask = bitmask; + this.inverted = inverted; + } + + public SimpleBitsetProperty(String name, int index, int bitmask) { + this(name, index, bitmask, false); + } + + @Override + public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { + EntityData oldData = properties.get(index); + byte oldValue = oldData == null ? 0 : (byte) oldData.getValue(); + boolean enabled = entity.getProperty(this); + if (inverted) enabled = !enabled; + properties.put(index, newEntityData(index, EntityDataTypes.BYTE, (byte) (oldValue | (enabled ? bitmask : 0)))); + } +}