From 640fa153f94150ba06bd3ae90dfc71b32b74901b Mon Sep 17 00:00:00 2001 From: Siroshun09 Date: Thu, 30 May 2024 22:52:07 +0900 Subject: [PATCH] refactor: reimplement command using brigadier --- pom.xml | 6 - .../ArmorStandEditorPlugin.java | 15 +- .../command/ArmorStandEditorCommand.java | 230 ++++++++++++------ .../command/ConstantsArgumentType.java | 70 ++++++ .../command/subcommand/AxisCommand.java | 95 -------- .../command/subcommand/EquipmentCommand.java | 57 ----- .../command/subcommand/ItemCommand.java | 58 ----- .../command/subcommand/ModeCommand.java | 93 ------- .../command/subcommand/ReloadCommand.java | 48 ---- .../armorstandeditor/lang/Messages.java | 12 + .../permission/Permissions.java | 12 +- src/main/resources/en.yml | 2 + src/main/resources/ja_JP.yml | 2 + src/main/resources/plugin.yml | 4 - 14 files changed, 254 insertions(+), 450 deletions(-) create mode 100644 src/main/java/net/okocraft/armorstandeditor/command/ConstantsArgumentType.java delete mode 100644 src/main/java/net/okocraft/armorstandeditor/command/subcommand/AxisCommand.java delete mode 100644 src/main/java/net/okocraft/armorstandeditor/command/subcommand/EquipmentCommand.java delete mode 100644 src/main/java/net/okocraft/armorstandeditor/command/subcommand/ItemCommand.java delete mode 100644 src/main/java/net/okocraft/armorstandeditor/command/subcommand/ModeCommand.java delete mode 100644 src/main/java/net/okocraft/armorstandeditor/command/subcommand/ReloadCommand.java diff --git a/pom.xml b/pom.xml index fbb8667..cf4d91e 100644 --- a/pom.xml +++ b/pom.xml @@ -62,12 +62,6 @@ 4.6.4 compile - - com.github.siroshun09.mccommand - paper - 3.0.0 - compile - com.github.siroshun09.translationloader translationloader diff --git a/src/main/java/net/okocraft/armorstandeditor/ArmorStandEditorPlugin.java b/src/main/java/net/okocraft/armorstandeditor/ArmorStandEditorPlugin.java index 38898f2..413150a 100644 --- a/src/main/java/net/okocraft/armorstandeditor/ArmorStandEditorPlugin.java +++ b/src/main/java/net/okocraft/armorstandeditor/ArmorStandEditorPlugin.java @@ -3,11 +3,10 @@ import com.github.siroshun09.configapi.api.Configuration; import com.github.siroshun09.configapi.api.util.ResourceUtils; import com.github.siroshun09.configapi.yaml.YamlConfiguration; -import com.github.siroshun09.mccommand.paper.PaperCommandFactory; -import com.github.siroshun09.mccommand.paper.listener.AsyncTabCompleteListener; import com.github.siroshun09.translationloader.ConfigurationLoader; import com.github.siroshun09.translationloader.TranslationLoader; import com.github.siroshun09.translationloader.directory.TranslationDirectory; +import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import net.kyori.adventure.key.Key; import net.okocraft.armorstandeditor.command.ArmorStandEditorCommand; import net.okocraft.armorstandeditor.item.EditToolItem; @@ -25,7 +24,6 @@ import java.io.IOException; import java.nio.file.Path; import java.util.Locale; -import java.util.Optional; import java.util.jar.JarFile; import java.util.logging.Level; @@ -70,15 +68,8 @@ public void onEnable() { manager.registerEvents(new InventoryListener(), this); manager.registerEvents(new PlayerListener(this), this); - var command = new ArmorStandEditorCommand(this); - - Optional.ofNullable(getCommand("armorstandeditor")) - .ifPresent(target -> { - PaperCommandFactory.register(target, command); - AsyncTabCompleteListener.register(this, command); - }); - - editToolItem = new EditToolItem(this); + this.editToolItem = new EditToolItem(this); + this.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, event -> ArmorStandEditorCommand.register(event.registrar(), this)); } @Override diff --git a/src/main/java/net/okocraft/armorstandeditor/command/ArmorStandEditorCommand.java b/src/main/java/net/okocraft/armorstandeditor/command/ArmorStandEditorCommand.java index fb022ea..d022299 100644 --- a/src/main/java/net/okocraft/armorstandeditor/command/ArmorStandEditorCommand.java +++ b/src/main/java/net/okocraft/armorstandeditor/command/ArmorStandEditorCommand.java @@ -1,95 +1,173 @@ package net.okocraft.armorstandeditor.command; -import com.github.siroshun09.mccommand.common.AbstractCommand; -import com.github.siroshun09.mccommand.common.Command; -import com.github.siroshun09.mccommand.common.CommandResult; -import com.github.siroshun09.mccommand.common.SubCommandHolder; -import com.github.siroshun09.mccommand.common.context.CommandContext; -import com.github.siroshun09.mccommand.common.filter.StringFilter; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; import net.okocraft.armorstandeditor.ArmorStandEditorPlugin; -import net.okocraft.armorstandeditor.command.subcommand.AxisCommand; -import net.okocraft.armorstandeditor.command.subcommand.EquipmentCommand; -import net.okocraft.armorstandeditor.command.subcommand.ItemCommand; -import net.okocraft.armorstandeditor.command.subcommand.ModeCommand; -import net.okocraft.armorstandeditor.command.subcommand.ReloadCommand; +import net.okocraft.armorstandeditor.editor.EditMode; +import net.okocraft.armorstandeditor.editor.PlayerEditor; +import net.okocraft.armorstandeditor.editor.PlayerEditorProvider; +import net.okocraft.armorstandeditor.item.EditToolItem; +import net.okocraft.armorstandeditor.lang.Components; import net.okocraft.armorstandeditor.lang.Messages; +import net.okocraft.armorstandeditor.menu.EquipmentMenuProvider; import net.okocraft.armorstandeditor.permission.Permissions; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; -import java.util.Collections; +import java.io.IOException; +import java.util.Arrays; import java.util.List; -import java.util.Set; - -public class ArmorStandEditorCommand extends AbstractCommand { - - private final SubCommandHolder holder; - - public ArmorStandEditorCommand(@NotNull ArmorStandEditorPlugin plugin) { - super("armorstandeditor", Permissions.COMMAND, Set.of("ase", "aseditor")); - - holder = SubCommandHolder.of( - new AxisCommand(), - new EquipmentCommand(), - new ItemCommand(plugin), - new ModeCommand(), - new ReloadCommand(plugin) +import java.util.Locale; + +@SuppressWarnings("UnstableApiUsage") +public final class ArmorStandEditorCommand { + + public static void register(Commands commands, ArmorStandEditorPlugin plugin) { + commands.register( + plugin.getPluginMeta(), + Commands.literal("armorstandeditor") + .requires(source -> source.getSender().hasPermission(Permissions.COMMAND)) + .executes(ctx -> { + ctx.getSource().getSender().sendMessage(Messages.COMMAND_ARGUMENT_NOT_ENOUGH); + return Command.SINGLE_SUCCESS; + }) + .then(axis()) + .then(equipment()) + .then(item(plugin.getEditToolItem())) + .then(mode()) + .then(reload(plugin)) + .build(), + "The root command of ArmorStandEditor", + List.of("ase", "aseditor") ); } - @Override - public @NotNull CommandResult onExecution(@NotNull CommandContext context) { - var sender = context.getSender(); - - if (!sender.hasPermission(getPermission())) { - sender.sendMessage(Messages.COMMAND_NO_PERMISSION); - return CommandResult.NO_PERMISSION; - } - - var arguments = context.getArguments(); - - if (arguments.isEmpty()) { - sender.sendMessage(Messages.COMMAND_ARGUMENT_NOT_ENOUGH); - return CommandResult.NO_ARGUMENT; - } - - var sub = holder.search(arguments.get(0)); - - if (sub != null) { - return sub.onExecution(context); - } else { - sender.sendMessage(Messages.COMMAND_SUB_COMMAND_NOT_FOUND); - return CommandResult.INVALID_ARGUMENTS; - } + public static LiteralArgumentBuilder axis() { + return Commands.literal("axis") + .requires(source -> + source.getSender().hasPermission(Permissions.COMMAND_AXIS) && + source.getSender() instanceof Player + ) + .then( + Commands.argument( + "axis", + new ConstantsArgumentType<>( + Arrays.stream(PlayerEditor.Axis.values()).map(Enum::name).map(name -> name.toLowerCase(Locale.ENGLISH)).toList(), + name -> { + try { + return PlayerEditor.Axis.valueOf(name.toUpperCase(Locale.ENGLISH)); + } catch (IllegalArgumentException ignored) { + return null; + } + }, + argument -> Messages.COMMAND_AXIS_INVALID_ARGUMENT.append(Component.text(argument, NamedTextColor.AQUA)), + Messages.COMMAND_AXIS_TOOLTIP + ) + ).executes(context -> { + var axis = context.getArgument("axis", PlayerEditor.Axis.class); + PlayerEditorProvider.getEditor((Player) context.getSource().getSender()).setAxis(axis); + + context.getSource().getSender().sendMessage( + Messages.COMMAND_AXIS_CHANGE.append(Components.AXIS_NAME.apply(axis)) + ); + + return Command.SINGLE_SUCCESS; + }) + ); } - @Override - public @NotNull List onTabCompletion(@NotNull CommandContext context) { - if (!context.getSender().hasPermission(getPermission())) { - return Collections.emptyList(); - } - - var arguments = context.getArguments(); - - if (arguments.isEmpty()) { - return Collections.emptyList(); - } + public static LiteralArgumentBuilder equipment() { + return Commands.literal("equipment") + .requires(source -> + source.getSender().hasPermission(Permissions.COMMAND_EQUIPMENT) && + source.getSender() instanceof Player + ) + .executes(context -> { + var player = (Player) context.getSource().getSender(); + var target = player.getTargetEntity(3); + + if (target instanceof ArmorStand armorStand) { + player.openInventory(EquipmentMenuProvider.getMenu(armorStand).getInventory()); + } else { + player.sendMessage(Messages.COMMAND_EQUIPMENT_ARMOR_STAND_NOT_FOUND); + } + + return Command.SINGLE_SUCCESS; + }); + } - if (arguments.size() == 1) { - var argumentFilter = StringFilter.startsWithIgnoreCase(arguments.get(0).get()); + public static LiteralArgumentBuilder item(@NotNull EditToolItem editToolItem) { + return Commands.literal("item") + .requires(source -> + source.getSender().hasPermission(Permissions.COMMAND_ITEM) && + source.getSender() instanceof Player + ) + .executes(context -> { + var player = (Player) context.getSource().getSender(); + + if (player.getInventory().addItem(editToolItem.createEditTool()).isEmpty()) { + player.sendMessage(Messages.COMMAND_ITEM_SUCCESS); + } else { + player.sendMessage(Messages.COMMAND_ITEM_FAILURE); + } + + return Command.SINGLE_SUCCESS; + }); + } - return holder.getSubCommands() - .stream() - .map(Command::getName) - .filter(argumentFilter) - .toList(); - } + public static LiteralArgumentBuilder mode() { + return Commands.literal("mode") + .requires(source -> + source.getSender().hasPermission(Permissions.COMMAND_MODE) && + source.getSender() instanceof Player + ) + .then( + Commands.argument( + "mode", + new ConstantsArgumentType<>( + EditMode.names().toList(), + EditMode::byName, + argument -> Messages.COMMAND_MODE_INVALID_ARGUMENT.append(Component.text(argument, NamedTextColor.AQUA)), + Messages.COMMAND_MODE_TOOLTIP + ) + ).executes(context -> { + var player = (Player) context.getSource().getSender(); + var mode = context.getArgument("mode", EditMode.class); + + if (player.hasPermission(mode.getPermission())) { + PlayerEditorProvider.getEditor(player).setMode(mode); + player.sendMessage(Messages.COMMAND_MODE_CHANGE.append(Components.MODE_NAME.apply(mode))); + } else { + player.sendMessage(Messages.COMMAND_MODE_NO_PERMISSION); + } + + return Command.SINGLE_SUCCESS; + }) + ); + } - var sub = holder.search(arguments.get(0)); + public static LiteralArgumentBuilder reload(@NotNull ArmorStandEditorPlugin plugin) { + return Commands.literal("reload") + .requires(source -> source.getSender().hasPermission(Permissions.COMMAND_RELOAD)) + .executes(context -> { + try { + plugin.getTranslationDirectory().load(); + } catch (IOException e) { + context.getSource().getSender().sendMessage(Messages.COMMAND_RELOAD_FAILURE); + return Command.SINGLE_SUCCESS; + } + + context.getSource().getSender().sendMessage(Messages.COMMAND_RELOAD_SUCCESS); + return Command.SINGLE_SUCCESS; + }); + } - if (sub != null) { - return sub.onTabCompletion(context); - } else { - return Collections.emptyList(); - } + private ArmorStandEditorCommand() { + throw new UnsupportedOperationException(); } } diff --git a/src/main/java/net/okocraft/armorstandeditor/command/ConstantsArgumentType.java b/src/main/java/net/okocraft/armorstandeditor/command/ConstantsArgumentType.java new file mode 100644 index 0000000..d7650fa --- /dev/null +++ b/src/main/java/net/okocraft/armorstandeditor/command/ConstantsArgumentType.java @@ -0,0 +1,70 @@ +package net.okocraft.armorstandeditor.command; + +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import io.papermc.paper.command.brigadier.MessageComponentSerializer; +import io.papermc.paper.command.brigadier.argument.CustomArgumentType; +import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +@SuppressWarnings("UnstableApiUsage") +public final class ConstantsArgumentType implements CustomArgumentType { + + private final Function messageForInvalidInput; + private final Function tooltipFunction; + private final List names; + private final Function finder; + + public ConstantsArgumentType(@NotNull List names, @NotNull Function finder, @NotNull Function messageForInvalidInput, @NotNull Function tooltipFunction) { + this.names = names; + this.finder = finder; + this.messageForInvalidInput = messageForInvalidInput; + this.tooltipFunction = tooltipFunction; + } + + @Override + public @NotNull ArgumentType getNativeType() { + return StringArgumentType.word(); + } + + @Override + public @NotNull T parse(@NotNull StringReader reader) throws CommandSyntaxException { + var input = reader.readUnquotedString(); + var found = this.finder.apply(input); + + if (found != null) { + return found; + } else { + var message = MessageComponentSerializer.message().serialize(this.messageForInvalidInput.apply(input)); + throw new CommandSyntaxException(new SimpleCommandExceptionType(message), message, reader.getString(), reader.getCursor()); + } + } + + @Override + public @NotNull CompletableFuture listSuggestions(@NotNull CommandContext context, @NotNull SuggestionsBuilder builder) { + var input = builder.getRemaining(); + boolean empty = input.isEmpty(); + + for (var name : this.names) { + if (empty || startsWith(name, input, input.length())) { + builder.suggest(name, MessageComponentSerializer.message().serialize(this.tooltipFunction.apply(this.finder.apply(name)))); + } + } + + return CompletableFuture.completedFuture(builder.build()); + } + + private static boolean startsWith(@NotNull String str, @NotNull String prefix, int prefixLength) { + return prefixLength <= str.length() && str.regionMatches(true, 0, prefix, 0, prefixLength); + } +} diff --git a/src/main/java/net/okocraft/armorstandeditor/command/subcommand/AxisCommand.java b/src/main/java/net/okocraft/armorstandeditor/command/subcommand/AxisCommand.java deleted file mode 100644 index 6e5204f..0000000 --- a/src/main/java/net/okocraft/armorstandeditor/command/subcommand/AxisCommand.java +++ /dev/null @@ -1,95 +0,0 @@ -package net.okocraft.armorstandeditor.command.subcommand; - -import com.github.siroshun09.mccommand.common.AbstractCommand; -import com.github.siroshun09.mccommand.common.CommandResult; -import com.github.siroshun09.mccommand.common.context.CommandContext; -import com.github.siroshun09.mccommand.common.filter.StringFilter; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; -import net.okocraft.armorstandeditor.editor.PlayerEditor; -import net.okocraft.armorstandeditor.editor.PlayerEditorProvider; -import net.okocraft.armorstandeditor.lang.Components; -import net.okocraft.armorstandeditor.lang.Messages; -import net.okocraft.armorstandeditor.permission.Permissions; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -public class AxisCommand extends AbstractCommand { - - public AxisCommand() { - super("axis", Permissions.COMMAND_PREFIX + "axis", Set.of("a")); - } - - @Override - public @NotNull CommandResult onExecution(@NotNull CommandContext context) { - var sender = context.getSender(); - - if (!sender.hasPermission(getPermission())) { - sender.sendMessage(Messages.COMMAND_NO_PERMISSION); - return CommandResult.NO_PERMISSION; - } - - var object = sender.getOriginalSender(); - - if (!(object instanceof Player player)) { - sender.sendMessage(Messages.COMMAND_ONLY_PLAYER); - return CommandResult.NOT_PLAYER; - } - - var arguments = context.getArguments(); - - if (arguments.size() < 2) { - sender.sendMessage(Messages.COMMAND_ARGUMENT_NOT_ENOUGH); - return CommandResult.NO_ARGUMENT; - } - - var secondArgument = arguments.get(1).get(); - PlayerEditor.Axis axis; - - try { - axis = PlayerEditor.Axis.valueOf(secondArgument.toUpperCase()); - } catch (IllegalArgumentException e) { - sender.sendMessage( - Messages.COMMAND_AXIS_INVALID_ARGUMENT.append(Component.text(secondArgument, NamedTextColor.AQUA)) - ); - return CommandResult.INVALID_ARGUMENTS; - } - - var editor = PlayerEditorProvider.getEditor(player); - editor.setAxis(axis); - - sender.sendMessage( - Messages.COMMAND_AXIS_CHANGE.append(Components.AXIS_NAME.apply(axis)) - ); - - return CommandResult.SUCCESS; - } - - @Override - public @NotNull List onTabCompletion(@NotNull CommandContext context) { - var sender = context.getSender(); - - if (!(sender.hasPermission(getPermission())) || - !(sender.getOriginalSender() instanceof Player)) { - return Collections.emptyList(); - } - - var arguments = context.getArguments(); - - if (arguments.size() == 2) { - var argumentFilter = StringFilter.startsWithIgnoreCase(arguments.get(1).get()); - - return Arrays.stream(PlayerEditor.Axis.values()) - .map(PlayerEditor.Axis::getName) - .filter(argumentFilter) - .toList(); - } - - return Collections.emptyList(); - } -} diff --git a/src/main/java/net/okocraft/armorstandeditor/command/subcommand/EquipmentCommand.java b/src/main/java/net/okocraft/armorstandeditor/command/subcommand/EquipmentCommand.java deleted file mode 100644 index cb500cd..0000000 --- a/src/main/java/net/okocraft/armorstandeditor/command/subcommand/EquipmentCommand.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.okocraft.armorstandeditor.command.subcommand; - -import com.github.siroshun09.mccommand.common.AbstractCommand; -import com.github.siroshun09.mccommand.common.CommandResult; -import com.github.siroshun09.mccommand.common.context.CommandContext; -import net.okocraft.armorstandeditor.lang.Messages; -import net.okocraft.armorstandeditor.menu.EquipmentMenuProvider; -import net.okocraft.armorstandeditor.permission.Permissions; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -import java.util.Collections; -import java.util.List; -import java.util.Set; - -public class EquipmentCommand extends AbstractCommand { - - public EquipmentCommand() { - super("equipment", Permissions.COMMAND_PREFIX + "equipment", Set.of("eq")); - } - - @Override - public @NotNull CommandResult onExecution(@NotNull CommandContext context) { - var sender = context.getSender(); - - if (!sender.hasPermission(getPermission())) { - sender.sendMessage(Messages.COMMAND_NO_PERMISSION); - return CommandResult.NO_PERMISSION; - } - - var object = sender.getOriginalSender(); - - if (!(object instanceof Player player)) { - sender.sendMessage(Messages.COMMAND_ONLY_PLAYER); - return CommandResult.NOT_PLAYER; - } - - var target = player.getTargetEntity(3); - - if (target instanceof ArmorStand armorStand) { - var menu = EquipmentMenuProvider.getMenu(armorStand); - player.openInventory(menu.getInventory()); - - return CommandResult.SUCCESS; - } else { - sender.sendMessage(Messages.COMMAND_EQUIPMENT_ARMOR_STAND_NOT_FOUND); - - return CommandResult.STATE_ERROR; - } - } - - @Override - public @NotNull List onTabCompletion(@NotNull CommandContext context) { - return Collections.emptyList(); - } -} diff --git a/src/main/java/net/okocraft/armorstandeditor/command/subcommand/ItemCommand.java b/src/main/java/net/okocraft/armorstandeditor/command/subcommand/ItemCommand.java deleted file mode 100644 index c8c2eda..0000000 --- a/src/main/java/net/okocraft/armorstandeditor/command/subcommand/ItemCommand.java +++ /dev/null @@ -1,58 +0,0 @@ -package net.okocraft.armorstandeditor.command.subcommand; - -import com.github.siroshun09.mccommand.common.AbstractCommand; -import com.github.siroshun09.mccommand.common.CommandResult; -import com.github.siroshun09.mccommand.common.context.CommandContext; -import net.okocraft.armorstandeditor.ArmorStandEditorPlugin; -import net.okocraft.armorstandeditor.lang.Messages; -import net.okocraft.armorstandeditor.permission.Permissions; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -import java.util.Collections; -import java.util.List; -import java.util.Set; - -public class ItemCommand extends AbstractCommand { - - private final ArmorStandEditorPlugin plugin; - - public ItemCommand(@NotNull ArmorStandEditorPlugin plugin) { - super("item", Permissions.COMMAND_PREFIX + "item", Set.of("i")); - this.plugin = plugin; - } - - @Override - public @NotNull CommandResult onExecution(@NotNull CommandContext context) { - var sender = context.getSender(); - - if (!sender.hasPermission(getPermission())) { - sender.sendMessage(Messages.COMMAND_NO_PERMISSION); - return CommandResult.NO_PERMISSION; - } - - var object = sender.getOriginalSender(); - - if (!(object instanceof Player player)) { - sender.sendMessage(Messages.COMMAND_ONLY_PLAYER); - return CommandResult.NOT_PLAYER; - } - - var item = plugin.getEditToolItem().createEditTool(); - - var success = player.getInventory().addItem(item).isEmpty(); - - if (success) { - sender.sendMessage(Messages.COMMAND_ITEM_SUCCESS); - return CommandResult.SUCCESS; - } else { - sender.sendMessage(Messages.COMMAND_ITEM_FAILURE); - return CommandResult.STATE_ERROR; - } - } - - @Override - public @NotNull List onTabCompletion(@NotNull CommandContext context) { - return Collections.emptyList(); - } -} diff --git a/src/main/java/net/okocraft/armorstandeditor/command/subcommand/ModeCommand.java b/src/main/java/net/okocraft/armorstandeditor/command/subcommand/ModeCommand.java deleted file mode 100644 index 8bf126e..0000000 --- a/src/main/java/net/okocraft/armorstandeditor/command/subcommand/ModeCommand.java +++ /dev/null @@ -1,93 +0,0 @@ -package net.okocraft.armorstandeditor.command.subcommand; - -import com.github.siroshun09.mccommand.common.AbstractCommand; -import com.github.siroshun09.mccommand.common.CommandResult; -import com.github.siroshun09.mccommand.common.context.CommandContext; -import com.github.siroshun09.mccommand.common.filter.StringFilter; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; -import net.okocraft.armorstandeditor.editor.EditMode; -import net.okocraft.armorstandeditor.editor.PlayerEditorProvider; -import net.okocraft.armorstandeditor.lang.Components; -import net.okocraft.armorstandeditor.lang.Messages; -import net.okocraft.armorstandeditor.permission.Permissions; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -import java.util.Collections; -import java.util.List; -import java.util.Set; - -public class ModeCommand extends AbstractCommand { - - public ModeCommand() { - super("mode", Permissions.COMMAND_PREFIX + "mode", Set.of("m")); - } - - @Override - public @NotNull CommandResult onExecution(@NotNull CommandContext context) { - var sender = context.getSender(); - - if (!context.getSender().hasPermission(getPermission())) { - sender.sendMessage(Messages.COMMAND_NO_PERMISSION); - return CommandResult.NO_PERMISSION; - } - - var object = sender.getOriginalSender(); - - if (!(object instanceof Player player)) { - sender.sendMessage(Messages.COMMAND_ONLY_PLAYER); - return CommandResult.NOT_PLAYER; - } - - var arguments = context.getArguments(); - - if (arguments.size() < 2) { - sender.sendMessage(Messages.COMMAND_ARGUMENT_NOT_ENOUGH); - return CommandResult.NO_ARGUMENT; - } - - var secondArgument = arguments.get(1).get(); - var mode = EditMode.byName(secondArgument); - - if (mode == null) { - sender.sendMessage( - Messages.COMMAND_MODE_INVALID_ARGUMENT.append(Component.text(secondArgument, NamedTextColor.AQUA)) - ); - return CommandResult.INVALID_ARGUMENTS; - } - - if (!player.hasPermission(mode.getPermission())) { - player.sendMessage(Messages.COMMAND_MODE_NO_PERMISSION); - return CommandResult.NO_PERMISSION; - } - - var editor = PlayerEditorProvider.getEditor(player); - editor.setMode(mode); - - sender.sendMessage( - Messages.COMMAND_MODE_CHANGE.append(Components.MODE_NAME.apply(mode)) - ); - - return CommandResult.SUCCESS; - } - - @Override - public @NotNull List onTabCompletion(@NotNull CommandContext context) { - var sender = context.getSender(); - - if (!(sender.hasPermission(getPermission())) || - !(sender.getOriginalSender() instanceof Player)) { - return Collections.emptyList(); - } - - var arguments = context.getArguments(); - - if (arguments.size() == 2) { - var argumentFilter = StringFilter.startsWithIgnoreCase(arguments.get(1).get()); - return EditMode.names().filter(argumentFilter).toList(); - } - - return Collections.emptyList(); - } -} diff --git a/src/main/java/net/okocraft/armorstandeditor/command/subcommand/ReloadCommand.java b/src/main/java/net/okocraft/armorstandeditor/command/subcommand/ReloadCommand.java deleted file mode 100644 index c34a8c5..0000000 --- a/src/main/java/net/okocraft/armorstandeditor/command/subcommand/ReloadCommand.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.okocraft.armorstandeditor.command.subcommand; - -import com.github.siroshun09.mccommand.common.AbstractCommand; -import com.github.siroshun09.mccommand.common.CommandResult; -import com.github.siroshun09.mccommand.common.context.CommandContext; -import net.okocraft.armorstandeditor.ArmorStandEditorPlugin; -import net.okocraft.armorstandeditor.lang.Messages; -import net.okocraft.armorstandeditor.permission.Permissions; -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; -import java.util.Collections; -import java.util.List; - -public class ReloadCommand extends AbstractCommand { - - private final ArmorStandEditorPlugin plugin; - - public ReloadCommand(@NotNull ArmorStandEditorPlugin plugin) { - super("reload", Permissions.COMMAND_PREFIX + "reload", Collections.emptySet()); - this.plugin = plugin; - } - - @Override - public @NotNull CommandResult onExecution(@NotNull CommandContext context) { - var sender = context.getSender(); - - if (!sender.hasPermission(getPermission())) { - sender.sendMessage(Messages.COMMAND_NO_PERMISSION); - return CommandResult.NO_PERMISSION; - } - - try { - plugin.getTranslationDirectory().load(); - } catch (IOException e) { - sender.sendMessage(Messages.COMMAND_RELOAD_FAILURE); - return CommandResult.EXCEPTION_OCCURRED; - } - - sender.sendMessage(Messages.COMMAND_RELOAD_SUCCESS); - return CommandResult.SUCCESS; - } - - @Override - public @NotNull List onTabCompletion(@NotNull CommandContext context) { - return Collections.emptyList(); - } -} diff --git a/src/main/java/net/okocraft/armorstandeditor/lang/Messages.java b/src/main/java/net/okocraft/armorstandeditor/lang/Messages.java index 05e1dc5..b591928 100644 --- a/src/main/java/net/okocraft/armorstandeditor/lang/Messages.java +++ b/src/main/java/net/okocraft/armorstandeditor/lang/Messages.java @@ -2,11 +2,17 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; +import net.okocraft.armorstandeditor.editor.EditMode; +import net.okocraft.armorstandeditor.editor.PlayerEditor; + +import java.util.function.Function; import static net.kyori.adventure.text.format.NamedTextColor.GRAY; import static net.kyori.adventure.text.format.NamedTextColor.RED; import static net.kyori.adventure.text.format.NamedTextColor.YELLOW; +import static net.okocraft.armorstandeditor.lang.Components.AXIS_NAME; import static net.okocraft.armorstandeditor.lang.Components.KEY_PREFIX; +import static net.okocraft.armorstandeditor.lang.Components.MODE_NAME; public final class Messages { @@ -47,12 +53,18 @@ private Messages() { public static final TranslatableComponent COMMAND_AXIS_INVALID_ARGUMENT = Components.PREFIX.append(Component.translatable(KEY_PREFIX + "command.axis.invalid-argument", RED)); + public static final Function COMMAND_AXIS_TOOLTIP = + axis -> Components.PREFIX.append(Component.translatable(KEY_PREFIX + "command.axis.tooltip", GRAY).arguments(AXIS_NAME.apply(axis))); + public static final TranslatableComponent COMMAND_AXIS_CHANGE = Components.PREFIX.append(Component.translatable(KEY_PREFIX + "command.axis.change", GRAY)); public static final TranslatableComponent COMMAND_MODE_INVALID_ARGUMENT = Components.PREFIX.append(Component.translatable(KEY_PREFIX + "command.mode.invalid-argument", RED)); + public static final Function COMMAND_MODE_TOOLTIP = + mode -> Components.PREFIX.append(Component.translatable(KEY_PREFIX + "command.mode.tooltip", GRAY).arguments(MODE_NAME.apply(mode))); + public static final TranslatableComponent COMMAND_MODE_CHANGE = Components.PREFIX.append(Component.translatable(KEY_PREFIX + "command.mode.change", GRAY)); diff --git a/src/main/java/net/okocraft/armorstandeditor/permission/Permissions.java b/src/main/java/net/okocraft/armorstandeditor/permission/Permissions.java index eb7c34d..b8a5302 100644 --- a/src/main/java/net/okocraft/armorstandeditor/permission/Permissions.java +++ b/src/main/java/net/okocraft/armorstandeditor/permission/Permissions.java @@ -14,7 +14,17 @@ private Permissions() { public static final String COMMAND = PREFIX + "command"; - public static final String COMMAND_PREFIX = COMMAND + '.'; + private static final String COMMAND_PREFIX = COMMAND + '.'; + + public static final String COMMAND_AXIS = COMMAND_PREFIX + "axis"; + + public static final String COMMAND_EQUIPMENT = COMMAND_PREFIX + "equipment"; + + public static final String COMMAND_ITEM = COMMAND_PREFIX + "item"; + + public static final String COMMAND_MODE = COMMAND_PREFIX + "mode"; + + public static final String COMMAND_RELOAD = COMMAND_PREFIX + "reload"; public static final String MODE_PREFIX = PREFIX + "mode."; diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index 1018ce3..14e6beb 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -161,9 +161,11 @@ armorstandeditor: failure: "Failed to reload the language file." axis: invalid-argument: "The specified axis does not exist: " + tooltip: "The axis for editing: {0}" change: "The axis has been changed: " mode: invalid-argument: "The specified edit mode does not exist: " + tooltip: "The edit mode: {0}" change: "The edit mode was changed: " no-permission: "You don't have permission to change to that mode." item: diff --git a/src/main/resources/ja_JP.yml b/src/main/resources/ja_JP.yml index 9cb9ddf..f3b4dc0 100644 --- a/src/main/resources/ja_JP.yml +++ b/src/main/resources/ja_JP.yml @@ -161,9 +161,11 @@ armorstandeditor: failure: "言語ファイルの再読み込みに失敗しました。" axis: invalid-argument: "指定された軸は存在しません: " + tooltip: "編集時の軸: {0}" change: "軸を変更しました: " mode: invalid-argument: "指定された編集モードは存在しません: " + tooltip: "編集モード: {0}" change: "編集モードを変更しました: " no-permission: "そのモードに変更する権限がありません。" item: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index fd91291..d60fc33 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,10 +4,6 @@ version: ${project.version} author: "Siroshun09" api-version: "1.16" folia-supported: true -commands: - armorstandeditor: - aliases: ["ase", "aseditor"] - permissions: armorstandeditor.edit: default: op