From 83e88eca26b194e8d80098ddc272ff1fe5c0c3ee Mon Sep 17 00:00:00 2001 From: virustotalop Date: Sat, 6 May 2023 17:39:29 -0700 Subject: [PATCH] 3.0.0 Release * Abstract PermissionManager * Cleanup permissions on world switch * Bumped gradle version * Bumped shadow version * Correctly log that plugin shuts down * Correctly cleanup permission plugin on shutdown --- build.gradle | 28 +++--- gradle/wrapper/gradle-wrapper.properties | 2 +- .../clubobsidian/foundry/FoundryPlugin.java | 25 +++-- .../foundry/permission/PermissionManager.java | 88 +---------------- .../permission/PermissionManagerImpl.java | 99 +++++++++++++++++++ 5 files changed, 135 insertions(+), 107 deletions(-) create mode 100644 src/main/java/com/clubobsidian/foundry/permission/PermissionManagerImpl.java diff --git a/build.gradle b/build.gradle index a2d6754..531166c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,22 +1,25 @@ plugins { id 'java' - id 'com.github.johnrengelman.shadow' version '5.0.0' - id 'jacoco' + id 'com.github.johnrengelman.shadow' version '7.1.2' id 'eclipse' id 'idea' - id 'maven' } group = 'com.clubobsidian' -version = '2.0.2' +version = '3.0.0' processResources { - from(sourceSets.main.resources) { - include 'plugin.yml' + filesMatching('plugin.yml') { expand 'pluginVersion': project.version } } +java { + toolchain { + languageVersion = JavaLanguageVersion.of(11) + } +} + repositories { flatDir { dirs 'libs'} maven { @@ -31,17 +34,12 @@ repositories { } } -configurations { - shade - compile.extendsFrom shade -} - shadowJar { - baseName = 'foundry' - classifier = null + archiveBaseName.set('foundry') + archiveClassifier.set('') } dependencies { - compileOnly 'org.bukkit:bukkit:1.12.2-R0.1-SNAPSHOT' - compileOnly name: 'LuckPerms' + compileOnly('org.bukkit:bukkit:1.12.2-R0.1-SNAPSHOT') + compileOnly(name: 'LuckPerms') } \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f4d7b2b..41dfb87 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/com/clubobsidian/foundry/FoundryPlugin.java b/src/main/java/com/clubobsidian/foundry/FoundryPlugin.java index eba177c..9c880ab 100644 --- a/src/main/java/com/clubobsidian/foundry/FoundryPlugin.java +++ b/src/main/java/com/clubobsidian/foundry/FoundryPlugin.java @@ -1,31 +1,44 @@ package com.clubobsidian.foundry; +import com.clubobsidian.foundry.permission.PermissionManagerImpl; +import com.clubobsidian.foundry.permission.PermissionPlugin; import org.bukkit.plugin.java.JavaPlugin; import com.clubobsidian.foundry.permission.PermissionManager; +import java.util.logging.Level; + public class FoundryPlugin extends JavaPlugin { private static FoundryPlugin instance; - + public static FoundryPlugin get() { return instance; } - + private PermissionManager permissionManager; - + @Override public void onEnable() { instance = this; - this.permissionManager = new PermissionManager(); + this.permissionManager = new PermissionManagerImpl(); if(this.permissionManager.getPlugin() == null) { + this.getLogger().log(Level.SEVERE, "No permission updater found, disabling..."); this.getPluginLoader().disablePlugin(this); } else { this.getServer().getPluginManager().registerEvents(this.permissionManager, this); - this.getLogger().info("Foundry is now enabled!"); + this.getLogger().log(Level.INFO, "Foundry is now enabled!"); } } - + + @Override + public void onDisable() { + PermissionPlugin plugin = this.permissionManager.getPlugin(); + if (plugin != null) { + plugin.unregister(); + } + } + public PermissionManager getPermissionManager() { return this.permissionManager; } diff --git a/src/main/java/com/clubobsidian/foundry/permission/PermissionManager.java b/src/main/java/com/clubobsidian/foundry/permission/PermissionManager.java index 37d7ca0..7ef866c 100644 --- a/src/main/java/com/clubobsidian/foundry/permission/PermissionManager.java +++ b/src/main/java/com/clubobsidian/foundry/permission/PermissionManager.java @@ -1,96 +1,14 @@ package com.clubobsidian.foundry.permission; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; - -import org.bukkit.Bukkit; import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.plugin.Plugin; - -import com.clubobsidian.foundry.FoundryPlugin; -import com.clubobsidian.foundry.permission.event.PermissionRecalculateEvent; -import com.clubobsidian.foundry.permission.event.PermissionUpdateEvent; -import com.clubobsidian.foundry.permission.plugin.LuckPermsPlugin; - - -public final class PermissionManager implements Listener { - private final Map> userPermissionCache = new ConcurrentHashMap<>(); - private final PermissionPlugin plugin; - - public PermissionManager() { - this.plugin = this.findUpdater(); - } - public boolean hasPermission(Player player, String permission) { - UUID uuid = player.getUniqueId(); - Map nodes = this.userPermissionCache.get(uuid); - if(nodes == null) { - nodes = new ConcurrentHashMap<>(); - this.userPermissionCache.put(uuid, nodes); - } - PermissionNode node = nodes.get(permission); - if(node != null) { - return node.hasPermission(); - } - boolean has = this.plugin.hasPermission(player, permission); - nodes.put(permission, new PermissionNode(permission, has)); - return has; - } +public interface PermissionManager extends Listener { - public PermissionPlugin getPlugin() { - return this.plugin; - } - private PermissionPlugin findUpdater() { - Collection updaters = new ArrayList<>(); - updaters.add(new LuckPermsPlugin()); - for(PermissionPlugin updater : updaters) { - Plugin plugin = Bukkit.getServer() - .getPluginManager() - .getPlugin(updater.getPluginName()); - if(plugin != null) { - return updater.register(); - } - } - FoundryPlugin.get().getLogger().info("No permission updater can be found, permissions will only update on relog!"); - return null; - } + boolean hasPermission(Player player, String permission); - @EventHandler(priority = EventPriority.HIGHEST) - public void onPermissionUpdate(PermissionRecalculateEvent event) { - Player player = event.getPlayer(); - UUID uuid = player.getUniqueId(); - Map nodes = this.userPermissionCache.get(uuid); - if(nodes != null) { - Iterator> it = nodes.entrySet().iterator(); - while(it.hasNext()) { - Entry next = it.next(); - String permission = next.getKey(); - PermissionNode node = next.getValue(); - boolean hasPermission = this.plugin.hasPermission(player, permission); - if(hasPermission != node.hasPermission()) { - node.setHasPermission(hasPermission); - } - } - } - PermissionUpdateEvent permissionEvent = new PermissionUpdateEvent(player); - Bukkit.getServer().getPluginManager().callEvent(permissionEvent); - } + PermissionPlugin getPlugin(); - @EventHandler(priority = EventPriority.HIGHEST) - public void permissionCacheCleanup(PlayerQuitEvent event) { - Player player = event.getPlayer(); - UUID uuid = player.getUniqueId(); - this.userPermissionCache.remove(uuid); - } } \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/foundry/permission/PermissionManagerImpl.java b/src/main/java/com/clubobsidian/foundry/permission/PermissionManagerImpl.java new file mode 100644 index 0000000..2108ded --- /dev/null +++ b/src/main/java/com/clubobsidian/foundry/permission/PermissionManagerImpl.java @@ -0,0 +1,99 @@ +package com.clubobsidian.foundry.permission; + +import com.clubobsidian.foundry.permission.event.PermissionRecalculateEvent; +import com.clubobsidian.foundry.permission.event.PermissionUpdateEvent; +import com.clubobsidian.foundry.permission.plugin.LuckPermsPlugin; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerChangedWorldEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.plugin.Plugin; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +public class PermissionManagerImpl implements PermissionManager { + + private final Map> userPermissionCache = new ConcurrentHashMap<>(); + private final PermissionPlugin plugin; + + public PermissionManagerImpl() { + this.plugin = this.findPlugin(); + } + + public boolean hasPermission(Player player, String permission) { + UUID uuid = player.getUniqueId(); + Map nodes = this.userPermissionCache.get(uuid); + if(nodes == null) { + nodes = new ConcurrentHashMap<>(); + this.userPermissionCache.put(uuid, nodes); + } + PermissionNode node = nodes.get(permission); + if(node != null) { + return node.hasPermission(); + } + boolean has = this.plugin.hasPermission(player, permission); + nodes.put(permission, new PermissionNode(permission, has)); + return has; + } + + public PermissionPlugin getPlugin() { + return this.plugin; + } + + private PermissionPlugin findPlugin() { + Collection plugins = Arrays.asList( + new LuckPermsPlugin() + ); + for(PermissionPlugin updater : plugins) { + Plugin plugin = Bukkit.getServer() + .getPluginManager() + .getPlugin(updater.getPluginName()); + if(plugin != null) { + return updater.register(); + } + } + return null; + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPermissionUpdate(PermissionRecalculateEvent event) { + Player player = event.getPlayer(); + UUID uuid = player.getUniqueId(); + Map nodes = this.userPermissionCache.get(uuid); + if(nodes != null) { + Iterator> it = nodes.entrySet().iterator(); + while(it.hasNext()) { + Map.Entry next = it.next(); + String permission = next.getKey(); + PermissionNode node = next.getValue(); + boolean hasPermission = this.plugin.hasPermission(player, permission); + if(hasPermission != node.hasPermission()) { + node.setHasPermission(hasPermission); + } + } + } + PermissionUpdateEvent permissionEvent = new PermissionUpdateEvent(player); + Bukkit.getServer().getPluginManager().callEvent(permissionEvent); + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void permissionCacheCleanup(PlayerQuitEvent event) { + Player player = event.getPlayer(); + UUID uuid = player.getUniqueId(); + this.userPermissionCache.remove(uuid); + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void permissionCacheCleanup(PlayerChangedWorldEvent event) { + Player player = event.getPlayer(); + UUID uuid = player.getUniqueId(); + this.userPermissionCache.remove(uuid); + } +} \ No newline at end of file