-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
701a660
commit 83e88ec
Showing
5 changed files
with
135 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 3 additions & 85 deletions
88
src/main/java/com/clubobsidian/foundry/permission/PermissionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/com/clubobsidian/foundry/permission/PermissionManagerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |