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

Add luckperms context support for worldgroup. #438

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the API something that has to be enabled by the user? Or is this an actual error?

Just wondering because it might be annoying if someone purposefully disables the API and gets a warning every time they start their server.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If luckperms is installed (checking alr done above), it should always be enabled unless luckperms crashed on their server.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, looks good then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I just took a look at ContextCalculator's Javadoc, do you know if the calculation is thread-safe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's what I read, I was asking if the code you wrote is thread-safe.

Copy link
Member Author

@benwoo1110 benwoo1110 Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worldName = worldName.toLowerCase();
List<WorldGroup> worldGroups = new ArrayList<>();
for (WorldGroup worldGroup : getGroupNames().values()) {
if (worldGroup.containsWorld(worldName)) {
worldGroups.add(worldGroup);
}
}
// Only use the default group for worlds managed by MV-Core
if (worldGroups.isEmpty() && plugin.getMVIConfig().isDefaultingUngroupedWorlds() &&
plugin.getCore().getMVWorldManager().isMVWorld(worldName)) {
Logging.finer("Returning default group for world: " + worldName);
worldGroups.add(getDefaultGroup());
}

hmm yea getGroupsForWorld does have the chance of modifying... I think using getGroups then filter is a safer option since makes a copy

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