add luckperms context
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package sh.okx.rankup.hook;
|
||||
|
||||
import java.util.UUID;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.context.ContextSet;
|
||||
import net.luckperms.api.context.ImmutableContextSet;
|
||||
import net.luckperms.api.model.group.Group;
|
||||
import net.luckperms.api.model.user.User;
|
||||
import net.luckperms.api.node.types.InheritanceNode;
|
||||
|
||||
public class LuckPermsGroupProvider implements GroupProvider {
|
||||
|
||||
private final LuckPerms luckPerms;
|
||||
private final ContextSet contextSet;
|
||||
|
||||
public LuckPermsGroupProvider(LuckPerms luckPerms, ContextSet contextSet) {
|
||||
this.luckPerms = luckPerms;
|
||||
this.contextSet = contextSet;
|
||||
}
|
||||
|
||||
public static LuckPermsGroupProvider createFromString(LuckPerms luckPerms, String context) {
|
||||
try {
|
||||
ImmutableContextSet.Builder builder = ImmutableContextSet.builder();
|
||||
for (String contextPair : context.split(" ")) {
|
||||
String[] keyValue = contextPair.split("=", 2);
|
||||
if (keyValue.length == 2) {
|
||||
builder.add(keyValue[0], keyValue[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return new LuckPermsGroupProvider(luckPerms, builder.build());
|
||||
} catch (NullPointerException | IllegalArgumentException ex) {
|
||||
throw new IllegalArgumentException("Context is invalid: " + context, ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean inGroup(UUID uuid, String group) {
|
||||
User user = luckPerms.getUserManager().getUser(uuid);
|
||||
for (Group lpGroup : user.getInheritedGroups(user.getQueryOptions().toBuilder().context(contextSet).build())) {
|
||||
if (lpGroup.getName().equals(group)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGroup(UUID uuid, String group) {
|
||||
User user = luckPerms.getUserManager().getUser(uuid);
|
||||
user.data().add(InheritanceNode.builder(group).context(contextSet).build());
|
||||
|
||||
luckPerms.getUserManager().saveUser(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeGroup(UUID uuid, String group) {
|
||||
User user = luckPerms.getUserManager().getUser(uuid);
|
||||
user.data().remove(InheritanceNode.builder(group).context(contextSet).build());
|
||||
|
||||
luckPerms.getUserManager().saveUser(user);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package sh.okx.rankup.hook;
|
||||
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import sh.okx.rankup.RankupPlugin;
|
||||
|
||||
@@ -26,6 +28,14 @@ public class VaultPermissionManager implements PermissionManager {
|
||||
if (!provider.hasGroupSupport()) {
|
||||
return null;
|
||||
}
|
||||
String lpContext = plugin.getConfig().getString("luckperms-context");
|
||||
if (lpContext != null && !lpContext.isEmpty()) {
|
||||
RegisteredServiceProvider<LuckPerms> lpProvider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);
|
||||
if (lpProvider != null) {
|
||||
return LuckPermsGroupProvider.createFromString(lpProvider.getProvider(), lpContext);
|
||||
}
|
||||
}
|
||||
|
||||
return new VaultGroupProvider(provider);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,15 @@ notify-update: true
|
||||
# use commands to change a player's group or permission.
|
||||
permission-rankup: false
|
||||
|
||||
# if not empty, these are the contexts to use when modifying groups if LuckPerms is enabled
|
||||
# if empty, this will be based on your LuckPerms config.yml 'use-vault-server' and 'vault-server'
|
||||
# this option will only if permission-rankup is disabled
|
||||
#
|
||||
# luckperms-context: 'server=global' # to make all rankups global
|
||||
# luckperms-context: 'server=survival' # to make all rankups specific to survival
|
||||
# luckperms-context: 'server=survival world=world_nether' # to make all rankups specific to the nether world in survival
|
||||
luckperms-context: ''
|
||||
|
||||
# if players can use /rankup noconfirm to bypass any confirmation
|
||||
# the permission rankup.noconfirm is used for this command, but it is true by default
|
||||
# the console can always do /rankup noconfirm <player>
|
||||
|
||||
@@ -3,7 +3,7 @@ version: "${version}"
|
||||
main: sh.okx.rankup.RankupPlugin
|
||||
author: Okx
|
||||
depend: [Vault]
|
||||
softdepend: [PlaceholderAPI, mcMMO, AdvancedAchievements, Towny, SuperbVote, VotingPlugin]
|
||||
softdepend: [PlaceholderAPI, mcMMO, AdvancedAchievements, Towny, SuperbVote, VotingPlugin, LuckPerms]
|
||||
api-version: 1.13
|
||||
|
||||
commands:
|
||||
|
||||
Reference in New Issue
Block a user