mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2024-12-23 01:37:37 +00:00
reduce the amount of synchronisation/locks being used unneecessarily and add nullcheck
This commit is contained in:
parent
318eca364d
commit
987ab7d2b6
@ -1,58 +1,44 @@
|
|||||||
package com.sk89q.worldedit.bukkit;
|
package com.sk89q.worldedit.bukkit;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.permissions.PermissionAttachment;
|
import org.bukkit.permissions.PermissionAttachment;
|
||||||
|
|
||||||
public class BukkitPermissionAttachmentManager {
|
public class BukkitPermissionAttachmentManager {
|
||||||
|
|
||||||
private final WorldEditPlugin plugin;
|
private final WorldEditPlugin plugin;
|
||||||
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
|
private final Map<Player, PermissionAttachment> attachments = new HashMap<>();
|
||||||
private final Map<Player, PermissionAttachment> attachments = new HashMap();
|
|
||||||
|
|
||||||
public BukkitPermissionAttachmentManager(WorldEditPlugin plugin) {
|
public BukkitPermissionAttachmentManager(WorldEditPlugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized PermissionAttachment addAttachment(Player p) {
|
public PermissionAttachment getOrAddAttachment(@Nullable final Player p) {
|
||||||
PermissionAttachment attach;
|
if (p == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
PermissionAttachment attachment = attachments.get(p);
|
||||||
|
|
||||||
//try find attachment
|
if (attachment != null) {
|
||||||
lock.readLock().lock();
|
return attachment;
|
||||||
try {
|
|
||||||
attach = attachments.get(p);
|
|
||||||
} finally {
|
|
||||||
lock.readLock().unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attach == null) {
|
synchronized (this) {
|
||||||
lock.writeLock().lock();
|
return attachments.computeIfAbsent(p, k -> k.addAttachment(plugin));
|
||||||
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) {
|
public void removeAttachment(@Nullable final Player p) {
|
||||||
PermissionAttachment attach;
|
if (p == null || attachments.get(p) == null) {
|
||||||
lock.writeLock().lock();
|
return;
|
||||||
try {
|
|
||||||
attach = attachments.remove(p);
|
|
||||||
} finally {
|
|
||||||
lock.writeLock().unlock();
|
|
||||||
}
|
}
|
||||||
|
synchronized (this) {
|
||||||
|
PermissionAttachment attach = attachments.remove(p);
|
||||||
if (attach != null) {
|
if (attach != null) {
|
||||||
p.removeAttachment(attach);
|
p.removeAttachment(attach);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -67,7 +67,6 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
@ -81,13 +80,13 @@ public class BukkitPlayer extends AbstractPlayerActor {
|
|||||||
super(getExistingMap(WorldEditPlugin.getInstance(), player));
|
super(getExistingMap(WorldEditPlugin.getInstance(), player));
|
||||||
this.plugin = WorldEditPlugin.getInstance();
|
this.plugin = WorldEditPlugin.getInstance();
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.permAttachment = plugin.getPermissionAttachmentManager().addAttachment(player);
|
this.permAttachment = plugin.getPermissionAttachmentManager().getOrAddAttachment(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BukkitPlayer(WorldEditPlugin plugin, Player player) {
|
public BukkitPlayer(WorldEditPlugin plugin, Player player) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.permAttachment = plugin.getPermissionAttachmentManager().addAttachment(player);
|
this.permAttachment = plugin.getPermissionAttachmentManager().getOrAddAttachment(player);
|
||||||
if (Settings.IMP.CLIPBOARD.USE_DISK) {
|
if (Settings.IMP.CLIPBOARD.USE_DISK) {
|
||||||
loadClipboardFromDisk();
|
loadClipboardFromDisk();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user