mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2024-12-28 17:37:52 +01:00
Split out the PermissionDataProvider into smaller parts, fix some generic use in ModuleManager, make module priorities more flexible
This commit is contained in:
parent
8fdd5e73e3
commit
0f5d36b407
@ -31,7 +31,10 @@ import org.bukkit.plugin.ServicesManager;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implements PermissionDataProvider {
|
||||
public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV>
|
||||
implements PermissionDataProvider.Permissions,
|
||||
PermissionDataProvider.Groups,
|
||||
PermissionDataProvider.PrefixAndSuffix {
|
||||
|
||||
private Permission permission;
|
||||
private Chat chat;
|
||||
@ -41,7 +44,7 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public int priority() {
|
||||
public int priority(Class<?> type) {
|
||||
// Lower priority than default
|
||||
return -1;
|
||||
}
|
||||
@ -179,10 +182,4 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
|
||||
return chat.getPlayerSuffix(null, offlinePlayer);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> getMeta(UUID player, String key) throws UnsupportedOperationException {
|
||||
// :(
|
||||
throw new UnsupportedOperationException("Vault does not support this operation");
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class LuckPermsIntegration extends PluginIntegration<DiscordSRV> implements PermissionDataProvider {
|
||||
public class LuckPermsIntegration extends PluginIntegration<DiscordSRV> implements PermissionDataProvider.All {
|
||||
|
||||
private LuckPerms luckPerms;
|
||||
|
||||
|
@ -33,8 +33,8 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
public class ModuleManager {
|
||||
|
||||
private final Set<AbstractModule> modules = new CopyOnWriteArraySet<>();
|
||||
private final Map<String, AbstractModule> moduleLookupTable = new ConcurrentHashMap<>();
|
||||
private final Set<AbstractModule<?>> modules = new CopyOnWriteArraySet<>();
|
||||
private final Map<String, AbstractModule<?>> moduleLookupTable = new ConcurrentHashMap<>();
|
||||
private final DiscordSRV discordSRV;
|
||||
|
||||
public ModuleManager(DiscordSRV discordSRV) {
|
||||
@ -44,25 +44,27 @@ public class ModuleManager {
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Module> T getModule(Class<T> moduleType) {
|
||||
return (T) moduleLookupTable.computeIfAbsent(moduleType.getName(), key -> {
|
||||
AbstractModule bestCandidate = null;
|
||||
for (AbstractModule module : modules) {
|
||||
if (moduleType.isAssignableFrom(module.getClass())
|
||||
&& (bestCandidate == null || module.priority() > bestCandidate.priority())) {
|
||||
AbstractModule<?> bestCandidate = null;
|
||||
int bestCandidatePriority = Integer.MIN_VALUE;
|
||||
for (AbstractModule<?> module : modules) {
|
||||
int priority;
|
||||
if (moduleType.isAssignableFrom(module.getClass()) && ((priority = module.priority(moduleType)) > bestCandidatePriority)) {
|
||||
bestCandidate = module;
|
||||
bestCandidatePriority = priority;
|
||||
}
|
||||
}
|
||||
return bestCandidate;
|
||||
});
|
||||
}
|
||||
|
||||
public void register(AbstractModule module) {
|
||||
public void register(AbstractModule<?> module) {
|
||||
this.modules.add(module);
|
||||
this.moduleLookupTable.put(module.getClass().getName(), module);
|
||||
|
||||
enable(module);
|
||||
}
|
||||
|
||||
private void enable(AbstractModule module) {
|
||||
private void enable(AbstractModule<?> module) {
|
||||
try {
|
||||
module.enableModule();
|
||||
} catch (Throwable t) {
|
||||
@ -70,14 +72,14 @@ public class ModuleManager {
|
||||
}
|
||||
}
|
||||
|
||||
public void unregister(AbstractModule module) {
|
||||
public void unregister(AbstractModule<?> module) {
|
||||
this.modules.remove(module);
|
||||
this.moduleLookupTable.values().removeIf(mod -> mod == module);
|
||||
|
||||
disable(module);
|
||||
}
|
||||
|
||||
private void disable(AbstractModule module) {
|
||||
private void disable(AbstractModule<?> module) {
|
||||
try {
|
||||
module.disable();
|
||||
} catch (Throwable t) {
|
||||
@ -87,14 +89,14 @@ public class ModuleManager {
|
||||
|
||||
@Subscribe(priority = EventPriority.EARLY)
|
||||
public void onShuttingDown(DiscordSRVShuttingDownEvent event) {
|
||||
for (AbstractModule module : modules) {
|
||||
for (AbstractModule<?> module : modules) {
|
||||
unregister(module);
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(priority = EventPriority.EARLY)
|
||||
public void onReload(DiscordSRVReloadEvent event) {
|
||||
for (AbstractModule module : modules) {
|
||||
for (AbstractModule<?> module : modules) {
|
||||
// Check if the module needs to be enabled due to reload
|
||||
enable(module);
|
||||
|
||||
|
@ -24,7 +24,13 @@ public interface Module {
|
||||
return true;
|
||||
}
|
||||
|
||||
default int priority() {
|
||||
/**
|
||||
* Returns the priority of this Module given the lookup type.
|
||||
* @param type the type being looked up this could be an interface
|
||||
* @return the priority of this module, higher is more important. Default is 0
|
||||
*/
|
||||
@SuppressWarnings("unused") // API
|
||||
default int priority(Class<?> type) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -25,15 +25,25 @@ public interface PermissionDataProvider extends Module {
|
||||
|
||||
boolean supportsOffline();
|
||||
|
||||
interface All extends Groups, Permissions, PrefixAndSuffix, Meta {}
|
||||
|
||||
interface Groups extends PermissionDataProvider {
|
||||
CompletableFuture<Boolean> hasGroup(UUID player, String groupName);
|
||||
CompletableFuture<Void> addGroup(UUID player, String groupName);
|
||||
CompletableFuture<Void> removeGroup(UUID player, String groupName);
|
||||
}
|
||||
|
||||
interface Permissions extends PermissionDataProvider {
|
||||
CompletableFuture<Boolean> hasPermission(UUID player, String permission);
|
||||
}
|
||||
|
||||
interface PrefixAndSuffix extends PermissionDataProvider {
|
||||
CompletableFuture<String> getPrefix(UUID player);
|
||||
CompletableFuture<String> getSuffix(UUID player);
|
||||
}
|
||||
|
||||
CompletableFuture<String> getMeta(UUID player, String key) throws UnsupportedOperationException;
|
||||
interface Meta extends PermissionDataProvider {
|
||||
CompletableFuture<String> getMeta(UUID player, String key);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user