Skip to content

Commit

Permalink
Add luckperms context support for worldgroup.
Browse files Browse the repository at this point in the history
  • Loading branch information
benwoo1110 committed Mar 9, 2021
1 parent 3baac10 commit 1d52822
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ and adjust the build number accordingly -->
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.luckperms</groupId>
<artifactId>api</artifactId>
<version>5.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.onarandombox.multiverseadventure</groupId>
<artifactId>Multiverse-Adventure</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Player> {

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();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1d52822

Please sign in to comment.