From dc91a6aab5b06d785a6c554753996ffeff694966 Mon Sep 17 00:00:00 2001 From: Kaley Date: Thu, 29 Aug 2024 14:43:28 +0200 Subject: [PATCH] maybe checking command permissions isn't a bad idea lmfao --- .../banmod/core/cmd/BanModCommands.java | 99 +++++++++---------- .../util/Command$Manager$Adapter$Fabric.java | 35 +++++-- 2 files changed, 71 insertions(+), 63 deletions(-) diff --git a/src/core/main/java/com/ampznetwork/banmod/core/cmd/BanModCommands.java b/src/core/main/java/com/ampznetwork/banmod/core/cmd/BanModCommands.java index 03d6f21..8b60976 100644 --- a/src/core/main/java/com/ampznetwork/banmod/core/cmd/BanModCommands.java +++ b/src/core/main/java/com/ampznetwork/banmod/core/cmd/BanModCommands.java @@ -24,35 +24,27 @@ import java.util.Map; import java.util.UUID; -import static com.ampznetwork.banmod.api.model.StandardInfractionFactory.base; -import static java.time.Instant.now; -import static net.kyori.adventure.text.Component.text; -import static net.kyori.adventure.text.event.ClickEvent.openUrl; -import static net.kyori.adventure.text.event.HoverEvent.showText; -import static net.kyori.adventure.text.format.NamedTextColor.AQUA; -import static net.kyori.adventure.text.format.NamedTextColor.DARK_GREEN; -import static net.kyori.adventure.text.format.NamedTextColor.DARK_RED; -import static net.kyori.adventure.text.format.NamedTextColor.GRAY; -import static net.kyori.adventure.text.format.NamedTextColor.GREEN; -import static net.kyori.adventure.text.format.NamedTextColor.RED; -import static net.kyori.adventure.text.format.NamedTextColor.YELLOW; -import static net.kyori.adventure.text.format.TextDecoration.BOLD; -import static net.kyori.adventure.text.format.TextDecoration.UNDERLINED; -import static org.comroid.api.Polyfill.ordinal; -import static org.comroid.api.Polyfill.parseDuration; -import static org.comroid.api.func.util.Command.Arg; +import static com.ampznetwork.banmod.api.model.StandardInfractionFactory.*; +import static java.time.Instant.*; +import static net.kyori.adventure.text.Component.*; +import static net.kyori.adventure.text.event.ClickEvent.*; +import static net.kyori.adventure.text.event.HoverEvent.*; +import static net.kyori.adventure.text.format.NamedTextColor.*; +import static net.kyori.adventure.text.format.TextDecoration.*; +import static org.comroid.api.Polyfill.*; +import static org.comroid.api.func.util.Command.*; @UtilityClass public class BanModCommands { - @Command - public Component reload(BanMod mod) { + @Command(permission = "banmod.reload") + public static Component reload(BanMod mod) { mod.reload(); return text("Configuration reloaded!") .color(GREEN); } - @Command - public Component cleanup(BanMod mod, UUID playerId, @NotNull @Arg(value = "method") CleanupMethod method) { + @Command(permission = "banmod.cleanup") + public static Component cleanup(BanMod mod, UUID playerId, @NotNull @Arg(value = "method") CleanupMethod method) { mod.getPlayerAdapter().send(playerId, text("Starting cleanup process...")); final var service = mod.getEntityService(); var text = text(); @@ -133,8 +125,8 @@ public Component cleanup(BanMod mod, UUID playerId, @NotNull @Arg(value = "metho return text.build(); } - @Command - public Component lookup(BanMod mod, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.Players.class) String name) { + @Command(permission = "banmod.lookup") + public static Component lookup(BanMod mod, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.Players.class) String name) { // todo: use book adapter here var target = mod.getPlayerAdapter().getId(name); var data = mod.getEntityService().getPlayerData(target) @@ -188,8 +180,8 @@ else for (var infraction : infractions) { return text; } - @Command - public Component punish( + @Command(permission = "banmod.punish") + public static Component punish( BanMod mod, UUID issuer, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.Players.class) String name, @@ -212,15 +204,15 @@ public Component punish( return BanMod.Displays.textPunishmentFull(mod, infraction); } - @Command - public Component mutelist( + @Command(permission = "banmod.mutelist") + public static Component mutelist( BanMod mod, @Nullable @Default("1") @Arg(value = "page", required = false, autoFillProvider = AutoFillProvider.PageNumber.class) Integer page ) { return BanMod.Displays.infractionList(mod, page == null ? 1 : page, Punishment.Mute); } - @Command - public Component tempmute( + @Command(permission = "banmod.tempmute") + public static Component tempmute( BanMod mod, UUID issuer, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.Players.class) String name, @@ -240,8 +232,8 @@ public Component tempmute( return BanMod.Displays.textPunishmentFull(mod, infraction); } - @Command - public Component mute( + @Command(permission = "banmod.mute") + public static Component mute( BanMod mod, UUID issuer, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.Players.class) String name, @@ -260,8 +252,8 @@ public Component mute( return BanMod.Displays.textPunishmentFull(mod, infraction); } - @Command - public Component unmute( + @Command(permission = "banmod.unmute") + public static Component unmute( BanMod mod, UUID issuer, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.PlayersByInfractionPunishment.class) String name @@ -278,8 +270,8 @@ public Component unmute( return text("User " + name + " was unmuted").color(GREEN); } - @Command - public Component kick( + @Command(permission = "banmod.kick") + public static Component kick( BanMod mod, UUID issuer, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.Players.class) String name, @@ -297,15 +289,15 @@ public Component kick( return BanMod.Displays.textPunishmentFull(mod, infraction); } - @Command - public Component banlist( + @Command(permission = "banmod.banlist") + public static Component banlist( BanMod mod, @Nullable @Default("1") @Arg(value = "page", required = false, autoFillProvider = AutoFillProvider.PageNumber.class) Integer page ) { return BanMod.Displays.infractionList(mod, page == null ? 1 : page, Punishment.Ban); } - @Command - public Component tempban( + @Command(permission = "banmod.tempban") + public static Component tempban( BanMod mod, UUID issuer, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.Players.class) String name, @@ -326,8 +318,8 @@ public Component tempban( return BanMod.Displays.textPunishmentFull(mod, infraction); } - @Command - public Component ban( + @Command(permission = "banmod.ban") + public static Component ban( BanMod mod, UUID issuer, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.Players.class) String name, @@ -347,8 +339,8 @@ public Component ban( return BanMod.Displays.textPunishmentFull(mod, infraction); } - @Command - public Component unban( + @Command(permission = "banmod.unban") + public static Component unban( BanMod mod, UUID issuer, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.PlayersByInfractionPunishment.class) String name @@ -369,12 +361,12 @@ public enum CleanupMethod implements Named, Bitmask.Attribute { infractions, players, everything } - @Command + @Command(permission = "banmod.infraction") @UtilityClass @Alias("punishment") public class infraction { @Command - public static Component list( + public Component list( BanMod mod, @Nullable @Arg(value = "page", autoFillProvider = AutoFillProvider.PageNumber.class, required = false) Integer page, @Nullable @Arg(value = "query", autoFillProvider = AutoFillProvider.InfractionQuery.class, required = false, stringMode = StringMode.GREEDY) @@ -386,7 +378,7 @@ public static Component list( } @Command - public static Component set( + public Component set( BanMod mod, @NotNull @Arg(value = "query", autoFillProvider = AutoFillProvider.InfractionQuery.class) String query, @NotNull @Arg(value = "property", autoFillProvider = AutoFillProvider.ObjectProperties.class) String propertyName, @@ -396,11 +388,11 @@ public static Component set( } } - @Command + @Command(permission = "banmod.category") @UtilityClass public class category { @Command - public static Component duration( + public Component duration( BanMod mod, @NotNull @Arg(value = "category", autoFillProvider = AutoFillProvider.Categories.class) String categoryName, @NotNull @Arg(value = "player", autoFillProvider = AutoFillProvider.Players.class) String playerName, @@ -423,10 +415,10 @@ public Component list(BanMod mod) { .sorted(Punishment.BY_SEVERITY) .toList(); var fltpd = thresholds.stream() - .filter(e -> !e.getValue().isInherentlyTemporary()) - .mapToInt(Map.Entry::getKey) - .findFirst() - .orElse(0) - 1; + .filter(e -> !e.getValue().isInherentlyTemporary()) + .mapToInt(Map.Entry::getKey) + .findFirst() + .orElse(0) - 1; for (int i = 0; i < thresholds.size(); i++) { var e = thresholds.get(i); var punishment = e.getValue(); @@ -501,7 +493,7 @@ public Component create( } @Command - public static Component set( + public Component set( BanMod mod, @NotNull @Arg(value = "name", autoFillProvider = AutoFillProvider.Categories.class) String categoryName, @NotNull @Arg(value = "property", autoFillProvider = AutoFillProvider.ObjectProperties.class) String propertyName, @@ -525,7 +517,6 @@ public Component delete(BanMod mod, @NotNull @Arg(value = "name", autoFillProvid @UtilityClass @Command(value = "import", permission = "4") public class Import { - @Command public Component vanilla(BanMod mod, UUID playerId, @Default("false") @Arg(value = "cleanup", required = false) boolean cleanup) { mod.getPlayerAdapter().send(playerId, text("Starting import process...")); diff --git a/src/fabric/main/java/org/comroid/api/func/util/Command$Manager$Adapter$Fabric.java b/src/fabric/main/java/org/comroid/api/func/util/Command$Manager$Adapter$Fabric.java index 13b88ab..6769aa6 100644 --- a/src/fabric/main/java/org/comroid/api/func/util/Command$Manager$Adapter$Fabric.java +++ b/src/fabric/main/java/org/comroid/api/func/util/Command$Manager$Adapter$Fabric.java @@ -21,6 +21,7 @@ import lombok.Value; import lombok.experimental.NonFinal; import lombok.extern.slf4j.Slf4j; +import me.lucko.fabric.api.permissions.v0.Permissions; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.kyori.adventure.text.Component; import net.minecraft.command.CommandRegistryAccess; @@ -42,16 +43,12 @@ import java.util.function.Supplier; import java.util.stream.Stream; -import static java.util.function.Predicate.not; -import static java.util.stream.Stream.concat; -import static java.util.stream.Stream.empty; +import static java.util.function.Predicate.*; import static java.util.stream.Stream.of; -import static net.minecraft.server.command.CommandManager.RegistrationEnvironment; -import static net.minecraft.server.command.CommandManager.argument; -import static net.minecraft.server.command.CommandManager.literal; -import static org.comroid.api.func.util.Debug.isDebug; -import static org.comroid.api.func.util.Streams.expand; -import static org.comroid.api.func.util.Streams.expandRecursive; +import static java.util.stream.Stream.*; +import static net.minecraft.server.command.CommandManager.*; +import static org.comroid.api.func.util.Debug.*; +import static org.comroid.api.func.util.Streams.*; @Value @Slf4j @@ -166,6 +163,26 @@ private Stream> convertNode(String p final var base = literal(node.getName()); if (isDebug()) System.out.printf("%s Command '%s'\n", pad, base.getLiteral()); + perm: + if (node instanceof Command.Node.Callable callable) { + var perm = callable.getAttribute().permission(); + + if (Command.EmptyAttribute.equals(perm)) + break perm; + + if (perm.matches("\\d+")) { + // command wants OP level + var lv = Integer.parseInt(perm); + if (isDebug()) System.out.printf("%s-> Requires OP level %d\n", pad, lv); + //Constraint.Range.inside(0, 4, lv, "OP Level").run(); + base.requires(scs -> scs.hasPermissionLevel(lv)); + } else { + // assume permission string + if (isDebug()) System.out.printf("%s-> Requires permission %s\n", pad, perm); + base.requires(Permissions.require(perm, 3)); // op level 3 is minecraft's default for kick and ban + } + } + if (node instanceof Command.Node.Call call) { var parameters = call.getParameters(); if (!parameters.isEmpty()) {