Added permission support for commands.
This commit is contained in:
parent
81bf64c463
commit
708f65fe36
10 changed files with 84 additions and 11 deletions
|
|
@ -5,4 +5,13 @@ author: 'Adrian Victor'
|
||||||
description: "Change what players can do based in custom criteria."
|
description: "Change what players can do based in custom criteria."
|
||||||
commands:
|
commands:
|
||||||
eye:
|
eye:
|
||||||
description: "Run /eye help to see all available commands."
|
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
|
||||||
|
|
@ -1,12 +1,16 @@
|
||||||
package io.github.adrianvic.nemesiseye.commands;
|
package io.github.adrianvic.nemesiseye.commands;
|
||||||
|
|
||||||
|
import io.github.adrianvic.nemesiseye.Nemesis;
|
||||||
import io.github.adrianvic.nemesiseye.commands.sub.*;
|
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.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class Commands {
|
public class Commands {
|
||||||
private static final Map<String, Subcommand> commands = new HashMap<>();
|
private static final Map<String, Subcommand> commands = new HashMap<>();
|
||||||
|
private static final Glimmer glim = Nemesis.getInstance().getGlimmer();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
commands.put("help", new Help());
|
commands.put("help", new Help());
|
||||||
|
|
@ -23,4 +27,9 @@ public class Commands {
|
||||||
public static Subcommand get(String type) {
|
public static Subcommand get(String type) {
|
||||||
return commands.get(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.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,17 +26,30 @@ public class EyeCore {
|
||||||
commandSender.sendMessage("Unknown command, try '/eye help' to list available commands.");
|
commandSender.sendMessage("Unknown command, try '/eye help' to list available commands.");
|
||||||
return true;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String [] strings) {
|
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String [] strings) {
|
||||||
if (strings.length == 1) {
|
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());
|
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 sub.onTabComplete(commandSender, Arrays.copyOfRange(strings, 1, strings.length));
|
||||||
}
|
}
|
||||||
return List.of();
|
return List.of();
|
||||||
|
|
|
||||||
|
|
@ -39,4 +39,9 @@ public class CurrentPolicies implements Subcommand {
|
||||||
public String usage() {
|
public String usage() {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String permission() {
|
||||||
|
return "nemsiseye.policies.list.self";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,12 @@ public class Help implements Subcommand {
|
||||||
rstr.add("No commands found.");
|
rstr.add("No commands found.");
|
||||||
}
|
}
|
||||||
for (Map.Entry<String, Subcommand> e : allSubcommands.entrySet()) {
|
for (Map.Entry<String, Subcommand> e : allSubcommands.entrySet()) {
|
||||||
rstr.add("""
|
if (e.getValue().hasPermission(commandSender)) {
|
||||||
%s - %s
|
rstr.add("""
|
||||||
Usage: /eye %s %s
|
%s - %s
|
||||||
""".formatted(e.getKey(), e.getValue().description(), e.getKey(), e.getValue()));
|
Usage: /eye %s %s
|
||||||
|
""".formatted(e.getKey(), e.getValue().description(), e.getKey(), e.getValue().usage()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
glim.sendMessage(commandSender, String.join("\n", rstr));
|
glim.sendMessage(commandSender, String.join("\n", rstr));
|
||||||
|
|
@ -44,4 +46,9 @@ public class Help implements Subcommand {
|
||||||
public String usage() {
|
public String usage() {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String permission() {
|
||||||
|
return "nemsiseye.help";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,4 +32,9 @@ public class ListPolicies implements Subcommand {
|
||||||
public String usage() {
|
public String usage() {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String permission() {
|
||||||
|
return "nemsiseye.policy.list.all";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,4 +47,9 @@ public class PolicyInfo implements Subcommand {
|
||||||
public String usage() {
|
public String usage() {
|
||||||
return "<policy>";
|
return "<policy>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String permission() {
|
||||||
|
return "nemsiseye.policy.info";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package io.github.adrianvic.nemesiseye.commands.sub;
|
package io.github.adrianvic.nemesiseye.commands.sub;
|
||||||
|
|
||||||
import io.github.adrianvic.nemesiseye.Config;
|
import io.github.adrianvic.nemesiseye.Config;
|
||||||
|
import io.github.adrianvic.nemesiseye.commands.Commands;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -8,8 +9,8 @@ import java.util.List;
|
||||||
public class Reload implements Subcommand {
|
public class Reload implements Subcommand {
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(CommandSender commandSender, String [] strings) {
|
public boolean execute(CommandSender commandSender, String [] strings) {
|
||||||
Config.getInstance().load();
|
Config.getInstance().load();
|
||||||
commandSender.sendMessage("Reloading...");
|
commandSender.sendMessage("Reloading...");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -18,6 +19,11 @@ public class Reload implements Subcommand {
|
||||||
return List.of();
|
return List.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String permission() {
|
||||||
|
return "nemsiseye.reload";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String description() {
|
public String description() {
|
||||||
return "Reloads the plugin configuration file.";
|
return "Reloads the plugin configuration file.";
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package io.github.adrianvic.nemesiseye.commands.sub;
|
package io.github.adrianvic.nemesiseye.commands.sub;
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.permissions.Permission;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -10,4 +11,8 @@ public interface Subcommand {
|
||||||
@SuppressWarnings("SameReturnValue")
|
@SuppressWarnings("SameReturnValue")
|
||||||
boolean execute(CommandSender commandSender, String[] strings);
|
boolean execute(CommandSender commandSender, String[] strings);
|
||||||
List<String> onTabComplete(CommandSender sender, String[] strings);
|
List<String> onTabComplete(CommandSender sender, String[] strings);
|
||||||
|
String permission();
|
||||||
|
default boolean hasPermission(CommandSender sender) {
|
||||||
|
return sender.hasPermission(permission());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -7,4 +7,13 @@ website: "https://github.io/adrianvic/NemesisEye"
|
||||||
description: "Change what players can do based in custom criteria."
|
description: "Change what players can do based in custom criteria."
|
||||||
commands:
|
commands:
|
||||||
eye:
|
eye:
|
||||||
usage: "/eye <option> (help for all available options)"
|
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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue