diff --git a/api/src/main/java/lol/pyr/znpcsplus/util/RabbitType.java b/api/src/main/java/lol/pyr/znpcsplus/util/RabbitType.java new file mode 100644 index 0000000..fbdb47f --- /dev/null +++ b/api/src/main/java/lol/pyr/znpcsplus/util/RabbitType.java @@ -0,0 +1,22 @@ +package lol.pyr.znpcsplus.util; + +public enum RabbitType { + BROWN(0), + WHITE(1), + BLACK(2), + BLACK_AND_WHITE(3), + GOLD(4), + SALT_AND_PEPPER(5), + THE_KILLER_BUNNY(99), + TOAST(100); + + private final int id; + + RabbitType(int id) { + this.id = id; + } + + public int getId() { + return id; + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index e4f548c..3de25dc 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -280,6 +280,7 @@ public class ZNpcsPlus extends JavaPlugin { registerEnumParser(manager, LookType.class, incorrectUsageMessage); registerEnumParser(manager, TropicalFishVariant.TropicalFishPattern.class, incorrectUsageMessage); registerEnumParser(manager, SnifferState.class, incorrectUsageMessage); + registerEnumParser(manager, RabbitType.class, incorrectUsageMessage); manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root")) .addSubcommand("center", new CenterCommand(npcRegistry)) 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 c7a188c..fd896f3 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -78,6 +78,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { registerEnumSerializer(PuffState.class); registerEnumSerializer(TropicalFishVariant.TropicalFishPattern.class); registerEnumSerializer(SnifferState.class); + registerEnumSerializer(RabbitType.class); registerPrimitiveSerializers(Integer.class, Boolean.class, Double.class, Float.class, Long.class, Short.class, Byte.class, String.class); @@ -339,6 +340,16 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { else pigIndex = 16; register(new BooleanProperty("pig_saddled", pigIndex, false, legacyBooleans)); + // Rabbit + int rabbitIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) rabbitIndex = 17; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) rabbitIndex = 16; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) rabbitIndex = 15; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) rabbitIndex = 13; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_9)) rabbitIndex = 12; + else rabbitIndex = 18; + register(new RabbitTypeProperty(rabbitIndex, legacyBooleans, legacyNames, optionalComponents)); + if (!ver.isNewerThanOrEquals(ServerVersion.V_1_10)) return; // Polar Bear int polarBearIndex; 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 new file mode 100644 index 0000000..3a7532a --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/RabbitTypeProperty.java @@ -0,0 +1,47 @@ +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 com.github.retrooper.packetevents.util.adventure.AdventureSerializer; +import lol.pyr.znpcsplus.entity.EntityPropertyImpl; +import lol.pyr.znpcsplus.entity.PacketEntity; +import lol.pyr.znpcsplus.util.RabbitType; +import net.kyori.adventure.text.Component; +import org.bukkit.entity.Player; + +import java.util.Map; +import java.util.Optional; + +public class RabbitTypeProperty extends EntityPropertyImpl { + private final int index; + private final boolean legacyBooleans; + private final Object serialized; + private final EntityDataType type; + + public RabbitTypeProperty(int index, boolean legacyBooleans, boolean legacyNames, boolean optional) { + super("rabbit_type", RabbitType.BROWN, RabbitType.class); + this.index = index; + this.legacyBooleans = legacyBooleans; + Component name = Component.text("Toast"); + String serialized = legacyNames ? + AdventureSerializer.getLegacyGsonSerializer().serialize(name) : + AdventureSerializer.getGsonSerializer().serialize(name); + this.serialized = optional ? Optional.of(serialized) : serialized; + this.type = optional ? EntityDataTypes.OPTIONAL_COMPONENT : EntityDataTypes.STRING; + } + + @Override + public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { + RabbitType rabbitType = entity.getProperty(this); + if (rabbitType == null) return; + if (!rabbitType.equals(RabbitType.TOAST)) { + properties.put(index, legacyBooleans ? + newEntityData(index, EntityDataTypes.BYTE, (byte) rabbitType.getId()) : + newEntityData(index, EntityDataTypes.INT, rabbitType.getId())); + properties.put(2, new EntityData(2, type, null)); + } else { + properties.put(2, new EntityData(2, type, serialized)); + } + } +} 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 1ff68cb..eee788f 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -115,7 +115,8 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { .addProperties("pig_saddled")); register(builder(p, "rabbit", EntityTypes.RABBIT) - .setHologramOffset(-1.475)); + .setHologramOffset(-1.475) + .addProperties("rabbit_type")); register(builder(p, "sheep", EntityTypes.SHEEP) .setHologramOffset(-0.675));