From f35b4f1fac5bafb786336c4e11c2a7946700ba33 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Tue, 24 Oct 2023 04:59:10 +0200 Subject: [PATCH] Add own permission for every sub command (#3501) --- .../viaversion/api/command/ViaSubCommand.java | 2 +- bukkit/src/main/resources/plugin.yml | 47 ++++++++++++++++++- .../commands/ViaCommandHandler.java | 18 +++++-- .../commands/defaultsubs/DebugSubCmd.java | 2 +- .../commands/defaultsubs/DontBugMeSubCmd.java | 2 +- .../commands/defaultsubs/HelpSubCmd.java | 40 ---------------- .../commands/defaultsubs/ListSubCmd.java | 2 +- .../commands/defaultsubs/PPSSubCmd.java | 2 +- .../commands/defaultsubs/ReloadSubCmd.java | 2 +- 9 files changed, 66 insertions(+), 51 deletions(-) delete mode 100644 common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/HelpSubCmd.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/command/ViaSubCommand.java b/api/src/main/java/com/viaversion/viaversion/api/command/ViaSubCommand.java index 58d46fa65..29aac6183 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/command/ViaSubCommand.java +++ b/api/src/main/java/com/viaversion/viaversion/api/command/ViaSubCommand.java @@ -58,7 +58,7 @@ public abstract class ViaSubCommand { * @return The permission required to use the commands */ public String permission() { - return "viaversion.admin"; + return "viaversion.admin." + name(); } /** diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index 1767de3df..929d2a151 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -9,6 +9,49 @@ loadbefore: [ ProtocolLib, ProxyPipe, SpigotLib, SkinRestorer ] softdepend: [ ProtocolSupport, PacketListenerApi ] commands: viaversion: - permission: viaversion.admin # The permission is also referenced here to filter root suggestions (/via) description: Shows ViaVersion Version and more. - aliases: [ viaver, vvbukkit ] \ No newline at end of file + aliases: [ viaver, vvbukkit ] + viaversion autoteam: + permission: viaversion.admin.autoteam + description: Toggle automatically teaming to prevent colliding. + aliases: [ autoteam ] + viaversion debug: + permission: viaversion.admin.debug + description: Toggles various debug modes. + aliases: /viaversion debug [clear/logposttransform/add/remove] + viaversion displayleaks: + permission: viaversion.admin.displayleaks + description: Toggles display of memory leaks. + aliases: [ displayleaks ] + viaversion dontbugme: + permission: viaversion.admin.dontbugme + description: Toggle checking for updates. + aliases: [ dontbugme ] + viaversion dump: + permission: viaversion.admin.dump + description: Dump information about your server, this is helpful if you report bugs. + aliases: [ dump ] + viaversion list: + permission: viaversion.admin.list + description: Lists all players with their protocol version. + aliases: [ list ] + viaversion pps: + permission: viaversion.admin.pps + description: Shows the packets per second of online players. + aliases: [ pps ] + viaversion reload: + permission: viaversion.admin.reload + description: Reloads all config files. + aliases: [ reload ] +permissions: + viaversion.admin: + default: op + children: + viaversion.admin.autoteam: true + viaversion.admin.debug: true + viaversion.admin.displayleaks: true + viaversion.admin.dontbugme: true + viaversion.admin.dump: true + viaversion.admin.list: true + viaversion.admin.pps: true + viaversion.admin.reload: true diff --git a/common/src/main/java/com/viaversion/viaversion/commands/ViaCommandHandler.java b/common/src/main/java/com/viaversion/viaversion/commands/ViaCommandHandler.java index 868111179..bf99042af 100644 --- a/common/src/main/java/com/viaversion/viaversion/commands/ViaCommandHandler.java +++ b/common/src/main/java/com/viaversion/viaversion/commands/ViaCommandHandler.java @@ -27,7 +27,6 @@ import com.viaversion.viaversion.commands.defaultsubs.DebugSubCmd; import com.viaversion.viaversion.commands.defaultsubs.DisplayLeaksSubCmd; import com.viaversion.viaversion.commands.defaultsubs.DontBugMeSubCmd; import com.viaversion.viaversion.commands.defaultsubs.DumpSubCmd; -import com.viaversion.viaversion.commands.defaultsubs.HelpSubCmd; import com.viaversion.viaversion.commands.defaultsubs.ListSubCmd; import com.viaversion.viaversion.commands.defaultsubs.PPSSubCmd; import com.viaversion.viaversion.commands.defaultsubs.ReloadSubCmd; @@ -40,6 +39,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; import static com.viaversion.viaversion.api.command.ViaSubCommand.color; @@ -69,6 +69,19 @@ public abstract class ViaCommandHandler implements ViaVersionCommand { @Override public boolean onCommand(ViaCommandSender sender, String[] args) { + boolean hasPermissions = sender.hasPermission("viaversion.admin"); + for (ViaSubCommand command : commandMap.values()) { + if (sender.hasPermission(command.permission())) { + hasPermissions = true; + break; + } + } + + if (!hasPermissions) { + sender.sendMessage(color("&cYou are not allowed to use this command!")); + return false; + } + if (args.length == 0) { showHelp(sender); return false; @@ -170,7 +183,7 @@ public abstract class ViaCommandHandler implements ViaVersionCommand { } private boolean hasPermission(ViaCommandSender sender, String permission) { - return permission == null || sender.hasPermission(permission); + return permission == null || sender.hasPermission("viaversion.admin") || sender.hasPermission(permission); } private void registerDefaults() { @@ -181,7 +194,6 @@ public abstract class ViaCommandHandler implements ViaVersionCommand { registerSubCommand(new DisplayLeaksSubCmd()); registerSubCommand(new DontBugMeSubCmd()); registerSubCommand(new AutoTeamSubCmd()); - registerSubCommand(new HelpSubCmd()); registerSubCommand(new ReloadSubCmd()); } } diff --git a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/DebugSubCmd.java b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/DebugSubCmd.java index 95c20c9ff..266ba3d66 100644 --- a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/DebugSubCmd.java +++ b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/DebugSubCmd.java @@ -34,7 +34,7 @@ public class DebugSubCmd extends ViaSubCommand { @Override public String description() { - return "Toggle debug mode"; + return "Toggle various debug modes."; } @Override diff --git a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/DontBugMeSubCmd.java b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/DontBugMeSubCmd.java index 284b2ac61..cf4600cfa 100644 --- a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/DontBugMeSubCmd.java +++ b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/DontBugMeSubCmd.java @@ -31,7 +31,7 @@ public class DontBugMeSubCmd extends ViaSubCommand { @Override public String description() { - return "Toggle checking for updates"; + return "Toggle checking for updates."; } @Override diff --git a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/HelpSubCmd.java b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/HelpSubCmd.java deleted file mode 100644 index c9d95b0e7..000000000 --- a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/HelpSubCmd.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2023 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.commands.defaultsubs; - -import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.api.command.ViaSubCommand; - -public class HelpSubCmd extends ViaSubCommand { - @Override - public String name() { - return "help"; - } - - @Override - public String description() { - return "You are looking at it right now!"; - } - - @Override - public boolean execute(ViaCommandSender sender, String[] args) { - Via.getManager().getCommandHandler().showHelp(sender); - return true; - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/ListSubCmd.java b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/ListSubCmd.java index ac7f41f28..7eeb450f7 100644 --- a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/ListSubCmd.java +++ b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/ListSubCmd.java @@ -34,7 +34,7 @@ public class ListSubCmd extends ViaSubCommand { @Override public String description() { - return "Shows lists of the versions from logged in players"; + return "Shows lists of the versions from logged in players."; } @Override diff --git a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/PPSSubCmd.java b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/PPSSubCmd.java index 84fed540f..309fe8f12 100644 --- a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/PPSSubCmd.java +++ b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/PPSSubCmd.java @@ -36,7 +36,7 @@ public class PPSSubCmd extends ViaSubCommand { @Override public String description() { - return "Shows the packets per second of online players"; + return "Shows the packets per second of online players."; } @Override diff --git a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/ReloadSubCmd.java b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/ReloadSubCmd.java index 919f2db16..0f7057aab 100644 --- a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/ReloadSubCmd.java +++ b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/ReloadSubCmd.java @@ -29,7 +29,7 @@ public class ReloadSubCmd extends ViaSubCommand { @Override public String description() { - return "Reload the config from the disk"; + return "Reload the config from the disk."; } @Override