From 1d528220700f918659b17fe201368814af8a9c83 Mon Sep 17 00:00:00 2001 From: benwoo1110 <30431861+benwoo1110@users.noreply.github.com> Date: Tue, 9 Mar 2021 22:50:06 +0800 Subject: [PATCH] Add luckperms context support for worldgroup. --- pom.xml | 6 +++ .../MultiverseInventories.java | 22 +++++++++- .../WorldGroupContextCalculator.java | 41 +++++++++++++++++++ src/main/resources/plugin.yml | 2 +- 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/onarandombox/multiverseinventories/dependencies/WorldGroupContextCalculator.java diff --git a/pom.xml b/pom.xml index 684c814b..6390248e 100644 --- a/pom.xml +++ b/pom.xml @@ -240,6 +240,12 @@ and adjust the build number accordingly --> + + net.luckperms + api + 5.3 + provided + com.onarandombox.multiverseadventure Multiverse-Adventure diff --git a/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java b/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java index bf2949ef..f522dbbb 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java +++ b/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java @@ -5,6 +5,7 @@ import com.onarandombox.MultiverseCore.api.MVPlugin; import com.onarandombox.MultiverseCore.commands.HelpCommand; import com.onarandombox.commandhandler.CommandHandler; +import com.onarandombox.multiverseinventories.dependencies.WorldGroupContextCalculator; import com.onarandombox.multiverseinventories.profile.ProfileDataSource; import com.onarandombox.multiverseinventories.profile.WorldGroupManager; import com.onarandombox.multiverseinventories.profile.container.ContainerType; @@ -30,6 +31,8 @@ import com.onarandombox.multiverseinventories.migration.ImportManager; import com.onarandombox.multiverseinventories.util.Perm; import me.drayshak.WorldInventories.WorldInventories; +import net.luckperms.api.LuckPerms; +import net.luckperms.api.LuckPermsProvider; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -174,8 +177,9 @@ public void onEnable() { // Register Commands this.registerCommands(); - // Hook plugins that can be imported from + // Hook plugins dependencies this.hookImportables(); + this.hookLuckPerms(); Sharables.init(this); @@ -220,6 +224,22 @@ private void hookImportables() { } } + private void hookLuckPerms() { + if (Bukkit.getPluginManager().getPlugin("LuckPerms") == null) { + Logging.finer("LuckPerms is not installed on your server."); + return; + } + LuckPerms luckPerms; + try { + luckPerms = LuckPermsProvider.get(); + } catch (IllegalArgumentException e) { + Logging.warning("Unable to hook into LuckPerms. API not enabled!"); + Logging.warning(e.getMessage()); + return; + } + luckPerms.getContextManager().registerCalculator(new WorldGroupContextCalculator(this)); + } + /** * @return A class used for managing importing data from other similar plugins. */ diff --git a/src/main/java/com/onarandombox/multiverseinventories/dependencies/WorldGroupContextCalculator.java b/src/main/java/com/onarandombox/multiverseinventories/dependencies/WorldGroupContextCalculator.java new file mode 100644 index 00000000..2a6237a4 --- /dev/null +++ b/src/main/java/com/onarandombox/multiverseinventories/dependencies/WorldGroupContextCalculator.java @@ -0,0 +1,41 @@ +package com.onarandombox.multiverseinventories.dependencies; + +import com.onarandombox.multiverseinventories.MultiverseInventories; +import com.onarandombox.multiverseinventories.WorldGroup; +import net.luckperms.api.context.ContextCalculator; +import net.luckperms.api.context.ContextConsumer; +import net.luckperms.api.context.ContextSet; +import net.luckperms.api.context.ImmutableContextSet; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class WorldGroupContextCalculator implements ContextCalculator { + + private static final String WORLD_GROUP_CONTEXT_KEY = "mvinv:worldgroup"; + + private final MultiverseInventories plugin; + + public WorldGroupContextCalculator(MultiverseInventories plugin) { + this.plugin = plugin; + } + + @Override + public void calculate(@NotNull Player player, @NotNull ContextConsumer contextConsumer) { + ImmutableContextSet.Builder contextBuilder = ImmutableContextSet.builder(); + this.plugin.getGroupManager() + .getGroupsForWorld(player.getWorld().getName()) + .forEach(worldGroup -> contextBuilder.add(WORLD_GROUP_CONTEXT_KEY, worldGroup.getName())); + + contextConsumer.accept(contextBuilder.build()); + } + + @Override + public ContextSet estimatePotentialContexts() { + ImmutableContextSet.Builder contextBuilder = ImmutableContextSet.builder(); + this.plugin.getGroupManager() + .getGroups() + .forEach(worldGroup -> contextBuilder.add(WORLD_GROUP_CONTEXT_KEY, worldGroup.getName())); + + return contextBuilder.build(); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e33c2744..7c903ef1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,7 +4,7 @@ version: maven-version-number api-version: 1.13 author: dumptruckman depend: ['Multiverse-Core'] -softdepend: [MultiInv, WorldInventories, Multiverse-Adventure] +softdepend: [LuckPerms, MultiInv, WorldInventories, Multiverse-Adventure] commands: mvinv: