From ba8a0eb6c2fdda98959dcb6c7790a4cd7df1031b Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Thu, 11 May 2023 10:54:11 +0100 Subject: [PATCH] add all npc types, command improvements --- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 41 ++---- .../pyr/znpcsplus/commands/CreateCommand.java | 13 +- .../pyr/znpcsplus/commands/DeleteCommand.java | 2 +- .../pyr/znpcsplus/commands/MoveCommand.java | 2 +- .../znpcsplus/commands/TeleportCommand.java | 2 +- .../commands/hologram/HoloAddCommand.java | 2 +- .../commands/hologram/HoloDeleteCommand.java | 2 +- .../commands/hologram/HoloInfoCommand.java | 2 +- .../commands/hologram/HoloInsertCommand.java | 2 +- .../commands/hologram/HoloSetCommand.java | 2 +- .../parsers/NamedTextColorParser.java | 22 ++++ .../pyr/znpcsplus/hologram/HologramImpl.java | 2 +- .../znpcsplus/metadata/MetadataFactory.java | 4 +- .../pyr/znpcsplus/metadata/V1_10Factory.java | 11 ++ .../pyr/znpcsplus/metadata/V1_13Factory.java | 2 +- .../pyr/znpcsplus/metadata/V1_8Factory.java | 5 + .../lol/pyr/znpcsplus/npc/NpcTypeImpl.java | 118 ++++++++++++++++-- .../pyr/znpcsplus/packets/PacketFactory.java | 8 +- .../pyr/znpcsplus/packets/V1_10Factory.java | 18 +++ .../pyr/znpcsplus/packets/V1_14Factory.java | 6 +- .../pyr/znpcsplus/packets/V1_8Factory.java | 30 +++-- .../pyr/znpcsplus/packets/V1_9Factory.java | 18 ++- 22 files changed, 221 insertions(+), 93 deletions(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NamedTextColorParser.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_10Factory.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_10Factory.java diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 3aef95a..3140e82 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -2,7 +2,6 @@ package lol.pyr.znpcsplus; import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.event.PacketListenerPriority; -import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder; import lol.pyr.director.adventure.command.CommandManager; import lol.pyr.director.adventure.command.MultiCommand; @@ -12,13 +11,12 @@ import lol.pyr.znpcsplus.api.ZApiProvider; import lol.pyr.znpcsplus.commands.*; import lol.pyr.znpcsplus.commands.hologram.*; import lol.pyr.znpcsplus.commands.parsers.EntityPropertyParser; +import lol.pyr.znpcsplus.commands.parsers.NamedTextColorParser; import lol.pyr.znpcsplus.commands.parsers.NpcEntryParser; import lol.pyr.znpcsplus.commands.parsers.NpcTypeParser; import lol.pyr.znpcsplus.config.Configs; import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.interaction.InteractionPacketListener; -import lol.pyr.znpcsplus.interaction.types.ConsoleCommandAction; -import lol.pyr.znpcsplus.interaction.types.MessageAction; import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; @@ -27,9 +25,6 @@ import lol.pyr.znpcsplus.scheduling.FoliaScheduler; import lol.pyr.znpcsplus.scheduling.SpigotScheduler; import lol.pyr.znpcsplus.scheduling.TaskScheduler; import lol.pyr.znpcsplus.skin.cache.SkinCacheCleanTask; -import lol.pyr.znpcsplus.skin.descriptor.FetchingDescriptor; -import lol.pyr.znpcsplus.skin.descriptor.MirrorDescriptor; -import lol.pyr.znpcsplus.skin.descriptor.PrefetchedDescriptor; import lol.pyr.znpcsplus.tasks.NpcVisibilityTask; import lol.pyr.znpcsplus.updater.UpdateChecker; import lol.pyr.znpcsplus.updater.UpdateNotificationListener; @@ -124,7 +119,7 @@ public class ZNpcsPlus extends JavaPlugin { PacketEvents.getAPI().init(); PLACEHOLDERS_SUPPORTED = Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI"); - if (PLACEHOLDERS_SUPPORTED) log(ChatColor.WHITE + " * Enabling PlaceholderAPI Support..."); + if (PLACEHOLDERS_SUPPORTED) log(ChatColor.WHITE + " * Enabling PlaceholderAPI support..."); PLUGIN_FOLDER.mkdirs(); PATH_FOLDER.mkdirs(); @@ -132,6 +127,9 @@ public class ZNpcsPlus extends JavaPlugin { log(ChatColor.WHITE + " * Loading configurations..."); Configs.init(PLUGIN_FOLDER); + log(ChatColor.WHITE + " * Defining NPC types..."); + NpcTypeImpl.defineTypes(); + log(ChatColor.WHITE + " * Registering components..."); getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); new Metrics(this, PLUGIN_ID); @@ -155,37 +153,16 @@ public class ZNpcsPlus extends JavaPlugin { log(""); if (Configs.config().debugEnabled()) { - int wrap = 20; - int x = 0; - int z = 0; World world = Bukkit.getWorld("world"); if (world == null) world = Bukkit.getWorlds().get(0); + int i = 0; for (NpcTypeImpl type : NpcTypeImpl.values()) { - NpcEntryImpl entry = NpcRegistryImpl.get().create(ZNpcsPlus.DEBUG_NPC_PREFIX + (z * wrap + x), world, type, new ZLocation(x * 3, 200, z * 3, 0, 0)); + NpcEntryImpl entry = NpcRegistryImpl.get().create(ZNpcsPlus.DEBUG_NPC_PREFIX + i, world, type, new ZLocation(i * 3, 200, 0, 0, 0)); entry.setProcessed(true); NpcImpl npc = entry.getNpc(); - if (type.getType() == EntityTypes.PLAYER) { - PrefetchedDescriptor.forPlayer("Notch").thenAccept(skin -> npc.setProperty(EntityPropertyImpl.SKIN, skin)); - npc.setProperty(EntityPropertyImpl.INVISIBLE, true); - } - npc.setProperty(EntityPropertyImpl.GLOW, NamedTextColor.RED); npc.getHologram().addLine(Component.text("Hello, World!")); - if (x++ > wrap) { - x = 0; - z++; - } + i++; } - NpcEntryImpl entry = NpcRegistryImpl.get().create(ZNpcsPlus.DEBUG_NPC_PREFIX + (z * wrap + x), world, NpcTypeImpl.byName("player"), new ZLocation(x * 3, 200, z * 3, 0, 0)); - entry.setProcessed(true); - NpcImpl npc = entry.getNpc(); - npc.setProperty(EntityPropertyImpl.SKIN, new FetchingDescriptor("jeb_")); - npc.addAction(new MessageAction(1000L, "Hi, I'm jeb!")); - x++; - entry = NpcRegistryImpl.get().create(ZNpcsPlus.DEBUG_NPC_PREFIX + (z * wrap + x), world, NpcTypeImpl.byName("player"), new ZLocation(x * 3, 200, z * 3, 0, 0)); - entry.setProcessed(true); - npc = entry.getNpc(); - npc.setProperty(EntityPropertyImpl.SKIN, new MirrorDescriptor()); - npc.addAction(new ConsoleCommandAction(1000L, "kick {player}")); } } @@ -202,11 +179,13 @@ public class ZNpcsPlus extends JavaPlugin { private void registerCommands() { // TODO: Messages in here CommandManager manager = new CommandManager(this, ADVENTURE, context -> {}); + manager.registerParser(NpcTypeImpl.class, new NpcTypeParser(context -> {})); manager.registerParser(NpcEntryImpl.class, new NpcEntryParser(context -> {})); manager.registerParser(EntityPropertyImpl.class, new EntityPropertyParser(context -> {})); manager.registerParser(Integer.class, new IntegerParser(context -> {})); manager.registerParser(Boolean.class, new BooleanParser(context -> {})); + manager.registerParser(NamedTextColor.class, new NamedTextColorParser(context -> {})); manager.registerCommand("npc", new MultiCommand() .addSubcommand("action", new ActionCommand()) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java index eaff078..5cabeea 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java @@ -3,12 +3,9 @@ package lol.pyr.znpcsplus.commands; import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.adventure.command.CommandHandler; import lol.pyr.director.common.command.CommandExecutionException; -import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl; -import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import lol.pyr.znpcsplus.npc.NpcTypeImpl; -import lol.pyr.znpcsplus.skin.descriptor.PrefetchedDescriptor; import lol.pyr.znpcsplus.util.ZLocation; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -20,23 +17,17 @@ import java.util.List; public class CreateCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getLabel() + " create "); + context.setUsage(context.getLabel() + " create "); Player player = context.ensureSenderIsPlayer(); String id = context.popString(); if (NpcRegistryImpl.get().get(id) != null) context.halt(Component.text("NPC with that ID already exists.", NamedTextColor.RED)); - NpcTypeImpl type = context.parse(NpcTypeImpl.class); - String name = context.popString(); NpcEntryImpl entry = NpcRegistryImpl.get().create(id, player.getWorld(), type, new ZLocation(player.getLocation())); entry.enableEverything(); - NpcImpl npc = entry.getNpc(); - if (type == NpcTypeImpl.PLAYER) PrefetchedDescriptor.forPlayer(name).thenAccept(skin -> npc.setProperty(EntityPropertyImpl.SKIN, skin)); - npc.getHologram().addLine(Component.text(name)); - - context.send(Component.text("Created NPC with ID " + id + " and name " + name + ".", NamedTextColor.GREEN)); + context.send(Component.text("Created a " + type.getName() + " NPC with ID " + id + ".", NamedTextColor.GREEN)); } @Override diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/DeleteCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/DeleteCommand.java index 51a7b36..ed3060f 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/DeleteCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/DeleteCommand.java @@ -15,7 +15,7 @@ import java.util.List; public class DeleteCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getLabel() + " delete "); + context.setUsage(context.getLabel() + " delete "); NpcEntryImpl entry = context.parse(NpcEntryImpl.class); NpcRegistryImpl.get().delete(entry.getId()); ZNpcsPlus.ADVENTURE.sender(context.getSender()).sendMessage(Component.text("Deleted NPC with ID: " + entry.getId(), NamedTextColor.GREEN)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/MoveCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/MoveCommand.java index 3bd41d0..65fdae4 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/MoveCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/MoveCommand.java @@ -17,7 +17,7 @@ import java.util.List; public class MoveCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getLabel() + " move "); + context.setUsage(context.getLabel() + " move "); Player player = context.ensureSenderIsPlayer(); NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); npc.setLocation(new ZLocation(player.getLocation())); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java index 8bda10e..25e3161 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java @@ -17,7 +17,7 @@ import java.util.List; public class TeleportCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getLabel() + " teleport "); + context.setUsage(context.getLabel() + " teleport "); Player player = context.ensureSenderIsPlayer(); NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); FoliaUtil.teleport(player, npc.getBukkitLocation()); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloAddCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloAddCommand.java index a9d3069..dc432cd 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloAddCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloAddCommand.java @@ -16,7 +16,7 @@ import java.util.List; public class HoloAddCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getLabel() + " holo add "); + context.setUsage(context.getLabel() + " holo add "); HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram(); hologram.addLine(ZNpcsPlus.LEGACY_AMPERSAND_SERIALIZER.deserialize(context.dumpAllArgs())); context.send(Component.text("NPC line added!", NamedTextColor.GREEN)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloDeleteCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloDeleteCommand.java index be5e2e3..ebd4c7c 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloDeleteCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloDeleteCommand.java @@ -16,7 +16,7 @@ import java.util.stream.Stream; public class HoloDeleteCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getLabel() + " holo delete "); + context.setUsage(context.getLabel() + " holo delete "); HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram(); int line = context.parse(Integer.class); if (line < 0 || line >= hologram.getLines().size()) context.halt(Component.text("Invalid line number!", NamedTextColor.RED)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloInfoCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloInfoCommand.java index 9364759..91864d3 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloInfoCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloInfoCommand.java @@ -16,7 +16,7 @@ import java.util.List; public class HoloInfoCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getLabel() + " holo info "); + context.setUsage(context.getLabel() + " holo info "); NpcEntryImpl entry = context.parse(NpcEntryImpl.class); HologramImpl hologram = entry.getNpc().getHologram(); Component component = Component.text("NPC Hologram Info of ID " + entry.getId() + ":", NamedTextColor.GREEN).appendNewline(); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloInsertCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloInsertCommand.java index f1e8853..afc8818 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloInsertCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloInsertCommand.java @@ -17,7 +17,7 @@ import java.util.stream.Stream; public class HoloInsertCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getLabel() + " holo insert "); + context.setUsage(context.getLabel() + " holo insert "); HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram(); int line = context.parse(Integer.class); if (line < 0 || line >= hologram.getLines().size()) context.halt(Component.text("Invalid line number!", NamedTextColor.RED)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloSetCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloSetCommand.java index c02bc81..d9a522d 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloSetCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloSetCommand.java @@ -17,7 +17,7 @@ import java.util.stream.Stream; public class HoloSetCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getLabel() + " holo set "); + context.setUsage(context.getLabel() + " holo set "); HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram(); int line = context.parse(Integer.class); if (line < 0 || line >= hologram.getLines().size()) context.halt(Component.text("Invalid line number!", NamedTextColor.RED)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NamedTextColorParser.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NamedTextColorParser.java new file mode 100644 index 0000000..1ad22d0 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NamedTextColorParser.java @@ -0,0 +1,22 @@ +package lol.pyr.znpcsplus.commands.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 net.kyori.adventure.text.format.NamedTextColor; + +import java.util.Deque; + +public class NamedTextColorParser extends ParserType { + public NamedTextColorParser(Message message) { + super(message); + } + + @Override + public NamedTextColor parse(Deque deque) throws CommandExecutionException { + NamedTextColor color = NamedTextColor.NAMES.value(deque.pop()); + if (color == null) throw new CommandExecutionException(); + return color; + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java index 3b748a0..6c8ea5e 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java @@ -73,7 +73,7 @@ public class HologramImpl extends Viewable implements Hologram { private void relocateLines(HologramLine newLine) { final double lineSpacing = Configs.config().lineSpacing(); - double height = location.getY() + lines.size() * lineSpacing; + double height = location.getY() + (lines.size() - 1) * lineSpacing; for (HologramLine line : lines) { line.setLocation(location.withY(height), line == newLine ? Collections.emptySet() : getViewers()); height -= lineSpacing; 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 4fe1b85..718d840 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java @@ -28,8 +28,9 @@ import java.util.Map; public interface MetadataFactory { EntityData skinLayers(boolean enabled); EntityData effects(boolean onFire, boolean glowing, boolean invisible); - Collection name(Component name); EntityData silent(boolean enabled); + Collection name(Component name); + EntityData noGravity(); MetadataFactory factory = get(); @@ -52,6 +53,7 @@ public interface MetadataFactory { HashMap> map = new HashMap<>(); map.put(ServerVersion.V_1_8, LazyLoader.of(V1_8Factory::new)); map.put(ServerVersion.V_1_9, LazyLoader.of(V1_9Factory::new)); + map.put(ServerVersion.V_1_10, LazyLoader.of(V1_10Factory::new)); map.put(ServerVersion.V_1_13, LazyLoader.of(V1_13Factory::new)); map.put(ServerVersion.V_1_14, LazyLoader.of(V1_14Factory::new)); map.put(ServerVersion.V_1_16, LazyLoader.of(V1_16Factory::new)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_10Factory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_10Factory.java new file mode 100644 index 0000000..0ccf4f0 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_10Factory.java @@ -0,0 +1,11 @@ +package lol.pyr.znpcsplus.metadata; + +import com.github.retrooper.packetevents.protocol.entity.data.EntityData; +import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; + +public class V1_10Factory extends V1_9Factory { + @Override + public EntityData noGravity() { + return new EntityData(5, EntityDataTypes.BOOLEAN, true); + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_13Factory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_13Factory.java index 9358011..ad8b233 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_13Factory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_13Factory.java @@ -9,7 +9,7 @@ import net.kyori.adventure.text.Component; import java.util.Collection; import java.util.Optional; -public class V1_13Factory extends V1_9Factory { +public class V1_13Factory extends V1_10Factory { @Override public Collection name(Component name) { return ListUtil.immutableList( diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8Factory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8Factory.java index 327be42..0522302 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8Factory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8Factory.java @@ -27,6 +27,11 @@ public class V1_8Factory implements MetadataFactory { ); } + @Override + public EntityData noGravity() { + throw new UnsupportedOperationException("The gravity entity data isn't supported on this version"); + } + @Override public EntityData silent(boolean enabled) { return new EntityData(4, EntityDataTypes.BYTE, enabled ? 1 : 0); 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 db4db94..2b69f85 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java @@ -56,22 +56,114 @@ public class NpcTypeImpl { return type; } - public static final NpcTypeImpl PLAYER = define( - new Builder("player", EntityTypes.PLAYER) - .addProperties(EntityPropertyImpl.SKIN, EntityPropertyImpl.SKIN_LAYERS) - .setHologramOffset(-0.45D)); + private static boolean defined = false; + public static void defineTypes() { + if (defined) return; + defined = true; + ServerVersion version = PacketEvents.getAPI().getServerManager().getVersion(); - public static final NpcTypeImpl CREEPER = define( - new Builder("creeper", EntityTypes.CREEPER) - .setHologramOffset(-0.6D)); + define(new Builder("player", EntityTypes.PLAYER).setHologramOffset(-0.15D) + .addProperties(EntityPropertyImpl.SKIN, EntityPropertyImpl.SKIN_LAYERS)); - public static final NpcTypeImpl ZOMBIE = define( - new Builder("zombie", EntityTypes.ZOMBIE) - .setHologramOffset(-0.3D)); + define(new Builder("armor_stand", EntityTypes.ARMOR_STAND)); + define(new Builder("bat", EntityTypes.BAT).setHologramOffset(-1.365)); + define(new Builder("blaze", EntityTypes.BLAZE)); + define(new Builder("cat", EntityTypes.CAT)); + define(new Builder("cave_spider", EntityTypes.CAVE_SPIDER)); + define(new Builder("chicken", EntityTypes.CHICKEN)); + define(new Builder("cow", EntityTypes.COW)); + define(new Builder("creeper", EntityTypes.CREEPER).setHologramOffset(-0.3D)); + define(new Builder("donkey", EntityTypes.DONKEY)); + define(new Builder("elder_guardian", EntityTypes.ELDER_GUARDIAN)); + define(new Builder("ender_dragon", EntityTypes.ENDER_DRAGON)); + define(new Builder("enderman", EntityTypes.ENDERMAN)); + define(new Builder("endermite", EntityTypes.ENDERMITE)); + define(new Builder("ghast", EntityTypes.GHAST)); + define(new Builder("giant", EntityTypes.GIANT)); + define(new Builder("guardian", EntityTypes.GUARDIAN)); + define(new Builder("horse", EntityTypes.HORSE)); + define(new Builder("iron_golem", EntityTypes.IRON_GOLEM)); + define(new Builder("magma_cube", EntityTypes.MAGMA_CUBE)); + define(new Builder("mooshroom", EntityTypes.MOOSHROOM)); + define(new Builder("mule", EntityTypes.MULE)); + define(new Builder("ocelot", EntityTypes.OCELOT)); + define(new Builder("pig", EntityTypes.PIG)); + define(new Builder("rabbit", EntityTypes.RABBIT)); + define(new Builder("sheep", EntityTypes.SHEEP)); + define(new Builder("silverfish", EntityTypes.SILVERFISH)); + define(new Builder("skeleton", EntityTypes.SKELETON)); + define(new Builder("skeleton_horse", EntityTypes.SKELETON_HORSE)); + define(new Builder("slime", EntityTypes.SLIME)); + define(new Builder("snow_golem", EntityTypes.SNOW_GOLEM)); + define(new Builder("spider", EntityTypes.SPIDER)); + define(new Builder("squid", EntityTypes.SQUID)); + define(new Builder("villager", EntityTypes.VILLAGER)); + define(new Builder("witch", EntityTypes.WITCH)); + define(new Builder("wither", EntityTypes.WITHER)); + define(new Builder("wither_skeleton", EntityTypes.WITHER_SKELETON)); + define(new Builder("wolf", EntityTypes.WOLF)); + define(new Builder("zombie", EntityTypes.ZOMBIE)); + define(new Builder("zombie_horse", EntityTypes.ZOMBIE_HORSE)); + define(new Builder("zombie_villager", EntityTypes.ZOMBIE_VILLAGER)); - public static final NpcTypeImpl SKELETON = define( - new Builder("skeleton", EntityTypes.SKELETON) - .setHologramOffset(-0.3D)); + if (!version.isNewerThanOrEquals(ServerVersion.V_1_9)) return; + define(new Builder("shulker", EntityTypes.SHULKER)); + + if (!version.isNewerThanOrEquals(ServerVersion.V_1_10)) return; + define(new Builder("husk", EntityTypes.HUSK)); + define(new Builder("polar_bear", EntityTypes.POLAR_BEAR)); + define(new Builder("stray", EntityTypes.STRAY)); + + if (!version.isNewerThanOrEquals(ServerVersion.V_1_11)) return; + define(new Builder("evoker", EntityTypes.EVOKER)); + define(new Builder("llama", EntityTypes.LLAMA)); + define(new Builder("vex", EntityTypes.VEX)); + define(new Builder("vindicator", EntityTypes.VINDICATOR)); + + if (!version.isNewerThanOrEquals(ServerVersion.V_1_12)) return; + define(new Builder("illusioner", EntityTypes.ILLUSIONER)); + define(new Builder("parrot", EntityTypes.PARROT)); + + if (!version.isNewerThanOrEquals(ServerVersion.V_1_13)) return; + define(new Builder("cod", EntityTypes.COD)); + define(new Builder("dolphin", EntityTypes.DOLPHIN)); + define(new Builder("drowned", EntityTypes.DROWNED)); + define(new Builder("phantom", EntityTypes.PHANTOM)); + define(new Builder("pufferfish", EntityTypes.PUFFERFISH)); + define(new Builder("salmon", EntityTypes.SALMON)); + define(new Builder("tropical_fish", EntityTypes.TROPICAL_FISH)); + define(new Builder("turtle", EntityTypes.TURTLE)); + + if (!version.isNewerThanOrEquals(ServerVersion.V_1_14)) return; + define(new Builder("fox", EntityTypes.FOX)); + define(new Builder("panda", EntityTypes.PANDA)); + define(new Builder("pillager", EntityTypes.PILLAGER)); + define(new Builder("ravager", EntityTypes.RAVAGER)); + define(new Builder("trader_llama", EntityTypes.TRADER_LLAMA)); + define(new Builder("wandering_trader", EntityTypes.WANDERING_TRADER)); + + if (!version.isNewerThanOrEquals(ServerVersion.V_1_15)) return; + define(new Builder("bee", EntityTypes.BEE)); + + if (!version.isNewerThanOrEquals(ServerVersion.V_1_16)) return; + define(new Builder("hoglin", EntityTypes.HOGLIN)); + define(new Builder("piglin", EntityTypes.PIGLIN)); + define(new Builder("piglin_brute", EntityTypes.PIGLIN_BRUTE)); + define(new Builder("strider", EntityTypes.STRIDER)); + define(new Builder("zoglin", EntityTypes.ZOGLIN)); + define(new Builder("zombified_piglin", EntityTypes.ZOMBIFIED_PIGLIN)); + + if (!version.isNewerThanOrEquals(ServerVersion.V_1_17)) return; + define(new Builder("axolotl", EntityTypes.AXOLOTL)); + define(new Builder("glow_squid", EntityTypes.GLOW_SQUID)); + define(new Builder("goat", EntityTypes.GOAT)); + + if (!version.isNewerThanOrEquals(ServerVersion.V_1_19)) return; + define(new Builder("allay", EntityTypes.ALLAY)); + define(new Builder("frog", EntityTypes.FROG)); + define(new Builder("tadpole", EntityTypes.TADPOLE)); + define(new Builder("warden", EntityTypes.WARDEN)); + } private static final class Builder { private final String name; diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java index 1f5711f..a7b075a 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java @@ -4,14 +4,12 @@ import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import lol.pyr.znpcsplus.ZNpcsPlus; -import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.api.entity.PropertyHolder; +import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.util.LazyLoader; import org.bukkit.entity.Player; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.CompletableFuture; public interface PacketFactory { @@ -23,6 +21,7 @@ public interface PacketFactory { void removeTabPlayer(Player player, PacketEntity entity); void createTeam(Player player, PacketEntity entity, PropertyHolder properties); void removeTeam(Player player, PacketEntity entity); + Map generateMetadata(Player player, PacketEntity entity, PropertyHolder properties); void sendAllMetadata(Player player, PacketEntity entity, PropertyHolder properties); void sendMetadata(Player player, PacketEntity entity, List data); @@ -47,6 +46,7 @@ public interface PacketFactory { HashMap> map = new HashMap<>(); map.put(ServerVersion.V_1_8, LazyLoader.of(V1_8Factory::new)); map.put(ServerVersion.V_1_9, LazyLoader.of(V1_9Factory::new)); + map.put(ServerVersion.V_1_10, LazyLoader.of(V1_10Factory::new)); map.put(ServerVersion.V_1_14, LazyLoader.of(V1_14Factory::new)); map.put(ServerVersion.V_1_19, LazyLoader.of(V1_19Factory::new)); return map; diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_10Factory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_10Factory.java new file mode 100644 index 0000000..faa0e9f --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_10Factory.java @@ -0,0 +1,18 @@ +package lol.pyr.znpcsplus.packets; + +import com.github.retrooper.packetevents.protocol.entity.data.EntityData; +import lol.pyr.znpcsplus.api.entity.PropertyHolder; +import lol.pyr.znpcsplus.entity.PacketEntity; +import lol.pyr.znpcsplus.metadata.MetadataFactory; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class V1_10Factory extends V1_9Factory { + @Override + public Map generateMetadata(Player player, PacketEntity entity, PropertyHolder properties) { + Map data = super.generateMetadata(player, entity, properties); + add(data, MetadataFactory.get().noGravity()); + return data; + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_14Factory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_14Factory.java index bcee102..48a13d6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_14Factory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_14Factory.java @@ -1,5 +1,6 @@ package lol.pyr.znpcsplus.packets; +import com.github.retrooper.packetevents.util.Vector3d; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnEntity; import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.util.ZLocation; @@ -9,13 +10,14 @@ import org.bukkit.entity.Player; import java.util.Optional; -public class V1_14Factory extends V1_9Factory { +public class V1_14Factory extends V1_10Factory { @Override public void spawnEntity(Player player, PacketEntity entity, PropertyHolder properties) { ZLocation location = entity.getLocation(); sendPacket(player, new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.of(entity.getUuid()), entity.getType(), - location.toVector3d(), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.empty())); + location.toVector3d(), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.of(new Vector3d()))); if (properties.hasProperty(EntityPropertyImpl.GLOW)) createTeam(player, entity, properties); sendAllMetadata(player, entity, properties); + createTeam(player, entity, properties); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8Factory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8Factory.java index 540d411..d9d1bf6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8Factory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8Factory.java @@ -21,10 +21,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.concurrent.CompletableFuture; public class V1_8Factory implements PacketFactory { @@ -106,14 +103,19 @@ public class V1_8Factory implements PacketFactory { sendPacket(player, new WrapperPlayServerTeams("npc_team_" + entity.getEntityId(), WrapperPlayServerTeams.TeamMode.REMOVE, (WrapperPlayServerTeams.ScoreBoardTeamInfo) null)); } + @Override + public Map generateMetadata(Player player, PacketEntity entity, PropertyHolder properties) { + HashMap data = new HashMap<>(); + if (entity.getType() == EntityTypes.PLAYER) add(data, MetadataFactory.get().skinLayers(properties.getProperty(EntityPropertyImpl.SKIN_LAYERS))); + add(data, MetadataFactory.get().effects(properties.getProperty(EntityPropertyImpl.FIRE), false, properties.getProperty(EntityPropertyImpl.INVISIBLE))); + add(data, MetadataFactory.get().silent(properties.getProperty(EntityPropertyImpl.SILENT))); + if (properties.hasProperty(EntityPropertyImpl.NAME)) addAll(data, MetadataFactory.get().name(properties.getProperty(EntityPropertyImpl.NAME))); + return data; + } + @Override public void sendAllMetadata(Player player, PacketEntity entity, PropertyHolder properties) { - ArrayList data = new ArrayList<>(); - if (entity.getType() == EntityTypes.PLAYER) data.add(MetadataFactory.get().skinLayers(properties.getProperty(EntityPropertyImpl.SKIN_LAYERS))); - data.add(MetadataFactory.get().effects(properties.getProperty(EntityPropertyImpl.FIRE), false, properties.getProperty(EntityPropertyImpl.INVISIBLE))); - data.add(MetadataFactory.get().silent(properties.getProperty(EntityPropertyImpl.SILENT))); - if (properties.hasProperty(EntityPropertyImpl.NAME)) data.addAll(MetadataFactory.get().name(properties.getProperty(EntityPropertyImpl.NAME))); - sendMetadata(player, entity, data); + sendMetadata(player, entity, new ArrayList<>(generateMetadata(player, entity, properties).values())); } @Override @@ -139,4 +141,12 @@ public class V1_8Factory implements PacketFactory { }); return future; } + + protected void add(Map map, EntityData data) { + map.put(data.getIndex(), data); + } + + protected void addAll(Map map, Collection data) { + for (EntityData d : data) add(map, d); + } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_9Factory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_9Factory.java index b0b1b92..f6d675f 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_9Factory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_9Factory.java @@ -1,23 +1,19 @@ package lol.pyr.znpcsplus.packets; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; -import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; +import lol.pyr.znpcsplus.api.entity.PropertyHolder; +import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.metadata.MetadataFactory; -import lol.pyr.znpcsplus.entity.EntityPropertyImpl; -import lol.pyr.znpcsplus.api.entity.PropertyHolder; import org.bukkit.entity.Player; -import java.util.ArrayList; +import java.util.Map; public class V1_9Factory extends V1_8Factory { @Override - public void sendAllMetadata(Player player, PacketEntity entity, PropertyHolder properties) { - ArrayList data = new ArrayList<>(); - if (entity.getType() == EntityTypes.PLAYER) data.add(MetadataFactory.get().skinLayers(properties.getProperty(EntityPropertyImpl.SKIN_LAYERS))); - data.add(MetadataFactory.get().effects(properties.getProperty(EntityPropertyImpl.FIRE), properties.hasProperty(EntityPropertyImpl.GLOW), properties.getProperty(EntityPropertyImpl.INVISIBLE))); - data.add(MetadataFactory.get().silent(properties.getProperty(EntityPropertyImpl.SILENT))); - if (properties.hasProperty(EntityPropertyImpl.NAME)) data.addAll(MetadataFactory.get().name(properties.getProperty(EntityPropertyImpl.NAME))); - sendMetadata(player, entity, data); + public Map generateMetadata(Player player, PacketEntity entity, PropertyHolder properties) { + Map data = super.generateMetadata(player, entity, properties); + add(data, MetadataFactory.get().effects(properties.getProperty(EntityPropertyImpl.FIRE), properties.hasProperty(EntityPropertyImpl.GLOW), properties.getProperty(EntityPropertyImpl.INVISIBLE))); + return data; } }