diff --git a/src/main/java/carpet/api/settings/SettingsManager.java b/src/main/java/carpet/api/settings/SettingsManager.java index b8a0430f72..20592a07b2 100644 --- a/src/main/java/carpet/api/settings/SettingsManager.java +++ b/src/main/java/carpet/api/settings/SettingsManager.java @@ -602,7 +602,7 @@ public void registerCommand(final CommandDispatcher dispatch } LiteralArgumentBuilder literalargumentbuilder = literal(identifier).requires((player) -> - CommandHelper.canUseCommand(player, CarpetSettings.carpetCommandPermissionLevel) && !locked()); + CommandHelper.hasPermission(player, identifier + ".commands.settings", CarpetSettings.carpetCommandPermissionLevel) && !locked()); literalargumentbuilder.executes((context)-> listAllSettings(context.getSource())). then(literal("list"). diff --git a/src/main/java/carpet/api/settings/Validators.java b/src/main/java/carpet/api/settings/Validators.java index 67cf099eb3..ab5c925c39 100644 --- a/src/main/java/carpet/api/settings/Validators.java +++ b/src/main/java/carpet/api/settings/Validators.java @@ -3,6 +3,8 @@ import java.util.List; import carpet.utils.CommandHelper; +import carpet.utils.FabricAPIHooks; +import carpet.utils.Messenger; import net.minecraft.commands.CommandSourceStack; /** @@ -20,7 +22,7 @@ public final class Validators { * so either a number from 0 to 4, or one of the keywords {@code true}, {@code false} or {@code ops}

* *

While there is no public API method for checking whether a source can execute a command, - * {@link CommandHelper#canUseCommand(CommandSourceStack, Object)} is not expected to change anytime soon.

+ * {@link CommandHelper#hasPermission(CommandSourceStack, String, Object)} is not expected to change anytime soon.

* */ public static class CommandLevel extends Validator { @@ -32,6 +34,9 @@ public String validate(CommandSourceStack source, CarpetRule currentRule { return null; } + if (FabricAPIHooks.PERMISSIONS_API) { + Messenger.m(source, "g Note that you can use your permission manager mod to control access to commands"); + } return newValue; } @Override public String description() { return "Can be limited to 'ops' only, true/false for everyone/no one, or a custom permission level";} diff --git a/src/main/java/carpet/commands/CounterCommand.java b/src/main/java/carpet/commands/CounterCommand.java index 2868381485..ffaf341f07 100644 --- a/src/main/java/carpet/commands/CounterCommand.java +++ b/src/main/java/carpet/commands/CounterCommand.java @@ -2,6 +2,7 @@ import carpet.CarpetSettings; import carpet.helpers.HopperCounter; +import carpet.utils.CommandHelper; import carpet.utils.Messenger; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.builder.LiteralArgumentBuilder; @@ -23,7 +24,7 @@ public class CounterCommand public static void register(CommandDispatcher dispatcher, CommandBuildContext commandBuildContext) { LiteralArgumentBuilder commandBuilder = literal("counter") - .requires(c -> CarpetSettings.hopperCounters) + .requires(c -> CarpetSettings.hopperCounters && CommandHelper.hasPermission(c, "carpet.commands.counter", true)) .executes(c -> listAllCounters(c.getSource(), false)) .then(literal("reset") .executes(c -> resetCounters(c.getSource()))); diff --git a/src/main/java/carpet/commands/DistanceCommand.java b/src/main/java/carpet/commands/DistanceCommand.java index fe5c2e1b6e..c0eb01a6fb 100644 --- a/src/main/java/carpet/commands/DistanceCommand.java +++ b/src/main/java/carpet/commands/DistanceCommand.java @@ -17,7 +17,7 @@ public class DistanceCommand public static void register(CommandDispatcher dispatcher, CommandBuildContext commandBuildContext) { LiteralArgumentBuilder command = literal("distance"). - requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandDistance)). + requires((player) -> CommandHelper.hasPermission(player, "carpet.commands.distance", CarpetSettings.commandDistance)). then(literal("from"). executes( (c) -> DistanceCalculator.setStart(c.getSource(), c.getSource().getPosition())). then(argument("from", Vec3Argument.vec3()). diff --git a/src/main/java/carpet/commands/DrawCommand.java b/src/main/java/carpet/commands/DrawCommand.java index df2e544c05..e8e9f1be71 100644 --- a/src/main/java/carpet/commands/DrawCommand.java +++ b/src/main/java/carpet/commands/DrawCommand.java @@ -40,7 +40,7 @@ public class DrawCommand public static void register(CommandDispatcher dispatcher, final CommandBuildContext context) { LiteralArgumentBuilder command = literal("draw"). - requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandDraw)). + requires((player) -> CommandHelper.hasPermission(player, "carpet.commands.draw", CarpetSettings.commandDraw)). then(literal("sphere"). then(argument("center", BlockPosArgument.blockPos()). then(argument("radius", IntegerArgumentType.integer(1)). diff --git a/src/main/java/carpet/commands/InfoCommand.java b/src/main/java/carpet/commands/InfoCommand.java index 4e36964c7e..1b9317438d 100644 --- a/src/main/java/carpet/commands/InfoCommand.java +++ b/src/main/java/carpet/commands/InfoCommand.java @@ -27,7 +27,7 @@ public class InfoCommand public static void register(CommandDispatcher dispatcher, CommandBuildContext commandBuildContext) { LiteralArgumentBuilder command = literal("info"). - requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandInfo)). + requires((player) -> CommandHelper.hasPermission(player, "carpet.commands.info", CarpetSettings.commandInfo)). then(literal("block"). then(argument("block position", BlockPosArgument.blockPos()). executes( (c) -> infoBlock( diff --git a/src/main/java/carpet/commands/LogCommand.java b/src/main/java/carpet/commands/LogCommand.java index bd9cd4eade..c597140b31 100644 --- a/src/main/java/carpet/commands/LogCommand.java +++ b/src/main/java/carpet/commands/LogCommand.java @@ -29,7 +29,7 @@ public class LogCommand public static void register(CommandDispatcher dispatcher, CommandBuildContext commandBuildContext) { LiteralArgumentBuilder literalargumentbuilder = Commands.literal("log"). - requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandLog)). + requires((player) -> CommandHelper.hasPermission(player, "carpet.commands.log", CarpetSettings.commandLog)). executes((context) -> listLogs(context.getSource())). then(Commands.literal("clear"). executes( (c) -> unsubFromAll(c.getSource(), c.getSource().getTextName())). diff --git a/src/main/java/carpet/commands/MobAICommand.java b/src/main/java/carpet/commands/MobAICommand.java index 5345583079..048bef42e4 100644 --- a/src/main/java/carpet/commands/MobAICommand.java +++ b/src/main/java/carpet/commands/MobAICommand.java @@ -21,7 +21,7 @@ public class MobAICommand public static void register(CommandDispatcher dispatcher, final CommandBuildContext commandBuildContext) { LiteralArgumentBuilder command = literal("track"). - requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandTrackAI)). + requires((player) -> CommandHelper.hasPermission(player, "carpet.commands.track", CarpetSettings.commandTrackAI)). then(argument("entity type", resource(commandBuildContext, Registries.ENTITY_TYPE)). suggests( (c, b) -> suggest(MobAI.availbleTypes(c.getSource()), b)). diff --git a/src/main/java/carpet/commands/PerimeterInfoCommand.java b/src/main/java/carpet/commands/PerimeterInfoCommand.java index bc4780958b..7436776586 100644 --- a/src/main/java/carpet/commands/PerimeterInfoCommand.java +++ b/src/main/java/carpet/commands/PerimeterInfoCommand.java @@ -27,7 +27,7 @@ public class PerimeterInfoCommand public static void register(CommandDispatcher dispatcher, CommandBuildContext commandBuildContext) { LiteralArgumentBuilder command = literal("perimeterinfo"). - requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandPerimeterInfo)). + requires((player) -> CommandHelper.hasPermission(player, "carpet.commands.perimeterinfo", CarpetSettings.commandPerimeterInfo)). executes( (c) -> perimeterDiagnose( c.getSource(), BlockPos.containing(c.getSource().getPosition()), diff --git a/src/main/java/carpet/commands/PlayerCommand.java b/src/main/java/carpet/commands/PlayerCommand.java index ca62991d86..2b631bc356 100644 --- a/src/main/java/carpet/commands/PlayerCommand.java +++ b/src/main/java/carpet/commands/PlayerCommand.java @@ -50,7 +50,7 @@ public class PlayerCommand public static void register(CommandDispatcher dispatcher, CommandBuildContext commandBuildContext) { LiteralArgumentBuilder command = literal("player") - .requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandPlayer)) + .requires((player) -> CommandHelper.hasPermission(player, "carpet.commands.player", CarpetSettings.commandPlayer)) .then(argument("player", StringArgumentType.word()) .suggests((c, b) -> suggest(getPlayerSuggestions(c.getSource()), b)) .then(literal("stop").executes(manipulation(EntityPlayerActionPack::stopAll))) diff --git a/src/main/java/carpet/commands/ProfileCommand.java b/src/main/java/carpet/commands/ProfileCommand.java index 553f31c8a7..ff6d7f4f66 100644 --- a/src/main/java/carpet/commands/ProfileCommand.java +++ b/src/main/java/carpet/commands/ProfileCommand.java @@ -19,7 +19,7 @@ public class ProfileCommand public static void register(CommandDispatcher dispatcher, CommandBuildContext commandBuildContext) { LiteralArgumentBuilder literalargumentbuilder = literal("profile"). - requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandProfile)). + requires((player) -> CommandHelper.hasPermission(player, "carpet.commands.profile", CarpetSettings.commandProfile)). executes( (c) -> healthReport(c.getSource(), 100)). then(literal("health"). executes( (c) -> healthReport(c.getSource(), 100)). diff --git a/src/main/java/carpet/commands/SpawnCommand.java b/src/main/java/carpet/commands/SpawnCommand.java index e8824aface..f56478bbfa 100644 --- a/src/main/java/carpet/commands/SpawnCommand.java +++ b/src/main/java/carpet/commands/SpawnCommand.java @@ -42,7 +42,7 @@ public class SpawnCommand public static void register(CommandDispatcher dispatcher, CommandBuildContext commandBuildContext) { LiteralArgumentBuilder literalargumentbuilder = literal("spawn"). - requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandSpawn)); + requires((player) -> CommandHelper.hasPermission(player, "carpet.commands.spawn", CarpetSettings.commandSpawn)); literalargumentbuilder. then(literal("list"). diff --git a/src/main/java/carpet/script/external/Vanilla.java b/src/main/java/carpet/script/external/Vanilla.java index 75ee9ab952..aefcd1042f 100644 --- a/src/main/java/carpet/script/external/Vanilla.java +++ b/src/main/java/carpet/script/external/Vanilla.java @@ -336,12 +336,12 @@ public static boolean ScriptServer_scriptDebugging(MinecraftServer server) public static boolean ServerPlayer_canScriptACE(CommandSourceStack player) { - return CommandHelper.canUseCommand(player, CarpetSettings.commandScriptACE); + return CommandHelper.hasPermission(player, "carpet.commands.script.run", CarpetSettings.commandScriptACE); } public static boolean ServerPlayer_canScriptGeneral(CommandSourceStack player) { - return CommandHelper.canUseCommand(player, CarpetSettings.commandScript); + return CommandHelper.hasPermission(player, "carpet.commands.script", CarpetSettings.commandScript); } public static int MinecraftServer_getFillLimit(MinecraftServer server) diff --git a/src/main/java/carpet/utils/CommandHelper.java b/src/main/java/carpet/utils/CommandHelper.java index d6825a914f..513dd90c17 100644 --- a/src/main/java/carpet/utils/CommandHelper.java +++ b/src/main/java/carpet/utils/CommandHelper.java @@ -36,7 +36,18 @@ public static void notifyPlayersCommandsChanged(MinecraftServer server) } })); } - + + /** + * Whether the given source has the given Permissions API permission if the api is installed, or otherwise the given + * permission level as returned by {@link #canUseCommand(CommandSourceStack, Object)} + */ + public static boolean hasPermission(CommandSourceStack source, String permission, Object fallbackLevel) { + if (FabricAPIHooks.PERMISSIONS_API) { + return FabricAPIHooks.checkPermission(source, permission); + } + return canUseCommand(source, fallbackLevel); + } + /** * Whether the given source has enough permission level to run a command that requires the given commandLevel */ diff --git a/src/main/java/carpet/utils/FabricAPIHooks.java b/src/main/java/carpet/utils/FabricAPIHooks.java index 8ece719b54..3e07a3a77b 100644 --- a/src/main/java/carpet/utils/FabricAPIHooks.java +++ b/src/main/java/carpet/utils/FabricAPIHooks.java @@ -3,12 +3,15 @@ import carpet.network.CarpetClient; import com.mojang.blaze3d.systems.RenderSystem; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; +*/ import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.SemanticVersion; import net.fabricmc.loader.api.Version; import net.fabricmc.loader.api.VersionParsingException; -*/ +import net.minecraft.commands.CommandSourceStack; + public class FabricAPIHooks { + public static final boolean PERMISSIONS_API = hasMod("fabric-permissions-api-v0", "0.2"); /* public static final boolean WORLD_RENDER_EVENTS = hasMod("fabric-rendering-v1", "1.5.0"); @@ -24,6 +27,11 @@ public static void initialize() { }); } } +*/ + public static boolean checkPermission(CommandSourceStack source, String perm) { + if (!PERMISSIONS_API) throw new IllegalStateException(); + throw null; // Assume this works for now, just have to get the dep and call a method + } private static boolean hasMod(String id, String minimumVersion) { return FabricLoader.getInstance().getModContainer(id).map(m -> { @@ -31,7 +39,7 @@ private static boolean hasMod(String id, String minimumVersion) { if (version instanceof SemanticVersion) { try { - return ((SemanticVersion) version).compareTo(SemanticVersion.parse(minimumVersion)) >= 0; + return version.compareTo(SemanticVersion.parse(minimumVersion)) >= 0; } catch (VersionParsingException ignored) { } } @@ -39,6 +47,4 @@ private static boolean hasMod(String id, String minimumVersion) { return false; }).orElse(false); } - - */ }