From fd14873d987d286085e88b439b8bb403f4b42a91 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sun, 14 Jul 2024 14:49:35 -0500 Subject: [PATCH] Permission improvements --- .../arena/module/teamcolors/TeamColors.java | 3 +- .../arena/command/BaseCommandExecutor.java | 57 +++++++++++-------- .../arena/util/CommandInjector.java | 1 + 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/module/team-colors/src/main/java/org/battleplugins/arena/module/teamcolors/TeamColors.java b/module/team-colors/src/main/java/org/battleplugins/arena/module/teamcolors/TeamColors.java index 3068462a..e9326b89 100644 --- a/module/team-colors/src/main/java/org/battleplugins/arena/module/teamcolors/TeamColors.java +++ b/module/team-colors/src/main/java/org/battleplugins/arena/module/teamcolors/TeamColors.java @@ -27,6 +27,7 @@ @ArenaModule(id = TeamColors.ID, name = "Team Colors", description = "Adds player team colors to their name.", authors = "BattlePlugins") public class TeamColors implements ArenaModuleInitializer { public static final String ID = "team-colors"; + public static final ArenaOptionType TEAM_PREFIXES = ArenaOptionType.create("team-prefixes", BooleanArenaOption::new); @EventHandler @@ -115,7 +116,7 @@ public void onTeamJoin(ArenaTeamJoinEvent event) { return; } - this.post(5, () -> this.joinTeam(event.getPlayer(), event.getCompetition(), event.getTeam())); + this.post(6, () -> this.joinTeam(event.getPlayer(), event.getCompetition(), event.getTeam())); } @EventHandler diff --git a/plugin/src/main/java/org/battleplugins/arena/command/BaseCommandExecutor.java b/plugin/src/main/java/org/battleplugins/arena/command/BaseCommandExecutor.java index 958ba976..b23ccce1 100644 --- a/plugin/src/main/java/org/battleplugins/arena/command/BaseCommandExecutor.java +++ b/plugin/src/main/java/org/battleplugins/arena/command/BaseCommandExecutor.java @@ -70,8 +70,13 @@ public final boolean onCommand(CommandSender sender, Command command, String lab } if ("help".equals(args[0])) { - int page = args.length > 1 ? Integer.parseInt(args[1]) : 1; - this.sendHelpMessage(sender, page); + try { + int page = args.length > 1 ? Integer.parseInt(args[1]) : 1; + this.sendHelpMessage(sender, page); + } catch (NumberFormatException e) { + this.sendHelpMessage(sender, 1); + } + return true; } @@ -310,8 +315,6 @@ public void sendHeader(CommandSender sender) { } public void sendHelpMessage(CommandSender sender, int page) { - this.sendHeader(sender); - // Compile all the command arguments Map commandWrappers = new HashMap<>(); for (Map.Entry> entry : this.commandMethods.entrySet()) { @@ -320,9 +323,19 @@ public void sendHelpMessage(CommandSender sender, int page) { } } - // Sort alphabetically - List commands = new ArrayList<>(commandWrappers.values()); - commands.sort(Comparator.comparing(wrapper -> wrapper.usage)); + // Sort alphabetically and filter out commands that the sender doesn't have permission for + List commands = new ArrayList<>(commandWrappers.values()).stream().filter(wrapper -> { + ArenaCommand arenaCommand = wrapper.getCommand(); + return arenaCommand.permissionNode().isEmpty() || this.hasPermission(sender, this.getPermissionNode(arenaCommand.permissionNode())); + }).sorted(Comparator.comparing(wrapper -> wrapper.usage)).toList(); + + // Player has no permissions to view any commands + if (commands.isEmpty()) { + Messages.NO_PERMISSION.send(sender); + return; + } + + this.sendHeader(sender); // Now send a certain page int maxPages = (int) Math.ceil(commands.size() / (double) COMMANDS_PER_PAGE); @@ -342,22 +355,20 @@ public void sendHelpMessage(CommandSender sender, int page) { continue; } - if (this.hasPermission(sender, this.getPermissionNode(wrapper.getCommand().permissionNode()))) { - ArenaCommand arenaCommand = wrapper.getCommand(); - String command = "/" + this.parentCommand + " " + (arenaCommand.commands().length > 0 ? arenaCommand.commands()[0] : ""); - if (arenaCommand.subCommands().length > 0) { - command += " " + arenaCommand.subCommands()[0]; - } - - HoverEvent hoverEvent = HoverEvent.showText(Messages.CLICK_TO_PREPARE.toComponent(command)); - ClickEvent clickEvent = ClickEvent.suggestCommand(command); - sender.sendMessage( - Component.text("/" + this.parentCommand + " " + wrapper.usage, Messages.PRIMARY_COLOR) - .append(Component.text(wrapper.getCommand().description(), Messages.SECONDARY_COLOR)) - .clickEvent(clickEvent) - .hoverEvent(hoverEvent) - ); + ArenaCommand arenaCommand = wrapper.getCommand(); + String command = "/" + this.parentCommand + " " + (arenaCommand.commands().length > 0 ? arenaCommand.commands()[0] : ""); + if (arenaCommand.subCommands().length > 0) { + command += " " + arenaCommand.subCommands()[0]; } + + HoverEvent hoverEvent = HoverEvent.showText(Messages.CLICK_TO_PREPARE.toComponent(command)); + ClickEvent clickEvent = ClickEvent.suggestCommand(command); + sender.sendMessage( + Component.text("/" + this.parentCommand + " " + wrapper.usage, Messages.PRIMARY_COLOR) + .append(Component.text(wrapper.getCommand().description(), Messages.SECONDARY_COLOR)) + .clickEvent(clickEvent) + .hoverEvent(hoverEvent) + ); } TextComponent.Builder rootComponent = Component.text(); @@ -704,7 +715,7 @@ public final List onTabComplete(CommandSender sender, Command command, S } protected String getPermissionNode(String node) { - return "battlearena.command." + (this.permissionSubNode == null ? "" : node + ".") + node; + return "battlearena.command." + (this.permissionSubNode == null ? "" : this.permissionSubNode + ".") + node; } protected boolean hasPermission(CommandSender sender, String permission) { diff --git a/plugin/src/main/java/org/battleplugins/arena/util/CommandInjector.java b/plugin/src/main/java/org/battleplugins/arena/util/CommandInjector.java index 138c37d9..cc1a644f 100644 --- a/plugin/src/main/java/org/battleplugins/arena/util/CommandInjector.java +++ b/plugin/src/main/java/org/battleplugins/arena/util/CommandInjector.java @@ -23,6 +23,7 @@ public static PluginCommand inject(String headerName, String commandName, String PluginCommand pluginCommand = constructor.newInstance(commandName, BattleArena.getInstance()); pluginCommand.setAliases(List.of(aliases)); pluginCommand.setDescription(description); + pluginCommand.setPermission("battlearena.command." + commandName); Bukkit.getCommandMap().register(commandName, "battlearena", pluginCommand); return pluginCommand;