Added permission support for commands.

This commit is contained in:
天クマ 2025-12-18 23:33:34 -03:00
commit 708f65fe36
10 changed files with 84 additions and 11 deletions

View file

@ -6,3 +6,12 @@ description: "Change what players can do based in custom criteria."
commands:
eye:
description: "Run /eye help to see all available commands."
permissions:
nemesiseye.reload:
default: op
nemesiseye.policy.list.all:
default: op
nemesiseye.policy.list.self:
default: true
nemesiseye.help:
default: true

View file

@ -1,12 +1,16 @@
package io.github.adrianvic.nemesiseye.commands;
import io.github.adrianvic.nemesiseye.Nemesis;
import io.github.adrianvic.nemesiseye.commands.sub.*;
import io.github.adrianvic.nemesiseye.reflection.Glimmer;
import org.bukkit.command.CommandSender;
import java.util.HashMap;
import java.util.Map;
public class Commands {
private static final Map<String, Subcommand> commands = new HashMap<>();
private static final Glimmer glim = Nemesis.getInstance().getGlimmer();
static {
commands.put("help", new Help());
@ -23,4 +27,9 @@ public class Commands {
public static Subcommand get(String type) {
return commands.get(type);
}
public static void sendNoPermissionError(CommandSender sender) {
glim.sendMessage(sender, "You do not have the necessary permission to use this command.");
}
}

View file

@ -26,17 +26,30 @@ public class EyeCore {
commandSender.sendMessage("Unknown command, try '/eye help' to list available commands.");
return true;
}
return sub.execute(commandSender, Arrays.copyOfRange(strings, 1, strings.length));
else if (commandSender.hasPermission(sub.permission())) {
return sub.execute(commandSender, Arrays.copyOfRange(strings, 1, strings.length));
} else {
Nemesis.getInstance().getLogger().info("does not have %s".formatted(sub.permission()));
Commands.sendNoPermissionError(commandSender);
return true;
}
}
return false;
}
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String [] strings) {
if (strings.length == 1) {
return new ArrayList<>(Commands.getAll().keySet());
Map<String, Subcommand> cmds = new HashMap<>();
for (Map.Entry<String, Subcommand> e : Commands.getAll().entrySet()) {
if (e.getValue().hasPermission(commandSender)) {
cmds.put(e.getKey(), e.getValue());
cmds.put(e.getKey(), e.getValue());
}
}
return new ArrayList<>(cmds.keySet());
}
Subcommand sub = Commands.get(strings[0].toLowerCase());
if (sub != null) {
if (sub != null && commandSender.hasPermission(sub.permission())) {
return sub.onTabComplete(commandSender, Arrays.copyOfRange(strings, 1, strings.length));
}
return List.of();

View file

@ -39,4 +39,9 @@ public class CurrentPolicies implements Subcommand {
public String usage() {
return "";
}
@Override
public String permission() {
return "nemsiseye.policies.list.self";
}
}

View file

@ -20,10 +20,12 @@ public class Help implements Subcommand {
rstr.add("No commands found.");
}
for (Map.Entry<String, Subcommand> e : allSubcommands.entrySet()) {
rstr.add("""
%s - %s
Usage: /eye %s %s
""".formatted(e.getKey(), e.getValue().description(), e.getKey(), e.getValue()));
if (e.getValue().hasPermission(commandSender)) {
rstr.add("""
%s - %s
Usage: /eye %s %s
""".formatted(e.getKey(), e.getValue().description(), e.getKey(), e.getValue().usage()));
}
}
glim.sendMessage(commandSender, String.join("\n", rstr));
@ -44,4 +46,9 @@ public class Help implements Subcommand {
public String usage() {
return "";
}
@Override
public String permission() {
return "nemsiseye.help";
}
}

View file

@ -32,4 +32,9 @@ public class ListPolicies implements Subcommand {
public String usage() {
return "";
}
@Override
public String permission() {
return "nemsiseye.policy.list.all";
}
}

View file

@ -47,4 +47,9 @@ public class PolicyInfo implements Subcommand {
public String usage() {
return "<policy>";
}
@Override
public String permission() {
return "nemsiseye.policy.info";
}
}

View file

@ -1,6 +1,7 @@
package io.github.adrianvic.nemesiseye.commands.sub;
import io.github.adrianvic.nemesiseye.Config;
import io.github.adrianvic.nemesiseye.commands.Commands;
import org.bukkit.command.CommandSender;
import java.util.List;
@ -8,8 +9,8 @@ import java.util.List;
public class Reload implements Subcommand {
@Override
public boolean execute(CommandSender commandSender, String [] strings) {
Config.getInstance().load();
commandSender.sendMessage("Reloading...");
Config.getInstance().load();
commandSender.sendMessage("Reloading...");
return true;
}
@ -18,6 +19,11 @@ public class Reload implements Subcommand {
return List.of();
}
@Override
public String permission() {
return "nemsiseye.reload";
}
@Override
public String description() {
return "Reloads the plugin configuration file.";

View file

@ -1,6 +1,7 @@
package io.github.adrianvic.nemesiseye.commands.sub;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import java.util.List;
@ -10,4 +11,8 @@ public interface Subcommand {
@SuppressWarnings("SameReturnValue")
boolean execute(CommandSender commandSender, String[] strings);
List<String> onTabComplete(CommandSender sender, String[] strings);
String permission();
default boolean hasPermission(CommandSender sender) {
return sender.hasPermission(permission());
}
}

View file

@ -8,3 +8,12 @@ description: "Change what players can do based in custom criteria."
commands:
eye:
usage: "/eye <option> (help for all available options)"
permissions:
nemesiseye.reload:
default: op
nemesiseye.policy.list.all:
default: op
nemesiseye.policy.list.self:
default: true
nemesiseye.help:
default: true