From d71df75036784210087397a83ba2f0f407e518e1 Mon Sep 17 00:00:00 2001 From: filoghost Date: Sat, 6 Feb 2021 15:53:43 +0100 Subject: [PATCH] Refactor commands using FCommons --- .../HolographicDisplays.java | 15 +- .../commands/CommandValidator.java | 81 ---------- .../commands/HologramCommandManager.java | 143 ++++++++++++++++++ .../commands/HologramCommandValidate.java | 51 +++++++ .../commands/HologramSubCommand.java | 93 ++++++++++++ .../commands/main/HologramSubCommand.java | 76 ---------- .../main/HologramsCommandHandler.java | 124 --------------- .../commands/main/subs/AddlineCommand.java | 68 --------- .../commands/main/subs/AlignCommand.java | 79 ---------- .../commands/main/subs/CopyCommand.java | 68 --------- .../commands/main/subs/DeleteCommand.java | 62 -------- .../commands/main/subs/InfoCommand.java | 64 -------- .../commands/main/subs/InsertlineCommand.java | 86 ----------- .../commands/main/subs/RemovelineCommand.java | 72 --------- .../commands/main/subs/SetlineCommand.java | 76 ---------- .../commands/main/subs/TeleportCommand.java | 62 -------- .../commands/subs/AddlineCommand.java | 46 ++++++ .../commands/subs/AlignCommand.java | 60 ++++++++ .../commands/subs/CopyCommand.java | 46 ++++++ .../{main => }/subs/CreateCommand.java | 64 +++----- .../{main => }/subs/DebugCommand.java | 34 +---- .../commands/subs/DeleteCommand.java | 39 +++++ .../commands/{main => }/subs/EditCommand.java | 92 +++++------ .../commands/{main => }/subs/HelpCommand.java | 43 ++---- .../commands/subs/InfoCommand.java | 41 +++++ .../commands/subs/InsertlineCommand.java | 64 ++++++++ .../commands/subs/LineEditingCommand.java | 17 +++ .../commands/{main => }/subs/ListCommand.java | 46 ++---- .../{main => }/subs/MovehereCommand.java | 45 ++---- .../commands/{main => }/subs/NearCommand.java | 47 ++---- .../{main => }/subs/ReadimageCommand.java | 78 ++++------ .../{main => }/subs/ReadtextCommand.java | 65 ++++---- .../{main => }/subs/ReloadCommand.java | 34 +---- .../commands/subs/RemovelineCommand.java | 50 ++++++ .../commands/subs/SetlineCommand.java | 54 +++++++ .../commands/subs/TeleportCommand.java | 42 +++++ .../exception/CommandException.java | 13 -- 37 files changed, 931 insertions(+), 1309 deletions(-) delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/CommandValidator.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandManager.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramSubCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/HologramSubCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/HologramsCommandHandler.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/AddlineCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/AlignCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/CopyCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/DeleteCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/InfoCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/InsertlineCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/RemovelineCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/SetlineCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/TeleportCommand.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AlignCommand.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CopyCommand.java rename plugin/src/main/java/me/filoghost/holographicdisplays/commands/{main => }/subs/CreateCommand.java (60%) rename plugin/src/main/java/me/filoghost/holographicdisplays/commands/{main => }/subs/DebugCommand.java (82%) create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DeleteCommand.java rename plugin/src/main/java/me/filoghost/holographicdisplays/commands/{main => }/subs/EditCommand.java (54%) rename plugin/src/main/java/me/filoghost/holographicdisplays/commands/{main => }/subs/HelpCommand.java (73%) create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InfoCommand.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/LineEditingCommand.java rename plugin/src/main/java/me/filoghost/holographicdisplays/commands/{main => }/subs/ListCommand.java (63%) rename plugin/src/main/java/me/filoghost/holographicdisplays/commands/{main => }/subs/MovehereCommand.java (51%) rename plugin/src/main/java/me/filoghost/holographicdisplays/commands/{main => }/subs/NearCommand.java (56%) rename plugin/src/main/java/me/filoghost/holographicdisplays/commands/{main => }/subs/ReadimageCommand.java (77%) rename plugin/src/main/java/me/filoghost/holographicdisplays/commands/{main => }/subs/ReadtextCommand.java (62%) rename plugin/src/main/java/me/filoghost/holographicdisplays/commands/{main => }/subs/ReloadCommand.java (83%) create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/RemovelineCommand.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/TeleportCommand.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/exception/CommandException.java diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/HolographicDisplays.java b/plugin/src/main/java/me/filoghost/holographicdisplays/HolographicDisplays.java index 65466cc3..2b6f54a1 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/HolographicDisplays.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/HolographicDisplays.java @@ -12,7 +12,7 @@ import me.filoghost.holographicdisplays.api.internal.BackendAPI; import me.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; import me.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook; import me.filoghost.holographicdisplays.bridge.protocollib.current.ProtocolLibHookImpl; -import me.filoghost.holographicdisplays.commands.main.HologramsCommandHandler; +import me.filoghost.holographicdisplays.commands.HologramCommandManager; import me.filoghost.holographicdisplays.disk.Configuration; import me.filoghost.holographicdisplays.disk.HologramDatabase; import me.filoghost.holographicdisplays.disk.UnicodeSymbols; @@ -51,7 +51,7 @@ public class HolographicDisplays extends BaseJavaPlugin { private static MainListener mainListener; // The command handler, just in case a plugin wants to register more commands. - private static HologramsCommandHandler commandHandler; + private static HologramCommandManager commandManager; // The new version found by the updater, null if there is no new version. private static String newVersion; @@ -132,8 +132,11 @@ public class HolographicDisplays extends BaseJavaPlugin { "This can be caused by edits to plugin.yml or other plugins."); } - getCommand("holograms").setExecutor(commandHandler = new HologramsCommandHandler()); - Bukkit.getPluginManager().registerEvents(mainListener = new MainListener(nmsManager), this); + commandManager = new HologramCommandManager(); + commandManager.register(this); + + mainListener = new MainListener(nmsManager); + Bukkit.getPluginManager().registerEvents(mainListener, this); // Register bStats metrics int pluginID = 3123; @@ -165,8 +168,8 @@ public class HolographicDisplays extends BaseJavaPlugin { return mainListener; } - public static HologramsCommandHandler getCommandHandler() { - return commandHandler; + public static HologramCommandManager getCommandManager() { + return commandManager; } public static HolographicDisplays getInstance() { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/CommandValidator.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/CommandValidator.java deleted file mode 100644 index c853649a..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/CommandValidator.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands; - -import me.filoghost.holographicdisplays.HolographicDisplays; -import me.filoghost.holographicdisplays.disk.HologramLineParser; -import me.filoghost.holographicdisplays.exception.CommandException; -import me.filoghost.holographicdisplays.exception.HologramLineParseException; -import me.filoghost.holographicdisplays.object.NamedHologram; -import me.filoghost.holographicdisplays.object.NamedHologramManager; -import me.filoghost.holographicdisplays.object.line.CraftHologramLine; -import me.filoghost.holographicdisplays.util.FileUtils; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -public class CommandValidator { - - public static CraftHologramLine parseHologramLine(NamedHologram hologram, String serializedLine, boolean validateMaterial) throws CommandException { - try { - return HologramLineParser.parseLine(hologram, serializedLine, validateMaterial); - } catch (HologramLineParseException e) { - throw new CommandException(e.getMessage()); - } - } - - public static NamedHologram getNamedHologram(String hologramName) throws CommandException { - NamedHologram hologram = NamedHologramManager.getHologram(hologramName); - notNull(hologram, "Cannot find a hologram named \"" + hologramName + "\"."); - return hologram; - } - - public static void notNull(Object obj, String string) throws CommandException { - if (obj == null) { - throw new CommandException(string); - } - } - - public static void isTrue(boolean b, String string) throws CommandException { - if (!b) { - throw new CommandException(string); - } - } - - public static int getInteger(String integer) throws CommandException { - try { - return Integer.parseInt(integer); - } catch (NumberFormatException ex) { - throw new CommandException("Invalid number: '" + integer + "'."); - } - } - - public static Player getPlayerSender(CommandSender sender) throws CommandException { - if (sender instanceof Player) { - return (Player) sender; - } else { - throw new CommandException("You must be a player to use this command."); - } - } - - public static Path getUserReadableFile(String fileName) throws CommandException, IOException { - Path dataFolder = HolographicDisplays.getDataFolderPath(); - Path targetFile = dataFolder.resolve(fileName); - CommandValidator.isTrue(FileUtils.isInsideDirectory(targetFile, dataFolder), "The specified file must be inside HolographicDisplays' folder."); - CommandValidator.isTrue(Files.exists(targetFile), "The specified file \"" + fileName + "\" does not exist inside HolographicDisplays' folder."); - CommandValidator.isTrue(!Files.isDirectory(targetFile), "The file cannot be a folder."); - CommandValidator.isTrue(!isConfigFile(targetFile), "Cannot read YML configuration files."); - return targetFile; - } - - private static boolean isConfigFile(Path file) { - return Files.isRegularFile(file) && file.getFileName().toString().toLowerCase().endsWith(".yml"); - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandManager.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandManager.java new file mode 100644 index 00000000..42b969aa --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandManager.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands; + +import me.filoghost.fcommons.command.CommandContext; +import me.filoghost.fcommons.command.sub.SubCommand; +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.sub.SubCommandManager; +import me.filoghost.holographicdisplays.Colors; +import me.filoghost.holographicdisplays.HolographicDisplays; +import me.filoghost.holographicdisplays.commands.subs.AddlineCommand; +import me.filoghost.holographicdisplays.commands.subs.AlignCommand; +import me.filoghost.holographicdisplays.commands.subs.CopyCommand; +import me.filoghost.holographicdisplays.commands.subs.CreateCommand; +import me.filoghost.holographicdisplays.commands.subs.DebugCommand; +import me.filoghost.holographicdisplays.commands.subs.DeleteCommand; +import me.filoghost.holographicdisplays.commands.subs.EditCommand; +import me.filoghost.holographicdisplays.commands.subs.HelpCommand; +import me.filoghost.holographicdisplays.commands.subs.InfoCommand; +import me.filoghost.holographicdisplays.commands.subs.InsertlineCommand; +import me.filoghost.holographicdisplays.commands.subs.ListCommand; +import me.filoghost.holographicdisplays.commands.subs.MovehereCommand; +import me.filoghost.holographicdisplays.commands.subs.NearCommand; +import me.filoghost.holographicdisplays.commands.subs.ReadimageCommand; +import me.filoghost.holographicdisplays.commands.subs.ReadtextCommand; +import me.filoghost.holographicdisplays.commands.subs.ReloadCommand; +import me.filoghost.holographicdisplays.commands.subs.RemovelineCommand; +import me.filoghost.holographicdisplays.commands.subs.SetlineCommand; +import me.filoghost.holographicdisplays.commands.subs.TeleportCommand; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; + +public class HologramCommandManager extends SubCommandManager { + + private final List subCommands; + private final Map, HologramSubCommand> subCommandsByClass; + + private final HelpCommand helpCommand; + + public HologramCommandManager() { + setName("holograms"); + subCommands = new ArrayList<>(); + subCommandsByClass = new HashMap<>(); + + registerSubCommand(new AddlineCommand()); + registerSubCommand(new CreateCommand()); + registerSubCommand(new DeleteCommand()); + registerSubCommand(new EditCommand()); + registerSubCommand(new ListCommand()); + registerSubCommand(new NearCommand()); + registerSubCommand(new TeleportCommand()); + registerSubCommand(new MovehereCommand()); + registerSubCommand(new AlignCommand()); + registerSubCommand(new CopyCommand()); + registerSubCommand(new ReloadCommand()); + + registerSubCommand(new RemovelineCommand()); + registerSubCommand(new SetlineCommand()); + registerSubCommand(new InsertlineCommand()); + registerSubCommand(new ReadtextCommand()); + registerSubCommand(new ReadimageCommand()); + registerSubCommand(new InfoCommand()); + + registerSubCommand(new DebugCommand()); + registerSubCommand(helpCommand = new HelpCommand()); + } + + @Override + protected SubCommand getSubCommandByName(String name) { + for (HologramSubCommand subCommand : subCommands) { + if (subCommand.getName().equalsIgnoreCase(name)) { + return subCommand; + } + + if (subCommand.getAliases() != null) { + for (String alias : subCommand.getAliases()) { + if (alias.equalsIgnoreCase(name)) { + return subCommand; + } + } + } + } + + return null; + } + + @Override + public Iterable getSubCommands() { + return subCommands; + } + + public void registerSubCommand(HologramSubCommand subCommand) { + subCommands.add(subCommand); + subCommandsByClass.put(subCommand.getClass(), subCommand); + } + + public HologramSubCommand getSubCommand(Class subCommandClass) { + return subCommandsByClass.get(subCommandClass); + } + + @Override + protected void sendNoArgsMessage(CommandContext context) { + CommandSender sender = context.getSender(); + sender.sendMessage(Colors.PRIMARY_SHADOW + "Server is running " + Colors.PRIMARY + "Holographic Displays " + Colors.PRIMARY_SHADOW + "v" + HolographicDisplays.getInstance().getDescription().getVersion() + " by " + Colors.PRIMARY + "filoghost"); + if (helpCommand.hasPermission(sender)) { + sender.sendMessage(Colors.PRIMARY_SHADOW + "Commands: " + Colors.PRIMARY + helpCommand.getFullUsageText(context)); + } + } + + @Override + protected void sendSubCommandDefaultPermissionMessage(SubCommandContext context) { + context.getSender().sendMessage(Colors.ERROR + "You don't have permission for this sub-command."); + } + + @Override + protected void sendUnknownSubCommandMessage(SubCommandContext context) { + context.getSender().sendMessage(Colors.ERROR + "Unknown sub-command." + + " Type \"" + helpCommand.getFullUsageText(context) + "\" for a list of commands."); + } + + @Override + protected void sendSubCommandUsage(SubCommandContext context) { + context.getSender().sendMessage(Colors.ERROR + "Usage: /" + context.getRootLabel() + " " + + context.getSubLabel() + " " + context.getSubCommand().getUsageArgs()); + } + + @Override + protected void handleUnexpectedException(CommandContext context, Throwable t) { + Bukkit.getLogger().log(Level.SEVERE, "Unhandled exception while executing /" + context.getRootLabel(), t); + context.getSender().sendMessage(Colors.ERROR + "Internal error while executing command." + + " Please look on the console for more details."); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java new file mode 100644 index 00000000..ce655c65 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands; + +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; +import me.filoghost.holographicdisplays.HolographicDisplays; +import me.filoghost.holographicdisplays.disk.HologramLineParser; +import me.filoghost.holographicdisplays.exception.HologramLineParseException; +import me.filoghost.holographicdisplays.object.NamedHologram; +import me.filoghost.holographicdisplays.object.NamedHologramManager; +import me.filoghost.holographicdisplays.object.line.CraftHologramLine; +import me.filoghost.holographicdisplays.util.FileUtils; + +import java.nio.file.Files; +import java.nio.file.Path; + +public class HologramCommandValidate { + + public static CraftHologramLine parseHologramLine(NamedHologram hologram, String serializedLine, boolean validateMaterial) throws CommandException { + try { + return HologramLineParser.parseLine(hologram, serializedLine, validateMaterial); + } catch (HologramLineParseException e) { + throw new CommandException(e.getMessage()); + } + } + + public static NamedHologram getNamedHologram(String hologramName) throws CommandException { + NamedHologram hologram = NamedHologramManager.getHologram(hologramName); + CommandValidate.notNull(hologram, "Cannot find a hologram named \"" + hologramName + "\"."); + return hologram; + } + + public static Path getUserReadableFile(String fileName) throws CommandException { + Path dataFolder = HolographicDisplays.getDataFolderPath(); + Path targetFile = dataFolder.resolve(fileName); + CommandValidate.check(FileUtils.isInsideDirectory(targetFile, dataFolder), "The specified file must be inside HolographicDisplays' folder."); + CommandValidate.check(Files.exists(targetFile), "The specified file \"" + fileName + "\" does not exist inside HolographicDisplays' folder."); + CommandValidate.check(!Files.isDirectory(targetFile), "The file cannot be a folder."); + CommandValidate.check(!isConfigFile(targetFile), "Cannot read YML configuration files."); + return targetFile; + } + + private static boolean isConfigFile(Path file) { + return Files.isRegularFile(file) && file.getFileName().toString().toLowerCase().endsWith(".yml"); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramSubCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramSubCommand.java new file mode 100644 index 00000000..5405ec98 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramSubCommand.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands; + +import me.filoghost.fcommons.command.CommandContext; +import me.filoghost.fcommons.command.sub.SubCommand; +import me.filoghost.holographicdisplays.Permissions; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public abstract class HologramSubCommand implements SubCommand { + + private final String name; + private final List aliases; + private final String permission; + + private String usageArgs; + private int minArgs; + + private List description; + private boolean showInHelpCommand; + + protected HologramSubCommand(String name, String... aliases) { + this.name = name; + this.aliases = aliases != null ? Arrays.asList(aliases) : Collections.emptyList(); + this.permission = Permissions.COMMAND_BASE + name.toLowerCase(); + + this.showInHelpCommand = true; + } + + public final String getFullUsageText(CommandContext context) { + return "/" + context.getRootLabel() + " " + name + (usageArgs != null ? " " + usageArgs : ""); + } + + @Override + public final String getName() { + return name; + } + + @Override + public final String getPermission() { + return permission; + } + + @Override + public final String getPermissionMessage() { + return null; + } + + @Override + public final String getUsageArgs() { + return usageArgs; + } + + public final void setUsageArgs(String usageArgs) { + this.usageArgs = usageArgs; + } + + @Override + public final int getMinArgs() { + return minArgs; + } + + public final void setMinArgs(int minArgs) { + this.minArgs = minArgs; + } + + public final List getAliases() { + return aliases; + } + + public final void setDescription(String... description) { + this.description = Arrays.asList(description); + } + + public List getDescription(CommandContext context) { + return description; + } + + public final void setShowInHelpCommand(boolean show) { + this.showInHelpCommand = show; + } + + public final boolean isShowInHelpCommand() { + return showInHelpCommand; + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/HologramSubCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/HologramSubCommand.java deleted file mode 100644 index 25467136..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/HologramSubCommand.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main; - -import me.filoghost.holographicdisplays.exception.CommandException; -import org.bukkit.command.CommandSender; - -import java.util.List; - -public abstract class HologramSubCommand { - - private String name; - private String permission; - private String[] aliases; - - public HologramSubCommand(String name) { - this(name, new String[0]); - } - - public HologramSubCommand(String name, String... aliases) { - this.name = name; - this.aliases = aliases; - } - - public String getName() { - return name; - } - - public void setPermission(String permission) { - this.permission = permission; - } - - public String getPermission() { - return permission; - } - - public final boolean hasPermission(CommandSender sender) { - if (permission == null) return true; - return sender.hasPermission(permission); - } - - public abstract String getPossibleArguments(); - - public abstract int getMinimumArguments(); - - public abstract void execute(CommandSender sender, String label, String[] args) throws CommandException; - - public abstract List getTutorial(); - - public abstract SubCommandType getType(); - - public enum SubCommandType { - GENERIC, EDIT_LINES, HIDDEN - } - - - public final boolean isValidTrigger(String name) { - if (this.name.equalsIgnoreCase(name)) { - return true; - } - - if (aliases != null) { - for (String alias : aliases) { - if (alias.equalsIgnoreCase(name)) { - return true; - } - } - } - - return false; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/HologramsCommandHandler.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/HologramsCommandHandler.java deleted file mode 100644 index d0d6a2e9..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/HologramsCommandHandler.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main; - -import me.filoghost.holographicdisplays.HolographicDisplays; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.main.subs.AddlineCommand; -import me.filoghost.holographicdisplays.commands.main.subs.AlignCommand; -import me.filoghost.holographicdisplays.commands.main.subs.CopyCommand; -import me.filoghost.holographicdisplays.commands.main.subs.CreateCommand; -import me.filoghost.holographicdisplays.commands.main.subs.DebugCommand; -import me.filoghost.holographicdisplays.commands.main.subs.DeleteCommand; -import me.filoghost.holographicdisplays.commands.main.subs.EditCommand; -import me.filoghost.holographicdisplays.commands.main.subs.HelpCommand; -import me.filoghost.holographicdisplays.commands.main.subs.InfoCommand; -import me.filoghost.holographicdisplays.commands.main.subs.InsertlineCommand; -import me.filoghost.holographicdisplays.commands.main.subs.ListCommand; -import me.filoghost.holographicdisplays.commands.main.subs.MovehereCommand; -import me.filoghost.holographicdisplays.commands.main.subs.NearCommand; -import me.filoghost.holographicdisplays.commands.main.subs.ReadimageCommand; -import me.filoghost.holographicdisplays.commands.main.subs.ReadtextCommand; -import me.filoghost.holographicdisplays.commands.main.subs.ReloadCommand; -import me.filoghost.holographicdisplays.commands.main.subs.RemovelineCommand; -import me.filoghost.holographicdisplays.commands.main.subs.SetlineCommand; -import me.filoghost.holographicdisplays.commands.main.subs.TeleportCommand; -import me.filoghost.holographicdisplays.exception.CommandException; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class HologramsCommandHandler implements CommandExecutor { - - private List subCommands; - private Map, HologramSubCommand> subCommandsByClass; - - public HologramsCommandHandler() { - subCommands = new ArrayList<>(); - subCommandsByClass = new HashMap<>(); - - registerSubCommand(new AddlineCommand()); - registerSubCommand(new CreateCommand()); - registerSubCommand(new DeleteCommand()); - registerSubCommand(new EditCommand()); - registerSubCommand(new ListCommand()); - registerSubCommand(new NearCommand()); - registerSubCommand(new TeleportCommand()); - registerSubCommand(new MovehereCommand()); - registerSubCommand(new AlignCommand()); - registerSubCommand(new CopyCommand()); - registerSubCommand(new ReloadCommand()); - - registerSubCommand(new RemovelineCommand()); - registerSubCommand(new SetlineCommand()); - registerSubCommand(new InsertlineCommand()); - registerSubCommand(new ReadtextCommand()); - registerSubCommand(new ReadimageCommand()); - registerSubCommand(new InfoCommand()); - - registerSubCommand(new DebugCommand()); - registerSubCommand(new HelpCommand()); - } - - public void registerSubCommand(HologramSubCommand subCommand) { - subCommands.add(subCommand); - subCommandsByClass.put(subCommand.getClass(), subCommand); - } - - public List getSubCommands() { - return new ArrayList<>(subCommands); - } - - public HologramSubCommand getSubCommand(Class subCommandClass) { - return subCommandsByClass.get(subCommandClass); - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (args.length == 0) { - sender.sendMessage(Colors.PRIMARY_SHADOW + "Server is running " + Colors.PRIMARY + "Holographic Displays " + Colors.PRIMARY_SHADOW + "v" + HolographicDisplays.getInstance().getDescription().getVersion() + " by " + Colors.PRIMARY + "filoghost"); - if (sender.hasPermission(Permissions.COMMAND_BASE + "help")) { - sender.sendMessage(Colors.PRIMARY_SHADOW + "Commands: " + Colors.PRIMARY + "/" + label + " help"); - } - return true; - } - - for (HologramSubCommand subCommand : subCommands) { - if (subCommand.isValidTrigger(args[0])) { - - if (!subCommand.hasPermission(sender)) { - sender.sendMessage(Colors.ERROR + "You don't have permission."); - return true; - } - - if (args.length - 1 >= subCommand.getMinimumArguments()) { - try { - subCommand.execute(sender, label, Arrays.copyOfRange(args, 1, args.length)); - } catch (CommandException e) { - sender.sendMessage(Colors.ERROR + e.getMessage()); - } catch (Exception e) { - e.printStackTrace(); - sender.sendMessage(Colors.ERROR + "Unhandled exception while executing command. Please look on the console for more details."); - } - } else { - sender.sendMessage(Colors.ERROR + "Usage: /" + label + " " + subCommand.getName() + " " + subCommand.getPossibleArguments()); - } - - return true; - } - } - - sender.sendMessage(Colors.ERROR + "Unknown sub-command. Type \"/" + label + " help\" for a list of commands."); - return true; - } -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/AddlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/AddlineCommand.java deleted file mode 100644 index a0db2885..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/AddlineCommand.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main.subs; - -import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.common.Utils; -import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import me.filoghost.holographicdisplays.exception.CommandException; -import me.filoghost.holographicdisplays.object.NamedHologram; -import me.filoghost.holographicdisplays.object.line.CraftHologramLine; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.List; - -public class AddlineCommand extends HologramSubCommand { - - public AddlineCommand() { - super("addline"); - setPermission(Permissions.COMMAND_BASE + "addline"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 2; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); - String serializedLine = Utils.join(args, " ", 1, args.length); - - CraftHologramLine line = CommandValidator.parseHologramLine(hologram, serializedLine, true); - hologram.getLinesUnsafe().add(line); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); - - sender.sendMessage(Colors.PRIMARY + "Line added!"); - EditCommand.sendQuickEditCommands(sender, label, hologram.getName()); - } - - @Override - public List getTutorial() { - return Arrays.asList("Adds a line to an existing hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/AlignCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/AlignCommand.java deleted file mode 100644 index b5d6708b..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/AlignCommand.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main.subs; - -import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.exception.CommandException; -import me.filoghost.holographicdisplays.object.NamedHologram; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.List; - -public class AlignCommand extends HologramSubCommand { - - public AlignCommand() { - super("align"); - setPermission(Permissions.COMMAND_BASE + "align"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 3; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = CommandValidator.getNamedHologram(args[1]); - NamedHologram referenceHologram = CommandValidator.getNamedHologram(args[2]); - - CommandValidator.isTrue(hologram != referenceHologram, "The hologram must not be the same!"); - - Location loc = hologram.getLocation(); - - if (args[0].equalsIgnoreCase("x")) { - loc.setX(referenceHologram.getX()); - } else if (args[0].equalsIgnoreCase("y")) { - loc.setY(referenceHologram.getY()); - } else if (args[0].equalsIgnoreCase("z")) { - loc.setZ(referenceHologram.getZ()); - } else if (args[0].equalsIgnoreCase("xz")) { - loc.setX(referenceHologram.getX()); - loc.setZ(referenceHologram.getZ()); - } else { - throw new CommandException("You must specify either X, Y, Z or XZ, " + args[0] + " is not a valid axis."); - } - - hologram.teleport(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ()); - hologram.despawnEntities(); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - sender.sendMessage(Colors.PRIMARY + "Hologram \"" + hologram.getName() + "\" aligned to the hologram \"" + referenceHologram.getName() + "\" on the " + args[0].toUpperCase() + " axis."); - } - - @Override - public List getTutorial() { - return Arrays.asList("Aligns the first hologram to the second, in the specified axis."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/CopyCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/CopyCommand.java deleted file mode 100644 index b7d236f1..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/CopyCommand.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main.subs; - -import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.exception.CommandException; -import me.filoghost.holographicdisplays.object.NamedHologram; -import me.filoghost.holographicdisplays.object.line.CraftHologramLine; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.List; - -public class CopyCommand extends HologramSubCommand { - - public CopyCommand() { - super("copy"); - setPermission(Permissions.COMMAND_BASE + "copy"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 2; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram fromHologram = CommandValidator.getNamedHologram(args[0]); - NamedHologram toHologram = CommandValidator.getNamedHologram(args[1]); - - toHologram.clearLines(); - for (CraftHologramLine line : fromHologram.getLinesUnsafe()) { - CraftHologramLine clonedLine = CommandValidator.parseHologramLine(toHologram, HologramDatabase.serializeHologramLine(line), false); - toHologram.getLinesUnsafe().add(clonedLine); - } - - toHologram.refreshAll(); - - HologramDatabase.saveHologram(toHologram); - HologramDatabase.trySaveToDisk(); - - sender.sendMessage(Colors.PRIMARY + "Hologram \"" + fromHologram.getName() + "\" copied into hologram \"" + toHologram.getName() + "\"!"); - } - - @Override - public List getTutorial() { - return Arrays.asList( - "Copies the contents of a hologram into another one."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/DeleteCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/DeleteCommand.java deleted file mode 100644 index a8c7dca3..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/DeleteCommand.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main.subs; - -import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.exception.CommandException; -import me.filoghost.holographicdisplays.object.NamedHologram; -import me.filoghost.holographicdisplays.object.NamedHologramManager; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.List; - -public class DeleteCommand extends HologramSubCommand { - - public DeleteCommand() { - super("delete", "remove"); - setPermission(Permissions.COMMAND_BASE + "delete"); - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); - - hologram.delete(); - NamedHologramManager.removeHologram(hologram); - HologramDatabase.deleteHologram(hologram.getName()); - - HologramDatabase.trySaveToDisk(); - sender.sendMessage(Colors.PRIMARY + "You deleted the hologram '" + hologram.getName() + "'."); - } - - @Override - public List getTutorial() { - return Arrays.asList("Deletes a hologram. Cannot be undone."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/InfoCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/InfoCommand.java deleted file mode 100644 index 8295924a..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/InfoCommand.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main.subs; - -import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.exception.CommandException; -import me.filoghost.holographicdisplays.object.NamedHologram; -import me.filoghost.holographicdisplays.object.line.CraftHologramLine; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.List; - -public class InfoCommand extends HologramSubCommand { - - public InfoCommand() { - super("info", "details"); - setPermission(Permissions.COMMAND_BASE + "info"); - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); - - sender.sendMessage(""); - Messages.sendTitle(sender, "Lines of the hologram '" + hologram.getName() + "'"); - int index = 0; - - for (CraftHologramLine line : hologram.getLinesUnsafe()) { - sender.sendMessage(Colors.SECONDARY + Colors.BOLD + (++index) + Colors.SECONDARY_SHADOW + ". " + Colors.SECONDARY + HologramDatabase.serializeHologramLine(line)); - } - EditCommand.sendQuickEditCommands(sender, label, hologram.getName()); - } - - @Override - public List getTutorial() { - return Arrays.asList("Shows the lines of a hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/InsertlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/InsertlineCommand.java deleted file mode 100644 index e8824235..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/InsertlineCommand.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main.subs; - -import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import me.filoghost.holographicdisplays.exception.CommandException; -import me.filoghost.holographicdisplays.object.NamedHologram; -import me.filoghost.holographicdisplays.object.line.CraftHologramLine; -import me.filoghost.holographicdisplays.common.Utils; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.List; - -public class InsertlineCommand extends HologramSubCommand { - - - public InsertlineCommand() { - super("insertline"); - setPermission(Permissions.COMMAND_BASE + "insertline"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 3; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); - int insertAfter = CommandValidator.getInteger(args[1]); - String serializedLine = Utils.join(args, " ", 2, args.length); - - int oldLinesAmount = hologram.size(); - - CommandValidator.isTrue(insertAfter >= 0 && insertAfter <= oldLinesAmount, "The number must be between 0 and " + hologram.size() + "(amount of lines of the hologram)."); - - CraftHologramLine line = CommandValidator.parseHologramLine(hologram, serializedLine, true); - hologram.getLinesUnsafe().add(insertAfter, line); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - - Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); - - if (insertAfter == 0) { - sender.sendMessage(Colors.PRIMARY + "Line inserted before line n.1!"); - } else if (insertAfter == oldLinesAmount) { - sender.sendMessage(Colors.PRIMARY + "Line appended at the end!"); - Messages.sendTip(sender, "You can use \"/" + label + " addline\" to add a line at the end."); - } else { - sender.sendMessage(Colors.PRIMARY + "Line inserted between lines " + insertAfter + " and " + (insertAfter + 1) + "!"); - } - EditCommand.sendQuickEditCommands(sender, label, hologram.getName()); - } - - @Override - public List getTutorial() { - return Arrays.asList("Inserts a line after the specified index.", - "If the index is 0, the line will be put before", - "the first line of the hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/RemovelineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/RemovelineCommand.java deleted file mode 100644 index 5554e0d7..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/RemovelineCommand.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main.subs; - -import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import me.filoghost.holographicdisplays.exception.CommandException; -import me.filoghost.holographicdisplays.object.NamedHologram; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.List; - -public class RemovelineCommand extends HologramSubCommand { - - public RemovelineCommand() { - super("removeline"); - setPermission(Permissions.COMMAND_BASE + "removeline"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 2; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); - - int lineNumber = CommandValidator.getInteger(args[1]); - - CommandValidator.isTrue(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); - int index = lineNumber - 1; - - CommandValidator.isTrue(hologram.size() > 1, "The hologram should have at least 1 line. If you want to delete it, use /" + label + " delete."); - - hologram.removeLine(index); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); - - sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " removed!"); - EditCommand.sendQuickEditCommands(sender, label, hologram.getName()); - } - - @Override - public List getTutorial() { - return Arrays.asList("Removes a line from a hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/SetlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/SetlineCommand.java deleted file mode 100644 index f9adabaa..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/SetlineCommand.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main.subs; - -import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import me.filoghost.holographicdisplays.exception.CommandException; -import me.filoghost.holographicdisplays.object.NamedHologram; -import me.filoghost.holographicdisplays.object.line.CraftHologramLine; -import me.filoghost.holographicdisplays.common.Utils; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.List; - -public class SetlineCommand extends HologramSubCommand { - - public SetlineCommand() { - super("setline"); - setPermission(Permissions.COMMAND_BASE + "setline"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 3; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); - String serializedLine = Utils.join(args, " ", 2, args.length); - - int lineNumber = CommandValidator.getInteger(args[1]); - CommandValidator.isTrue(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); - int index = lineNumber - 1; - - CraftHologramLine line = CommandValidator.parseHologramLine(hologram, serializedLine, true); - - hologram.getLinesUnsafe().get(index).despawn(); - hologram.getLinesUnsafe().set(index, line); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); - - sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " changed!"); - EditCommand.sendQuickEditCommands(sender, label, hologram.getName()); - - } - - @Override - public List getTutorial() { - return Arrays.asList("Changes a line of a hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/TeleportCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/TeleportCommand.java deleted file mode 100644 index 7b78ea28..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/TeleportCommand.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.commands.main.subs; - -import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.exception.CommandException; -import me.filoghost.holographicdisplays.object.NamedHologram; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; - -import java.util.Arrays; -import java.util.List; - - -public class TeleportCommand extends HologramSubCommand { - - public TeleportCommand() { - super("teleport", "tp"); - setPermission(Permissions.COMMAND_BASE + "teleport"); - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - Player player = CommandValidator.getPlayerSender(sender); - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); - - Location loc = hologram.getLocation(); - loc.setPitch(90); - player.teleport(loc, TeleportCause.PLUGIN); - player.sendMessage(Colors.PRIMARY + "You were teleported to the hologram named '" + hologram.getName() + "'."); - - } - - @Override - public List getTutorial() { - return Arrays.asList("Teleports you to the given hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java new file mode 100644 index 00000000..95db5a58 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands.subs; + +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.holographicdisplays.Colors; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.common.Utils; +import me.filoghost.holographicdisplays.disk.HologramDatabase; +import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; +import me.filoghost.holographicdisplays.object.NamedHologram; +import me.filoghost.holographicdisplays.object.line.CraftHologramLine; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +public class AddlineCommand extends LineEditingCommand { + + public AddlineCommand() { + super("addline"); + setMinArgs(2); + setUsageArgs(" "); + setDescription("Adds a line to an existing hologram."); + } + + @Override + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); + String serializedLine = Utils.join(args, " ", 1, args.length); + + CraftHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine, true); + hologram.getLinesUnsafe().add(line); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); + + sender.sendMessage(Colors.PRIMARY + "Line added!"); + EditCommand.sendQuickEditCommands(context, hologram); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AlignCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AlignCommand.java new file mode 100644 index 00000000..55ed1623 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AlignCommand.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands.subs; + +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; +import me.filoghost.holographicdisplays.Colors; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; +import me.filoghost.holographicdisplays.disk.HologramDatabase; +import me.filoghost.holographicdisplays.object.NamedHologram; +import org.bukkit.Location; +import org.bukkit.command.CommandSender; + +public class AlignCommand extends HologramSubCommand { + + public AlignCommand() { + super("align"); + setMinArgs(3); + setUsageArgs(" "); + setDescription("Aligns the first hologram to the second, in the specified axis."); + } + + @Override + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[1]); + NamedHologram referenceHologram = HologramCommandValidate.getNamedHologram(args[2]); + + CommandValidate.check(hologram != referenceHologram, "The hologram must not be the same!"); + + Location loc = hologram.getLocation(); + + String axis = args[0]; + if (axis.equalsIgnoreCase("x")) { + loc.setX(referenceHologram.getX()); + } else if (axis.equalsIgnoreCase("y")) { + loc.setY(referenceHologram.getY()); + } else if (axis.equalsIgnoreCase("z")) { + loc.setZ(referenceHologram.getZ()); + } else if (axis.equalsIgnoreCase("xz")) { + loc.setX(referenceHologram.getX()); + loc.setZ(referenceHologram.getZ()); + } else { + throw new CommandException("You must specify either X, Y, Z or XZ, " + axis + " is not a valid axis."); + } + + hologram.teleport(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ()); + hologram.despawnEntities(); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + sender.sendMessage(Colors.PRIMARY + "Hologram \"" + hologram.getName() + "\" aligned to the hologram \"" + referenceHologram.getName() + "\" on the " + axis.toUpperCase() + " axis."); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CopyCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CopyCommand.java new file mode 100644 index 00000000..1bebcf03 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CopyCommand.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands.subs; + +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.holographicdisplays.Colors; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; +import me.filoghost.holographicdisplays.disk.HologramDatabase; +import me.filoghost.holographicdisplays.object.NamedHologram; +import me.filoghost.holographicdisplays.object.line.CraftHologramLine; +import org.bukkit.command.CommandSender; + +public class CopyCommand extends HologramSubCommand { + + public CopyCommand() { + super("copy"); + setMinArgs(2); + setUsageArgs(" "); + setDescription("Copies the contents of a hologram into another one."); + } + + @Override + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + NamedHologram fromHologram = HologramCommandValidate.getNamedHologram(args[0]); + NamedHologram toHologram = HologramCommandValidate.getNamedHologram(args[1]); + + toHologram.clearLines(); + for (CraftHologramLine line : fromHologram.getLinesUnsafe()) { + CraftHologramLine clonedLine = HologramCommandValidate.parseHologramLine(toHologram, HologramDatabase.serializeHologramLine(line), false); + toHologram.getLinesUnsafe().add(clonedLine); + } + + toHologram.refreshAll(); + + HologramDatabase.saveHologram(toHologram); + HologramDatabase.trySaveToDisk(); + + sender.sendMessage(Colors.PRIMARY + "Hologram \"" + fromHologram.getName() + "\" copied into hologram \"" + toHologram.getName() + "\"!"); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/CreateCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java similarity index 60% rename from plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/CreateCommand.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java index 58e3c952..b6bef42e 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/CreateCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java @@ -3,54 +3,47 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.commands.main.subs; +package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; +import me.filoghost.holographicdisplays.common.Utils; import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.exception.CommandException; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.NamedHologramManager; import me.filoghost.holographicdisplays.object.line.CraftHologramLine; -import me.filoghost.holographicdisplays.common.Utils; import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import java.util.Arrays; -import java.util.List; - public class CreateCommand extends HologramSubCommand { public CreateCommand() { super("create"); - setPermission(Permissions.COMMAND_BASE + "create"); - } - - @Override - public String getPossibleArguments() { - return " [text]"; - } - - @Override - public int getMinimumArguments() { - return 1; + setMinArgs(1); + setUsageArgs(" [text]"); + setDescription( + "Creates a new hologram with the given name, that must", + "be alphanumeric. The name will be used as reference to", + "that hologram for editing commands."); } @SuppressWarnings("deprecation") @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - Player player = CommandValidator.getPlayerSender(sender); + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + Player player = CommandValidate.getPlayerSender(sender); String hologramName = args[0]; if (!hologramName.matches("[a-zA-Z0-9_\\-]+")) { throw new CommandException("The name must contain only alphanumeric chars, underscores and hyphens."); } - CommandValidator.isTrue(!NamedHologramManager.isExistingHologram(hologramName), "A hologram with that name already exists."); + CommandValidate.check(!NamedHologramManager.isExistingHologram(hologramName), "A hologram with that name already exists."); Location spawnLoc = player.getLocation(); boolean moveUp = player.isOnGround(); @@ -63,13 +56,13 @@ public class CreateCommand extends HologramSubCommand { if (args.length > 1) { String text = Utils.join(args, " ", 1, args.length); - CommandValidator.isTrue(!text.equalsIgnoreCase("{empty}"), "The first line should not be empty."); + CommandValidate.check(!text.equalsIgnoreCase("{empty}"), "The first line should not be empty."); - CraftHologramLine line = CommandValidator.parseHologramLine(hologram, text, true); + CraftHologramLine line = HologramCommandValidate.parseHologramLine(hologram, text, true); hologram.getLinesUnsafe().add(line); - player.sendMessage(Colors.SECONDARY_SHADOW + "(Change the lines with /" + label + " edit " + hologram.getName() + ")"); + player.sendMessage(Colors.SECONDARY_SHADOW + "(Change the lines with /" + context.getRootLabel() + " edit " + hologram.getName() + ")"); } else { - hologram.appendTextLine("Default hologram. Change it with " + Colors.PRIMARY + "/" + label + " edit " + hologram.getName()); + hologram.appendTextLine("Default hologram. Change it with " + Colors.PRIMARY + "/" + context.getRootLabel() + " edit " + hologram.getName()); } NamedHologramManager.addHologram(hologram); @@ -83,21 +76,8 @@ public class CreateCommand extends HologramSubCommand { player.sendMessage(Colors.PRIMARY + "You created a hologram named '" + hologram.getName() + "'."); if (moveUp) { - player.sendMessage(Colors.SECONDARY_SHADOW + "(You were on the ground, the hologram was automatically moved up. If you use /" + label + " movehere " + hologram.getName() + ", the hologram will be moved to your feet)"); + player.sendMessage(Colors.SECONDARY_SHADOW + "(You were on the ground, the hologram was automatically moved up. If you use /" + context.getRootLabel() + " movehere " + hologram.getName() + ", the hologram will be moved to your feet)"); } } - - @Override - public List getTutorial() { - return Arrays.asList( - "Creates a new hologram with the given name, that must", - "be alphanumeric. The name will be used as reference to", - "that hologram for editing commands."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/DebugCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DebugCommand.java similarity index 82% rename from plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/DebugCommand.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DebugCommand.java index ae421792..82da548c 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/DebugCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DebugCommand.java @@ -3,13 +3,13 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.commands.main.subs; +package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.command.sub.SubCommandContext; import me.filoghost.holographicdisplays.Colors; import me.filoghost.holographicdisplays.HolographicDisplays; -import me.filoghost.holographicdisplays.Permissions; import me.filoghost.holographicdisplays.api.Hologram; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; import me.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.PluginHologram; @@ -19,9 +19,7 @@ import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; -import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -29,21 +27,12 @@ public class DebugCommand extends HologramSubCommand { public DebugCommand() { super("debug"); - setPermission(Permissions.COMMAND_BASE + "debug"); + setShowInHelpCommand(false); + setDescription("Displays information useful for debugging."); } @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 0; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) { + public void execute(CommandSender sender, String[] args, SubCommandContext context) { boolean foundAnyHologram = false; for (World world : Bukkit.getWorlds()) { @@ -97,17 +86,6 @@ public class DebugCommand extends HologramSubCommand { return hologram.toString(); } } - - @Override - public List getTutorial() { - return Arrays.asList("Displays information useful for debugging."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.HIDDEN; - } - private static class HologramDebugInfo { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DeleteCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DeleteCommand.java new file mode 100644 index 00000000..98fd430d --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DeleteCommand.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands.subs; + +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.holographicdisplays.Colors; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; +import me.filoghost.holographicdisplays.disk.HologramDatabase; +import me.filoghost.holographicdisplays.object.NamedHologram; +import me.filoghost.holographicdisplays.object.NamedHologramManager; +import org.bukkit.command.CommandSender; + +public class DeleteCommand extends HologramSubCommand { + + public DeleteCommand() { + super("delete", "remove"); + setMinArgs(1); + setUsageArgs(""); + setDescription("Deletes a hologram. Cannot be undone."); + } + + @Override + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); + + hologram.delete(); + NamedHologramManager.removeHologram(hologram); + HologramDatabase.deleteHologram(hologram.getName()); + + HologramDatabase.trySaveToDisk(); + sender.sendMessage(Colors.PRIMARY + "You deleted the hologram '" + hologram.getName() + "'."); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/EditCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/EditCommand.java similarity index 54% rename from plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/EditCommand.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/EditCommand.java index 031db9b7..1af26ccc 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/EditCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/EditCommand.java @@ -3,17 +3,17 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.commands.main.subs; +package me.filoghost.holographicdisplays.commands.subs; import com.google.common.collect.Lists; -import me.filoghost.holographicdisplays.HolographicDisplays; +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; +import me.filoghost.holographicdisplays.HolographicDisplays; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; import me.filoghost.holographicdisplays.disk.Configuration; -import me.filoghost.holographicdisplays.exception.CommandException; import me.filoghost.holographicdisplays.object.NamedHologram; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.ClickEvent; @@ -25,7 +25,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class EditCommand extends HologramSubCommand { @@ -40,35 +39,25 @@ public class EditCommand extends HologramSubCommand { public EditCommand() { super("edit"); - setPermission(Permissions.COMMAND_BASE + "edit"); + setMinArgs(1); + setUsageArgs(""); + setDescription("Shows the commands to manipulate an existing hologram."); } @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); sender.sendMessage(""); Messages.sendTitle(sender, "How to edit the hologram '" + hologram.getName() + "'"); - for (HologramSubCommand subCommand : HolographicDisplays.getCommandHandler().getSubCommands()) { - if (subCommand.getType() == SubCommandType.EDIT_LINES) { - String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments().replace("", hologram.getName()).replace("", hologram.getName()) : ""); + for (HologramSubCommand subCommand : HolographicDisplays.getCommandManager().getSubCommands()) { + if (subCommand instanceof LineEditingCommand) { + String usage = subCommand.getFullUsageText(context).replace("", hologram.getName()); if (sender instanceof Player) { - List help = new ArrayList<>(); help.add(Colors.PRIMARY + usage); - for (String tutLine : subCommand.getTutorial()) { + for (String tutLine : subCommand.getDescription(context)) { help.add(Colors.SECONDARY_SHADOW + tutLine); } @@ -88,50 +77,39 @@ public class EditCommand extends HologramSubCommand { HelpCommand.sendHoverTip((Player) sender); } } - - public static void sendQuickEditCommands(CommandSender sender, String label, String hologramName) { + + public static void sendQuickEditCommands(SubCommandContext commandContext, NamedHologram hologram) { if (!Configuration.quickEditCommands) { return; } - if (!(sender instanceof Player)) { + if (!(commandContext.getSender() instanceof Player)) { return; } ComponentBuilder message = new ComponentBuilder("EDIT LINES:").color(ChatColor.GRAY).bold(true).append(" ", FormatRetention.NONE); for (QuickCommandInfo quickEditCommand : QUICK_EDIT_COMMANDS) { - HologramSubCommand subCommand = HolographicDisplays.getCommandHandler().getSubCommand(quickEditCommand.commandClass); + HologramSubCommand subCommand = HolographicDisplays.getCommandManager().getSubCommand(quickEditCommand.commandClass); - // Assume first argument is always "" and remove it - String arguments = subCommand.getPossibleArguments(); - if (arguments.contains(" ")) { - arguments = arguments.substring(arguments.indexOf(" ") + 1); - } else { - arguments = ""; - } - - String usage = "/" + label + " " + subCommand.getName() + " " + hologramName + " "; - message.append("[" + quickEditCommand.chatName + "]").color(ChatColor.DARK_AQUA) - .event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, usage)) - .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText( - ChatColor.GRAY + "Click to insert in chat the highlighted part of the command:\n" + - ChatColor.YELLOW + usage + ChatColor.DARK_GRAY + arguments))); - message.append(" ", FormatRetention.NONE); + // Assume first argument is always "" and remove it + String usageArgs = subCommand.getUsageArgs(); + if (usageArgs != null && usageArgs.contains(" ")) { + usageArgs = usageArgs.substring(usageArgs.indexOf(" ") + 1); + } else { + usageArgs = ""; + } + + String usage = "/" + commandContext.getRootLabel() + " " + subCommand.getName() + " " + hologram + " "; + message.append("[" + quickEditCommand.chatName + "]").color(ChatColor.DARK_AQUA) + .event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, usage)) + .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText( + ChatColor.GRAY + "Click to insert in chat the highlighted part of the command:\n" + + ChatColor.YELLOW + usage + ChatColor.DARK_GRAY + usageArgs))); + message.append(" ", FormatRetention.NONE); } - ((Player) sender).spigot().sendMessage(message.create()); + ((Player) commandContext.getSender()).spigot().sendMessage(message.create()); } - - @Override - public List getTutorial() { - return Arrays.asList("Shows the commands to manipulate an existing hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - private static class QuickCommandInfo { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/HelpCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/HelpCommand.java similarity index 73% rename from plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/HelpCommand.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/HelpCommand.java index 959721c9..37e5ce61 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/HelpCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/HelpCommand.java @@ -3,13 +3,13 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.commands.main.subs; +package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.command.sub.SubCommandContext; import me.filoghost.holographicdisplays.Colors; import me.filoghost.holographicdisplays.HolographicDisplays; -import me.filoghost.holographicdisplays.Permissions; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; @@ -26,33 +26,23 @@ public class HelpCommand extends HologramSubCommand { public HelpCommand() { super("help"); - setPermission(Permissions.COMMAND_BASE + "help"); + setShowInHelpCommand(false); + setDescription("Show the list of commands."); } @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 0; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) { + public void execute(CommandSender sender, String[] args, SubCommandContext context) { sender.sendMessage(""); Messages.sendTitle(sender, "Holographic Displays Commands"); - for (HologramSubCommand subCommand : HolographicDisplays.getCommandHandler().getSubCommands()) { - if (subCommand.getType() == SubCommandType.GENERIC) { - String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments() : ""); + for (HologramSubCommand subCommand : HolographicDisplays.getCommandManager().getSubCommands()) { + if (subCommand.isShowInHelpCommand()) { + String usage = subCommand.getFullUsageText(context); if (sender instanceof Player) { List help = new ArrayList<>(); help.add(Colors.PRIMARY + usage); - for (String tutLine : subCommand.getTutorial()) { + for (String tutLine : subCommand.getDescription(context)) { help.add(Colors.SECONDARY_SHADOW + tutLine); } @@ -72,7 +62,7 @@ public class HelpCommand extends HologramSubCommand { sendHoverTip((Player) sender); } } - + public static void sendHoverTip(Player player) { player.sendMessage(""); player.spigot().sendMessage(new ComponentBuilder("TIP:").color(ChatColor.YELLOW).bold(true) @@ -86,15 +76,4 @@ public class HelpCommand extends HologramSubCommand { .create()); } - @Override - public List getTutorial() { - return null; - } - - @Override - public SubCommandType getType() { - return SubCommandType.HIDDEN; - } - - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InfoCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InfoCommand.java new file mode 100644 index 00000000..e621e16e --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InfoCommand.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands.subs; + +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.holographicdisplays.Colors; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.commands.Messages; +import me.filoghost.holographicdisplays.disk.HologramDatabase; +import me.filoghost.holographicdisplays.object.NamedHologram; +import me.filoghost.holographicdisplays.object.line.CraftHologramLine; +import org.bukkit.command.CommandSender; + +public class InfoCommand extends LineEditingCommand { + + public InfoCommand() { + super("info", "details"); + setMinArgs(1); + setUsageArgs(""); + setDescription("Shows the lines of a hologram."); + } + + @Override + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); + + sender.sendMessage(""); + Messages.sendTitle(sender, "Lines of the hologram '" + hologram.getName() + "'"); + int index = 0; + + for (CraftHologramLine line : hologram.getLinesUnsafe()) { + sender.sendMessage(Colors.SECONDARY + Colors.BOLD + (++index) + Colors.SECONDARY_SHADOW + ". " + Colors.SECONDARY + HologramDatabase.serializeHologramLine(line)); + } + EditCommand.sendQuickEditCommands(context, hologram); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java new file mode 100644 index 00000000..bcdaac9b --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands.subs; + +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; +import me.filoghost.holographicdisplays.Colors; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.commands.Messages; +import me.filoghost.holographicdisplays.common.Utils; +import me.filoghost.holographicdisplays.disk.HologramDatabase; +import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; +import me.filoghost.holographicdisplays.object.NamedHologram; +import me.filoghost.holographicdisplays.object.line.CraftHologramLine; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +public class InsertlineCommand extends LineEditingCommand { + + public InsertlineCommand() { + super("insertline"); + setMinArgs(3); + setUsageArgs(" "); + setDescription( + "Inserts a line after the specified index.", + "If the index is 0, the line will be put before", + "the first line of the hologram."); + } + + @Override + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); + int insertAfter = CommandValidate.parseInteger(args[1]); + String serializedLine = Utils.join(args, " ", 2, args.length); + + int oldLinesAmount = hologram.size(); + + CommandValidate.check(insertAfter >= 0 && insertAfter <= oldLinesAmount, "The number must be between 0 and " + hologram.size() + "(amount of lines of the hologram)."); + + CraftHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine, true); + hologram.getLinesUnsafe().add(insertAfter, line); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + + Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); + + if (insertAfter == 0) { + sender.sendMessage(Colors.PRIMARY + "Line inserted before line n.1!"); + } else if (insertAfter == oldLinesAmount) { + sender.sendMessage(Colors.PRIMARY + "Line appended at the end!"); + Messages.sendTip(sender, "You can use \"/" + context.getRootLabel() + " addline\" to add a line at the end."); + } else { + sender.sendMessage(Colors.PRIMARY + "Line inserted between lines " + insertAfter + " and " + (insertAfter + 1) + "!"); + } + EditCommand.sendQuickEditCommands(context, hologram); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/LineEditingCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/LineEditingCommand.java new file mode 100644 index 00000000..0389be52 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/LineEditingCommand.java @@ -0,0 +1,17 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands.subs; + +import me.filoghost.holographicdisplays.commands.HologramSubCommand; + +public abstract class LineEditingCommand extends HologramSubCommand { + + protected LineEditingCommand(String name, String... aliases) { + super(name, aliases); + setShowInHelpCommand(false); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ListCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ListCommand.java similarity index 63% rename from plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ListCommand.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ListCommand.java index 6159b013..12e4e9f8 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ListCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ListCommand.java @@ -3,44 +3,33 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.commands.main.subs; +package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.exception.CommandException; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.NamedHologramManager; import org.bukkit.command.CommandSender; -import java.util.Arrays; -import java.util.List; - public class ListCommand extends HologramSubCommand { private static final int HOLOGRAMS_PER_PAGE = 10; public ListCommand() { super("list"); - setPermission(Permissions.COMMAND_BASE + "list"); + setMinArgs(0); + setUsageArgs("[page]"); + setDescription("Lists all the existing holograms."); } @Override - public String getPossibleArguments() { - return "[page]"; - } + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { - @Override - public int getMinimumArguments() { - return 0; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - - int page = args.length > 0 ? CommandValidator.getInteger(args[0]) : 1; + int page = args.length > 0 ? CommandValidate.parseInteger(args[0]) : 1; if (page < 1) { throw new CommandException("Page number must be 1 or greater."); @@ -51,9 +40,8 @@ public class ListCommand extends HologramSubCommand { totalPages++; } - if (NamedHologramManager.size() == 0) { - throw new CommandException("There are no holograms yet. Create one with /" + label + " create."); + throw new CommandException("There are no holograms yet. Create one with /" + context.getRootLabel() + " create."); } sender.sendMessage(""); @@ -68,19 +56,9 @@ public class ListCommand extends HologramSubCommand { } } if (page < totalPages) { - Messages.sendTip(sender, "See the next page with /" + label + " list " + (page + 1)); + Messages.sendTip(sender, "See the next page with /" + context.getRootLabel() + " list " + (page + 1)); } } - @Override - public List getTutorial() { - return Arrays.asList("Lists all the existing holograms."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/MovehereCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/MovehereCommand.java similarity index 51% rename from plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/MovehereCommand.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/MovehereCommand.java index 67134437..9a3f5e9d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/MovehereCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/MovehereCommand.java @@ -3,46 +3,35 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.commands.main.subs; +package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.exception.CommandException; import me.filoghost.holographicdisplays.object.NamedHologram; import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import java.util.Arrays; -import java.util.List; - public class MovehereCommand extends HologramSubCommand { public MovehereCommand() { super("movehere"); - setPermission(Permissions.COMMAND_BASE + "movehere"); + setMinArgs(1); + setUsageArgs(""); + setDescription("Moves a hologram to your location."); } @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - Player player = CommandValidator.getPlayerSender(sender); - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + Player player = CommandValidate.getPlayerSender(sender); + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); hologram.teleport(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ()); hologram.despawnEntities(); @@ -55,15 +44,5 @@ public class MovehereCommand extends HologramSubCommand { player.teleport(to, TeleportCause.PLUGIN); player.sendMessage(Colors.PRIMARY + "You moved the hologram '" + hologram.getName() + "' near to you."); } - - @Override - public List getTutorial() { - return Arrays.asList("Moves a hologram to your location."); - } - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/NearCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/NearCommand.java similarity index 56% rename from plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/NearCommand.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/NearCommand.java index 5bbd3af8..267df493 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/NearCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/NearCommand.java @@ -3,14 +3,14 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.commands.main.subs; +package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.commands.CommandValidator; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.exception.CommandException; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.NamedHologramManager; import org.bukkit.World; @@ -18,31 +18,22 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class NearCommand extends HologramSubCommand { public NearCommand() { super("near"); - setPermission(Permissions.COMMAND_BASE + "near"); + setMinArgs(1); + setUsageArgs(""); + setDescription("Get a list of near holograms."); } @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - Player player = CommandValidator.getPlayerSender(sender); - int radius = CommandValidator.getInteger(args[0]); - CommandValidator.isTrue(radius > 0, "Radius must be at least 1."); + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + Player player = CommandValidate.getPlayerSender(sender); + int radius = CommandValidate.parseInteger(args[0]); + CommandValidate.check(radius > 0, "Radius must be at least 1."); World world = player.getWorld(); int radiusSquared = radius * radius; @@ -54,22 +45,12 @@ public class NearCommand extends HologramSubCommand { } } - CommandValidator.isTrue(!nearHolograms.isEmpty(), "There are no holograms in the given radius."); + CommandValidate.check(!nearHolograms.isEmpty(), "There are no holograms in the given radius."); Messages.sendTitle(player, "Near holograms"); for (NamedHologram nearHologram : nearHolograms) { player.sendMessage(Colors.SECONDARY_SHADOW + "- " + Colors.SECONDARY + Colors.BOLD + nearHologram.getName() + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) nearHologram.getX() + ", y: " + (int) nearHologram.getY() + ", z: " + (int) nearHologram.getZ() + " (lines: " + nearHologram.size() + ")"); } } - - @Override - public List getTutorial() { - return Arrays.asList("Get a list of near holograms."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ReadimageCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java similarity index 77% rename from plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ReadimageCommand.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java index 4974945b..47e7abd3 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ReadimageCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java @@ -3,16 +3,17 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.commands.main.subs; +package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.command.CommandContext; +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.commands.CommandValidator; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; import me.filoghost.holographicdisplays.disk.HologramDatabase; import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import me.filoghost.holographicdisplays.exception.CommandException; import me.filoghost.holographicdisplays.exception.TooWideException; import me.filoghost.holographicdisplays.exception.UnreadableImageException; import me.filoghost.holographicdisplays.image.ImageMessage; @@ -32,27 +33,35 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -public class ReadimageCommand extends HologramSubCommand { - - +public class ReadimageCommand extends LineEditingCommand { + public ReadimageCommand() { super("readimage", "image"); - setPermission(Permissions.COMMAND_BASE + "readimage"); + setMinArgs(3); + setUsageArgs(" "); } @Override - public String getPossibleArguments() { - return " "; + public List getDescription(CommandContext context) { + return Arrays.asList( + "Reads an image from a file. Tutorial:", + "1) Move the image in the plugin's folder", + "2) Do not use spaces in the name", + "3) Do " + getFullUsageText(context), + "4) Choose to automatically resize the image", + "5) (Optional) Use the flag '-a' if you only want to append", + " the image to the hologram without clearing the lines", + "", + "Example: you have an image named 'logo.png', you want to append", + "it to the lines of the hologram named 'test', with a width of", + "50 pixels. In this case you would execute the following command:", + ChatColor.YELLOW + "/" + context.getRootLabel() + " " + getName() + " test logo.png 50 -a", + "", + "The symbols used to create the image are taken from the config.yml."); } - + @Override - public int getMinimumArguments() { - return 3; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { boolean append = false; @@ -68,11 +77,11 @@ public class ReadimageCommand extends HologramSubCommand { args = newArgs.toArray(new String[0]); - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); - int width = CommandValidator.getInteger(args[2]); + int width = CommandValidate.parseInteger(args[2]); - CommandValidator.isTrue(width >= 2, "The width of the image must be 2 or greater."); + CommandValidate.check(width >= 2, "The width of the image must be 2 or greater."); boolean isUrl = false; @@ -89,7 +98,7 @@ public class ReadimageCommand extends HologramSubCommand { Messages.sendWarning(sender, "The image path seems to be an URL. If so, please use http:// or https:// in the path."); } - Path targetImage = CommandValidator.getUserReadableFile(fileName); + Path targetImage = HologramCommandValidate.getUserReadableFile(fileName); image = FileUtils.readImage(targetImage); } @@ -132,28 +141,5 @@ public class ReadimageCommand extends HologramSubCommand { throw new CommandException("I/O exception while reading the image. " + (isUrl ? "Is the URL valid?" : "Is it in use?")); } } - - @Override - public List getTutorial() { - return Arrays.asList("Reads an image from a file. Tutorial:", - "1) Move the image in the plugin's folder", - "2) Do not use spaces in the name", - "3) Do /holograms read ", - "4) Choose to automatically resize the image", - "5) (Optional) Use the flag '-a' if you only want to append", - " the image to the hologram without clearing the lines", - "", - "Example: you have an image named 'logo.png', you want to append", - "it to the lines of the hologram named 'test', with a width of", - "50 pixels. In this case you would execute the following command:", - ChatColor.YELLOW + "/holograms readimage test logo.png 50 -a", - "", - "The symbols used to create the image are taken from the config.yml."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ReadtextCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java similarity index 62% rename from plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ReadtextCommand.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java index 029200a6..5139c13d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ReadtextCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java @@ -3,18 +3,18 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.commands.main.subs; +package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.command.CommandContext; +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.holographicdisplays.Colors; -import me.filoghost.holographicdisplays.Permissions; -import me.filoghost.holographicdisplays.commands.CommandValidator; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; import me.filoghost.holographicdisplays.common.Utils; import me.filoghost.holographicdisplays.disk.HologramDatabase; import me.filoghost.holographicdisplays.disk.HologramLineParser; import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import me.filoghost.holographicdisplays.exception.CommandException; import me.filoghost.holographicdisplays.exception.HologramLineParseException; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.line.CraftHologramLine; @@ -29,30 +29,35 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -public class ReadtextCommand extends HologramSubCommand { +public class ReadtextCommand extends LineEditingCommand { public ReadtextCommand() { super("readtext", "readlines"); - setPermission(Permissions.COMMAND_BASE + "readtext"); + setMinArgs(2); + setUsageArgs(" "); } @Override - public String getPossibleArguments() { - return " "; + public List getDescription(CommandContext context) { + return Arrays.asList( + "Reads the lines from a text file. Tutorial:", + "1) Create a new text file in the plugin's folder", + "2) Do not use spaces in the name", + "3) Each line will be a line in the hologram", + "4) Do " + getFullUsageText(context), + "", + "Example: you have a file named 'info.txt', and you want", + "to paste it in the hologram named 'test'. In this case you", + "would execute " + ChatColor.YELLOW + "/" + context.getRootLabel() + " " + getName() + " test info.txt"); } - + @Override - public int getMinimumArguments() { - return 2; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = CommandValidator.getNamedHologram(args[0]); + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); String fileName = args[1]; try { - Path targetFile = CommandValidator.getUserReadableFile(fileName); + Path targetFile = HologramCommandValidate.getUserReadableFile(fileName); List serializedLines = Files.readAllLines(targetFile); int linesAmount = serializedLines.size(); @@ -78,9 +83,9 @@ public class ReadtextCommand extends HologramSubCommand { HologramDatabase.saveHologram(hologram); HologramDatabase.trySaveToDisk(); - if (args[1].contains(".")) { - if (isImageExtension(args[1].substring(args[1].lastIndexOf('.') + 1))) { - Messages.sendWarning(sender, "The read file has an image's extension. If it is an image, you should use /" + label + " readimage."); + if (fileName.contains(".")) { + if (isImageExtension(fileName.substring(fileName.lastIndexOf('.') + 1))) { + Messages.sendWarning(sender, "The read file has an image's extension. If it is an image, you should use /" + context.getRootLabel() + " readimage."); } } @@ -92,24 +97,6 @@ public class ReadtextCommand extends HologramSubCommand { } } - @Override - public List getTutorial() { - return Arrays.asList("Reads the lines from a text file. Tutorial:", - "1) Create a new text file in the plugin's folder", - "2) Do not use spaces in the name", - "3) Each line will be a line in the hologram", - "4) Do /holograms readlines ", - "", - "Example: you have a file named 'info.txt', and you want", - "to paste it in the hologram named 'test'. In this case you", - "would execute "+ ChatColor.YELLOW + "/holograms readlines test info.txt"); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - private boolean isImageExtension(String input) { return Arrays.asList("jpg", "png", "jpeg", "gif").contains(input.toLowerCase()); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ReloadCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReloadCommand.java similarity index 83% rename from plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ReloadCommand.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReloadCommand.java index da0c7f34..ce47eb4f 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/main/subs/ReloadCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReloadCommand.java @@ -3,15 +3,15 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.commands.main.subs; +package me.filoghost.holographicdisplays.commands.subs; +import me.filoghost.fcommons.command.sub.SubCommandContext; import me.filoghost.fcommons.logging.Log; import me.filoghost.holographicdisplays.Colors; import me.filoghost.holographicdisplays.HolographicDisplays; -import me.filoghost.holographicdisplays.Permissions; import me.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.commands.main.HologramSubCommand; import me.filoghost.holographicdisplays.common.Utils; import me.filoghost.holographicdisplays.disk.Configuration; import me.filoghost.holographicdisplays.disk.HologramDatabase; @@ -29,29 +29,17 @@ import me.filoghost.holographicdisplays.placeholder.PlaceholdersManager; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; -import java.util.Arrays; -import java.util.List; import java.util.Set; public class ReloadCommand extends HologramSubCommand { public ReloadCommand() { super("reload"); - setPermission(Permissions.COMMAND_BASE + "reload"); + setDescription("Reloads the holograms from the database."); } - + @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 0; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) { + public void execute(CommandSender sender, String[] args, SubCommandContext context) { long startMillis = System.currentTimeMillis(); UnicodeSymbols.load(HolographicDisplays.getInstance()); @@ -98,15 +86,5 @@ public class ReloadCommand extends HologramSubCommand { Bukkit.getPluginManager().callEvent(new HolographicDisplaysReloadEvent()); } - - @Override - public List getTutorial() { - return Arrays.asList("Reloads the holograms from the database."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/RemovelineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/RemovelineCommand.java new file mode 100644 index 00000000..6f0672f9 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/RemovelineCommand.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands.subs; + +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; +import me.filoghost.holographicdisplays.Colors; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.disk.HologramDatabase; +import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; +import me.filoghost.holographicdisplays.object.NamedHologram; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +public class RemovelineCommand extends LineEditingCommand { + + public RemovelineCommand() { + super("removeline"); + setMinArgs(2); + setUsageArgs(" "); + setDescription("Removes a line from a hologram."); + } + + @Override + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); + + int lineNumber = CommandValidate.parseInteger(args[1]); + + CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); + int index = lineNumber - 1; + + CommandValidate.check(hologram.size() > 1, "The hologram should have at least 1 line. If you want to delete it, use /" + context.getRootLabel() + " delete."); + + hologram.removeLine(index); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); + + sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " removed!"); + EditCommand.sendQuickEditCommands(context, hologram); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java new file mode 100644 index 00000000..b0ab3c2b --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands.subs; + +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; +import me.filoghost.holographicdisplays.Colors; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.common.Utils; +import me.filoghost.holographicdisplays.disk.HologramDatabase; +import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; +import me.filoghost.holographicdisplays.object.NamedHologram; +import me.filoghost.holographicdisplays.object.line.CraftHologramLine; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +public class SetlineCommand extends LineEditingCommand { + + public SetlineCommand() { + super("setline"); + setMinArgs(3); + setUsageArgs(" "); + setDescription("Changes a line of a hologram."); + } + + @Override + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); + String serializedLine = Utils.join(args, " ", 2, args.length); + + int lineNumber = CommandValidate.parseInteger(args[1]); + CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); + int index = lineNumber - 1; + + CraftHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine, true); + + hologram.getLinesUnsafe().get(index).despawn(); + hologram.getLinesUnsafe().set(index, line); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); + + sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " changed!"); + EditCommand.sendQuickEditCommands(context, hologram); + + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/TeleportCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/TeleportCommand.java new file mode 100644 index 00000000..63459933 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/TeleportCommand.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.commands.subs; + +import me.filoghost.fcommons.command.sub.SubCommandContext; +import me.filoghost.fcommons.command.validation.CommandException; +import me.filoghost.fcommons.command.validation.CommandValidate; +import me.filoghost.holographicdisplays.Colors; +import me.filoghost.holographicdisplays.commands.HologramCommandValidate; +import me.filoghost.holographicdisplays.commands.HologramSubCommand; +import me.filoghost.holographicdisplays.object.NamedHologram; +import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; + + +public class TeleportCommand extends HologramSubCommand { + + public TeleportCommand() { + super("teleport", "tp"); + setMinArgs(1); + setUsageArgs(""); + setDescription("Teleports you to the given hologram."); + } + + @Override + public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { + Player player = CommandValidate.getPlayerSender(sender); + NamedHologram hologram = HologramCommandValidate.getNamedHologram(args[0]); + + Location loc = hologram.getLocation(); + loc.setPitch(90); + player.teleport(loc, TeleportCause.PLUGIN); + player.sendMessage(Colors.PRIMARY + "You were teleported to the hologram named '" + hologram.getName() + "'."); + + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/CommandException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/exception/CommandException.java deleted file mode 100644 index d37d3d6a..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/CommandException.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.exception; - -public class CommandException extends Exception { - - public CommandException(String message) { - super(message); - } -}