From 60b5ac96830bfe6e66bfd3c3d25e5d26f647051a Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Mon, 19 Jun 2023 15:29:34 +0200 Subject: [PATCH] add npc toggle command --- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 1 + .../pyr/znpcsplus/commands/ListCommand.java | 2 +- .../pyr/znpcsplus/commands/ToggleCommand.java | 36 +++++++++++++++++++ .../java/lol/pyr/znpcsplus/npc/NpcImpl.java | 10 ++++++ .../znpcsplus/storage/yaml/YamlStorage.java | 3 ++ .../pyr/znpcsplus/tasks/NpcProcessorTask.java | 1 + .../src/main/resources/help-messages/root.txt | 1 + 7 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/ToggleCommand.java diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 5706fb3..bee6c89 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -233,6 +233,7 @@ public class ZNpcsPlus extends JavaPlugin { manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root")) .addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry)) + .addSubcommand("toggle", new ToggleCommand(npcRegistry)) .addSubcommand("skin", new SkinCommand(skinCache, npcRegistry, typeRegistry, propertyRegistry)) .addSubcommand("delete", new DeleteCommand(npcRegistry, adventure)) .addSubcommand("move", new MoveCommand(npcRegistry)) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ListCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ListCommand.java index a7f9690..e8bf2c6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ListCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ListCommand.java @@ -24,7 +24,7 @@ public class ListCommand implements CommandHandler { for (String id : npcRegistry.getModifiableIds()) { NpcImpl npc = npcRegistry.getById(id).getNpc(); NpcLocation location = npc.getLocation(); - component.append(Component.text("ID: " + id, NamedTextColor.GREEN)) + component.append(Component.text("ID: " + id, npc.isEnabled() ? NamedTextColor.GREEN : NamedTextColor.RED)) .append(Component.text(" | ", NamedTextColor.GRAY)) .append(Component.text("Type: ", NamedTextColor.GREEN)) .append(Component.text(npc.getType().getName(), NamedTextColor.GREEN)) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ToggleCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ToggleCommand.java new file mode 100644 index 0000000..c6d1c06 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ToggleCommand.java @@ -0,0 +1,36 @@ +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 ToggleCommand implements CommandHandler { + private final NpcRegistryImpl npcRegistry; + + public ToggleCommand(NpcRegistryImpl npcRegistry) { + this.npcRegistry = npcRegistry; + } + + @Override + public void run(CommandContext context) throws CommandExecutionException { + context.setUsage(context.getLabel() + " toggle "); + NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); + boolean enabled = !npc.isEnabled(); + npc.setEnabled(enabled); + context.send(Component.text("NPC has been " + (enabled ? "enabled" : "disabled"), 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/java/lol/pyr/znpcsplus/npc/NpcImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java index f9b97bb..2fee6a6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java @@ -24,6 +24,7 @@ public class NpcImpl extends Viewable implements Npc { private PacketEntity entity; private NpcLocation location; private NpcTypeImpl type; + private boolean enabled = true; private final HologramImpl hologram; private final UUID uuid; @@ -78,6 +79,15 @@ public class NpcImpl extends Viewable implements Npc { return hologram; } + public void setEnabled(boolean enabled) { + this.enabled = enabled; + if (!enabled) delete(); + } + + public boolean isEnabled() { + return enabled; + } + public UUID getUuid() { return uuid; } 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 3b150e5..dcbbcec 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 @@ -52,6 +52,8 @@ public class YamlStorage implements NpcStorage { NpcImpl npc = new NpcImpl(uuid, configManager, packetFactory, textSerializer, config.getString("world"), typeRegistry.getByName(config.getString("type")), deserializeLocation(config.getConfigurationSection("location"))); + if (config.isBoolean("enabled")) npc.setEnabled(config.getBoolean("enabled")); + ConfigurationSection properties = config.getConfigurationSection("properties"); if (properties != null) { for (String key : properties.getKeys(false)) { @@ -84,6 +86,7 @@ public class YamlStorage implements NpcStorage { config.set("allow-commands", entry.isAllowCommandModification()); NpcImpl npc = entry.getNpc(); + config.set("enabled", npc.isEnabled()); config.set("uuid", npc.getUuid().toString()); config.set("world", npc.getWorldName()); config.set("location", serializeLocation(npc.getLocation())); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/tasks/NpcProcessorTask.java b/plugin/src/main/java/lol/pyr/znpcsplus/tasks/NpcProcessorTask.java index 471695c..426a854 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/tasks/NpcProcessorTask.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/tasks/NpcProcessorTask.java @@ -31,6 +31,7 @@ public class NpcProcessorTask extends BukkitRunnable { EntityPropertyImpl lookProperty = propertyRegistry.getByName("look", Boolean.class); for (NpcEntryImpl entry : npcRegistry.getProcessable()) { NpcImpl npc = entry.getNpc(); + if (!npc.isEnabled()) continue; double closestDist = Double.MAX_VALUE; Player closest = null; diff --git a/plugin/src/main/resources/help-messages/root.txt b/plugin/src/main/resources/help-messages/root.txt index 674b221..a0daed0 100644 --- a/plugin/src/main/resources/help-messages/root.txt +++ b/plugin/src/main/resources/help-messages/root.txt @@ -4,6 +4,7 @@ * /npc create * /npc delete + * /npc toggle * /npc list * /npc type