From ea3a5a7a7001402e3af0ed6f56f5a9d749314342 Mon Sep 17 00:00:00 2001 From: david Date: Thu, 25 Jul 2024 18:17:53 +0200 Subject: [PATCH] extracted arguments into separate classes --- .../commander/paper/CommanderPlugin.java | 2 +- .../paper/command/CommanderCommand.java | 229 +----------------- .../commander/paper/command/HideCommand.java | 44 ++++ .../paper/command/PermissionCommand.java | 21 ++ .../paper/command/PermissionQueryCommand.java | 45 ++++ .../paper/command/PermissionResetCommand.java | 42 ++++ .../paper/command/PermissionSetCommand.java | 56 +++++ .../paper/command/PermissionUnsetCommand.java | 45 ++++ .../paper/command/RegisterCommand.java | 42 ++++ .../commander/paper/command/ResetCommand.java | 45 ++++ .../paper/command/RevealCommand.java | 42 ++++ .../paper/command/UnregisterCommand.java | 44 ++++ 12 files changed, 434 insertions(+), 223 deletions(-) create mode 100644 plugin/src/main/java/net/thenextlvl/commander/paper/command/HideCommand.java create mode 100644 plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionCommand.java create mode 100644 plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionQueryCommand.java create mode 100644 plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionResetCommand.java create mode 100644 plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionSetCommand.java create mode 100644 plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionUnsetCommand.java create mode 100644 plugin/src/main/java/net/thenextlvl/commander/paper/command/RegisterCommand.java create mode 100644 plugin/src/main/java/net/thenextlvl/commander/paper/command/ResetCommand.java create mode 100644 plugin/src/main/java/net/thenextlvl/commander/paper/command/RevealCommand.java create mode 100644 plugin/src/main/java/net/thenextlvl/commander/paper/command/UnregisterCommand.java diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/CommanderPlugin.java b/plugin/src/main/java/net/thenextlvl/commander/paper/CommanderPlugin.java index 29654eb..6f5bc8c 100644 --- a/plugin/src/main/java/net/thenextlvl/commander/paper/CommanderPlugin.java +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/CommanderPlugin.java @@ -50,7 +50,7 @@ public void onEnable() { } private void registerCommands() { - new CommanderCommand(this).register(); + new CommanderCommand().register(this); } private void registerListeners() { diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/CommanderCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/CommanderCommand.java index 6cbaf78..c1cff5d 100644 --- a/plugin/src/main/java/net/thenextlvl/commander/paper/command/CommanderCommand.java +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/CommanderCommand.java @@ -1,238 +1,23 @@ package net.thenextlvl.commander.paper.command; -import com.mojang.brigadier.Command; -import com.mojang.brigadier.arguments.StringArgumentType; -import com.mojang.brigadier.context.CommandContext; -import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.command.brigadier.Commands; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; -import lombok.RequiredArgsConstructor; -import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.thenextlvl.commander.paper.CommanderPlugin; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.permissions.Permission; -@RequiredArgsConstructor @SuppressWarnings("UnstableApiUsage") public class CommanderCommand { - private final CommanderPlugin plugin; - public void register() { + public void register(CommanderPlugin plugin) { var command = Commands.literal("command") .requires(stack -> stack.getSender().hasPermission("commander.admin")) - .then(Commands.literal("hide") - .then(Commands.argument("command", StringArgumentType.string()) - .suggests((context, suggestions) -> { - Bukkit.getCommandMap().getKnownCommands().values().stream() - .map(org.bukkit.command.Command::getLabel) - .filter(s -> !plugin.commandRegistry().isHidden(s)) - .filter(s -> s.contains(suggestions.getRemaining())) - .map(StringArgumentType::escapeIfRequired) - .forEach(suggestions::suggest); - return suggestions.buildFuture(); - }) - .executes(this::hide))) - .then(Commands.literal("permission") - .then(Commands.literal("query") - .then(Commands.argument("command", StringArgumentType.string()) - .suggests((context, suggestions) -> { - Bukkit.getCommandMap().getKnownCommands().values().stream() - .map(org.bukkit.command.Command::getLabel) - .filter(s -> s.contains(suggestions.getRemaining())) - .map(StringArgumentType::escapeIfRequired) - .forEach(suggestions::suggest); - return suggestions.buildFuture(); - }) - .executes(this::permissionQuery))) - .then(Commands.literal("reset") - .then(Commands.argument("command", StringArgumentType.string()) - .suggests((context, suggestions) -> { - plugin.permissionOverride().originalPermissions().keySet().stream() - .filter(s -> s.contains(suggestions.getRemaining())) - .map(StringArgumentType::escapeIfRequired) - .forEach(suggestions::suggest); - return suggestions.buildFuture(); - }) - .executes(this::permissionReset))) - .then(Commands.literal("set") - .then(Commands.argument("command", StringArgumentType.string()) - .suggests((context, suggestions) -> { - Bukkit.getCommandMap().getKnownCommands().values().stream() - .map(org.bukkit.command.Command::getLabel) - .filter(s -> s.contains(suggestions.getRemaining())) - .map(StringArgumentType::escapeIfRequired) - .forEach(suggestions::suggest); - return suggestions.buildFuture(); - }) - .then(Commands.argument("permission", StringArgumentType.string()) - .suggests((context, suggestions) -> { - Bukkit.getPluginManager().getPermissions().stream() - .map(Permission::getName) - .filter(s -> s.contains(suggestions.getRemaining())) - .map(StringArgumentType::escapeIfRequired) - .forEach(suggestions::suggest); - return suggestions.buildFuture(); - }) - .executes(this::permissionSet)))) - .then(Commands.literal("unset") - .then(Commands.argument("command", StringArgumentType.string()) - .suggests((context, suggestions) -> { - Bukkit.getCommandMap().getKnownCommands().values().stream() - .map(org.bukkit.command.Command::getLabel) - .filter(s -> s.contains(suggestions.getRemaining())) - .map(StringArgumentType::escapeIfRequired) - .forEach(suggestions::suggest); - return suggestions.buildFuture(); - }) - .executes(this::permissionUnset)))) - .then(Commands.literal("register") - .then(Commands.argument("command", StringArgumentType.string()) - .suggests((context, suggestions) -> { - plugin.commandRegistry().unregisteredCommands().stream() - .filter(s -> s.contains(suggestions.getRemaining())) - .map(StringArgumentType::escapeIfRequired) - .forEach(suggestions::suggest); - return suggestions.buildFuture(); - }) - .executes(this::register))) - .then(Commands.literal("reset") - .then(Commands.argument("command", StringArgumentType.string()) - .suggests((context, suggestions) -> { - plugin.commandRegistry().hiddenCommands().stream() - .filter(s -> s.contains(suggestions.getRemaining())) - .map(StringArgumentType::escapeIfRequired) - .forEach(suggestions::suggest); - plugin.commandRegistry().unregisteredCommands().stream() - .filter(s -> s.contains(suggestions.getRemaining())) - .map(StringArgumentType::escapeIfRequired) - .forEach(suggestions::suggest); - return suggestions.buildFuture(); - }) - .executes(this::reset))) - .then(Commands.literal("reveal") - .then(Commands.argument("command", StringArgumentType.string()) - .suggests((context, suggestions) -> { - plugin.commandRegistry().hiddenCommands().stream() - .map(StringArgumentType::escapeIfRequired) - .filter(s -> s.contains(suggestions.getRemaining())) - .forEach(suggestions::suggest); - return suggestions.buildFuture(); - }) - .executes(this::reveal))) - .then(Commands.literal("unregister") - .then(Commands.argument("command", StringArgumentType.string()) - .suggests((context, suggestions) -> { - Bukkit.getCommandMap().getKnownCommands().values().stream() - .map(org.bukkit.command.Command::getLabel) - .filter(s -> !plugin.commandRegistry().isUnregistered(s)) - .filter(s -> s.contains(suggestions.getRemaining())) - .map(StringArgumentType::escapeIfRequired) - .forEach(suggestions::suggest); - return suggestions.buildFuture(); - }) - .executes(this::unregister))) + .then(new HideCommand(plugin).create()) + .then(new PermissionCommand(plugin).create()) + .then(new RegisterCommand(plugin).create()) + .then(new ResetCommand(plugin).create()) + .then(new RevealCommand(plugin).create()) + .then(new UnregisterCommand(plugin).create()) .build(); plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS.newHandler(event -> event.registrar().register(command))); } - - private int hide(CommandContext context) { - var sender = context.getSource().getSender(); - var command = context.getArgument("command", String.class); - var success = plugin.commandRegistry().hide(command); - var message = success ? "command.hidden" : "nothing.changed"; - plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); - if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); - return Command.SINGLE_SUCCESS; - } - - private int permissionQuery(CommandContext context) { - var sender = context.getSource().getSender(); - var command = context.getArgument("command", String.class); - var registered = Bukkit.getCommandMap().getKnownCommands().get(command); - var permission = registered != null ? registered.getPermission() : null; - var message = registered == null ? "command.unknown" : permission != null ? - "permission.query.defined" : "permission.query.undefined"; - plugin.bundle().sendMessage(sender, message, - Placeholder.parsed("permission", String.valueOf(permission)), - Placeholder.parsed("command", command)); - return Command.SINGLE_SUCCESS; - } - - private int permissionReset(CommandContext context) { - var sender = context.getSource().getSender(); - var command = context.getArgument("command", String.class); - var success = plugin.permissionOverride().reset(command); - var message = success ? "permission.reset" : "nothing.changed"; - plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); - if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); - return Command.SINGLE_SUCCESS; - } - - private int permissionSet(CommandContext context) { - var sender = context.getSource().getSender(); - var command = context.getArgument("command", String.class); - var permission = context.getArgument("permission", String.class); - var success = plugin.permissionOverride().override(command, permission); - var message = success ? "permission.set" : "nothing.changed"; - plugin.bundle().sendMessage(sender, message, - Placeholder.parsed("permission", permission), - Placeholder.parsed("command", command)); - if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); - return Command.SINGLE_SUCCESS; - } - - private int permissionUnset(CommandContext context) { - var sender = context.getSource().getSender(); - var command = context.getArgument("command", String.class); - var success = plugin.permissionOverride().override(command, null); - var message = success ? "permission.set" : "nothing.changed"; - plugin.bundle().sendMessage(sender, message, - Placeholder.parsed("permission", "null"), - Placeholder.parsed("command", command)); - if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); - return Command.SINGLE_SUCCESS; - } - - private int register(CommandContext context) { - var sender = context.getSource().getSender(); - var command = context.getArgument("command", String.class); - var success = plugin.commandRegistry().register(command); - var message = success ? "command.registered" : "nothing.changed"; - plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); - if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); - return Command.SINGLE_SUCCESS; - } - - private int reset(CommandContext context) { - var sender = context.getSource().getSender(); - var command = context.getArgument("command", String.class); - var s1 = plugin.permissionOverride().reset(command); - var s3 = plugin.commandRegistry().register(command); - var s2 = plugin.commandRegistry().reveal(command); - var message = s1 || s2 || s3 ? "command.reset" : "nothing.changed"; - plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); - return Command.SINGLE_SUCCESS; - } - - private int reveal(CommandContext context) { - var sender = context.getSource().getSender(); - var command = context.getArgument("command", String.class); - var success = plugin.commandRegistry().reveal(command); - var message = success ? "command.revealed" : "nothing.changed"; - plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); - if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); - return Command.SINGLE_SUCCESS; - } - - private int unregister(CommandContext context) { - var sender = context.getSource().getSender(); - var command = context.getArgument("command", String.class); - var success = plugin.commandRegistry().unregister(command); - var message = success ? "command.unregistered" : "nothing.changed"; - plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); - if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); - return Command.SINGLE_SUCCESS; - } } diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/HideCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/HideCommand.java new file mode 100644 index 0000000..cc7563f --- /dev/null +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/HideCommand.java @@ -0,0 +1,44 @@ +package net.thenextlvl.commander.paper.command; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.paper.CommanderPlugin; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +@RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") +class HideCommand { + private final CommanderPlugin plugin; + + ArgumentBuilder create() { + return Commands.literal("hide") + .then(Commands.argument("command", StringArgumentType.string()) + .suggests((context, suggestions) -> { + Bukkit.getCommandMap().getKnownCommands().values().stream() + .map(org.bukkit.command.Command::getLabel) + .filter(s -> !plugin.commandRegistry().isHidden(s)) + .filter(s -> s.contains(suggestions.getRemaining())) + .map(StringArgumentType::escapeIfRequired) + .forEach(suggestions::suggest); + return suggestions.buildFuture(); + }) + .executes(this::hide)); + } + + private int hide(CommandContext context) { + var sender = context.getSource().getSender(); + var command = context.getArgument("command", String.class); + var success = plugin.commandRegistry().hide(command); + var message = success ? "command.hidden" : "nothing.changed"; + plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); + if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); + return Command.SINGLE_SUCCESS; + } +} diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionCommand.java new file mode 100644 index 0000000..b175595 --- /dev/null +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionCommand.java @@ -0,0 +1,21 @@ +package net.thenextlvl.commander.paper.command; + +import com.mojang.brigadier.builder.ArgumentBuilder; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import lombok.RequiredArgsConstructor; +import net.thenextlvl.commander.paper.CommanderPlugin; + +@RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") +class PermissionCommand { + private final CommanderPlugin plugin; + + public ArgumentBuilder create() { + return Commands.literal("permission") + .then(new PermissionQueryCommand(plugin).create()) + .then(new PermissionResetCommand(plugin).create()) + .then(new PermissionSetCommand(plugin).create()) + .then(new PermissionUnsetCommand(plugin).create()); + } +} diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionQueryCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionQueryCommand.java new file mode 100644 index 0000000..61bf8c6 --- /dev/null +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionQueryCommand.java @@ -0,0 +1,45 @@ +package net.thenextlvl.commander.paper.command; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.paper.CommanderPlugin; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; + +@RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") +class PermissionQueryCommand { + private final CommanderPlugin plugin; + + public ArgumentBuilder create() { + return Commands.literal("query") + .then(Commands.argument("command", StringArgumentType.string()) + .suggests((context, suggestions) -> { + Bukkit.getCommandMap().getKnownCommands().values().stream() + .map(Command::getLabel) + .filter(s -> s.contains(suggestions.getRemaining())) + .map(StringArgumentType::escapeIfRequired) + .forEach(suggestions::suggest); + return suggestions.buildFuture(); + }) + .executes(this::query)); + } + + private int query(CommandContext context) { + var sender = context.getSource().getSender(); + var command = context.getArgument("command", String.class); + var registered = Bukkit.getCommandMap().getKnownCommands().get(command); + var permission = registered != null ? registered.getPermission() : null; + var message = registered == null ? "command.unknown" : permission != null ? + "permission.query.defined" : "permission.query.undefined"; + plugin.bundle().sendMessage(sender, message, + Placeholder.parsed("permission", String.valueOf(permission)), + Placeholder.parsed("command", command)); + return com.mojang.brigadier.Command.SINGLE_SUCCESS; + } +} diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionResetCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionResetCommand.java new file mode 100644 index 0000000..dab82a8 --- /dev/null +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionResetCommand.java @@ -0,0 +1,42 @@ +package net.thenextlvl.commander.paper.command; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.paper.CommanderPlugin; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +@RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") +class PermissionResetCommand { + private final CommanderPlugin plugin; + + public ArgumentBuilder create() { + return Commands.literal("reset") + .then(Commands.argument("command", StringArgumentType.string()) + .suggests((context, suggestions) -> { + plugin.permissionOverride().originalPermissions().keySet().stream() + .filter(s -> s.contains(suggestions.getRemaining())) + .map(StringArgumentType::escapeIfRequired) + .forEach(suggestions::suggest); + return suggestions.buildFuture(); + }) + .executes(this::reset)); + } + + private int reset(CommandContext context) { + var sender = context.getSource().getSender(); + var command = context.getArgument("command", String.class); + var success = plugin.permissionOverride().reset(command); + var message = success ? "permission.reset" : "nothing.changed"; + plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); + if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); + return Command.SINGLE_SUCCESS; + } +} diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionSetCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionSetCommand.java new file mode 100644 index 0000000..e337000 --- /dev/null +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionSetCommand.java @@ -0,0 +1,56 @@ +package net.thenextlvl.commander.paper.command; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.paper.CommanderPlugin; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.entity.Player; +import org.bukkit.permissions.Permission; + +@RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") +class PermissionSetCommand { + private final CommanderPlugin plugin; + + public ArgumentBuilder create() { + return Commands.literal("set") + .then(Commands.argument("command", StringArgumentType.string()) + .suggests((context, suggestions) -> { + Bukkit.getCommandMap().getKnownCommands().values().stream() + .map(Command::getLabel) + .filter(s -> s.contains(suggestions.getRemaining())) + .map(StringArgumentType::escapeIfRequired) + .forEach(suggestions::suggest); + return suggestions.buildFuture(); + }) + .then(Commands.argument("permission", StringArgumentType.string()) + .suggests((context, suggestions) -> { + Bukkit.getPluginManager().getPermissions().stream() + .map(Permission::getName) + .filter(s -> s.contains(suggestions.getRemaining())) + .map(StringArgumentType::escapeIfRequired) + .forEach(suggestions::suggest); + return suggestions.buildFuture(); + }) + .executes(this::set))); + } + + private int set(CommandContext context) { + var sender = context.getSource().getSender(); + var command = context.getArgument("command", String.class); + var permission = context.getArgument("permission", String.class); + var success = plugin.permissionOverride().override(command, permission); + var message = success ? "permission.set" : "nothing.changed"; + plugin.bundle().sendMessage(sender, message, + Placeholder.parsed("permission", permission), + Placeholder.parsed("command", command)); + if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); + return com.mojang.brigadier.Command.SINGLE_SUCCESS; + } +} diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionUnsetCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionUnsetCommand.java new file mode 100644 index 0000000..61a2e27 --- /dev/null +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/PermissionUnsetCommand.java @@ -0,0 +1,45 @@ +package net.thenextlvl.commander.paper.command; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.paper.CommanderPlugin; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +@RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") +class PermissionUnsetCommand { + private final CommanderPlugin plugin; + + public ArgumentBuilder create() { + return Commands.literal("unset") + .then(Commands.argument("command", StringArgumentType.string()) + .suggests((context, suggestions) -> { + Bukkit.getCommandMap().getKnownCommands().values().stream() + .map(Command::getLabel) + .filter(s -> s.contains(suggestions.getRemaining())) + .map(StringArgumentType::escapeIfRequired) + .forEach(suggestions::suggest); + return suggestions.buildFuture(); + }) + .executes(this::unset)); + } + + private int unset(CommandContext context) { + var sender = context.getSource().getSender(); + var command = context.getArgument("command", String.class); + var success = plugin.permissionOverride().override(command, null); + var message = success ? "permission.set" : "nothing.changed"; + plugin.bundle().sendMessage(sender, message, + Placeholder.parsed("permission", "null"), + Placeholder.parsed("command", command)); + if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); + return com.mojang.brigadier.Command.SINGLE_SUCCESS; + } +} diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/RegisterCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/RegisterCommand.java new file mode 100644 index 0000000..6235d56 --- /dev/null +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/RegisterCommand.java @@ -0,0 +1,42 @@ +package net.thenextlvl.commander.paper.command; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.paper.CommanderPlugin; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +@RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") +class RegisterCommand { + private final CommanderPlugin plugin; + + public ArgumentBuilder create() { + return Commands.literal("register") + .then(Commands.argument("command", StringArgumentType.string()) + .suggests((context, suggestions) -> { + plugin.commandRegistry().unregisteredCommands().stream() + .filter(s -> s.contains(suggestions.getRemaining())) + .map(StringArgumentType::escapeIfRequired) + .forEach(suggestions::suggest); + return suggestions.buildFuture(); + }) + .executes(this::register)); + } + + private int register(CommandContext context) { + var sender = context.getSource().getSender(); + var command = context.getArgument("command", String.class); + var success = plugin.commandRegistry().register(command); + var message = success ? "command.registered" : "nothing.changed"; + plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); + if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); + return Command.SINGLE_SUCCESS; + } +} diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/ResetCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/ResetCommand.java new file mode 100644 index 0000000..e6ee450 --- /dev/null +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/ResetCommand.java @@ -0,0 +1,45 @@ +package net.thenextlvl.commander.paper.command; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.paper.CommanderPlugin; + +@RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") +class ResetCommand { + private final CommanderPlugin plugin; + + public ArgumentBuilder create() { + return Commands.literal("reset") + .then(Commands.argument("command", StringArgumentType.string()) + .suggests((context, suggestions) -> { + plugin.commandRegistry().hiddenCommands().stream() + .filter(s -> s.contains(suggestions.getRemaining())) + .map(StringArgumentType::escapeIfRequired) + .forEach(suggestions::suggest); + plugin.commandRegistry().unregisteredCommands().stream() + .filter(s -> s.contains(suggestions.getRemaining())) + .map(StringArgumentType::escapeIfRequired) + .forEach(suggestions::suggest); + return suggestions.buildFuture(); + }) + .executes(this::reset)); + } + + private int reset(CommandContext context) { + var sender = context.getSource().getSender(); + var command = context.getArgument("command", String.class); + var s1 = plugin.permissionOverride().reset(command); + var s3 = plugin.commandRegistry().register(command); + var s2 = plugin.commandRegistry().reveal(command); + var message = s1 || s2 || s3 ? "command.reset" : "nothing.changed"; + plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); + return Command.SINGLE_SUCCESS; + } +} diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/RevealCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/RevealCommand.java new file mode 100644 index 0000000..ec38fd6 --- /dev/null +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/RevealCommand.java @@ -0,0 +1,42 @@ +package net.thenextlvl.commander.paper.command; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.paper.CommanderPlugin; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +@RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") +class RevealCommand { + private final CommanderPlugin plugin; + + public ArgumentBuilder create() { + return Commands.literal("reveal") + .then(Commands.argument("command", StringArgumentType.string()) + .suggests((context, suggestions) -> { + plugin.commandRegistry().hiddenCommands().stream() + .map(StringArgumentType::escapeIfRequired) + .filter(s -> s.contains(suggestions.getRemaining())) + .forEach(suggestions::suggest); + return suggestions.buildFuture(); + }) + .executes(this::reveal)); + } + + private int reveal(CommandContext context) { + var sender = context.getSource().getSender(); + var command = context.getArgument("command", String.class); + var success = plugin.commandRegistry().reveal(command); + var message = success ? "command.revealed" : "nothing.changed"; + plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); + if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); + return Command.SINGLE_SUCCESS; + } +} diff --git a/plugin/src/main/java/net/thenextlvl/commander/paper/command/UnregisterCommand.java b/plugin/src/main/java/net/thenextlvl/commander/paper/command/UnregisterCommand.java new file mode 100644 index 0000000..6ba71ca --- /dev/null +++ b/plugin/src/main/java/net/thenextlvl/commander/paper/command/UnregisterCommand.java @@ -0,0 +1,44 @@ +package net.thenextlvl.commander.paper.command; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.thenextlvl.commander.paper.CommanderPlugin; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +@RequiredArgsConstructor +@SuppressWarnings("UnstableApiUsage") +class UnregisterCommand { + private final CommanderPlugin plugin; + + public ArgumentBuilder create() { + return Commands.literal("unregister") + .then(Commands.argument("command", StringArgumentType.string()) + .suggests((context, suggestions) -> { + Bukkit.getCommandMap().getKnownCommands().values().stream() + .map(Command::getLabel) + .filter(s -> !plugin.commandRegistry().isUnregistered(s)) + .filter(s -> s.contains(suggestions.getRemaining())) + .map(StringArgumentType::escapeIfRequired) + .forEach(suggestions::suggest); + return suggestions.buildFuture(); + }) + .executes(this::unregister)); + } + + private int unregister(CommandContext context) { + var sender = context.getSource().getSender(); + var command = context.getArgument("command", String.class); + var success = plugin.commandRegistry().unregister(command); + var message = success ? "command.unregistered" : "nothing.changed"; + plugin.bundle().sendMessage(sender, message, Placeholder.parsed("command", command)); + if (success) Bukkit.getOnlinePlayers().forEach(Player::updateCommands); + return com.mojang.brigadier.Command.SINGLE_SUCCESS; + } +}