Skip to content

Commit

Permalink
Permission improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Jul 14, 2024
1 parent 23a5a72 commit fd14873
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<BooleanArenaOption> TEAM_PREFIXES = ArenaOptionType.create("team-prefixes", BooleanArenaOption::new);

@EventHandler
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<String, CommandWrapper> commandWrappers = new HashMap<>();
for (Map.Entry<String, Set<CommandWrapper>> entry : this.commandMethods.entrySet()) {
Expand All @@ -320,9 +323,19 @@ public void sendHelpMessage(CommandSender sender, int page) {
}
}

// Sort alphabetically
List<CommandWrapper> 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<CommandWrapper> 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);
Expand All @@ -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<Component> 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<Component> 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();
Expand Down Expand Up @@ -704,7 +715,7 @@ public final List<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fd14873

Please sign in to comment.