From bdfc3d3dcf568563c2889669cab1d3a6274ac3ba Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 20 May 2016 16:58:43 +0200 Subject: [PATCH] NmsPermissions and PermUtil imporovement. --- .../massivecraft/massivecore/MassiveCore.java | 2 + .../massivecore/nms/NmsBasics17R4P.java | 2 +- .../massivecore/nms/NmsPermissions.java | 86 +++++++ .../massivecore/nms/NmsPermissions17R4P.java | 83 +++++++ .../massivecore/util/PermUtil.java | 209 +++++++----------- 5 files changed, 246 insertions(+), 136 deletions(-) create mode 100644 src/com/massivecraft/massivecore/nms/NmsPermissions.java create mode 100644 src/com/massivecraft/massivecore/nms/NmsPermissions17R4P.java diff --git a/src/com/massivecraft/massivecore/MassiveCore.java b/src/com/massivecraft/massivecore/MassiveCore.java index 5d165443..f45aa01b 100644 --- a/src/com/massivecraft/massivecore/MassiveCore.java +++ b/src/com/massivecraft/massivecore/MassiveCore.java @@ -91,6 +91,7 @@ import com.massivecraft.massivecore.nms.NmsEntityGet; import com.massivecraft.massivecore.nms.NmsItemStackCreate; import com.massivecraft.massivecore.nms.NmsItemStackCreate17R4P; import com.massivecraft.massivecore.nms.NmsItemStackTooltip; +import com.massivecraft.massivecore.nms.NmsPermissions; import com.massivecraft.massivecore.nms.NmsPlayerInventoryCreate; import com.massivecraft.massivecore.nms.NmsSkullMeta; import com.massivecraft.massivecore.ps.PS; @@ -264,6 +265,7 @@ public class MassiveCore extends MassivePlugin NmsEntityGet.class, NmsItemStackCreate.class, NmsItemStackTooltip.class, + NmsPermissions.class, NmsPlayerInventoryCreate.class, NmsSkullMeta.class, diff --git a/src/com/massivecraft/massivecore/nms/NmsBasics17R4P.java b/src/com/massivecraft/massivecore/nms/NmsBasics17R4P.java index 505df530..4730e5a3 100644 --- a/src/com/massivecraft/massivecore/nms/NmsBasics17R4P.java +++ b/src/com/massivecraft/massivecore/nms/NmsBasics17R4P.java @@ -2,7 +2,6 @@ package com.massivecraft.massivecore.nms; import java.lang.reflect.Field; import java.lang.reflect.Method; - import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -181,6 +180,7 @@ public class NmsBasics17R4P extends NmsBasics // PING // -------------------------------------------- // + @Override public int getPing(Player player) { Object handle = this.getHandle(player); diff --git a/src/com/massivecraft/massivecore/nms/NmsPermissions.java b/src/com/massivecraft/massivecore/nms/NmsPermissions.java new file mode 100644 index 00000000..f42a303f --- /dev/null +++ b/src/com/massivecraft/massivecore/nms/NmsPermissions.java @@ -0,0 +1,86 @@ +package com.massivecraft.massivecore.nms; + +import java.util.List; +import java.util.Map; + +import org.bukkit.entity.Player; +import org.bukkit.permissions.Permissible; +import org.bukkit.permissions.PermissibleBase; +import org.bukkit.permissions.PermissionAttachment; +import com.massivecraft.massivecore.mixin.Mixin; + +public class NmsPermissions extends Mixin +{ + // -------------------------------------------- // + // DEFAULT + // -------------------------------------------- // + + private static NmsPermissions d = new NmsPermissions().setAlternatives( + NmsPermissions17R4P.class + ); + + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static NmsPermissions i = d; + public static NmsPermissions get() { return i; } + + // -------------------------------------------- // + // BASE + // -------------------------------------------- // + + public List getAttachments(PermissibleBase base) + { + throw this.notImplemented(); + } + + // -------------------------------------------- // + // PLAYER + // -------------------------------------------- // + + public PermissibleBase getBase(Player player) + { + throw this.notImplemented(); + } + + public List getAttachments(Player player) + { + PermissibleBase base = this.getBase(player); + return this.getAttachments(base); + } + + // -------------------------------------------- // + // ATTACHMENT + // -------------------------------------------- // + + public Map getAttachmentPermissionsRaw(PermissionAttachment permissionAttachment) + { + throw this.notImplemented(); + } + + public void setAttachmentPermissionsRaw(PermissionAttachment permissionAttachment, Map permissions) + { + throw this.notImplemented(); + } + + public boolean updateAttachmentPermissions(PermissionAttachment attachment, Map permissions) + { + if (attachment == null) throw new NullPointerException("attachment"); + if (permissions == null) throw new NullPointerException("permissions"); + + Map inner = this.getAttachmentPermissionsRaw(attachment); + if (inner.equals(permissions)) return false; + + inner.clear(); + inner.putAll(permissions); + + Permissible permissible = attachment.getPermissible(); + if (permissible != null) permissible.recalculatePermissions(); + + return true; + } + + + +} diff --git a/src/com/massivecraft/massivecore/nms/NmsPermissions17R4P.java b/src/com/massivecraft/massivecore/nms/NmsPermissions17R4P.java new file mode 100644 index 00000000..4a87046c --- /dev/null +++ b/src/com/massivecraft/massivecore/nms/NmsPermissions17R4P.java @@ -0,0 +1,83 @@ +package com.massivecraft.massivecore.nms; + +import java.lang.reflect.Field; +import java.util.List; +import java.util.Map; + +import org.bukkit.entity.Player; +import org.bukkit.permissions.PermissibleBase; +import org.bukkit.permissions.PermissionAttachment; + +import com.massivecraft.massivecore.particleeffect.ReflectionUtils.PackageType; +import com.massivecraft.massivecore.util.ReflectionUtil; + +public class NmsPermissions17R4P extends NmsPermissions +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static NmsPermissions17R4P i = new NmsPermissions17R4P(); + public static NmsPermissions17R4P get() { return i; } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + protected Class classCraftHumanEntity; + protected Field fieldCraftHumanEntityBase; + + protected Field fieldPermissibleBaseAttachments; + + protected Field fieldAttachmentPermissions; + + // -------------------------------------------- // + // SETUP + // -------------------------------------------- // + + @Override + public void setup() throws Throwable + { + this.classCraftHumanEntity = PackageType.CRAFTBUKKIT_ENTITY.getClass("CraftHumanEntity"); + this.fieldCraftHumanEntityBase = ReflectionUtil.getField(this.classCraftHumanEntity, "perm"); + + this.fieldPermissibleBaseAttachments = ReflectionUtil.getField(PermissibleBase.class, "attachments"); + + this.fieldAttachmentPermissions = ReflectionUtil.getField(PermissionAttachment.class, "permissions"); + } + + // -------------------------------------------- // + // BASE + // -------------------------------------------- // + + public List getAttachments(PermissibleBase base) + { + return ReflectionUtil.getField(this.fieldPermissibleBaseAttachments, base); + } + + // -------------------------------------------- // + // PLAYER + // -------------------------------------------- // + + public PermissibleBase getBase(Player player) + { + return ReflectionUtil.getField(this.fieldCraftHumanEntityBase, player); + } + + // -------------------------------------------- // + // ATTACHMENT + // -------------------------------------------- // + + @Override + public Map getAttachmentPermissionsRaw(PermissionAttachment permissionAttachment) + { + return ReflectionUtil.getField(this.fieldAttachmentPermissions, permissionAttachment); + } + + @Override + public void setAttachmentPermissionsRaw(PermissionAttachment permissionAttachment, Map permissions) + { + ReflectionUtil.setField(this.fieldAttachmentPermissions, permissionAttachment, permissions); + } + +} diff --git a/src/com/massivecraft/massivecore/util/PermUtil.java b/src/com/massivecraft/massivecore/util/PermUtil.java index 51f6b168..ea5d8b21 100644 --- a/src/com/massivecraft/massivecore/util/PermUtil.java +++ b/src/com/massivecraft/massivecore/util/PermUtil.java @@ -140,27 +140,40 @@ public class PermUtil // ONE FIELD - public static void set(Permission permission, String description) + public static boolean set(Permission permission, String description) { + if (permission == null) throw new NullPointerException("permission"); + if (description == null) return false; + // Recalculation need created: FALSE // Recalculation auto-performed: FALSE permission.setDescription(description); + + return false; } - public static void set(Permission permission, PermissionDefault defaultValue) + public static boolean set(Permission permission, PermissionDefault defaultValue) { - if (defaultValue == null) return; - if (permission.getDefault() == defaultValue) return; + if (permission == null) throw new NullPointerException("permission"); + if (defaultValue == null) return false; + + // NoChange + if (permission.getDefault() == defaultValue) return false; // Recalculation need created: TRUE // Recalculation auto-performed: TRUE permission.setDefault(defaultValue); + + return true; } - public static void set(Permission permission, Map children) + public static boolean set(Permission permission, Map children) { - if (children == null) return; - if (permission.getChildren().equals(children)) return; + if (permission == null) throw new NullPointerException("permission"); + if (children == null) return false; + + // NoChange + if (children.equals(permission.getChildren())) return false; // Recalculation need created: TRUE // Recalculation auto-performed: FALSE @@ -169,28 +182,34 @@ public class PermUtil // Manual Recalculation permission.recalculatePermissibles(); + + return true; } // TWO FIELDS - public static void set(Permission permission, String description, PermissionDefault defaultValue) + public static boolean set(Permission permission, String description, PermissionDefault defaultValue) { - set(permission, defaultValue); - set(permission, description); + boolean ret = false; + ret |= set(permission, description); + ret |= set(permission, defaultValue); + return ret; } - public static void set(Permission permission, String description, Map children) + public static boolean set(Permission permission, String description, Map children) { - set(permission, children); - set(permission, description); + boolean ret = false; + ret |= set(permission, description); + ret |= set(permission, children); + return ret; } - public static void set(Permission permission, PermissionDefault defaultValue, Map children) + public static boolean set(Permission permission, PermissionDefault defaultValue, Map children) { boolean childrenChanged = false; boolean defaultChanged = false; - - if ( ! permission.getChildren().equals(children)) + + if (children != null && ! children.equals(permission.getChildren())) { // Recalculation need created: TRUE // Recalculation auto-performed: FALSE @@ -199,7 +218,7 @@ public class PermUtil childrenChanged = true; } - if (permission.getDefault() != defaultValue) + if (defaultValue != null && defaultValue != permission.getDefault()) { // Recalculation need created: TRUE // Recalculation auto-performed: TRUE @@ -213,22 +232,47 @@ public class PermUtil // Manual Recalculation permission.recalculatePermissibles(); } + + return childrenChanged || defaultChanged; } // THREE FIELDS - public static void set(Permission permission, String description, PermissionDefault defaultValue, Map children) + public static boolean set(Permission permission, String description, PermissionDefault defaultValue, Map children) { - set(permission, defaultValue, children); - set(permission, description); + boolean ret = false; + ret |= set(permission, defaultValue, children); + ret |= set(permission, description); + return ret; + } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + // This is just a nicer constructor. + // It will not register in any way. + + public static Permission construct(String name, String description, PermissionDefault defaultValue, Map children) + { + if (name == null) throw new NullPointerException("name"); + + if (description != null && defaultValue != null && children != null) return new Permission(name, description, defaultValue, children); + + if (description != null && defaultValue != null) return new Permission(name, description, defaultValue); + if (description != null && children != null) return new Permission(name, description, defaultValue, children); + if (defaultValue != null && children != null) return new Permission(name, description, defaultValue, children); + + if (description != null) return new Permission(name, description); + if (defaultValue != null) return new Permission(name, defaultValue); + if (children != null) return new Permission(name, children); + + return new Permission(name); } // -------------------------------------------- // // GET PERMISSION // -------------------------------------------- // - // This is the original logic - // The other below are just copy pastes with argument permutation public static Permission get(boolean create, boolean update, String name, String description, PermissionDefault defaultValue, Map children) { Permission ret = Bukkit.getPluginManager().getPermission(name); @@ -236,7 +280,7 @@ public class PermUtil { if (create) { - ret = new Permission(name, description, defaultValue, children); + ret = construct(name, description, defaultValue, children); Bukkit.getPluginManager().addPermission(ret); } } @@ -254,146 +298,41 @@ public class PermUtil public static Permission get(boolean create, String name) { - Permission ret = Bukkit.getPluginManager().getPermission(name); - if (ret == null) - { - if (create) - { - ret = new Permission(name); - Bukkit.getPluginManager().addPermission(ret); - } - } - return ret; + return get(create, false, name, null, null, null); } // ONE FIELD public static Permission get(boolean create, boolean update, String name, String description) { - Permission ret = Bukkit.getPluginManager().getPermission(name); - if (ret == null) - { - if (create) - { - ret = new Permission(name, description); - Bukkit.getPluginManager().addPermission(ret); - } - } - else - { - if (update) - { - set(ret, description); - } - } - return ret; + return get(create, update, name, description, null, null); } public static Permission get(boolean create, boolean update, String name, PermissionDefault defaultValue) { - Permission ret = Bukkit.getPluginManager().getPermission(name); - if (ret == null) - { - if (create) - { - ret = new Permission(name, defaultValue); - Bukkit.getPluginManager().addPermission(ret); - } - } - else - { - if (update) - { - set(ret, defaultValue); - } - } - return ret; + return get(create, update, name, null, defaultValue, null); } public static Permission get(boolean create, boolean update, String name, Map children) { - Permission ret = Bukkit.getPluginManager().getPermission(name); - if (ret == null) - { - if (create) - { - ret = new Permission(name, children); - Bukkit.getPluginManager().addPermission(ret); - } - } - else - { - if (update) - { - set(ret, children); - } - } - return ret; + return get(create, false, name, null, null, children); } // TWO FIELDS public static Permission get(boolean create, boolean update, String name, String description, PermissionDefault defaultValue) { - Permission ret = Bukkit.getPluginManager().getPermission(name); - if (ret == null) - { - if (create) - { - ret = new Permission(name, description, defaultValue); - Bukkit.getPluginManager().addPermission(ret); - } - } - else - { - if (update) - { - set(ret, description, defaultValue); - } - } - return ret; + return get(create, false, name, description, defaultValue, null); } public static Permission get(boolean create, boolean update, String name, String description, Map children) { - Permission ret = Bukkit.getPluginManager().getPermission(name); - if (ret == null) - { - if (create) - { - ret = new Permission(name, description, children); - Bukkit.getPluginManager().addPermission(ret); - } - } - else - { - if (update) - { - set(ret, description, children); - } - } - return ret; + return get(create, false, name, description, null, children); } public static Permission get(boolean create, boolean update, String name, PermissionDefault defaultValue, Map children) { - Permission ret = Bukkit.getPluginManager().getPermission(name); - if (ret == null) - { - if (create) - { - ret = new Permission(name, defaultValue, children); - Bukkit.getPluginManager().addPermission(ret); - } - } - else - { - if (update) - { - set(ret, defaultValue, children); - } - } - return ret; + return get(create, false, name, null, defaultValue, children); } }