Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permissions API support concept 2 #1864

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/carpet/api/settings/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ public void registerCommand(final CommandDispatcher<CommandSourceStack> dispatch
}

LiteralArgumentBuilder<CommandSourceStack> 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").
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/carpet/api/settings/Validators.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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} </p>
*
* <p>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.</p>
* {@link CommandHelper#hasPermission(CommandSourceStack, String, Object)} is not expected to change anytime soon.</p>
*
*/
public static class CommandLevel extends Validator<String> {
Expand All @@ -32,6 +34,9 @@ public String validate(CommandSourceStack source, CarpetRule<String> 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";}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/carpet/commands/CounterCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +24,7 @@ public class CounterCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> 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())));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carpet/commands/DistanceCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DistanceCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> 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()).
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carpet/commands/DrawCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class DrawCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context)
{
LiteralArgumentBuilder<CommandSourceStack> 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)).
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carpet/commands/InfoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class InfoCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> 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(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carpet/commands/LogCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class LogCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> 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())).
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carpet/commands/MobAICommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MobAICommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> 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)).
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carpet/commands/PerimeterInfoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class PerimeterInfoCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> 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()),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carpet/commands/PlayerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class PlayerCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> 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)))
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carpet/commands/ProfileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ProfileCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> 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)).
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carpet/commands/SpawnCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class SpawnCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> literalargumentbuilder = literal("spawn").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandSpawn));
requires((player) -> CommandHelper.hasPermission(player, "carpet.commands.spawn", CarpetSettings.commandSpawn));

literalargumentbuilder.
then(literal("list").
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/carpet/script/external/Vanilla.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/carpet/utils/CommandHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/carpet/utils/FabricAPIHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -24,21 +27,24 @@ 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 -> {
Version version = m.getMetadata().getVersion();

if (version instanceof SemanticVersion) {
try {
return ((SemanticVersion) version).compareTo(SemanticVersion.parse(minimumVersion)) >= 0;
return version.compareTo(SemanticVersion.parse(minimumVersion)) >= 0;
} catch (VersionParsingException ignored) {
}
}

return false;
}).orElse(false);
}

*/
}