Skip to content

Commit

Permalink
3.0.0 Release
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
virustotalop committed May 7, 2023
1 parent 701a660 commit 83e88ec
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 107 deletions.
28 changes: 13 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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')
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
25 changes: 19 additions & 6 deletions src/main/java/com/clubobsidian/foundry/FoundryPlugin.java
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UUID, Map<String, PermissionNode>> 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<String, PermissionNode> 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<PermissionPlugin> 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<String, PermissionNode> nodes = this.userPermissionCache.get(uuid);
if(nodes != null) {
Iterator<Entry<String, PermissionNode>> it = nodes.entrySet().iterator();
while(it.hasNext()) {
Entry<String, PermissionNode> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<UUID, Map<String, PermissionNode>> 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<String, PermissionNode> 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<PermissionPlugin> 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<String, PermissionNode> nodes = this.userPermissionCache.get(uuid);
if(nodes != null) {
Iterator<Map.Entry<String, PermissionNode>> it = nodes.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<String, PermissionNode> 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);
}
}

0 comments on commit 83e88ec

Please sign in to comment.