diff options
author | Luck <git@lucko.me> | 2021-12-22 23:32:09 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2021-12-22 23:32:09 +0000 |
commit | 0def5ca6b5c78f7afa7e728129612f5878dd8209 (patch) | |
tree | 5e4c737e7d037bd7e965640af907c7fe585740b6 /spark-forge/src/main/java | |
parent | a7a534cdbb4dd6c8180cf857bbf846ee4c6fc2f8 (diff) | |
download | spark-0def5ca6b5c78f7afa7e728129612f5878dd8209.tar.gz spark-0def5ca6b5c78f7afa7e728129612f5878dd8209.tar.bz2 spark-0def5ca6b5c78f7afa7e728129612f5878dd8209.zip |
Update to new Forge permission API
Diffstat (limited to 'spark-forge/src/main/java')
-rw-r--r-- | spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java index 5c2f6dd..4a2416f 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java @@ -20,6 +20,7 @@ package me.lucko.spark.forge.plugin; +import com.google.common.collect.ImmutableMap; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; @@ -40,19 +41,26 @@ import me.lucko.spark.forge.ForgeTickReporter; import net.minecraft.commands.CommandSource; import net.minecraft.commands.CommandSourceStack; import net.minecraft.server.MinecraftServer; -import net.minecraft.world.entity.player.Player; +import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.event.TickEvent; import net.minecraftforge.event.server.ServerStoppingEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.server.ServerLifecycleHooks; -import net.minecraftforge.server.permission.DefaultPermissionLevel; import net.minecraftforge.server.permission.PermissionAPI; +import net.minecraftforge.server.permission.events.PermissionGatherEvent; +import net.minecraftforge.server.permission.nodes.PermissionNode; +import net.minecraftforge.server.permission.nodes.PermissionNode.PermissionResolver; +import net.minecraftforge.server.permission.nodes.PermissionTypes; import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; +import java.util.stream.Collectors; import java.util.stream.Stream; public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command<CommandSourceStack>, SuggestionProvider<CommandSourceStack> { @@ -67,10 +75,10 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command< // register commands & permissions CommandDispatcher<CommandSourceStack> dispatcher = event.getDispatcher(); registerCommands(dispatcher, plugin, plugin, "spark"); - PermissionAPI.registerNode("spark", DefaultPermissionLevel.OP, "Access to the spark command"); } private final Supplier<MinecraftServer> server; + private Map<String, PermissionNode<Boolean>> registeredPermissions = Collections.emptyMap(); public ForgeServerSparkPlugin(ForgeSparkMod mod, Supplier<MinecraftServer> server) { super(mod); @@ -82,6 +90,28 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command< disable(); } + @SubscribeEvent + public void onPermissionGather(PermissionGatherEvent.Nodes e) { + PermissionResolver<Boolean> defaultValue = (player, playerUUID, context) -> player != null && player.hasPermissions(4); + + // collect all possible permissions + List<String> permissions = this.platform.getCommands().stream() + .map(me.lucko.spark.common.command.Command::primaryAlias) + .collect(Collectors.toList()); + + // special case for the "spark" permission: map it to "spark.all" + permissions.add("all"); + + // register permissions with forge & keep a copy for lookup + ImmutableMap.Builder<String, PermissionNode<Boolean>> builder = ImmutableMap.builder(); + for (String permission : permissions) { + PermissionNode<Boolean> node = new PermissionNode<>("spark", permission, PermissionTypes.BOOLEAN, defaultValue); + e.addNodes(node); + builder.put("spark." + permission, node); + } + this.registeredPermissions = builder.build(); + } + @Override public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException { String[] args = processArgs(context, false); @@ -115,8 +145,16 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command< @Override public boolean hasPermission(CommandSource sender, String permission) { - if (sender instanceof Player) { - return PermissionAPI.hasPermission((Player) sender, permission); + if (sender instanceof ServerPlayer) { + if (permission.equals("spark")) { + permission = "spark.all"; + } + + PermissionNode<Boolean> permissionNode = this.registeredPermissions.get(permission); + if (permissionNode == null) { + throw new IllegalStateException("spark permission not registered: " + permission); + } + return PermissionAPI.getPermission((ServerPlayer) sender, permissionNode); } else { return true; } |