mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2024-12-28 17:37:52 +01:00
Some fixes, improvements & annotations for group sync
This commit is contained in:
parent
14d7b0eed7
commit
51fd9ef6d8
@ -174,9 +174,8 @@ public class BukkitDiscordSRV extends ServerDiscordSRV<BukkitConfig, BukkitConne
|
||||
// Service provider
|
||||
server().getServicesManager().register(DiscordSRVApi.class, this, plugin(), ServicePriority.Normal);
|
||||
|
||||
// Adventure audiences, connection listener
|
||||
// Adventure audiences
|
||||
this.audiences = BukkitAudiences.create(bootstrap.getPlugin());
|
||||
server().getPluginManager().registerEvents(new BukkitConnectionListener(this), plugin());
|
||||
|
||||
// Command handler
|
||||
commandHandler = AbstractBukkitCommandHandler.get(this);
|
||||
@ -193,6 +192,9 @@ public class BukkitDiscordSRV extends ServerDiscordSRV<BukkitConfig, BukkitConne
|
||||
registerIntegration("com.discordsrv.bukkit.integration.VaultIntegration");
|
||||
|
||||
super.enable();
|
||||
|
||||
// Connection listener
|
||||
server().getPluginManager().registerEvents(new BukkitConnectionListener(this), plugin());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.ServicesManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -137,7 +138,7 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Boolean> hasGroup(UUID player, String groupName, boolean includeInherited) {
|
||||
public CompletableFuture<Boolean> hasGroup(@NotNull UUID player, @NotNull String groupName, boolean includeInherited) {
|
||||
if (permission == null || !permission.isEnabled() || !permission.hasGroupSupport()) {
|
||||
return unsupported(permission);
|
||||
}
|
||||
@ -149,7 +150,7 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> addGroup(UUID player, String groupName) {
|
||||
public CompletableFuture<Void> addGroup(@NotNull UUID player, @NotNull String groupName) {
|
||||
if (permission == null || !permission.isEnabled() || !permission.hasGroupSupport()) {
|
||||
return unsupported(permission);
|
||||
}
|
||||
@ -162,7 +163,7 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> removeGroup(UUID player, String groupName) {
|
||||
public CompletableFuture<Void> removeGroup(@NotNull UUID player, @NotNull String groupName) {
|
||||
if (permission == null || !permission.isEnabled() || !permission.hasGroupSupport()) {
|
||||
return unsupported(permission);
|
||||
}
|
||||
@ -175,7 +176,7 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Boolean> hasPermission(UUID player, String permissionNode) {
|
||||
public CompletableFuture<Boolean> hasPermission(@NotNull UUID player, @NotNull String permissionNode) {
|
||||
if (permission == null || !permission.isEnabled()) {
|
||||
return unsupported(permission);
|
||||
}
|
||||
@ -187,7 +188,7 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> getPrefix(UUID player) {
|
||||
public CompletableFuture<String> getPrefix(@NotNull UUID player) {
|
||||
if (chat == null || !chat.isEnabled()) {
|
||||
return unsupported(chat);
|
||||
}
|
||||
@ -199,7 +200,7 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> getSuffix(UUID player) {
|
||||
public CompletableFuture<String> getSuffix(@NotNull UUID player) {
|
||||
if (chat == null || !chat.isEnabled()) {
|
||||
return unsupported(chat);
|
||||
}
|
||||
|
@ -323,9 +323,10 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
|
||||
Object module;
|
||||
try {
|
||||
Class<?> clazz = Class.forName(className);
|
||||
Constructor<?> constructor = clazz.getConstructor(getClass());
|
||||
Constructor<?> constructor = clazz.getConstructors()[0];
|
||||
module = constructor.newInstance(this);
|
||||
} catch (Throwable ignored) {
|
||||
} catch (Throwable e) {
|
||||
moduleManager.logger().debug("Failed to load integration: " + className, e);
|
||||
return;
|
||||
}
|
||||
moduleManager.registerModule(this, d -> (AbstractModule<?>) module);
|
||||
|
@ -201,7 +201,7 @@ public class GroupSyncModule extends AbstractModule<DiscordSRV> {
|
||||
PermissionDataProvider.Groups permissionProvider = getPermissionProvider();
|
||||
if (permissionProvider instanceof PermissionDataProvider.GroupsContext) {
|
||||
return ((PermissionDataProvider.GroupsContext) permissionProvider)
|
||||
.hasGroup(player, groupName, false, serverContext);
|
||||
.hasGroup(player, groupName, false, serverContext != null ? Collections.singleton(serverContext) : null);
|
||||
} else {
|
||||
return permissionProvider.hasGroup(player, groupName, false);
|
||||
}
|
||||
@ -215,7 +215,7 @@ public class GroupSyncModule extends AbstractModule<DiscordSRV> {
|
||||
PermissionDataProvider.Groups permissionProvider = getPermissionProvider();
|
||||
if (permissionProvider instanceof PermissionDataProvider.GroupsContext) {
|
||||
return ((PermissionDataProvider.GroupsContext) permissionProvider)
|
||||
.addGroup(player, groupName, serverContext);
|
||||
.addGroup(player, groupName, Collections.singleton(serverContext));
|
||||
} else {
|
||||
return permissionProvider.addGroup(player, groupName);
|
||||
}
|
||||
@ -229,7 +229,7 @@ public class GroupSyncModule extends AbstractModule<DiscordSRV> {
|
||||
PermissionDataProvider.Groups permissionProvider = getPermissionProvider();
|
||||
if (permissionProvider instanceof PermissionDataProvider.GroupsContext) {
|
||||
return ((PermissionDataProvider.GroupsContext) permissionProvider)
|
||||
.removeGroup(player, groupName, serverContext);
|
||||
.removeGroup(player, groupName, Collections.singleton(serverContext));
|
||||
} else {
|
||||
return permissionProvider.removeGroup(player, groupName);
|
||||
}
|
||||
|
@ -47,6 +47,8 @@ import net.luckperms.api.node.NodeType;
|
||||
import net.luckperms.api.node.types.InheritanceNode;
|
||||
import net.luckperms.api.query.QueryMode;
|
||||
import net.luckperms.api.query.QueryOptions;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@ -109,13 +111,15 @@ public class LuckPermsIntegration extends PluginIntegration<DiscordSRV> implemen
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Boolean> hasGroup(UUID player, String groupName, boolean includeInherited, String serverContext) {
|
||||
public CompletableFuture<Boolean> hasGroup(@NotNull UUID player, @NotNull String groupName, boolean includeInherited, @Nullable Set<String> serverContext) {
|
||||
return user(player).thenApply(user -> {
|
||||
MutableContextSet context = luckPerms.getContextManager().getStaticContext().mutableCopy();
|
||||
if (serverContext != null) {
|
||||
context.removeAll(DefaultContextKeys.SERVER_KEY);
|
||||
if (!serverContext.equals("global")) {
|
||||
context.add(DefaultContextKeys.SERVER_KEY, serverContext);
|
||||
if (isNotGlobalOnly(serverContext)) {
|
||||
for (String ctx : serverContext) {
|
||||
context.add(DefaultContextKeys.SERVER_KEY, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,23 +130,23 @@ public class LuckPermsIntegration extends PluginIntegration<DiscordSRV> implemen
|
||||
.map(Group::getName)
|
||||
: user.getNodes(NodeType.INHERITANCE)
|
||||
.stream()
|
||||
.filter(node -> node.getContexts().isSatisfiedBy(context))
|
||||
.filter(node -> context.isSatisfiedBy(node.getContexts()))
|
||||
.map(InheritanceNode::getGroupName)
|
||||
).anyMatch(name -> name.equalsIgnoreCase(groupName));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> addGroup(UUID player, String groupName, String serverContext) {
|
||||
public CompletableFuture<Void> addGroup(@NotNull UUID player, @NotNull String groupName, @Nullable Set<String> serverContext) {
|
||||
return groupMutate(player, groupName, serverContext, NodeMap::add);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> removeGroup(UUID player, String groupName, String serverContext) {
|
||||
public CompletableFuture<Void> removeGroup(@NotNull UUID player, @NotNull String groupName, @Nullable Set<String> serverContext) {
|
||||
return groupMutate(player, groupName, serverContext, NodeMap::remove);
|
||||
}
|
||||
|
||||
private CompletableFuture<Void> groupMutate(UUID player, String groupName, String serverContext, BiFunction<NodeMap, Node, DataMutateResult> function) {
|
||||
private CompletableFuture<Void> groupMutate(UUID player, String groupName, Set<String> serverContext, BiFunction<NodeMap, Node, DataMutateResult> function) {
|
||||
Group group = luckPerms.getGroupManager().getGroup(groupName);
|
||||
if (group == null) {
|
||||
return CompletableFutureUtil.failed(new MessageException("Group does not exist"));
|
||||
@ -150,8 +154,12 @@ public class LuckPermsIntegration extends PluginIntegration<DiscordSRV> implemen
|
||||
return user(player).thenCompose(user -> {
|
||||
ContextSet contexts;
|
||||
if (serverContext != null) {
|
||||
if (!serverContext.equals("global")) {
|
||||
contexts = ImmutableContextSet.of(DefaultContextKeys.SERVER_KEY, serverContext);
|
||||
if (isNotGlobalOnly(serverContext)) {
|
||||
ImmutableContextSet.Builder builder = ImmutableContextSet.builder();
|
||||
for (String ctx : serverContext) {
|
||||
builder.add(DefaultContextKeys.SERVER_KEY, ctx);
|
||||
}
|
||||
contexts = builder.build();
|
||||
} else {
|
||||
contexts = ImmutableContextSet.empty();
|
||||
}
|
||||
@ -173,24 +181,28 @@ public class LuckPermsIntegration extends PluginIntegration<DiscordSRV> implemen
|
||||
});
|
||||
}
|
||||
|
||||
private boolean isNotGlobalOnly(Set<String> context) {
|
||||
return context.size() != 1 || !context.iterator().next().equals("global");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Boolean> hasPermission(UUID player, String permission) {
|
||||
public CompletableFuture<Boolean> hasPermission(@NotNull UUID player, @NotNull String permission) {
|
||||
return user(player).thenApply(
|
||||
user -> user.getCachedData().getPermissionData().checkPermission(permission).asBoolean());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> getPrefix(UUID player) {
|
||||
public CompletableFuture<String> getPrefix(@NotNull UUID player) {
|
||||
return user(player).thenApply(user -> user.getCachedData().getMetaData().getPrefix());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> getSuffix(UUID player) {
|
||||
public CompletableFuture<String> getSuffix(@NotNull UUID player) {
|
||||
return user(player).thenApply(user -> user.getCachedData().getMetaData().getSuffix());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> getMeta(UUID player, String key) throws UnsupportedOperationException {
|
||||
public CompletableFuture<String> getMeta(@NotNull UUID player, @NotNull String key) throws UnsupportedOperationException {
|
||||
return user(player).thenApply(user -> user.getCachedData().getMetaData().getMetaValue(key));
|
||||
}
|
||||
|
||||
|
@ -62,6 +62,9 @@ public class ModuleManager {
|
||||
Module bestCandidate = null;
|
||||
int bestCandidatePriority = Integer.MIN_VALUE;
|
||||
for (Module module : modules) {
|
||||
if (!module.isEnabled()) {
|
||||
continue;
|
||||
}
|
||||
int priority;
|
||||
if (moduleType.isAssignableFrom(module.getClass()) && ((priority = module.priority(moduleType)) > bestCandidatePriority)) {
|
||||
bestCandidate = module;
|
||||
|
@ -19,6 +19,7 @@
|
||||
package com.discordsrv.common.module.type;
|
||||
|
||||
import com.discordsrv.api.module.type.Module;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
@ -35,43 +36,43 @@ public interface PermissionDataProvider extends Module {
|
||||
|
||||
interface Groups extends PermissionDataProvider {
|
||||
List<String> getGroups();
|
||||
CompletableFuture<Boolean> hasGroup(UUID player, String groupName, boolean includeInherited);
|
||||
CompletableFuture<Void> addGroup(UUID player, String groupName);
|
||||
CompletableFuture<Void> removeGroup(UUID player, String groupName);
|
||||
CompletableFuture<Boolean> hasGroup(@NotNull UUID player, @NotNull String groupName, boolean includeInherited);
|
||||
CompletableFuture<Void> addGroup(@NotNull UUID player, @NotNull String groupName);
|
||||
CompletableFuture<Void> removeGroup(@NotNull UUID player, @NotNull String groupName);
|
||||
}
|
||||
|
||||
interface Permissions extends PermissionDataProvider {
|
||||
CompletableFuture<Boolean> hasPermission(UUID player, String permission);
|
||||
CompletableFuture<Boolean> hasPermission(@NotNull UUID player, @NotNull String permission);
|
||||
}
|
||||
|
||||
interface PrefixAndSuffix extends PermissionDataProvider {
|
||||
CompletableFuture<String> getPrefix(UUID player);
|
||||
CompletableFuture<String> getSuffix(UUID player);
|
||||
CompletableFuture<String> getPrefix(@NotNull UUID player);
|
||||
CompletableFuture<String> getSuffix(@NotNull UUID player);
|
||||
}
|
||||
|
||||
interface Meta extends PermissionDataProvider {
|
||||
CompletableFuture<String> getMeta(UUID player, String key);
|
||||
CompletableFuture<String> getMeta(@NotNull UUID player, @NotNull String key);
|
||||
}
|
||||
|
||||
interface GroupsContext extends Groups {
|
||||
|
||||
Set<String> getDefaultServerContext();
|
||||
CompletableFuture<Boolean> hasGroup(UUID player, String groupName, boolean includeInherited, @Nullable String serverContext);
|
||||
CompletableFuture<Void> addGroup(UUID player, String groupName, @Nullable String serverContext);
|
||||
CompletableFuture<Void> removeGroup(UUID player, String groupName, @Nullable String serverContext);
|
||||
CompletableFuture<Boolean> hasGroup(@NotNull UUID player, @NotNull String groupName, boolean includeInherited, @Nullable Set<String> serverContext);
|
||||
CompletableFuture<Void> addGroup(@NotNull UUID player, @NotNull String groupName, @Nullable Set<String> serverContext);
|
||||
CompletableFuture<Void> removeGroup(@NotNull UUID player, @NotNull String groupName, @Nullable Set<String> serverContext);
|
||||
|
||||
@Override
|
||||
default CompletableFuture<Boolean> hasGroup(UUID player, String groupName, boolean includeInherited) {
|
||||
return hasGroup(player, groupName, includeInherited, null);
|
||||
default CompletableFuture<Boolean> hasGroup(@NotNull UUID player, @NotNull String groupName, boolean includeInherited) {
|
||||
return hasGroup(player, groupName, includeInherited, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
default CompletableFuture<Void> addGroup(UUID player, String groupName) {
|
||||
default CompletableFuture<Void> addGroup(@NotNull UUID player, @NotNull String groupName) {
|
||||
return addGroup(player, groupName, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
default CompletableFuture<Void> removeGroup(UUID player, String groupName) {
|
||||
default CompletableFuture<Void> removeGroup(@NotNull UUID player, @NotNull String groupName) {
|
||||
return removeGroup(player, groupName, null);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user