Fix memory leak by Citizen NPCs (#1693)

* fix: ignore uuid v2 for BukkitPermissionAttachmentManager

Fixes a memory leak that attaches and caches PermissionAttachments to NPCs. Citiziens uses version 2 UUIDs, which are not used for regular player uuids. Those v2 uuids are now excluded.

* fix: create Permission when required

* fix: use citizens documented way to check for NPCs
This commit is contained in:
Pierre Maurice Schwang 2022-04-06 16:22:53 +02:00 committed by GitHub
parent ed9317b4d6
commit 2483eacff5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package com.fastasyncworldedit.bukkit;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissibleBase;
import org.bukkit.permissions.PermissionAttachment;
import javax.annotation.Nullable;
@ -12,6 +13,7 @@ public class BukkitPermissionAttachmentManager {
private final WorldEditPlugin plugin;
private final Map<Player, PermissionAttachment> attachments = new ConcurrentHashMap<>();
private PermissionAttachment noopAttachment;
public BukkitPermissionAttachmentManager(WorldEditPlugin plugin) {
this.plugin = plugin;
@ -21,6 +23,12 @@ public class BukkitPermissionAttachmentManager {
if (p == null) {
return null;
}
if (p.hasMetadata("NPC")) {
if (this.noopAttachment == null) {
this.noopAttachment = new PermissionAttachment(plugin, new PermissibleBase(null));
}
return noopAttachment;
}
return attachments.computeIfAbsent(p, k -> k.addAttachment(plugin));
}
@ -28,6 +36,10 @@ public class BukkitPermissionAttachmentManager {
if (p == null) {
return;
}
if (p.hasMetadata("NPC") && noopAttachment != null) {
p.removeAttachment(noopAttachment);
return;
}
PermissionAttachment attach = attachments.remove(p);
if (attach != null) {
p.removeAttachment(attach);