Split out the PermissionDataProvider into smaller parts, fix some generic use in ModuleManager, make module priorities more flexible

This commit is contained in:
Vankka 2022-01-19 21:40:44 +02:00
parent 8fdd5e73e3
commit 0f5d36b407
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
5 changed files with 44 additions and 29 deletions

View File

@ -31,7 +31,10 @@ import org.bukkit.plugin.ServicesManager;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; 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 Permission permission;
private Chat chat; private Chat chat;
@ -41,7 +44,7 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
} }
@Override @Override
public int priority() { public int priority(Class<?> type) {
// Lower priority than default // Lower priority than default
return -1; return -1;
} }
@ -179,10 +182,4 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
return chat.getPlayerSuffix(null, offlinePlayer); 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");
}
} }

View File

@ -35,7 +35,7 @@ import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction; 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; private LuckPerms luckPerms;

View File

@ -33,8 +33,8 @@ import java.util.concurrent.CopyOnWriteArraySet;
public class ModuleManager { public class ModuleManager {
private final Set<AbstractModule> modules = new CopyOnWriteArraySet<>(); private final Set<AbstractModule<?>> modules = new CopyOnWriteArraySet<>();
private final Map<String, AbstractModule> moduleLookupTable = new ConcurrentHashMap<>(); private final Map<String, AbstractModule<?>> moduleLookupTable = new ConcurrentHashMap<>();
private final DiscordSRV discordSRV; private final DiscordSRV discordSRV;
public ModuleManager(DiscordSRV discordSRV) { public ModuleManager(DiscordSRV discordSRV) {
@ -44,25 +44,27 @@ public class ModuleManager {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends Module> T getModule(Class<T> moduleType) { public <T extends Module> T getModule(Class<T> moduleType) {
return (T) moduleLookupTable.computeIfAbsent(moduleType.getName(), key -> { return (T) moduleLookupTable.computeIfAbsent(moduleType.getName(), key -> {
AbstractModule bestCandidate = null; AbstractModule<?> bestCandidate = null;
for (AbstractModule module : modules) { int bestCandidatePriority = Integer.MIN_VALUE;
if (moduleType.isAssignableFrom(module.getClass()) for (AbstractModule<?> module : modules) {
&& (bestCandidate == null || module.priority() > bestCandidate.priority())) { int priority;
if (moduleType.isAssignableFrom(module.getClass()) && ((priority = module.priority(moduleType)) > bestCandidatePriority)) {
bestCandidate = module; bestCandidate = module;
bestCandidatePriority = priority;
} }
} }
return bestCandidate; return bestCandidate;
}); });
} }
public void register(AbstractModule module) { public void register(AbstractModule<?> module) {
this.modules.add(module); this.modules.add(module);
this.moduleLookupTable.put(module.getClass().getName(), module); this.moduleLookupTable.put(module.getClass().getName(), module);
enable(module); enable(module);
} }
private void enable(AbstractModule module) { private void enable(AbstractModule<?> module) {
try { try {
module.enableModule(); module.enableModule();
} catch (Throwable t) { } 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.modules.remove(module);
this.moduleLookupTable.values().removeIf(mod -> mod == module); this.moduleLookupTable.values().removeIf(mod -> mod == module);
disable(module); disable(module);
} }
private void disable(AbstractModule module) { private void disable(AbstractModule<?> module) {
try { try {
module.disable(); module.disable();
} catch (Throwable t) { } catch (Throwable t) {
@ -87,14 +89,14 @@ public class ModuleManager {
@Subscribe(priority = EventPriority.EARLY) @Subscribe(priority = EventPriority.EARLY)
public void onShuttingDown(DiscordSRVShuttingDownEvent event) { public void onShuttingDown(DiscordSRVShuttingDownEvent event) {
for (AbstractModule module : modules) { for (AbstractModule<?> module : modules) {
unregister(module); unregister(module);
} }
} }
@Subscribe(priority = EventPriority.EARLY) @Subscribe(priority = EventPriority.EARLY)
public void onReload(DiscordSRVReloadEvent event) { public void onReload(DiscordSRVReloadEvent event) {
for (AbstractModule module : modules) { for (AbstractModule<?> module : modules) {
// Check if the module needs to be enabled due to reload // Check if the module needs to be enabled due to reload
enable(module); enable(module);

View File

@ -24,7 +24,13 @@ public interface Module {
return true; 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; return 0;
} }

View File

@ -25,15 +25,25 @@ public interface PermissionDataProvider extends Module {
boolean supportsOffline(); boolean supportsOffline();
CompletableFuture<Boolean> hasGroup(UUID player, String groupName); interface All extends Groups, Permissions, PrefixAndSuffix, Meta {}
CompletableFuture<Void> addGroup(UUID player, String groupName);
CompletableFuture<Void> removeGroup(UUID player, String groupName);
CompletableFuture<Boolean> hasPermission(UUID player, String permission); 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);
}
CompletableFuture<String> getPrefix(UUID player); interface Permissions extends PermissionDataProvider {
CompletableFuture<String> getSuffix(UUID player); CompletableFuture<Boolean> hasPermission(UUID player, String permission);
}
CompletableFuture<String> getMeta(UUID player, String key) throws UnsupportedOperationException; interface PrefixAndSuffix extends PermissionDataProvider {
CompletableFuture<String> getPrefix(UUID player);
CompletableFuture<String> getSuffix(UUID player);
}
interface Meta extends PermissionDataProvider {
CompletableFuture<String> getMeta(UUID player, String key);
}
} }