From a5de0680c85b5a0c1ae235db9b88953720ce63bb Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Mon, 2 Oct 2023 09:50:20 +0530 Subject: [PATCH 01/12] added ability to register dummy property using api --- .../api/entity/EntityPropertyRegistry.java | 1 + .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 1 + .../entity/EntityPropertyRegistryImpl.java | 5 +++++ .../pyr/znpcsplus/parsers/StringParser.java | 19 +++++++++++++++++++ plugin/src/main/resources/plugin.yml | 3 +++ 5 files changed, 29 insertions(+) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/parsers/StringParser.java diff --git a/api/src/main/java/lol/pyr/znpcsplus/api/entity/EntityPropertyRegistry.java b/api/src/main/java/lol/pyr/znpcsplus/api/entity/EntityPropertyRegistry.java index 0009ee9..dad18d8 100644 --- a/api/src/main/java/lol/pyr/znpcsplus/api/entity/EntityPropertyRegistry.java +++ b/api/src/main/java/lol/pyr/znpcsplus/api/entity/EntityPropertyRegistry.java @@ -6,4 +6,5 @@ public interface EntityPropertyRegistry { Collection> getAll(); EntityProperty getByName(String name); EntityProperty getByName(String name, Class type); + void registerDummy(String name, Class type); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 4fae9cf..2013141 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -253,6 +253,7 @@ public class ZNpcsPlus extends JavaPlugin { manager.registerParser(InteractionType.class, new InteractionTypeParser(incorrectUsageMessage)); manager.registerParser(Color.class, new ColorParser(incorrectUsageMessage)); manager.registerParser(Vector3f.class, new Vector3fParser(incorrectUsageMessage)); + manager.registerParser(String.class, new StringParser(incorrectUsageMessage)); // TODO: Need to find a better way to do this registerEnumParser(manager, NpcPose.class, incorrectUsageMessage); 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 eaaea72..f26741f 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -569,6 +569,11 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { return (EntityPropertyImpl) getByName(name); } + @Override + public void registerDummy(String name, Class type) { + register(new DummyProperty<>(name, type)); + } + public EntityPropertyImpl getByName(String name) { return byName.get(name.toLowerCase()); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/parsers/StringParser.java b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/StringParser.java new file mode 100644 index 0000000..a14ac42 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/StringParser.java @@ -0,0 +1,19 @@ +package lol.pyr.znpcsplus.parsers; + +import lol.pyr.director.adventure.command.CommandContext; +import lol.pyr.director.adventure.parse.ParserType; +import lol.pyr.director.common.command.CommandExecutionException; +import lol.pyr.director.common.message.Message; + +import java.util.Deque; + +public class StringParser extends ParserType { + public StringParser(Message message) { + super(message); + } + + @Override + public String parse(Deque deque) throws CommandExecutionException { + return deque.pollFirst(); + } +} diff --git a/plugin/src/main/resources/plugin.yml b/plugin/src/main/resources/plugin.yml index e1abf93..d6beb5b 100644 --- a/plugin/src/main/resources/plugin.yml +++ b/plugin/src/main/resources/plugin.yml @@ -21,6 +21,9 @@ softdepend: - ViaRewind - Geyser-Spigot +loadbefore: + - Quests + commands: npc: aliases: From 256f47bb9456d4200bd657e4f6beb6e00e287e08 Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Mon, 2 Oct 2023 09:52:27 +0530 Subject: [PATCH 02/12] ignore applying disallowed property --- .../znpcsplus/commands/property/PropertyRemoveCommand.java | 2 +- plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java | 7 ++++++- .../src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java | 4 ++++ .../java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertyRemoveCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertyRemoveCommand.java index b517d74..88b6ff1 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertyRemoveCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertyRemoveCommand.java @@ -37,7 +37,7 @@ public class PropertyRemoveCommand implements CommandHandler { public List suggest(CommandContext context) throws CommandExecutionException { if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); if (context.argSize() == 2) return context.suggestStream(context.suggestionParse(0, NpcEntryImpl.class) - .getNpc().getAppliedProperties().stream().filter(EntityProperty::isPlayerModifiable).map(EntityProperty::getName)); + .getNpc().getAllProperties().stream().filter(EntityProperty::isPlayerModifiable).map(EntityProperty::getName)); return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java index fb17ad4..8ee3a58 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java @@ -20,6 +20,7 @@ import org.bukkit.World; import org.bukkit.entity.Player; import java.util.*; +import java.util.stream.Collectors; public class NpcImpl extends Viewable implements Npc { private final PacketFactory packetFactory; @@ -163,9 +164,13 @@ public class NpcImpl extends Viewable implements Npc { setProperty((EntityPropertyImpl) property, (T) value); } + public Set> getAllProperties() { + return Collections.unmodifiableSet(propertyMap.keySet()); + } + @Override public Set> getAppliedProperties() { - return Collections.unmodifiableSet(propertyMap.keySet()); + return Collections.unmodifiableSet(propertyMap.keySet()).stream().filter(type::isAllowedProperty).collect(Collectors.toSet()); } public List getActions() { diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java index 56cbd06..f072bd6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java @@ -50,6 +50,10 @@ public class NpcTypeImpl implements NpcType { } } + public boolean isAllowedProperty(EntityPropertyImpl entityProperty) { + return !entityProperty.isPlayerModifiable() || allowedProperties.contains(entityProperty); + } + protected static final class Builder { private final static Logger logger = Logger.getLogger("NpcTypeBuilder"); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java b/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java index 7c676b4..0ce9aef 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java @@ -111,7 +111,7 @@ public class YamlStorage implements NpcStorage { config.set("location", serializeLocation(npc.getLocation())); config.set("type", npc.getType().getName()); - for (EntityProperty property : npc.getAppliedProperties()) try { + for (EntityProperty property : npc.getAllProperties()) try { PropertySerializer serializer = propertyRegistry.getSerializer(((EntityPropertyImpl) property).getType()); if (serializer == null) { Bukkit.getLogger().log(Level.WARNING, "Unknown serializer for property '" + property.getName() + "' for npc '" + entry.getId() + "'. skipping ..."); From 8637b5b5c2c7d71b7bbe44e1fae18ee7009435e9 Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Mon, 2 Oct 2023 09:56:52 +0530 Subject: [PATCH 03/12] added Tropical Fish properties --- .../znpcsplus/util/TropicalFishVariant.java | 104 ++++++++++++++++++ .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 1 + .../entity/EntityPropertyRegistryImpl.java | 15 ++- .../TropicalFishVariantProperty.java | 49 +++++++++ .../znpcsplus/npc/NpcTypeRegistryImpl.java | 3 +- 5 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 api/src/main/java/lol/pyr/znpcsplus/util/TropicalFishVariant.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/TropicalFishVariantProperty.java diff --git a/api/src/main/java/lol/pyr/znpcsplus/util/TropicalFishVariant.java b/api/src/main/java/lol/pyr/znpcsplus/util/TropicalFishVariant.java new file mode 100644 index 0000000..e201886 --- /dev/null +++ b/api/src/main/java/lol/pyr/znpcsplus/util/TropicalFishVariant.java @@ -0,0 +1,104 @@ +package lol.pyr.znpcsplus.util; + +import org.bukkit.DyeColor; + +import java.util.function.IntFunction; + +public class TropicalFishVariant { + + private final TropicalFishPattern pattern; + private final DyeColor bodyColor; + private final DyeColor patternColor; + + public TropicalFishVariant(TropicalFishPattern pattern, DyeColor bodyColor, DyeColor patternColor) { + this.pattern = pattern; + this.bodyColor = bodyColor; + this.patternColor = patternColor; + } + + public int getVariant() { + return pattern.getId() & '\uffff' | (bodyColor.ordinal() & 255) << 16 | (patternColor.ordinal() & 255) << 24; + } + + public enum TropicalFishPattern { + KOB(0, 0), + SUNSTREAK(0, 1), + SNOOPER(0, 2), + DASHER(0, 3), + BRINELY(0, 4), + SPOTTY(0, 5), + FLOPPER(1, 0), + STRIPEY(1, 1), + GLITTER(1, 2), + BLOCKFISH(1, 3), + BETTY(1, 4), + CLAYFISH(1, 5); + + private final int size; + private final int id; + private static final IntFunction BY_ID = (id) -> { + for (TropicalFishPattern pattern : values()) { + if (pattern.id == id) { + return pattern; + } + } + return null; + }; + + TropicalFishPattern(int size, int pattern) { + this.size = size; + this.id = size | pattern << 8; + } + + public int getSize() { + return size; + } + + public int getId() { + return id; + } + + public static TropicalFishPattern fromVariant(int variant) { + return BY_ID.apply(variant & '\uffff'); + } + } + + public static class Builder { + private TropicalFishPattern pattern; + private DyeColor bodyColor; + private DyeColor patternColor; + + public Builder() { + this.pattern = TropicalFishPattern.KOB; + this.bodyColor = DyeColor.WHITE; + this.patternColor = DyeColor.WHITE; + } + + public Builder pattern(TropicalFishPattern pattern) { + this.pattern = pattern; + return this; + } + + public Builder bodyColor(DyeColor bodyColor) { + this.bodyColor = bodyColor; + return this; + } + + public Builder patternColor(DyeColor patternColor) { + this.patternColor = patternColor; + return this; + } + + public static Builder fromInt(int variant) { + Builder builder = new Builder(); + builder.pattern = TropicalFishPattern.fromVariant(variant); + builder.bodyColor = DyeColor.values()[(variant >> 16) & 0xFF]; + builder.patternColor = DyeColor.values()[(variant >> 24) & 0xFF]; + return builder; + } + + public TropicalFishVariant build() { + return new TropicalFishVariant(pattern, bodyColor, patternColor); + } + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 2013141..f4736d6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -278,6 +278,7 @@ public class ZNpcsPlus extends JavaPlugin { registerEnumParser(manager, PandaGene.class, incorrectUsageMessage); registerEnumParser(manager, PuffState.class, incorrectUsageMessage); registerEnumParser(manager, LookType.class, incorrectUsageMessage); + registerEnumParser(manager, TropicalFishVariant.TropicalFishPattern.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 f26741f..e97658c 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -76,6 +76,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { registerEnumSerializer(OcelotType.class); registerEnumSerializer(PandaGene.class); registerEnumSerializer(PuffState.class); + registerEnumSerializer(TropicalFishVariant.TropicalFishPattern.class); registerPrimitiveSerializers(Integer.class, Boolean.class, Double.class, Float.class, Long.class, Short.class, Byte.class, String.class); @@ -96,9 +97,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { // Guardian registerType("is_elder", false); // TODO: ensure it only works till 1.10. Note: index is wrong on wiki.vg - // Tropical Fish - registerType("tropical_fish_variant", null); // TODO: Maybe make an enum class for this? its just an int on wiki.vg - // Sniffer registerType("sniffer_state", null); // TODO: Nothing on wiki.vg, look in mc source @@ -398,6 +396,17 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { else pufferfishIndex = 13; register(new EncodedIntegerProperty<>("puff_state", PuffState.DEFLATED, pufferfishIndex, Enum::ordinal)); + // Tropical Fish + int tropicalFishIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) tropicalFishIndex = 17; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) tropicalFishIndex = 16; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) tropicalFishIndex = 15; + else tropicalFishIndex = 13; + register(new TropicalFishVariantProperty<>("tropical_fish_pattern", TropicalFishVariant.TropicalFishPattern.KOB, tropicalFishIndex, TropicalFishVariant.Builder::pattern)); + register(new TropicalFishVariantProperty<>("tropical_fish_body_color", DyeColor.WHITE, tropicalFishIndex, TropicalFishVariant.Builder::bodyColor)); + register(new TropicalFishVariantProperty<>("tropical_fish_pattern_color", DyeColor.WHITE, tropicalFishIndex, TropicalFishVariant.Builder::patternColor)); + linkProperties("tropical_fish_pattern", "tropical_fish_body_color", "tropical_fish_pattern_color"); + if (!ver.isNewerThanOrEquals(ServerVersion.V_1_14)) return; // Pose register(new NpcPoseProperty()); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/TropicalFishVariantProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/TropicalFishVariantProperty.java new file mode 100644 index 0000000..99c8d68 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/TropicalFishVariantProperty.java @@ -0,0 +1,49 @@ +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.util.TropicalFishVariant; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class TropicalFishVariantProperty extends EntityPropertyImpl { + private final int index; + private final BuilderDecoder decoder; + + public TropicalFishVariantProperty(String name, T defaultValue, Class type, int index, BuilderDecoder decoder) { + super(name, defaultValue, type); + this.index = index; + this.decoder = decoder; + } + + @SuppressWarnings("unchecked") + public TropicalFishVariantProperty(String name, T defaultValue, int index, BuilderDecoder decoder) { + this(name, defaultValue, (Class) defaultValue.getClass(), index, decoder); + } + + @Override + public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { + T value = entity.getProperty(this); + if (value == null) { + return; + } + EntityData oldData = properties.get(index); + TropicalFishVariant.Builder builder; + if (oldData != null && oldData.getType() == EntityDataTypes.INT && oldData.getValue() != null) { + int oldVal = (int) oldData.getValue(); + builder = TropicalFishVariant.Builder.fromInt(oldVal); + } else { + builder = new TropicalFishVariant.Builder(); + } + builder = decoder.decode(builder, value); + int variant = builder.build().getVariant(); + properties.put(index, newEntityData(index, EntityDataTypes.INT, variant)); + } + + public interface BuilderDecoder { + TropicalFishVariant.Builder decode(TropicalFishVariant.Builder builder, T obj); + } +} 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 e12d9e2..9227d99 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -257,7 +257,8 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { .setHologramOffset(-1.575)); register(builder(p, "tropical_fish", EntityTypes.TROPICAL_FISH) - .setHologramOffset(-1.575)); + .setHologramOffset(-1.575) + .addProperties("tropical_fish_pattern", "tropical_fish_body_color", "tropical_fish_pattern_color")); register(builder(p, "turtle", EntityTypes.TURTLE) .setHologramOffset(-1.575)); From 82cd0d1a81ee6a59a767ddf46e3c02c129f331de Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Mon, 2 Oct 2023 09:59:14 +0530 Subject: [PATCH 04/12] added sniffer_state property --- .../lol/pyr/znpcsplus/util/SnifferState.java | 11 +++++++ .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 1 + .../entity/EntityPropertyRegistryImpl.java | 1 + .../properties/SnifferStateProperty.java | 30 +++++++++++++++++++ .../znpcsplus/npc/NpcTypeRegistryImpl.java | 3 +- 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/lol/pyr/znpcsplus/util/SnifferState.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SnifferStateProperty.java diff --git a/api/src/main/java/lol/pyr/znpcsplus/util/SnifferState.java b/api/src/main/java/lol/pyr/znpcsplus/util/SnifferState.java new file mode 100644 index 0000000..a532947 --- /dev/null +++ b/api/src/main/java/lol/pyr/znpcsplus/util/SnifferState.java @@ -0,0 +1,11 @@ +package lol.pyr.znpcsplus.util; + +public enum SnifferState { + IDLING, + FEELING_HAPPY, + SCENTING, + SNIFFING, + SEARCHING, + DIGGING, + RISING +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index f4736d6..e4f548c 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -279,6 +279,7 @@ public class ZNpcsPlus extends JavaPlugin { registerEnumParser(manager, PuffState.class, incorrectUsageMessage); registerEnumParser(manager, LookType.class, incorrectUsageMessage); registerEnumParser(manager, TropicalFishVariant.TropicalFishPattern.class, incorrectUsageMessage); + registerEnumParser(manager, SnifferState.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 e97658c..c7a188c 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -77,6 +77,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { registerEnumSerializer(PandaGene.class); registerEnumSerializer(PuffState.class); registerEnumSerializer(TropicalFishVariant.TropicalFishPattern.class); + registerEnumSerializer(SnifferState.class); registerPrimitiveSerializers(Integer.class, Boolean.class, Double.class, Float.class, Long.class, Short.class, Byte.class, String.class); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SnifferStateProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SnifferStateProperty.java new file mode 100644 index 0000000..c0e1ef5 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SnifferStateProperty.java @@ -0,0 +1,30 @@ +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.util.SnifferState; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class SnifferStateProperty extends EntityPropertyImpl { + private final int index; + + public SnifferStateProperty(int index) { + super("sniffer_state", SnifferState.IDLING, SnifferState.class); + this.index = index; + } + + @Override + public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { + SnifferState state = entity.getProperty(this); + if (state == null) return; + try { + properties.put(index, newEntityData(index, EntityDataTypes.SNIFFER_STATE, com.github.retrooper.packetevents.protocol.entity.sniffer.SnifferState.valueOf(state.name()))); + } catch (IllegalArgumentException e) { + // ignore + } + } +} 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 9227d99..1ff68cb 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -352,7 +352,8 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { if (!version.isNewerThanOrEquals(ServerVersion.V_1_20)) return; register(builder(p, "sniffer", EntityTypes.SNIFFER) - .setHologramOffset(0.125)); + .setHologramOffset(0.125) + .addProperties("sniffer_state")); register(builder(p, "camel", EntityTypes.CAMEL) .setHologramOffset(0.25) From 58d81a740cae665dabf5d6e899c33d076f9c160a Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Mon, 2 Oct 2023 10:02:52 +0530 Subject: [PATCH 05/12] added rabbit_type property --- .../lol/pyr/znpcsplus/util/RabbitType.java | 22 +++++++++ .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 1 + .../entity/EntityPropertyRegistryImpl.java | 11 +++++ .../entity/properties/RabbitTypeProperty.java | 47 +++++++++++++++++++ .../znpcsplus/npc/NpcTypeRegistryImpl.java | 3 +- 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/lol/pyr/znpcsplus/util/RabbitType.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/RabbitTypeProperty.java 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)); From 6faf0a3e93ccfba433760e0a19cafd45868c885f Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Mon, 2 Oct 2023 10:15:18 +0530 Subject: [PATCH 06/12] added sheep_color and sheep_sheared properties --- .../znpcsplus/entity/EntityPropertyRegistryImpl.java | 12 ++++++++++++ .../lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) 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 fd896f3..dacfb60 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -350,6 +350,18 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { else rabbitIndex = 18; register(new RabbitTypeProperty(rabbitIndex, legacyBooleans, legacyNames, optionalComponents)); + // Sheep + int sheepIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) sheepIndex = 17; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) sheepIndex = 16; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) sheepIndex = 15; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) sheepIndex = 13; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_9)) sheepIndex = 12; + else sheepIndex = 16; + // noinspection deprecation + register(new EncodedByteProperty<>("sheep_color", DyeColor.WHITE, sheepIndex, DyeColor::getWoolData)); + register(new BitsetProperty("sheep_sheared", sheepIndex, 0x10, false, legacyBooleans)); // no need to link because sheep_sheared is only visible when sheep_color is WHITE + if (!ver.isNewerThanOrEquals(ServerVersion.V_1_10)) return; // Polar Bear int polarBearIndex; 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 eee788f..9983c2a 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -119,7 +119,8 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { .addProperties("rabbit_type")); register(builder(p, "sheep", EntityTypes.SHEEP) - .setHologramOffset(-0.675)); + .setHologramOffset(-0.675) + .addProperties("sheep_color", "sheep_sheared")); register(builder(p, "silverfish", EntityTypes.SILVERFISH) .setHologramOffset(-1.675)); From b1b10a04ee705730b348a30d37923d3bb55cc898 Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Mon, 2 Oct 2023 10:20:47 +0530 Subject: [PATCH 07/12] added Strider properties (needs small fix) and fixed sniffer property --- .../entity/EntityPropertyRegistryImpl.java | 24 +++++++------------ .../znpcsplus/npc/NpcTypeRegistryImpl.java | 3 ++- 2 files changed, 11 insertions(+), 16 deletions(-) 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 dacfb60..b958e5e 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -99,20 +99,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { // Guardian registerType("is_elder", false); // TODO: ensure it only works till 1.10. Note: index is wrong on wiki.vg - // Sniffer - registerType("sniffer_state", null); // TODO: Nothing on wiki.vg, look in mc source - - // Rabbit - registerType("rabbit_type", 0); // TODO: Figure this out - - // Sheep - registerType("sheep_color", DyeColor.WHITE); // TODO: Figure this out - registerType("sheep_sheared", false); // TODO - - // Strider - registerType("strider_shaking", false); // TODO - registerType("strider_saddle", false); // TODO - // Wolf registerType("wolf_collar_color", DyeColor.RED); // TODO registerType("wolf_angry", false); // TODO @@ -511,7 +497,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { // Hoglin and Piglin Zombification final int zombificationIndex; - if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) zombificationIndex = 17; // Change piglinIndex and pillagerIndex if you change this + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) zombificationIndex = 17; // Change piglinIndex, pillagerIndex, striderIndex and vindicatorIndex if you change this else zombificationIndex = 16; register(new BooleanProperty("hoglin_immune_to_zombification", zombificationIndex, false, legacyBooleans)); register(new BooleanProperty("piglin_immune_to_zombification", zombificationIndex-1, false, legacyBooleans)); @@ -525,6 +511,11 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { // Pillager register(new BooleanProperty("pillager_charging", zombificationIndex, false, legacyBooleans)); + // Strider + int striderIndex = zombificationIndex + 1; + register(new BooleanProperty("strider_shaking", striderIndex++, false, legacyBooleans)); // TODO: Fix this, it needs to be set constantly i guess + register(new BooleanProperty("strider_saddled", striderIndex, false, legacyBooleans)); + // Vindicator int vindicatorIndex = zombificationIndex -1; register(new BooleanProperty("celebrating", vindicatorIndex, false, legacyBooleans)); @@ -547,6 +538,9 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { // Camel register(new BooleanProperty("bashing", 18, false, legacyBooleans)); + + // Sniffer + register(new SnifferStateProperty(17)); } private void registerSerializer(PropertySerializer serializer) { 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 9983c2a..acb5bc7 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -317,7 +317,8 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { .addEquipmentProperties()); register(builder(p, "strider", EntityTypes.STRIDER) - .setHologramOffset(-0.275)); + .setHologramOffset(-0.275) + .addProperties("strider_shaking", "strider_saddled")); register(builder(p, "zoglin", EntityTypes.ZOGLIN) .setHologramOffset(-0.575)); From e7601f8e4b25b798134bb431120960436b0ab120 Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Mon, 2 Oct 2023 10:24:31 +0530 Subject: [PATCH 08/12] added Wolf properties and sitting property for tameable --- .../entity/EntityPropertyRegistryImpl.java | 42 +++++++++++++------ .../lol/pyr/znpcsplus/npc/NpcTypeImpl.java | 3 ++ .../znpcsplus/npc/NpcTypeRegistryImpl.java | 4 +- 3 files changed, 35 insertions(+), 14 deletions(-) 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 b958e5e..6bc2f5d 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -99,10 +99,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { // Guardian registerType("is_elder", false); // TODO: ensure it only works till 1.10. Note: index is wrong on wiki.vg - // Wolf - registerType("wolf_collar_color", DyeColor.RED); // TODO - registerType("wolf_angry", false); // TODO - // Show Golem registerType("pumpkin", true); // TODO @@ -144,14 +140,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)); + final int tameableIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) tameableIndex = 17; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) tameableIndex = 16; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) tameableIndex = 15; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) tameableIndex = 13; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_9)) tameableIndex = 12; + else tameableIndex = 16; + register(new BitsetProperty("sitting", tameableIndex, 0x01)); + register(new BitsetProperty("tamed", tameableIndex, 0x04)); int potionIndex; if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) potionIndex = 10; @@ -348,6 +345,27 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { register(new EncodedByteProperty<>("sheep_color", DyeColor.WHITE, sheepIndex, DyeColor::getWoolData)); register(new BitsetProperty("sheep_sheared", sheepIndex, 0x10, false, legacyBooleans)); // no need to link because sheep_sheared is only visible when sheep_color is WHITE + // Wolf + int wolfIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) wolfIndex = 19; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) wolfIndex = 18; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) wolfIndex = 16; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_9)) wolfIndex = 15; + else wolfIndex = 19; + register(new BooleanProperty("wolf_begging", wolfIndex++, false, legacyBooleans)); + if (legacyBooleans) { + // noinspection deprecation + register(new EncodedByteProperty<>("wolf_collar", DyeColor.BLUE, wolfIndex++, DyeColor::getDyeData)); + } else register(new EncodedIntegerProperty<>("wolf_collar", DyeColor.RED, wolfIndex++, Enum::ordinal)); + if (ver.isNewerThanOrEquals(ServerVersion.V_1_16)) { + register(new EncodedIntegerProperty<>("wolf_angry", false, wolfIndex, b -> b ? 1 : 0)); + linkProperties("tamed", "sitting"); + } + else { + register(new BitsetProperty("wolf_angry", tameableIndex, 0x02)); + linkProperties("wolf_angry", "tamed", "sitting"); + } + if (!ver.isNewerThanOrEquals(ServerVersion.V_1_10)) return; // Polar Bear int polarBearIndex; diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java index f072bd6..b9c6884 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java @@ -158,6 +158,9 @@ public class NpcTypeImpl implements NpcType { addProperties("panda_eating"); } } + if (EntityTypes.isTypeInstanceOf(type, EntityTypes.ABSTRACT_TAMEABLE_ANIMAL)) { + addProperties("tamed", "sitting"); + } return new NpcTypeImpl(name, type, hologramOffset, new HashSet<>(allowedProperties), defaultProperties); } } 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 acb5bc7..fba1fad 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -156,7 +156,7 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { register(builder(p, "wolf", EntityTypes.WOLF) .setHologramOffset(-1.125) - .addProperties("tamed")); + .addProperties("wolf_begging", "wolf_collar", "wolf_angry")); register(builder(p, "zombie", EntityTypes.ZOMBIE) .setHologramOffset(-0.025) @@ -269,7 +269,7 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { register(builder(p, "cat", EntityTypes.CAT) .setHologramOffset(-1.275) - .addProperties("cat_variant", "cat_laying", "cat_relaxed", "cat_collar", "tamed")); + .addProperties("cat_variant", "cat_laying", "cat_relaxed", "cat_collar")); register(builder(p, "fox", EntityTypes.FOX) .setHologramOffset(-1.275) From b3187883bb89e4d44f1183a041828ae16b197065 Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Mon, 2 Oct 2023 10:26:47 +0530 Subject: [PATCH 09/12] added derpy_snowgolem property --- .../entity/EntityPropertyRegistryImpl.java | 13 ++++++++--- .../properties/DerpySnowgolemProperty.java | 23 +++++++++++++++++++ .../znpcsplus/npc/NpcTypeRegistryImpl.java | 3 ++- 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DerpySnowgolemProperty.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 6bc2f5d..5c018ef 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -99,9 +99,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { // Guardian registerType("is_elder", false); // TODO: ensure it only works till 1.10. Note: index is wrong on wiki.vg - // Show Golem - registerType("pumpkin", true); // TODO - // Shulker registerType("attach_direction", null); // TODO: make a direction enum registerType("shield_height", 0); // TODO: figure this out @@ -366,6 +363,16 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { linkProperties("wolf_angry", "tamed", "sitting"); } + if (!ver.isNewerThanOrEquals(ServerVersion.V_1_9)) return; + // Snow Golem + int snowGolemIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) snowGolemIndex = 16; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) snowGolemIndex = 15; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) snowGolemIndex = 14; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) snowGolemIndex = 12; + else snowGolemIndex = 10; + register(new DerpySnowgolemProperty(snowGolemIndex)); + if (!ver.isNewerThanOrEquals(ServerVersion.V_1_10)) return; // Polar Bear int polarBearIndex; diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DerpySnowgolemProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DerpySnowgolemProperty.java new file mode 100644 index 0000000..3a3529c --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DerpySnowgolemProperty.java @@ -0,0 +1,23 @@ +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 DerpySnowgolemProperty extends EntityPropertyImpl { + private final int index; + + public DerpySnowgolemProperty(int index) { + super("derpy_snowgolem", false, Boolean.class); + this.index = index; + } + + @Override + public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { + properties.put(index, new EntityData(index, EntityDataTypes.BYTE, (byte) (entity.getProperty(this) ? 0x00 : 0x10))); + } +} 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 fba1fad..efaaba1 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -135,7 +135,8 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { register(builder(p, "slime", EntityTypes.SLIME)); // TODO: Hologram offset scaling with size property register(builder(p, "snow_golem", EntityTypes.SNOW_GOLEM) - .setHologramOffset(-0.075)); + .setHologramOffset(-0.075) + .addProperties("derpy_snowgolem")); register(builder(p, "spider", EntityTypes.SPIDER) .setHologramOffset(-1.075)); From 6128cac3796a1d58a48ecba61b4c190001f2b71d Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Tue, 3 Oct 2023 13:37:16 +0530 Subject: [PATCH 10/12] added Shulker properties --- .../pyr/znpcsplus/util/AttachDirection.java | 10 ++++++ .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 6 ++-- .../entity/EntityPropertyRegistryImpl.java | 14 ++++++++ .../entity/properties/CustomTypeProperty.java | 32 +++++++++++++++++++ .../znpcsplus/npc/NpcTypeRegistryImpl.java | 3 +- 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 api/src/main/java/lol/pyr/znpcsplus/util/AttachDirection.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/CustomTypeProperty.java diff --git a/api/src/main/java/lol/pyr/znpcsplus/util/AttachDirection.java b/api/src/main/java/lol/pyr/znpcsplus/util/AttachDirection.java new file mode 100644 index 0000000..9322749 --- /dev/null +++ b/api/src/main/java/lol/pyr/znpcsplus/util/AttachDirection.java @@ -0,0 +1,10 @@ +package lol.pyr.znpcsplus.util; + +public enum AttachDirection { + DOWN, + UP, + NORTH, + SOUTH, + WEST, + EAST +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 3de25dc..e076bc5 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -65,10 +65,7 @@ import java.io.PrintWriter; import java.io.Reader; import java.nio.file.Files; import java.nio.file.StandardOpenOption; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; +import java.util.*; public class ZNpcsPlus extends JavaPlugin { private final LegacyComponentSerializer textSerializer = LegacyComponentSerializer.builder() @@ -281,6 +278,7 @@ public class ZNpcsPlus extends JavaPlugin { registerEnumParser(manager, TropicalFishVariant.TropicalFishPattern.class, incorrectUsageMessage); registerEnumParser(manager, SnifferState.class, incorrectUsageMessage); registerEnumParser(manager, RabbitType.class, incorrectUsageMessage); + registerEnumParser(manager, AttachDirection.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 5c018ef..23e8d52 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -7,6 +7,7 @@ import com.github.retrooper.packetevents.protocol.nbt.NBTCompound; import com.github.retrooper.packetevents.protocol.nbt.NBTInt; import com.github.retrooper.packetevents.protocol.nbt.NBTString; import com.github.retrooper.packetevents.protocol.player.EquipmentSlot; +import com.github.retrooper.packetevents.protocol.world.BlockFace; import lol.pyr.znpcsplus.api.entity.EntityProperty; import lol.pyr.znpcsplus.api.entity.EntityPropertyRegistry; import lol.pyr.znpcsplus.api.skin.SkinDescriptor; @@ -79,6 +80,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { registerEnumSerializer(TropicalFishVariant.TropicalFishPattern.class); registerEnumSerializer(SnifferState.class); registerEnumSerializer(RabbitType.class); + registerEnumSerializer(AttachDirection.class); registerPrimitiveSerializers(Integer.class, Boolean.class, Double.class, Float.class, Long.class, Short.class, Byte.class, String.class); @@ -364,6 +366,18 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { } if (!ver.isNewerThanOrEquals(ServerVersion.V_1_9)) return; + // Shulker + int shulkerIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) shulkerIndex = 16; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) shulkerIndex = 15; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) shulkerIndex = 14; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) shulkerIndex = 12; + else shulkerIndex = 11; + register(new CustomTypeProperty<>("attach_direction", shulkerIndex++, AttachDirection.DOWN, EntityDataTypes.BLOCK_FACE, attachDir -> BlockFace.valueOf(attachDir.name()))); + register(new EncodedByteProperty<>("shield_height", 0, shulkerIndex++, value -> (byte) Math.max(0, Math.min(100, value)))); + // noinspection deprecation + register(new EncodedByteProperty<>("shulker_color", DyeColor.class, shulkerIndex, DyeColor::getWoolData)); + // Snow Golem int snowGolemIndex; if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) snowGolemIndex = 16; diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/CustomTypeProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/CustomTypeProperty.java new file mode 100644 index 0000000..9946778 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/CustomTypeProperty.java @@ -0,0 +1,32 @@ +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 lol.pyr.znpcsplus.entity.EntityPropertyImpl; +import lol.pyr.znpcsplus.entity.PacketEntity; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class CustomTypeProperty extends EntityPropertyImpl { + private final int index; + private final EntityDataType type; + private final TypeDecoder decoder; + + @SuppressWarnings("unchecked") + public CustomTypeProperty(String name, int index, T def, EntityDataType type, TypeDecoder decoder) { + super(name, def, (Class) def.getClass()); + this.index = index; + this.type = type; + this.decoder = decoder; + } + + @Override + public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { + properties.put(index, newEntityData(index, type, decoder.decode(entity.getProperty(this)))); + } + + public interface TypeDecoder { + U decode(T obj); + } +} 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 efaaba1..3780ed0 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -173,7 +173,8 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { if (!version.isNewerThanOrEquals(ServerVersion.V_1_9)) return; register(builder(p, "shulker", EntityTypes.SHULKER) - .setHologramOffset(-0.975)); + .setHologramOffset(-0.975) + .addProperties("attach_direction", "shield_height", "shulker_color")); if (!version.isNewerThanOrEquals(ServerVersion.V_1_10)) return; From 6ebc60c11e9b752c5f512eddd10135de29aedaea Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Tue, 3 Oct 2023 13:56:56 +0530 Subject: [PATCH 11/12] change some properties to CustomTypeProperty --- .../entity/EntityPropertyRegistryImpl.java | 7 +++-- .../properties/DerpySnowgolemProperty.java | 23 -------------- .../entity/properties/NpcPoseProperty.java | 23 -------------- .../properties/SnifferStateProperty.java | 30 ------------------- 4 files changed, 4 insertions(+), 79 deletions(-) delete mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DerpySnowgolemProperty.java delete mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/NpcPoseProperty.java delete mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SnifferStateProperty.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 23e8d52..c3906cf 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -3,6 +3,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.entity.pose.EntityPose; import com.github.retrooper.packetevents.protocol.nbt.NBTCompound; import com.github.retrooper.packetevents.protocol.nbt.NBTInt; import com.github.retrooper.packetevents.protocol.nbt.NBTString; @@ -385,7 +386,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) snowGolemIndex = 14; else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) snowGolemIndex = 12; else snowGolemIndex = 10; - register(new DerpySnowgolemProperty(snowGolemIndex)); + register(new CustomTypeProperty<>("derpy_snowgolem", snowGolemIndex, false, EntityDataTypes.BYTE, b -> (byte) (b ? 0x00 : 0x10))); if (!ver.isNewerThanOrEquals(ServerVersion.V_1_10)) return; // Polar Bear @@ -458,7 +459,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { if (!ver.isNewerThanOrEquals(ServerVersion.V_1_14)) return; // Pose - register(new NpcPoseProperty()); + register(new CustomTypeProperty<>("pose", 6, NpcPose.STANDING, EntityDataTypes.ENTITY_POSE, npcPose -> EntityPose.valueOf(npcPose.name()))); // Villager final int villagerIndex; @@ -579,7 +580,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { register(new BooleanProperty("bashing", 18, false, legacyBooleans)); // Sniffer - register(new SnifferStateProperty(17)); + register(new CustomTypeProperty<>("sniffer_state", 17, SnifferState.IDLING, EntityDataTypes.SNIFFER_STATE, state -> com.github.retrooper.packetevents.protocol.entity.sniffer.SnifferState.valueOf(state.name()))); } private void registerSerializer(PropertySerializer serializer) { diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DerpySnowgolemProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DerpySnowgolemProperty.java deleted file mode 100644 index 3a3529c..0000000 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/DerpySnowgolemProperty.java +++ /dev/null @@ -1,23 +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 DerpySnowgolemProperty extends EntityPropertyImpl { - private final int index; - - public DerpySnowgolemProperty(int index) { - super("derpy_snowgolem", false, Boolean.class); - this.index = index; - } - - @Override - public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { - properties.put(index, new EntityData(index, EntityDataTypes.BYTE, (byte) (entity.getProperty(this) ? 0x00 : 0x10))); - } -} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/NpcPoseProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/NpcPoseProperty.java deleted file mode 100644 index 3599f64..0000000 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/NpcPoseProperty.java +++ /dev/null @@ -1,23 +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 com.github.retrooper.packetevents.protocol.entity.pose.EntityPose; -import lol.pyr.znpcsplus.entity.EntityPropertyImpl; -import lol.pyr.znpcsplus.entity.PacketEntity; -import lol.pyr.znpcsplus.util.NpcPose; -import org.bukkit.entity.Player; - -import java.util.Map; - -public class NpcPoseProperty extends EntityPropertyImpl { - - public NpcPoseProperty() { - super("pose", NpcPose.STANDING, NpcPose.class); - } - - @Override - public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { - properties.put(6, newEntityData(6, EntityDataTypes.ENTITY_POSE, EntityPose.valueOf(entity.getProperty(this).name()))); - } -} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SnifferStateProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SnifferStateProperty.java deleted file mode 100644 index c0e1ef5..0000000 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/SnifferStateProperty.java +++ /dev/null @@ -1,30 +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 lol.pyr.znpcsplus.util.SnifferState; -import org.bukkit.entity.Player; - -import java.util.Map; - -public class SnifferStateProperty extends EntityPropertyImpl { - private final int index; - - public SnifferStateProperty(int index) { - super("sniffer_state", SnifferState.IDLING, SnifferState.class); - this.index = index; - } - - @Override - public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { - SnifferState state = entity.getProperty(this); - if (state == null) return; - try { - properties.put(index, newEntityData(index, EntityDataTypes.SNIFFER_STATE, com.github.retrooper.packetevents.protocol.entity.sniffer.SnifferState.valueOf(state.name()))); - } catch (IllegalArgumentException e) { - // ignore - } - } -} From 51f4f709f6eebbcd2dafe0a16a542000227e6f4b Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Tue, 3 Oct 2023 14:35:33 +0530 Subject: [PATCH 12/12] added invulnerable_time property for Wither --- .../entity/EntityPropertyRegistryImpl.java | 19 +++++++++++-------- .../znpcsplus/npc/NpcTypeRegistryImpl.java | 3 ++- 2 files changed, 13 insertions(+), 9 deletions(-) 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 c3906cf..08ede74 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -102,14 +102,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { // Guardian registerType("is_elder", false); // TODO: ensure it only works till 1.10. Note: index is wrong on wiki.vg - // Shulker - registerType("attach_direction", null); // TODO: make a direction enum - registerType("shield_height", 0); // TODO: figure this out - registerType("shulker_color", DyeColor.RED); // TODO - - // Wither - registerType("invulnerable_time", 0); // TODO - */ } @@ -366,6 +358,17 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { linkProperties("wolf_angry", "tamed", "sitting"); } + // Wither + int witherIndex; + if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) witherIndex = 16; // using the first index, so we can add the other properties later if needed + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) witherIndex = 15; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) witherIndex = 14; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) witherIndex = 12; + else if (ver.isNewerThanOrEquals(ServerVersion.V_1_9)) witherIndex = 11; + else witherIndex = 17; + witherIndex += 3; // skip the first 3 indexes, will be used for the other properties later + register(new IntegerProperty("invulnerable_time", witherIndex++, 0, false)); + if (!ver.isNewerThanOrEquals(ServerVersion.V_1_9)) return; // Shulker int shulkerIndex; 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 3780ed0..4a8f77b 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -153,7 +153,8 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { .addProperties("hand")); register(builder(p, "wither", EntityTypes.WITHER) - .setHologramOffset(1.525)); + .setHologramOffset(1.525) + .addProperties("invulnerable_time")); register(builder(p, "wolf", EntityTypes.WOLF) .setHologramOffset(-1.125)