NmsPermissions and PermUtil imporovement.

This commit is contained in:
Olof Larsson 2016-05-20 16:58:43 +02:00
parent b780fd8f30
commit bdfc3d3dcf
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
5 changed files with 246 additions and 136 deletions

View File

@ -91,6 +91,7 @@ import com.massivecraft.massivecore.nms.NmsEntityGet;
import com.massivecraft.massivecore.nms.NmsItemStackCreate; import com.massivecraft.massivecore.nms.NmsItemStackCreate;
import com.massivecraft.massivecore.nms.NmsItemStackCreate17R4P; import com.massivecraft.massivecore.nms.NmsItemStackCreate17R4P;
import com.massivecraft.massivecore.nms.NmsItemStackTooltip; import com.massivecraft.massivecore.nms.NmsItemStackTooltip;
import com.massivecraft.massivecore.nms.NmsPermissions;
import com.massivecraft.massivecore.nms.NmsPlayerInventoryCreate; import com.massivecraft.massivecore.nms.NmsPlayerInventoryCreate;
import com.massivecraft.massivecore.nms.NmsSkullMeta; import com.massivecraft.massivecore.nms.NmsSkullMeta;
import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.ps.PS;
@ -264,6 +265,7 @@ public class MassiveCore extends MassivePlugin
NmsEntityGet.class, NmsEntityGet.class,
NmsItemStackCreate.class, NmsItemStackCreate.class,
NmsItemStackTooltip.class, NmsItemStackTooltip.class,
NmsPermissions.class,
NmsPlayerInventoryCreate.class, NmsPlayerInventoryCreate.class,
NmsSkullMeta.class, NmsSkullMeta.class,

View File

@ -2,7 +2,6 @@ package com.massivecraft.massivecore.nms;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -181,6 +180,7 @@ public class NmsBasics17R4P extends NmsBasics
// PING // PING
// -------------------------------------------- // // -------------------------------------------- //
@Override
public int getPing(Player player) public int getPing(Player player)
{ {
Object handle = this.getHandle(player); Object handle = this.getHandle(player);

View File

@ -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<PermissionAttachment> getAttachments(PermissibleBase base)
{
throw this.notImplemented();
}
// -------------------------------------------- //
// PLAYER
// -------------------------------------------- //
public PermissibleBase getBase(Player player)
{
throw this.notImplemented();
}
public List<PermissionAttachment> getAttachments(Player player)
{
PermissibleBase base = this.getBase(player);
return this.getAttachments(base);
}
// -------------------------------------------- //
// ATTACHMENT
// -------------------------------------------- //
public Map<String, Boolean> getAttachmentPermissionsRaw(PermissionAttachment permissionAttachment)
{
throw this.notImplemented();
}
public void setAttachmentPermissionsRaw(PermissionAttachment permissionAttachment, Map<String, Boolean> permissions)
{
throw this.notImplemented();
}
public boolean updateAttachmentPermissions(PermissionAttachment attachment, Map<String, Boolean> permissions)
{
if (attachment == null) throw new NullPointerException("attachment");
if (permissions == null) throw new NullPointerException("permissions");
Map<String, Boolean> 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;
}
}

View File

@ -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<PermissionAttachment> 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<String, Boolean> getAttachmentPermissionsRaw(PermissionAttachment permissionAttachment)
{
return ReflectionUtil.getField(this.fieldAttachmentPermissions, permissionAttachment);
}
@Override
public void setAttachmentPermissionsRaw(PermissionAttachment permissionAttachment, Map<String, Boolean> permissions)
{
ReflectionUtil.setField(this.fieldAttachmentPermissions, permissionAttachment, permissions);
}
}

View File

