Permission Checker

This commit is contained in:
Muhammad Tamir
2025-06-20 11:09:59 +07:00
parent 634b8fa31b
commit d919ac0db8
2 changed files with 53 additions and 13 deletions

View File

@@ -23,23 +23,24 @@ public class SubcommandHandler {
private final Map<String, SubcommandExecutor> commands = new HashMap<>(); private final Map<String, SubcommandExecutor> commands = new HashMap<>();
private final String filePassword; private final String filePassword;
private final String dbPassword; private final String dbPassword;
private final Map<String, String> permissions = new HashMap<>();
public SubcommandHandler(JavaPlugin plugin, String filePassword, String dbPassword) { public SubcommandHandler(JavaPlugin plugin, String filePassword, String dbPassword) {
this.filePassword = filePassword; this.filePassword = filePassword;
this.dbPassword = dbPassword; this.dbPassword = dbPassword;
commands.put("add", new GitAddSubcommand(plugin)); register("add", new GitAddSubcommand(plugin));
commands.put("commit", new GitCommitSubcommand(plugin)); register("commit", new GitCommitSubcommand(plugin));
commands.put("reset", new GitResetSubcommand(plugin)); register("reset", new GitResetSubcommand(plugin));
commands.put("init", new GitInitSubcommand(plugin)); register("init", new GitInitSubcommand(plugin));
commands.put("remote", new GitRemoteSubcommand(plugin)); register("remote", new GitRemoteSubcommand(plugin));
commands.put("status", new GitStatusSubcommand(plugin)); register("status", new GitStatusSubcommand(plugin));
commands.put("help", new GitHelpSubcommand(plugin)); register("help", new GitHelpSubcommand(plugin));
commands.put("push", new GitPushSubcommand(plugin, filePassword, dbPassword)); register("push", new GitPushSubcommand(plugin, filePassword, dbPassword));
commands.put("fetch", new GitFetchSubcommand(plugin, filePassword, dbPassword)); register("fetch", new GitFetchSubcommand(plugin, filePassword, dbPassword));
commands.put("pull", new GitPullSubcommand(plugin, filePassword, dbPassword)); register("pull", new GitPullSubcommand(plugin, filePassword, dbPassword));
commands.put("login", new GitLoginSubcommand(plugin, filePassword, dbPassword)); register("login", new GitLoginSubcommand(plugin, filePassword, dbPassword));
commands.put("whoami", new GitWhoamiSubcommand(plugin, filePassword, dbPassword)); register("whoami", new GitWhoamiSubcommand(plugin, filePassword, dbPassword));
commands.put("logout", new GitLogoutSubcommand(plugin, filePassword, dbPassword)); register("logout", new GitLogoutSubcommand(plugin, filePassword, dbPassword));
} }
public void handle(CommandSender sender, String name, String[] args) { public void handle(CommandSender sender, String name, String[] args) {
@@ -49,6 +50,17 @@ public class SubcommandHandler {
return; return;
} }
String permission = permissions.get(name.toLowerCase());
if (permission != null && !sender.hasPermission(permission)) {
sender.sendMessage("§cYou do not have permission to run /git " + name);
return;
}
executor.execute(sender, args); executor.execute(sender, args);
} }
private void register(String name, SubcommandExecutor executor) {
commands.put(name, executor);
permissions.put(name, "gitcraft.command." + name);
}
} }

View File

@@ -8,3 +8,31 @@ commands:
git: git:
description: Git subcommand dispatcher description: Git subcommand dispatcher
usage: /git <add|commit|push|clone> usage: /git <add|commit|push|clone>
permissions:
gitcraft.command.add:
default: op
gitcraft.command.commit:
default: op
gitcraft.command.reset:
default: op
gitcraft.command.init:
default: op
gitcraft.command.remote:
default: op
gitcraft.command.status:
default: op
gitcraft.command.help:
default: op
gitcraft.command.push:
default: op
gitcraft.command.fetch:
default: op
gitcraft.command.pull:
default: op
gitcraft.command.login:
default: op
gitcraft.command.whoami:
default: op
gitcraft.command.logout:
default: op