From ba04fb320bbc2d529c19e9a19dff69a88734dd63 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 5 Sep 2016 16:44:59 +0100 Subject: [PATCH] Remove unnecessary reflection --- .../java/me/lucko/luckperms/BukkitListener.java | 6 ++++-- .../me/lucko/luckperms/users/BukkitUser.java | 16 +++++++++++++--- .../lucko/luckperms/users/BukkitUserManager.java | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/BukkitListener.java b/bukkit/src/main/java/me/lucko/luckperms/BukkitListener.java index 2d054859..a5008727 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/BukkitListener.java +++ b/bukkit/src/main/java/me/lucko/luckperms/BukkitListener.java @@ -32,6 +32,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.*; import org.bukkit.permissions.PermissionAttachment; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; class BukkitListener extends AbstractListener implements Listener { @@ -67,13 +68,14 @@ class BukkitListener extends AbstractListener implements Listener { BukkitUser u = (BukkitUser) user; PermissionAttachment attachment = player.addAttachment(plugin); + Map newPermMap = new ConcurrentHashMap<>(); try { - BukkitUser.getPermissionsField().set(attachment, new ConcurrentHashMap<>()); + BukkitUser.getPermissionsField().set(attachment, newPermMap); } catch (Throwable t) { t.printStackTrace(); } - u.setAttachment(attachment); + u.setAttachment(new BukkitUser.PermissionAttachmentHolder(attachment, newPermMap)); } plugin.doAsync(user::refreshPermissions); diff --git a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUser.java b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUser.java index 372ec0b3..49c995c3 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUser.java +++ b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUser.java @@ -22,6 +22,8 @@ package me.lucko.luckperms.users; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import me.lucko.luckperms.LPBukkitPlugin; @@ -52,7 +54,7 @@ public class BukkitUser extends User { @Getter @Setter - private PermissionAttachment attachment = null; + private PermissionAttachmentHolder attachment = null; BukkitUser(UUID uuid, LPBukkitPlugin plugin) { super(uuid, plugin); @@ -83,7 +85,7 @@ public class BukkitUser extends User { try { // The map in the LP PermissionAttachment is a ConcurrentHashMap. We can modify it's contents async. - Map existing = (Map) getPermissionsField().get(attachment); + Map existing = attachment.getPermissions(); boolean different = false; if (toApply.size() != existing.size()) { @@ -107,7 +109,7 @@ public class BukkitUser extends User { Shouldn't be too taxing on the server. This only gets called when permissions have actually changed, which is like once per user per login, assuming their permissions don't get modified. */ plugin.doSync(() -> { - attachment.getPermissible().recalculatePermissions(); + attachment.getAttachment().getPermissible().recalculatePermissions(); plugin.getApiProvider().fireEventAsync(new UserPermissionRefreshEvent(new UserLink(this))); }); @@ -115,4 +117,12 @@ public class BukkitUser extends User { e.printStackTrace(); } } + + @Getter + @EqualsAndHashCode + @AllArgsConstructor + public static class PermissionAttachmentHolder { + private final PermissionAttachment attachment; + private final Map permissions; + } } diff --git a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java index ad935eff..f29bd45b 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java @@ -52,7 +52,7 @@ public class BukkitUserManager extends UserManager { Player player = plugin.getServer().getPlayer(plugin.getUuidCache().getExternalUUID(u.getUuid())); if (player != null) { - player.removeAttachment(u.getAttachment()); + player.removeAttachment(u.getAttachment().getAttachment()); } u.setAttachment(null); }