fix performance issues in BukkitPlayer permission (#665)

* fix performance issues in BukkitPlayer permission stuff

* Put PermissionAttachmentManager in seperate file

Co-authored-by: @weaondara
Dieser Commit ist enthalten in:
Aurora 2020-09-28 19:11:28 +02:00 committet von GitHub
Ursprung 6332b8c965
Commit 48cd763d3b
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
3 geänderte Dateien mit 77 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,58 @@
package com.sk89q.worldedit.bukkit;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
public class BukkitPermissionAttachmentManager {
private final WorldEditPlugin plugin;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final Map<Player, PermissionAttachment> attachments = new HashMap();
public BukkitPermissionAttachmentManager(WorldEditPlugin plugin) {
this.plugin = plugin;
}
public synchronized PermissionAttachment addAttachment(Player p) {
PermissionAttachment attach;
//try find attachment
lock.readLock().lock();
try {
attach = attachments.get(p);
} finally {
lock.readLock().unlock();
}
if (attach == null) {
lock.writeLock().lock();
try {
//check again - do not remove this
attach = attachments.get(p);
if (attach == null) {
attach = p.addAttachment(plugin);
attachments.put(p, attach);
}
} finally {
lock.writeLock().unlock();
}
}
return attach;
}
public void removeAttachment(Player p) {
PermissionAttachment attach;
lock.writeLock().lock();
try {
attach = attachments.remove(p);
} finally {
lock.writeLock().unlock();
}
if (attach != null) {
p.removeAttachment(attach);
}
}
}

Datei anzeigen

@ -67,26 +67,27 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.annotation.Nullable;
public class BukkitPlayer extends AbstractPlayerActor {
private Player player;
private WorldEditPlugin plugin;
private PermissionAttachment permAttachment;
private final Player player;
private final WorldEditPlugin plugin;
private final PermissionAttachment permAttachment;
public BukkitPlayer(Player player) {
super(getExistingMap(WorldEditPlugin.getInstance(), player));
this.plugin = WorldEditPlugin.getInstance();
this.player = player;
this.permAttachment = player.addAttachment(plugin);
this.permAttachment = plugin.getPermissionAttachmentManager().addAttachment(player);
}
public BukkitPlayer(WorldEditPlugin plugin, Player player) {
this.plugin = plugin;
this.player = player;
this.permAttachment = player.addAttachment(plugin);
this.permAttachment = plugin.getPermissionAttachmentManager().addAttachment(player);
if (Settings.IMP.CLIPBOARD.USE_DISK) {
loadClipboardFromDisk();
}
@ -402,7 +403,7 @@ public class BukkitPlayer extends AbstractPlayerActor {
@Override
public void unregister() {
player.removeMetadata("WE", WorldEditPlugin.getInstance());
plugin.getPermissionAttachmentManager().removeAttachment(player);
super.unregister();
}
}

Datei anzeigen

@ -112,6 +112,7 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter
private BukkitImplAdapter bukkitAdapter;
private BukkitServerInterface server;
private BukkitConfiguration config;
private BukkitPermissionAttachmentManager permissionAttachmentManager;
@Override
public void onLoad() {
@ -126,6 +127,8 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter
server = new BukkitServerInterface(this, getServer());
worldEdit.getPlatformManager().register(server);
permissionAttachmentManager = new BukkitPermissionAttachmentManager(this);
Path delChunks = Paths.get(getDataFolder().getPath(), DELCHUNKS_FILE_NAME);
if (Files.exists(delChunks)) {
ChunkDeleter.runFromFile(delChunks, true);
@ -481,6 +484,15 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter
return PermissionsResolverManager.getInstance();
}
/**
* Get the permissions resolver in use.
*
* @return the permissions resolver
*/
public BukkitPermissionAttachmentManager getPermissionAttachmentManager() {
return permissionAttachmentManager;
}
/**
* Used to wrap a Bukkit Player as a WorldEdit Player.
*