Skip to content

Commit

Permalink
Add support in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Dec 17, 2021
1 parent 5dccf4b commit 58b1a2b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import xyz.nucleoid.leukocyte.command.argument.ProtectionRuleArgument;
import xyz.nucleoid.leukocyte.command.argument.RoleArgument;
import xyz.nucleoid.leukocyte.command.argument.RuleResultArgument;
import xyz.nucleoid.leukocyte.roles.PermissionAccessor;
import xyz.nucleoid.leukocyte.rule.ProtectionRule;
import xyz.nucleoid.leukocyte.rule.RuleResult;
import xyz.nucleoid.leukocyte.shape.ProtectionShape;
Expand All @@ -41,7 +42,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
// @formatter:off
dispatcher.register(
literal("protect")
.requires(source -> source.hasPermissionLevel(4))
.requires(source -> PermissionAccessor.INSTANCE.hasPermission(source, "leukocyte.commands", 4))
.then(literal("add")
.then(argument("authority", StringArgumentType.string())
.executes(ProtectCommand::addAuthority)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.util.Formatting;
import xyz.nucleoid.leukocyte.Leukocyte;
import xyz.nucleoid.leukocyte.command.argument.AuthorityArgument;
import xyz.nucleoid.leukocyte.roles.PermissionAccessor;
import xyz.nucleoid.leukocyte.shape.ProtectionShape;
import xyz.nucleoid.leukocyte.shape.ShapeBuilder;

Expand All @@ -37,7 +38,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
// @formatter:off
dispatcher.register(
literal("protect")
.requires(source -> source.hasPermissionLevel(4))
.requires(source -> PermissionAccessor.INSTANCE.hasPermission(source, "leukocyte.commands", 4))
.then(literal("shape")
.then(literal("start").executes(ShapeCommand::startShape))
.then(literal("stop").executes(ShapeCommand::stopShape))
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/xyz/nucleoid/leukocyte/roles/PermissionAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;

public interface PermissionAccessor {
PermissionAccessor INSTANCE = FabricLoader.getInstance().isModLoaded("fabric-permissions-api-v0") ? new FabricPermissionsV0() : new None();

boolean hasPermission(ServerPlayerEntity player, String permission);

boolean hasPermission(ServerCommandSource source, String permission, int opLevel);

final class None implements PermissionAccessor {
None() {
}
Expand All @@ -17,6 +20,11 @@ final class None implements PermissionAccessor {
public boolean hasPermission(ServerPlayerEntity player, String permission) {
return false;
}

@Override
public boolean hasPermission(ServerCommandSource source, String permission, int opLevel) {
return source.hasPermissionLevel(opLevel);
}
}

final class FabricPermissionsV0 implements PermissionAccessor {
Expand All @@ -27,5 +35,10 @@ final class FabricPermissionsV0 implements PermissionAccessor {
public boolean hasPermission(ServerPlayerEntity player, String permission) {
return Permissions.check(player, permission);
}

@Override
public boolean hasPermission(ServerCommandSource source, String permission, int opLevel) {
return Permissions.check(source, permission, opLevel);
}
}
}

0 comments on commit 58b1a2b

Please sign in to comment.