From 91c1cc17a5404995a000e2e80e91dadf18e0ae50 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Wed, 23 Aug 2023 01:11:48 +0200 Subject: [PATCH] npc center command --- .../lol/pyr/znpcsplus/util/NpcLocation.java | 4 +++ .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 1 + .../pyr/znpcsplus/commands/CenterCommand.java | 35 +++++++++++++++++++ .../src/main/resources/help-messages/root.txt | 1 + 4 files changed, 41 insertions(+) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/CenterCommand.java diff --git a/api/src/main/java/lol/pyr/znpcsplus/util/NpcLocation.java b/api/src/main/java/lol/pyr/znpcsplus/util/NpcLocation.java index bd5b45c..422258d 100644 --- a/api/src/main/java/lol/pyr/znpcsplus/util/NpcLocation.java +++ b/api/src/main/java/lol/pyr/znpcsplus/util/NpcLocation.java @@ -57,6 +57,10 @@ public class NpcLocation { return this.pitch; } + public NpcLocation centered() { + return new NpcLocation(Math.floor(x) + 0.5, y, Math.floor(z) + 0.5, yaw, pitch); + } + public Location toBukkitLocation(World world) { return new Location(world, this.x, this.y, this.z, this.yaw, this.pitch); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index f99e34a..a9715c1 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -293,6 +293,7 @@ public class ZNpcsPlus extends JavaPlugin { registerEnumParser(manager, VillagerLevel.class, incorrectUsageMessage); manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root")) + .addSubcommand("center", new CenterCommand(npcRegistry)) .addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry)) .addSubcommand("reloadconfig", new ReloadConfigCommand(configManager)) .addSubcommand("toggle", new ToggleCommand(npcRegistry)) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/CenterCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/CenterCommand.java new file mode 100644 index 0000000..b1a806d --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/CenterCommand.java @@ -0,0 +1,35 @@ +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.npc.NpcEntryImpl; +import lol.pyr.znpcsplus.npc.NpcImpl; +import lol.pyr.znpcsplus.npc.NpcRegistryImpl; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.Collections; +import java.util.List; + +public class CenterCommand implements CommandHandler { + private final NpcRegistryImpl npcRegistry; + + public CenterCommand(NpcRegistryImpl npcRegistry) { + this.npcRegistry = npcRegistry; + } + + @Override + public void run(CommandContext context) throws CommandExecutionException { + context.setUsage(context.getLabel() + " center "); + NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); + npc.setLocation(npc.getLocation().centered()); + context.send(Component.text("NPC has been centered on it's current block.", NamedTextColor.GREEN)); + } + + @Override + public List suggest(CommandContext context) throws CommandExecutionException { + if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); + return Collections.emptyList(); + } +} diff --git a/plugin/src/main/resources/help-messages/root.txt b/plugin/src/main/resources/help-messages/root.txt index ba24f1d..4030145 100644 --- a/plugin/src/main/resources/help-messages/root.txt +++ b/plugin/src/main/resources/help-messages/root.txt @@ -9,6 +9,7 @@ * /npc type * /npc near + * /npc center * /npc move * /npc teleport