From a2503cb3cb3411f589a3c05eaa201702f03a5241 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Wed, 10 May 2023 16:01:14 +0100 Subject: [PATCH] further command improvements --- plugin/build.gradle | 2 +- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 8 +++ .../pyr/znpcsplus/commands/ActionCommand.java | 7 +-- .../commands/ConversationsCommand.java | 7 +-- .../pyr/znpcsplus/commands/CreateCommand.java | 53 +++++++------------ .../pyr/znpcsplus/commands/DeleteCommand.java | 16 ++---- .../pyr/znpcsplus/commands/ListCommand.java | 22 ++++---- .../pyr/znpcsplus/commands/MoveCommand.java | 35 ++++-------- .../pyr/znpcsplus/commands/PathCommand.java | 7 +-- .../znpcsplus/commands/PropertiesCommand.java | 7 +-- .../znpcsplus/commands/TeleportCommand.java | 33 ++++-------- .../commands/hologram/HoloAddCommand.java | 32 ++++------- .../commands/hologram/HoloDeleteCommand.java | 48 +++++------------ .../commands/hologram/HoloInfoCommand.java | 43 ++++----------- .../commands/hologram/HoloInsertCommand.java | 50 +++++------------ .../commands/hologram/HoloSetCommand.java | 52 ++++++------------ .../commands/parsers/NpcEntryParser.java | 23 ++++++++ .../commands/parsers/NpcTypeParser.java | 22 ++++++++ 18 files changed, 187 insertions(+), 280 deletions(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NpcEntryParser.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NpcTypeParser.java diff --git a/plugin/build.gradle b/plugin/build.gradle index f872efa..1cbf477 100644 --- a/plugin/build.gradle +++ b/plugin/build.gradle @@ -28,7 +28,7 @@ dependencies { implementation "com.github.retrooper.packetevents:spigot:2.0.0-SNAPSHOT" implementation "space.arim.dazzleconf:dazzleconf-ext-snakeyaml:1.2.1" - implementation "lol.pyr:director-adventure:2.0.4" + implementation "lol.pyr:director-adventure:2.0.6" implementation project(":api") } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 5bfe5f5..aed6624 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -6,9 +6,12 @@ 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; +import lol.pyr.director.adventure.parse.primitive.IntegerParser; import lol.pyr.znpcsplus.api.ZApiProvider; import lol.pyr.znpcsplus.commands.*; import lol.pyr.znpcsplus.commands.hologram.*; +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; @@ -185,7 +188,12 @@ 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(Integer.class, new IntegerParser(context -> {})); + manager.registerCommand("npc", new MultiCommand() .addSubcommand("action", new ActionCommand()) .addSubcommand("conversations", new ConversationsCommand()) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ActionCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ActionCommand.java index 0475f9a..0ea3686 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ActionCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ActionCommand.java @@ -4,17 +4,18 @@ import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.adventure.command.CommandHandler; import lol.pyr.director.common.command.CommandExecutionException; +import java.util.Collections; import java.util.List; public class ActionCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - commandContext.getSender().sendMessage("Not implemented yet."); + public void run(CommandContext context) throws CommandExecutionException { + context.send("Not implemented yet."); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - return CommandHandler.super.suggest(context); + return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ConversationsCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ConversationsCommand.java index bedb154..d809b3b 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ConversationsCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ConversationsCommand.java @@ -4,16 +4,17 @@ import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.adventure.command.CommandHandler; import lol.pyr.director.common.command.CommandExecutionException; +import java.util.Collections; import java.util.List; public class ConversationsCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - commandContext.getSender().sendMessage("Not implemented yet."); + public void run(CommandContext context) throws CommandExecutionException { + context.send("Not implemented yet."); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - return CommandHandler.super.suggest(context); + return Collections.emptyList(); } } 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 860b3a5..eaff078 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java @@ -3,13 +3,12 @@ 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.ZNpcsPlus; 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.FetchingDescriptor; +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; @@ -21,39 +20,23 @@ import java.util.List; public class CreateCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - if (!(context.getSender() instanceof Player)) { - ZNpcsPlus.ADVENTURE.sender(context.getSender()).sendMessage(Component.text("Only players can use this command.", NamedTextColor.RED)); - return; - } - Player player = (Player) context.getSender(); - if (context.argSize() < 3) { - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("Usage: /npc create ", NamedTextColor.RED)); - return; - } - String npcId = context.popString(); - if (npcId.toLowerCase().startsWith(ZNpcsPlus.DEBUG_NPC_PREFIX)) { - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("You can not create an Npc with that ID", NamedTextColor.RED)); - return; - } - NpcEntryImpl npcEntry = NpcRegistryImpl.get().get(npcId); - if (npcEntry != null) { - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("NPC with that ID already exists.", NamedTextColor.RED)); - return; - } - NpcTypeImpl npcType = NpcTypeImpl.byName(context.popString().toUpperCase()); - if (npcType == null) { - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("Invalid NPC type.", NamedTextColor.RED)); - return; - } - String npcName = context.popString(); - NpcEntryImpl npcEntry1 = NpcRegistryImpl.get().create(npcId, player.getWorld(), npcType, new ZLocation(player.getLocation())); - npcEntry1.enableEverything(); - NpcImpl npc = npcEntry1.getNpc(); - if (npcType == NpcTypeImpl.PLAYER) { - npc.setProperty(EntityPropertyImpl.SKIN, new FetchingDescriptor(npcName)); - } - npc.getHologram().addLine(Component.text(npcName)); - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("Created NPC with ID " + npcId + " and name " + npcName + ".", NamedTextColor.GREEN)); + 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)); } @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 d750128..51a7b36 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/DeleteCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/DeleteCommand.java @@ -15,18 +15,10 @@ import java.util.List; public class DeleteCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - if (context.argSize() != 1) { - ZNpcsPlus.ADVENTURE.sender(context.getSender()).sendMessage(Component.text("Usage: /npc delete ", NamedTextColor.RED)); - return; - } - String id = context.popString(); - NpcEntryImpl npcEntry = NpcRegistryImpl.get().get(id); - if (npcEntry == null) { - ZNpcsPlus.ADVENTURE.sender(context.getSender()).sendMessage(Component.text("NPC not found!", NamedTextColor.RED)); - return; - } - NpcRegistryImpl.get().delete(id); - ZNpcsPlus.ADVENTURE.sender(context.getSender()).sendMessage(Component.text("Deleted NPC with ID: " + id, NamedTextColor.GREEN)); + 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)); } @Override 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 121a62b..f8536d3 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ListCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ListCommand.java @@ -3,8 +3,7 @@ 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.ZNpcsPlus; -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.TextComponent; @@ -12,34 +11,35 @@ import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Location; +import java.util.Collections; import java.util.List; public class ListCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { + public void run(CommandContext context) throws CommandExecutionException { TextComponent.Builder component = Component.text("Npc's:\n").color(NamedTextColor.GOLD).toBuilder(); - for (String id : lol.pyr.znpcsplus.npc.NpcRegistryImpl.get().ids().stream().filter(s -> !s.startsWith(ZNpcsPlus.DEBUG_NPC_PREFIX)).toArray(String[]::new)) { - NpcEntryImpl npcEntry = NpcRegistryImpl.get().get(id); - Location location = npcEntry.getNpc().getLocation().toBukkitLocation(npcEntry.getNpc().getWorld()); + for (String id : NpcRegistryImpl.get().modifiableIds()) { + NpcImpl npc = NpcRegistryImpl.get().get(id).getNpc(); + Location location = npc.getLocation().toBukkitLocation(npc.getWorld()); component.append(Component.text("ID: " + id, NamedTextColor.GREEN)) .append(Component.text(" | ", NamedTextColor.GRAY)) .append(Component.text("Type: ", NamedTextColor.GREEN)) - .append(Component.text(npcEntry.getNpc().getType().getName(), NamedTextColor.GREEN)) + .append(Component.text(npc.getType().getName(), NamedTextColor.GREEN)) .append(Component.text(" | ", NamedTextColor.GRAY)) .append(Component.text("Name: ", NamedTextColor.GREEN)) - .append(npcEntry.getNpc().getHologram().getLine(0).color(NamedTextColor.GREEN)) + .append(npc.getHologram().getLine(0).color(NamedTextColor.GREEN)) .append(Component.text(" | ", NamedTextColor.GRAY)) - .append(Component.text("Location: " + npcEntry.getNpc().getWorld().getName() + " X:" + location.getBlockX() + " Y:" + location.getBlockY() + " Z:" + location.getBlockZ(), NamedTextColor.GREEN)) + .append(Component.text("Location: " + npc.getWorld().getName() + " X:" + location.getBlockX() + " Y:" + location.getBlockY() + " Z:" + location.getBlockZ(), NamedTextColor.GREEN)) .append(Component.text(" | ", NamedTextColor.GRAY)) .append(Component.text("[TELEPORT]", NamedTextColor.DARK_GREEN).clickEvent(ClickEvent.runCommand("/npc teleport " + id))) .append(Component.text("\n", NamedTextColor.GRAY)); } - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(component.build()); + context.send(component.build()); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - return CommandHandler.super.suggest(context); + return Collections.emptyList(); } } 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 e0b982c..192cdb3 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/MoveCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/MoveCommand.java @@ -3,44 +3,31 @@ 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.ZNpcsPlus; import lol.pyr.znpcsplus.npc.NpcEntryImpl; +import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import lol.pyr.znpcsplus.util.ZLocation; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.entity.Player; +import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; public class MoveCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - if (!(commandContext.getSender() instanceof Player)) { - commandContext.getSender().sendMessage("Only players can use this command!"); - return; - } - Player player = (Player) commandContext.getSender(); - if (commandContext.argSize() == 0) { - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("Usage: /npc ", NamedTextColor.RED)); - return; - } - NpcEntryImpl npcEntry = NpcRegistryImpl.get().get(commandContext.popString()); - if (npcEntry == null) { - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("NPC not found!", NamedTextColor.RED)); - return; - } - npcEntry.getNpc().setLocation(new ZLocation(player.getLocation())); - npcEntry.getNpc().respawn(); - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("NPC moved!", NamedTextColor.GREEN)); + public void run(CommandContext context) throws CommandExecutionException { + context.setUsage(context.getLabel() + " move "); + Player player = context.ensureSenderIsPlayer(); + NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); + npc.setLocation(new ZLocation(player.getLocation())); + npc.respawn(); + context.send(Component.text("NPC moved to your current location.", NamedTextColor.GREEN)); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - if (context.argSize() == 1) { - return context.suggestCollection(NpcRegistryImpl.get().ids().stream().filter(s -> !s.startsWith(ZNpcsPlus.DEBUG_NPC_PREFIX)).collect(Collectors.toList())); - } - return CommandHandler.super.suggest(context); + if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); + return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/PathCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/PathCommand.java index 68a45ad..55d1b79 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/PathCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/PathCommand.java @@ -4,16 +4,17 @@ import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.adventure.command.CommandHandler; import lol.pyr.director.common.command.CommandExecutionException; +import java.util.Collections; import java.util.List; public class PathCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - commandContext.getSender().sendMessage("Not implemented yet."); + public void run(CommandContext context) throws CommandExecutionException { + context.send("Not implemented yet."); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - return CommandHandler.super.suggest(context); + return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertiesCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertiesCommand.java index 124ccc9..b9d27b1 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertiesCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertiesCommand.java @@ -4,16 +4,17 @@ import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.adventure.command.CommandHandler; import lol.pyr.director.common.command.CommandExecutionException; +import java.util.Collections; import java.util.List; public class PropertiesCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - commandContext.getSender().sendMessage("Not implemented yet!"); + public void run(CommandContext context) throws CommandExecutionException { + context.send("Not implemented yet!"); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - return CommandHandler.super.suggest(context); + return Collections.emptyList(); } } 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 f68082b..82ec5a1 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java @@ -3,42 +3,29 @@ 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.ZNpcsPlus; 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 org.bukkit.entity.Player; +import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; public class TeleportCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - if (!(commandContext.getSender() instanceof Player)) { - commandContext.getSender().sendMessage("Only players can use this command!"); - return; - } - Player player = (Player) commandContext.getSender(); - if (commandContext.argSize() == 0) { - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("Usage: /npc teleport ", NamedTextColor.RED)); - return; - } - NpcEntryImpl npcEntry = NpcRegistryImpl.get().get(commandContext.popString()); - if (npcEntry == null) { - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("NPC not found!", NamedTextColor.RED)); - return; - } - player.teleport(npcEntry.getNpc().getLocation().toBukkitLocation(npcEntry.getNpc().getWorld())); - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("Teleported to NPC!", NamedTextColor.GREEN)); + public void run(CommandContext context) throws CommandExecutionException { + context.setUsage(context.getLabel() + " teleport "); + Player player = context.ensureSenderIsPlayer(); + NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); + player.teleport(npc.getLocation().toBukkitLocation(npc.getWorld())); + context.send(Component.text("Teleported to NPC!", NamedTextColor.GREEN)); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - if (context.argSize() == 1) { - return context.suggestCollection(NpcRegistryImpl.get().ids().stream().filter(s -> !s.startsWith(ZNpcsPlus.DEBUG_NPC_PREFIX)).collect(Collectors.toList())); - } - return CommandHandler.super.suggest(context); + if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); + return Collections.emptyList(); } } 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 500ebb3..d1f1535 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 @@ -3,41 +3,27 @@ package lol.pyr.znpcsplus.commands.hologram; 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.ZNpcsPlus; +import lol.pyr.znpcsplus.hologram.HologramImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl; 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; -import java.util.stream.Collectors; -import java.util.stream.Stream; public class HoloAddCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - if (commandContext.argSize() < 2) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("Usage: /npc holo add ", NamedTextColor.RED)); - return; - } - String id = commandContext.popString(); - NpcEntryImpl npcEntry = NpcRegistryImpl.get().get(id); - if (npcEntry == null) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("NPC not found!", NamedTextColor.RED)); - return; - } - npcEntry.getNpc().getHologram().addLine(Component.text(commandContext.dumpAllArgs())); - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("NPC line added!", NamedTextColor.GREEN)); + public void run(CommandContext context) throws CommandExecutionException { + context.setUsage(context.getLabel() + " holo add "); + HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram(); + hologram.addLine(Component.text(context.dumpAllArgs())); + context.send(Component.text("NPC line added!", NamedTextColor.GREEN)); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - if (context.argSize() == 1) { - return context.suggestCollection(NpcRegistryImpl.get().ids().stream().filter(s -> !s.startsWith(ZNpcsPlus.DEBUG_NPC_PREFIX)).collect(Collectors.toList())); - } - if (context.argSize() == 2) { - return context.suggestStream(Stream.of("")); - } - return CommandHandler.super.suggest(context); + if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); + return Collections.emptyList(); } } 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 bc5d553..de5a95e 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 @@ -3,53 +3,33 @@ package lol.pyr.znpcsplus.commands.hologram; 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.ZNpcsPlus; +import lol.pyr.znpcsplus.hologram.HologramImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl; 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; -import java.util.stream.Collectors; import java.util.stream.Stream; public class HoloDeleteCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - if (commandContext.argSize() < 2) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("Usage: /npc holo delete ", NamedTextColor.RED)); - return; - } - String id = commandContext.popString(); - NpcEntryImpl npcEntry = NpcRegistryImpl.get().get(id); - if (npcEntry == null) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("NPC not found!", NamedTextColor.RED)); - return; - } - int line; - try { - line = Integer.parseInt(commandContext.popString()); - } catch (NumberFormatException e) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("Invalid line number!", NamedTextColor.RED)); - return; - } - if (line < 0 || line >= npcEntry.getNpc().getHologram().getLines().size()) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("Invalid line number!", NamedTextColor.RED)); - return; - } - npcEntry.getNpc().getHologram().removeLine(line); - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("NPC line removed!", NamedTextColor.GREEN)); + public void run(CommandContext context) throws CommandExecutionException { + 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)); + hologram.removeLine(line); + context.send(Component.text("NPC line removed.", NamedTextColor.GREEN)); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - if (context.argSize() == 1) { - return context.suggestCollection(NpcRegistryImpl.get().ids().stream().filter(s -> !s.startsWith(ZNpcsPlus.DEBUG_NPC_PREFIX)).collect(Collectors.toList())); - } - if (context.argSize() == 2) { - int lines = NpcRegistryImpl.get().get(context.popString()).getNpc().getHologram().getLines().size(); - return context.suggestStream(Stream.iterate(0, n -> n + 1).limit(lines).map(String::valueOf)); - } - return CommandHandler.super.suggest(context); + if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); + if (context.argSize() == 2) return context.suggestStream(Stream.iterate(0, n -> n + 1) + .limit(context.suggestionParse(1, NpcEntryImpl.class).getNpc().getHologram().getLines().size()) + .map(String::valueOf)); + return Collections.emptyList(); } } 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 5f43b04..9364759 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 @@ -3,51 +3,30 @@ package lol.pyr.znpcsplus.commands.hologram; 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.ZNpcsPlus; +import lol.pyr.znpcsplus.hologram.HologramImpl; import lol.pyr.znpcsplus.hologram.HologramLine; import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.format.NamedTextColor; -import org.bukkit.entity.Player; +import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; public class HoloInfoCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - if (!(commandContext.getSender() instanceof Player)) { - commandContext.getSender().sendMessage("Only players can use this command!"); - return; - } - Player player = (Player) commandContext.getSender(); - if (commandContext.argSize() < 1) { - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("Usage: /npc holo info ", NamedTextColor.RED)); - return; - } - String id = commandContext.popString(); - NpcEntryImpl npcEntry = NpcRegistryImpl.get().get(id); - if (npcEntry == null) { - ZNpcsPlus.ADVENTURE.player(player).sendMessage(Component.text("NPC not found!", NamedTextColor.RED)); - return; - } - List lines = npcEntry.getNpc().getHologram().getLines(); - TextComponent component = Component.text("NPC Hologram Info of ID " + npcEntry.getId() + ":", NamedTextColor.GREEN); - component = component.append(Component.newline()); - for (HologramLine line : lines) { - component = component.append(line.getText()); - component = component.append(Component.newline()); - } - ZNpcsPlus.ADVENTURE.player(player).sendMessage(component); + public void run(CommandContext context) throws CommandExecutionException { + 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(); + for (HologramLine line : hologram.getLines()) component = component.append(line.getText()).appendNewline(); + context.send(component); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - if (context.argSize() == 1) { - return context.suggestCollection(NpcRegistryImpl.get().ids().stream().filter(s -> !s.startsWith(ZNpcsPlus.DEBUG_NPC_PREFIX)).collect(Collectors.toList())); - } - return CommandHandler.super.suggest(context); + if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); + return Collections.emptyList(); } } 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 64da8f4..2c3134d 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 @@ -3,55 +3,33 @@ package lol.pyr.znpcsplus.commands.hologram; 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.ZNpcsPlus; +import lol.pyr.znpcsplus.hologram.HologramImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl; 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; -import java.util.stream.Collectors; import java.util.stream.Stream; public class HoloInsertCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - if (commandContext.argSize() < 3) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("Usage: /npc holo insert ", NamedTextColor.RED)); - return; - } - String id = commandContext.popString(); - NpcEntryImpl npcEntry = NpcRegistryImpl.get().get(id); - if (npcEntry == null) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("NPC not found!", NamedTextColor.RED)); - return; - } - int line; - try { - line = Integer.parseInt(commandContext.popString()); - } catch (NumberFormatException e) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("Invalid line number!", NamedTextColor.RED)); - return; - } - if (line < 0 || line >= npcEntry.getNpc().getHologram().getLines().size()) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("Invalid line number!", NamedTextColor.RED)); - } - npcEntry.getNpc().getHologram().insertLine(line, Component.text(commandContext.dumpAllArgs())); - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("NPC line inserted!", NamedTextColor.GREEN)); + public void run(CommandContext context) throws CommandExecutionException { + 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)); + hologram.insertLine(line, Component.text(context.dumpAllArgs())); + context.send(Component.text("NPC line inserted!", NamedTextColor.GREEN)); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - if (context.argSize() == 1) { - return context.suggestCollection(NpcRegistryImpl.get().ids().stream().filter(s -> !s.startsWith(ZNpcsPlus.DEBUG_NPC_PREFIX)).collect(Collectors.toList())); - } - if (context.argSize() == 2) { - int lines = NpcRegistryImpl.get().get(context.popString()).getNpc().getHologram().getLines().size(); - return context.suggestStream(Stream.iterate(0, n -> n + 1).limit(lines).map(String::valueOf)); - } - if (context.argSize() == 3) { - return context.suggestStream(Stream.of("")); - } - return CommandHandler.super.suggest(context); + if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); + if (context.argSize() == 2) return context.suggestStream(Stream.iterate(0, n -> n + 1) + .limit(context.suggestionParse(1, NpcEntryImpl.class).getNpc().getHologram().getLines().size()) + .map(String::valueOf)); + return Collections.emptyList(); } } 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 6b1b429..645a5ea 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 @@ -3,56 +3,34 @@ package lol.pyr.znpcsplus.commands.hologram; 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.ZNpcsPlus; +import lol.pyr.znpcsplus.hologram.HologramImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl; 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; -import java.util.stream.Collectors; import java.util.stream.Stream; public class HoloSetCommand implements CommandHandler { @Override - public void run(CommandContext commandContext) throws CommandExecutionException { - if (commandContext.argSize() < 2) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("Usage: /npc holo set ", NamedTextColor.RED)); - return; - } - String id = commandContext.popString(); - NpcEntryImpl npcEntry = NpcRegistryImpl.get().get(id); - if (npcEntry == null) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("NPC not found!", NamedTextColor.RED)); - return; - } - int line; - try { - line = Integer.parseInt(commandContext.popString()); - } catch (NumberFormatException e) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("Invalid line number!", NamedTextColor.RED)); - return; - } - if (line < 0 || line >= npcEntry.getNpc().getHologram().getLines().size()) { - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("Invalid line number!", NamedTextColor.RED)); - } - npcEntry.getNpc().getHologram().removeLine(line); - npcEntry.getNpc().getHologram().insertLine(line, Component.text(commandContext.dumpAllArgs())); - ZNpcsPlus.ADVENTURE.sender(commandContext.getSender()).sendMessage(Component.text("NPC line set!", NamedTextColor.GREEN)); + public void run(CommandContext context) throws CommandExecutionException { + 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)); + hologram.removeLine(line); + hologram.insertLine(line, Component.text(context.dumpAllArgs())); + context.send(Component.text("NPC line set!", NamedTextColor.GREEN)); } @Override public List suggest(CommandContext context) throws CommandExecutionException { - if (context.argSize() == 1) { - return context.suggestCollection(NpcRegistryImpl.get().ids().stream().filter(s -> !s.startsWith(ZNpcsPlus.DEBUG_NPC_PREFIX)).collect(Collectors.toList())); - } - if (context.argSize() == 2) { - int lines = NpcRegistryImpl.get().get(context.popString()).getNpc().getHologram().getLines().size(); - return context.suggestStream(Stream.iterate(0, n -> n + 1).limit(lines).map(String::valueOf)); - } - if (context.argSize() == 3) { - return context.suggestStream(Stream.of("")); - } - return CommandHandler.super.suggest(context); + if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); + if (context.argSize() == 2) return context.suggestStream(Stream.iterate(0, n -> n + 1) + .limit(context.suggestionParse(1, NpcEntryImpl.class).getNpc().getHologram().getLines().size()) + .map(String::valueOf)); + return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NpcEntryParser.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NpcEntryParser.java new file mode 100644 index 0000000..3562ec2 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NpcEntryParser.java @@ -0,0 +1,23 @@ +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 lol.pyr.znpcsplus.npc.NpcEntryImpl; +import lol.pyr.znpcsplus.npc.NpcRegistryImpl; + +import java.util.Deque; + +public class NpcEntryParser extends ParserType { + public NpcEntryParser(Message message) { + super(message); + } + + @Override + public NpcEntryImpl parse(Deque deque) throws CommandExecutionException { + NpcEntryImpl entry = NpcRegistryImpl.get().get(deque.pop()); + if (entry == null || !entry.isAllowCommandModification()) throw new CommandExecutionException(); + return entry; + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NpcTypeParser.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NpcTypeParser.java new file mode 100644 index 0000000..4317d9e --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/NpcTypeParser.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 lol.pyr.znpcsplus.npc.NpcTypeImpl; + +import java.util.Deque; + +public class NpcTypeParser extends ParserType { + public NpcTypeParser(Message message) { + super(message); + } + + @Override + public NpcTypeImpl parse(Deque deque) throws CommandExecutionException { + NpcTypeImpl type = NpcTypeImpl.byName(deque.pop()); + if (type == null) throw new CommandExecutionException(); + return type; + } +}