@ -140,27 +140,40 @@ public class PermUtil
// ONE FIELD // 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 need created: FALSE
// Recalculation auto-performed: FALSE // Recalculation auto-performed: FALSE
permission.setDescription(description); 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 == null) throw new NullPointerException("permission");
if (permission.getDefault() == defaultValue) return; if (defaultValue == null) return false;
// NoChange
if (permission.getDefault() == defaultValue) return false;
// Recalculation need created: TRUE // Recalculation need created: TRUE
// Recalculation auto-performed: TRUE // Recalculation auto-performed: TRUE
permission.setDefault(defaultValue); permission.setDefault(defaultValue);
return true;
} }
public static void set(Permission permission, Map<String, Boolean> children) public static boolean set(Permission permission, Map<String, Boolean> children)
{ {
if (children == null) return; if (permission == null) throw new NullPointerException("permission");
if (permission.getChildren().equals(children)) return; if (children == null) return false;
// NoChange
if (children.equals(permission.getChildren())) return false;
// Recalculation need created: TRUE // Recalculation need created: TRUE
// Recalculation auto-performed: FALSE // Recalculation auto-performed: FALSE
@ -169,28 +182,34 @@ public class PermUtil
// Manual Recalculation // Manual Recalculation
permission.recalculatePermissibles(); permission.recalculatePermissibles();
return true;
} }
// TWO FIELDS // 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); boolean ret = false;
set(permission, description); ret |= set(permission, description);
ret |= set(permission, defaultValue);
return ret;
} }
public static void set(Permission permission, String description, Map<String, Boolean> children) public static boolean set(Permission permission, String description, Map<String, Boolean> children)
{ {
set(permission, children); boolean ret = false;
set(permission, description); ret |= set(permission, description);
ret |= set(permission, children);
return ret;
} }
public static void set(Permission permission, PermissionDefault defaultValue, Map<String, Boolean> children) public static boolean set(Permission permission, PermissionDefault defaultValue, Map<String, Boolean> children)
{ {
boolean childrenChanged = false; boolean childrenChanged = false;
boolean defaultChanged = false; boolean defaultChanged = false;
if ( ! permission.getChildren().equals(children)) if (children != null && ! children.equals(permission.getChildren()))
{ {
// Recalculation need created: TRUE // Recalculation need created: TRUE
// Recalculation auto-performed: FALSE // Recalculation auto-performed: FALSE
@ -199,7 +218,7 @@ public class PermUtil
childrenChanged = true; childrenChanged = true;
} }
if (permission.getDefault() != defaultValue) if (defaultValue != null && defaultValue != permission.getDefault())
{ {
// Recalculation need created: TRUE // Recalculation need created: TRUE
// Recalculation auto-performed: TRUE // Recalculation auto-performed: TRUE
@ -213,22 +232,47 @@ public class PermUtil
// Manual Recalculation // Manual Recalculation
permission.recalculatePermissibles(); permission.recalculatePermissibles();
} }
return childrenChanged || defaultChanged;
} }
// THREE FIELDS // THREE FIELDS
public static void set(Permission permission, String description, PermissionDefault defaultValue, Map<String, Boolean> children) public static boolean set(Permission permission, String description, PermissionDefault defaultValue, Map<String, Boolean> children)
{ {
set(permission, defaultValue, children); boolean ret = false;
set(permission, description); 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<String, Boolean> 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 // 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<String, Boolean> children) public static Permission get(boolean create, boolean update, String name, String description, PermissionDefault defaultValue, Map<String, Boolean> children)
{ {
Permission ret = Bukkit.getPluginManager().getPermission(name); Permission ret = Bukkit.getPluginManager().getPermission(name);
@ -236,7 +280,7 @@ public class PermUtil
{ {
if (create) if (create)
{ {
ret = new Permission(name, description, defaultValue, children); ret = construct(name, description, defaultValue, children);
Bukkit.getPluginManager().addPermission(ret); Bukkit.getPluginManager().addPermission(ret);
} }
} }
@ -254,146 +298,41 @@ public class PermUtil
public static Permission get(boolean create, String name) public static Permission get(boolean create, String name)
{ {
Permission ret = Bukkit.getPluginManager().getPermission(name); return get(create, false, name, null, null, null);
if (ret == null)
{
if (create)
{
ret = new Permission(name);
Bukkit.getPluginManager().addPermission(ret);
}
}
return ret;
} }
// ONE FIELD // ONE FIELD
public static Permission get(boolean create, boolean update, String name, String description) public static Permission get(boolean create, boolean update, String name, String description)
{ {
Permission ret = Bukkit.getPluginManager().getPermission(name); return get(create, update, name, description, null, null);
if (ret == null)
{
if (create)
{
ret = new Permission(name, description);
Bukkit.getPluginManager().addPermission(ret);
}
}
else
{
if (update)
{
set(ret, description);
}
}
return ret;
} }
public static Permission get(boolean create, boolean update, String name, PermissionDefault defaultValue) public static Permission get(boolean create, boolean update, String name, PermissionDefault defaultValue)
{ {
Permission ret = Bukkit.getPluginManager().getPermission(name); return get(create, update, name, null, defaultValue, null);
if (ret == null)
{
if (create)
{
ret = new Permission(name, defaultValue);
Bukkit.getPluginManager().addPermission(ret);
}
}
else
{
if (update)
{
set(ret, defaultValue);
}
}
return ret;
} }
public static Permission get(boolean create, boolean update, String name, Map<String, Boolean> children) public static Permission get(boolean create, boolean update, String name, Map<String, Boolean> children)
{ {
Permission ret = Bukkit.getPluginManager().getPermission(name); return get(create, false, name, null, null, children);
if (ret == null)
{
if (create)
{
ret = new Permission(name, children);
Bukkit.getPluginManager().addPermission(ret);
}
}
else
{
if (update)
{
set(ret, children);
}
}
return ret;
} }
// TWO FIELDS // TWO FIELDS
public static Permission get(boolean create, boolean update, String name, String description, PermissionDefault defaultValue) public static Permission get(boolean create, boolean update, String name, String description, PermissionDefault defaultValue)
{ {
Permission ret = Bukkit.getPluginManager().getPermission(name); return get(create, false, name, description, defaultValue, null);
if (ret == null)
{
if (create)
{
ret = new Permission(name, description, defaultValue);
Bukkit.getPluginManager().addPermission(ret);
}
}
else
{
if (update)
{
set(ret, description, defaultValue);
}
}
return ret;
} }
public static Permission get(boolean create, boolean update, String name, String description, Map<String, Boolean> children) public static Permission get(boolean create, boolean update, String name, String description, Map<String, Boolean> children)
{ {
Permission ret = Bukkit.getPluginManager().getPermission(name); return get(create, false, name, description, null, children);
if (ret == null)
{
if (create)
{
ret = new Permission(name, description, children);
Bukkit.getPluginManager().addPermission(ret);
}
}
else
{
if (update)
{
set(ret, description, children);
}
}
return ret;
} }
public static Permission get(boolean create, boolean update, String name, PermissionDefault defaultValue, Map<String, Boolean> children) public static Permission get(boolean create, boolean update, String name, PermissionDefault defaultValue, Map<String, Boolean> children)
{ {
Permission ret = Bukkit.getPluginManager().getPermission(name); return get(create, false, name, null, defaultValue, children);
if (ret == null)
{
if (create)
{
ret = new Permission(name, defaultValue, children);
Bukkit.getPluginManager().addPermission(ret);
}
}
else
{
if (update)
{
set(ret, defaultValue, children);
}
}
return ret;
} }
} }