From 8d406a75e981ce211eaa81f0a814c8bfb5224ee8 Mon Sep 17 00:00:00 2001 From: Petr Ilin Date: Mon, 23 Jan 2023 23:04:30 +0300 Subject: [PATCH] Ability to deny/allow commands without permission plugins --- README.md | 2 +- VERSION | 2 +- build.gradle | 4 +- .../net/elytrium/limbofilter/Settings.java | 51 ++++++++++++------ .../commands/CommandPermissionState.java | 38 ++++++++++++++ .../commands/LimboFilterCommand.java | 52 +++++++++++-------- .../commands/SendFilterCommand.java | 3 +- 7 files changed, 112 insertions(+), 40 deletions(-) create mode 100644 src/main/java/net/elytrium/limbofilter/commands/CommandPermissionState.java diff --git a/README.md b/README.md index 51cc9c9..f3069e7 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Test server: [``ely.su``](https://hotmc.ru/minecraft-server-203216) - ***limbofilter.commands.sendfilter* | /sendfilter** - Send Player to Filter Command - ***limbofilter.admin.stats* | /lfilter stats** - Plugin Statistics Command - ***limbofilter.admin.reload* | /lfilter reload** - Reload Plugin Command -- ***limboauth.admin.***\* - Gives All Admin Permissions +- ***limbofilter.admin.***\* - Gives All Admin Permissions ## LimboFilter /vs/ popular antibot solutions: diff --git a/VERSION b/VERSION index 524cb55..45a1b3f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.1 +1.1.2 diff --git a/build.gradle b/build.gradle index 5412944..c0649f0 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ plugins { } setGroup("net.elytrium") -setVersion("1.1.1") +setVersion("1.1.2") compileJava { getOptions().setEncoding("UTF-8") @@ -113,4 +113,6 @@ String getCurrentShortRevision() { setStandardOutput(outputStream) } + + return outputStream.toString().trim() } diff --git a/src/main/java/net/elytrium/limbofilter/Settings.java b/src/main/java/net/elytrium/limbofilter/Settings.java index 8c19cb8..cf9e4d8 100644 --- a/src/main/java/net/elytrium/limbofilter/Settings.java +++ b/src/main/java/net/elytrium/limbofilter/Settings.java @@ -23,6 +23,7 @@ import net.elytrium.commons.kyori.serialization.Serializers; import net.elytrium.limboapi.api.chunk.Dimension; import net.elytrium.limboapi.api.player.GameMode; +import net.elytrium.limbofilter.commands.CommandPermissionState; import net.elytrium.limbofilter.handler.BotFilterSessionHandler; public class Settings extends YamlConfig { @@ -327,6 +328,21 @@ public static class OFFSET_1_7 { ) public Dimension BOTFILTER_DIMENSION = Dimension.THE_END; + @Create + public COORDS COORDS; + + public static class COORDS { + + public double CAPTCHA_X = 0; + @Comment("If your server supports Minecraft 1.7, don't set captcha-y to 0. https://media.discordapp.net/attachments/878241549857738793/915165038464098314/unknown.png") + public double CAPTCHA_Y = 0; + public double CAPTCHA_Z = 0; + public double CAPTCHA_YAW = 90; + public double CAPTCHA_PITCH = 38; + public double FALLING_CHECK_YAW = 90; + public double FALLING_CHECK_PITCH = 10; + } + @Create public MAIN.TCP_LISTENER TCP_LISTENER; @@ -361,6 +377,26 @@ public static class TCP_LISTENER { public boolean DEBUG_ON_SUCCESS = false; } + @Create + public MAIN.COMMAND_PERMISSION_STATE COMMAND_PERMISSION_STATE; + + @Comment({ + "Available values: FALSE, TRUE, PERMISSION", + " FALSE - the command will be disallowed", + " TRUE - the command will be allowed if player has false permission state", + " PERMISSION - the command will be allowed if player has true permission state" + }) + public static class COMMAND_PERMISSION_STATE { + @Comment("Permission: limbofilter.admin.sendfilter") + public CommandPermissionState SEND_FILTER = CommandPermissionState.PERMISSION; + @Comment("Permission: limbofilter.admin.reload") + public CommandPermissionState RELOAD = CommandPermissionState.PERMISSION; + @Comment("Permission: limbofilter.admin.stats") + public CommandPermissionState STATS = CommandPermissionState.PERMISSION; + @Comment("Permission: limbofilter.admin.help") + public CommandPermissionState HELP = CommandPermissionState.TRUE; + } + @Create public MAIN.STRINGS STRINGS; @@ -399,20 +435,5 @@ public static class STRINGS { public String CAPTCHA_NOT_READY_YET = "{PRFX} Captcha is not ready yet. Try again in a few seconds"; } - - @Create - public COORDS COORDS; - - public static class COORDS { - - public double CAPTCHA_X = 0; - @Comment("If your server supports Minecraft 1.7, don't set captcha-y to 0. https://media.discordapp.net/attachments/878241549857738793/915165038464098314/unknown.png") - public double CAPTCHA_Y = 0; - public double CAPTCHA_Z = 0; - public double CAPTCHA_YAW = 90; - public double CAPTCHA_PITCH = 38; - public double FALLING_CHECK_YAW = 90; - public double FALLING_CHECK_PITCH = 10; - } } } diff --git a/src/main/java/net/elytrium/limbofilter/commands/CommandPermissionState.java b/src/main/java/net/elytrium/limbofilter/commands/CommandPermissionState.java new file mode 100644 index 0000000..fe777c4 --- /dev/null +++ b/src/main/java/net/elytrium/limbofilter/commands/CommandPermissionState.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2021 - 2023 Elytrium + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package net.elytrium.limbofilter.commands; + +import com.velocitypowered.api.permission.PermissionSubject; +import com.velocitypowered.api.permission.Tristate; +import java.util.function.BiFunction; + +public enum CommandPermissionState { + FALSE((source, permission) -> false), + TRUE((source, permission) -> source.getPermissionValue(permission) != Tristate.FALSE), + PERMISSION(PermissionSubject::hasPermission); + + private final BiFunction hasPermissionFunction; + + CommandPermissionState(BiFunction hasPermissionFunction) { + this.hasPermissionFunction = hasPermissionFunction; + } + + public boolean hasPermission(PermissionSubject permissionSubject, String permission) { + return this.hasPermissionFunction.apply(permissionSubject, permission); + } +} diff --git a/src/main/java/net/elytrium/limbofilter/commands/LimboFilterCommand.java b/src/main/java/net/elytrium/limbofilter/commands/LimboFilterCommand.java index 32ad244..f77258c 100644 --- a/src/main/java/net/elytrium/limbofilter/commands/LimboFilterCommand.java +++ b/src/main/java/net/elytrium/limbofilter/commands/LimboFilterCommand.java @@ -126,6 +126,12 @@ public void execute(SimpleCommand.Invocation invocation) { } } + @Override + public boolean hasPermission(Invocation invocation) { + return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.HELP + .hasPermission(invocation.source(), "limbofilter.commands.help"); + } + private void showHelp(CommandSource source) { HELP_MESSAGE.forEach(source::sendMessage); @@ -157,38 +163,42 @@ private Component createStatsComponent(InetAddress address, long ping) { } private enum Subcommand { - RELOAD("Reload config.", (LimboFilterCommand parent, CommandSource source, String[] args) -> { - parent.plugin.reload(); - source.sendMessage(parent.reloadComponent); - }), - STATS("Enable/Disable statistics of connections and blocked bots.", (LimboFilterCommand parent, CommandSource source, String[] args) -> { - if (source instanceof Player) { - Player player = (Player) source; - UUID playerUuid = player.getUniqueId(); - if (PLAYERS_WITH_STATS.contains(playerUuid)) { - PLAYERS_WITH_STATS.remove(playerUuid); - source.sendMessage(parent.statsDisabledComponent); - } else { - PLAYERS_WITH_STATS.add(playerUuid); - source.sendMessage(parent.statsEnabledComponent); - } - } else { - source.sendMessage(parent.createStatsComponent(null, -1)); - } - }); + RELOAD("Reload config.", Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.RELOAD, + (LimboFilterCommand parent, CommandSource source, String[] args) -> { + parent.plugin.reload(); + source.sendMessage(parent.reloadComponent); + }), + STATS("Enable/Disable statistics of connections and blocked bots.", Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.STATS, + (LimboFilterCommand parent, CommandSource source, String[] args) -> { + if (source instanceof Player) { + Player player = (Player) source; + UUID playerUuid = player.getUniqueId(); + if (PLAYERS_WITH_STATS.contains(playerUuid)) { + PLAYERS_WITH_STATS.remove(playerUuid); + source.sendMessage(parent.statsDisabledComponent); + } else { + PLAYERS_WITH_STATS.add(playerUuid); + source.sendMessage(parent.statsEnabledComponent); + } + } else { + source.sendMessage(parent.createStatsComponent(null, -1)); + } + }); private final String command; private final String description; + private final CommandPermissionState permissionState; private final SubcommandExecutor executor; - Subcommand(String description, SubcommandExecutor executor) { + Subcommand(String description, CommandPermissionState permissionState, SubcommandExecutor executor) { + this.permissionState = permissionState; this.command = this.name().toLowerCase(Locale.ROOT); this.description = description; this.executor = executor; } public boolean hasPermission(CommandSource source) { - return source.hasPermission("limbofilter.admin." + this.command); + return this.permissionState.hasPermission(source, "limbofilter.admin." + this.command); } public Component getMessageLine() { diff --git a/src/main/java/net/elytrium/limbofilter/commands/SendFilterCommand.java b/src/main/java/net/elytrium/limbofilter/commands/SendFilterCommand.java index 88bf3c1..4fa4e17 100644 --- a/src/main/java/net/elytrium/limbofilter/commands/SendFilterCommand.java +++ b/src/main/java/net/elytrium/limbofilter/commands/SendFilterCommand.java @@ -75,6 +75,7 @@ public void execute(SimpleCommand.Invocation invocation) { @Override public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().hasPermission("limbofilter.commands.sendfilter"); + return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.SEND_FILTER + .hasPermission(invocation.source(), "limbofilter.admin.sendfilter"); } }