MassiveCore - PermissionUtil Improvements

This commit is contained in:
Olof Larsson 2016-05-21 14:49:22 +02:00
parent f7183d440e
commit c485282d60
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
8 changed files with 315 additions and 185 deletions

View File

@ -73,7 +73,7 @@ public enum MassiveCorePerm
public boolean has(Permissible permissible, boolean informSenderIfNot)
{
return PermissionUtil.has(permissible, this.node, informSenderIfNot);
return PermissionUtil.hasPermission(permissible, this.node, informSenderIfNot);
}
public boolean has(Permissible permissible)

View File

@ -739,7 +739,7 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
String perm = this.getDescPermission();
if (perm != null)
{
String pdesc = PermissionUtil.getDescription(perm);
String pdesc = PermissionUtil.getPermissionAction(perm);
if (pdesc != null)
{
return pdesc;

View File

@ -36,7 +36,7 @@ public class RequirementHasPerm extends RequirementAbstract
@Override
public String createErrorMessage(CommandSender sender, MassiveCommand command)
{
return PermissionUtil.getDeniedMessage(this.perm);
return PermissionUtil.getPermissionDeniedMessage(this.perm);
}
}

View File

@ -3,7 +3,6 @@ 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;
@ -36,51 +35,26 @@ public class NmsPermissions extends Mixin
}
// -------------------------------------------- //
// PLAYER
// PERMISSIBLE
// -------------------------------------------- //
public PermissibleBase getBase(Player player)
public PermissibleBase getBase(Permissible permissible)
{
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)
public Map<String, Boolean> getAttachmentPermissions(PermissionAttachment permissionAttachment)
{
throw this.notImplemented();
}
public void setAttachmentPermissionsRaw(PermissionAttachment permissionAttachment, Map<String, Boolean> permissions)
public void setAttachmentPermissions(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

@ -5,6 +5,7 @@ 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;
@ -50,6 +51,7 @@ public class NmsPermissions17R4P extends NmsPermissions
// BASE
// -------------------------------------------- //
@Override
public List<PermissionAttachment> getAttachments(PermissibleBase base)
{
return ReflectionUtil.getField(this.fieldPermissibleBaseAttachments, base);
@ -59,9 +61,14 @@ public class NmsPermissions17R4P extends NmsPermissions
// PLAYER
// -------------------------------------------- //
public PermissibleBase getBase(Player player)
@Override
public PermissibleBase getBase(Permissible permissible)
{
return ReflectionUtil.getField(this.fieldCraftHumanEntityBase, player);
if (permissible instanceof Player)
{
return ReflectionUtil.getField(this.fieldCraftHumanEntityBase, permissible);
}
return null;
}
// -------------------------------------------- //
@ -69,13 +76,13 @@ public class NmsPermissions17R4P extends NmsPermissions
// -------------------------------------------- //
@Override
public Map<String, Boolean> getAttachmentPermissionsRaw(PermissionAttachment permissionAttachment)
public Map<String, Boolean> getAttachmentPermissions(PermissionAttachment permissionAttachment)
{
return ReflectionUtil.getField(this.fieldAttachmentPermissions, permissionAttachment);
}
@Override
public void setAttachmentPermissionsRaw(PermissionAttachment permissionAttachment, Map<String, Boolean> permissions)
public void setAttachmentPermissions(PermissionAttachment permissionAttachment, Map<String, Boolean> permissions)
{
ReflectionUtil.setField(this.fieldAttachmentPermissions, permissionAttachment, permissions);
}

View File

@ -75,7 +75,7 @@ public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E>
T ret = super.convertGet(value, defaultValue);
// Permission Requirement
if ( ! Objects.equal(value, defaultValue) && ! PermissionUtil.has(this.getSender(), permission)) return defaultValue;
if ( ! Objects.equal(value, defaultValue) && ! PermissionUtil.hasPermission(this.getSender(), permission)) return defaultValue;
// Return
return ret;

View File

@ -1,99 +1,32 @@
package com.massivecraft.massivecore.util;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.PermissibleBase;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.Plugin;
import com.massivecraft.massivecore.Lang;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.event.EventMassiveCorePermissionDeniedFormat;
import com.massivecraft.massivecore.nms.NmsPermissions;
public class PermissionUtil
{
// -------------------------------------------- //
// HAS
// -------------------------------------------- //
public static boolean has(Permissible permissable, Permission permission)
{
return has(permissable, permission.getName());
}
public static boolean has(Permissible permissable, String perm)
{
if (permissable == null) return false;
return permissable.hasPermission(perm);
}
public static boolean has(Permissible permissable, Permission permission, boolean verbose)
{
return has(permissable, permission.getName(), verbose);
}
public static boolean has(Permissible permissible, String perm, boolean verbose)
{
if (has(permissible, perm))
{
return true;
}
else if (verbose && permissible != null)
{
if (permissible instanceof CommandSender)
{
CommandSender sender = (CommandSender)permissible;
sender.sendMessage(getDeniedMessage(perm));
}
}
return false;
}
// -------------------------------------------- //
// DESCRIPTIONS AND MESSAGES
// -------------------------------------------- //
public static String getDescription(String perm)
{
if (perm == null) return Lang.PERM_DEFAULT_DESCRIPTION;
Permission permission = Bukkit.getPluginManager().getPermission(perm);
return getDescription(permission);
}
public static String getDescription(Permission perm)
{
if (perm == null) return Lang.PERM_DEFAULT_DESCRIPTION;
String desc = perm.getDescription();
if (desc == null || desc.length() == 0) return Lang.PERM_DEFAULT_DESCRIPTION;
return desc;
}
public static String getDeniedFormat(String perm)
{
EventMassiveCorePermissionDeniedFormat event = new EventMassiveCorePermissionDeniedFormat(perm);
event.run();
String ret = event.getFormat();
if (ret == null) ret = Lang.PERM_DEFAULT_DENIED_FORMAT;
return ret;
}
public static String getDeniedFormat(Permission perm)
{
return getDeniedFormat(perm == null ? null : perm.getName());
}
public static String getDeniedMessage(String perm)
{
return Txt.parse(getDeniedFormat(perm), getDescription(perm));
}
public static String getDeniedMessage(Permission perm)
{
return Txt.parse(getDeniedFormat(perm), getDescription(perm));
}
// -------------------------------------------- //
// RANDOM UTILS
// -------------------------------------------- //
// TODO: Gather more versions spread out over plugins,
// TODO: Place them all here. Were there some in MUtil?
public static <T> T pickFirstVal(Permissible permissible, Map<String, T> perm2val)
{
if (perm2val == null) return null;
@ -102,7 +35,7 @@ public class PermissionUtil
for (Entry<String, T> entry : perm2val.entrySet())
{
ret = entry.getValue();
if (has(permissible, entry.getKey())) break;
if (hasPermission(permissible, entry.getKey())) break;
}
return ret;
@ -112,6 +45,9 @@ public class PermissionUtil
// ENSURE HAS
// -------------------------------------------- //
// TODO: This methodology I don't beleive in any more.
// TODO: Rework and improve technology.
public static void ensureHas(Permissible permissible, String permissionName)
{
if (permissible.hasPermission(permissionName))
@ -130,50 +66,98 @@ public class PermissionUtil
}
// -------------------------------------------- //
// EFFICIENT UPDATERS
// PERMISSION > CONSTRUCT
// -------------------------------------------- //
// Our own constructor for name symmetry.
// Supplying null means using the default values.
public static Permission constructPermission(String id, String description, PermissionDefault standard, Map<String, Boolean> children)
{
if (id == null) throw new NullPointerException("id");
return new Permission(id, description, standard, children);
}
// -------------------------------------------- //
// PERMISSION > GET
// -------------------------------------------- //
// We supply and make use of our own getters for the same of naming symmetry.
// They are just wrappers but if we have setters we might as well have getters.
// According to MassiveCraft names are changeable and ids are not.
// Bukkit permissions lack names. They do however have ids.
public static String getPermissionId(Permission permission)
{
return permission.getName();
}
public static String getPermissionDescription(Permission permission)
{
return permission.getDescription();
}
// Default is a reserved Java keyword. Standard will work better.
public static PermissionDefault getPermissionStandard(Permission permission)
{
return permission.getDefault();
}
public static Map<String, Boolean> getPermissionChildren(Permission permission)
{
return permission.getChildren();
}
// -------------------------------------------- //
// PERMISSION > SET
// -------------------------------------------- //
// These setters offer bulk-ways of updating already created permissions.
// For the best performance you should enter all information into the Permission constructor when creating the Permission.
// At times you will however need to update permissions after they were created.
// In these cases the order and approach with witch you alter the fields matter a lot performance wise.
// These setter will ensure you get optimal performance.
// NoChange detection is also included to avoid recalculating in vain.
// Supplying null means no change is actually requested.
// ONE FIELD
public static boolean set(Permission permission, String description)
public static boolean setPermissionDescription(Permission permission, String description)
{
if (permission == null) throw new NullPointerException("permission");
if (description == null) return false;
String before = getPermissionDescription(permission);
if (description.equals(before)) return false;
// Recalculation need created: FALSE
// Recalculation auto-performed: FALSE
permission.setDescription(description);
return false;
return true;
}
public static boolean set(Permission permission, PermissionDefault defaultValue)
public static boolean setPermissionStandard(Permission permission, PermissionDefault standard)
{
if (permission == null) throw new NullPointerException("permission");
if (defaultValue == null) return false;
if (standard == null) return false;
// NoChange
if (permission.getDefault() == defaultValue) return false;
PermissionDefault before = getPermissionStandard(permission);
if (standard == before) return false;
// Recalculation need created: TRUE
// Recalculation auto-performed: TRUE
permission.setDefault(defaultValue);
permission.setDefault(standard);
return true;
}
public static boolean set(Permission permission, Map<String, Boolean> children)
public static boolean setPermissionChildren(Permission permission, Map<String, Boolean> children)
{
if (permission == null) throw new NullPointerException("permission");
if (children == null) return false;
// NoChange
if (children.equals(permission.getChildren())) return false;
Map<String, Boolean> before = getPermissionChildren(permission);
if (children.equals(before)) return false;
// Recalculation need created: TRUE
// Recalculation auto-performed: FALSE
@ -188,28 +172,29 @@ public class PermissionUtil
// TWO FIELDS
public static boolean set(Permission permission, String description, PermissionDefault defaultValue)
public static boolean setPermissionDescriptionStandard(Permission permission, String description, PermissionDefault standard)
{
boolean ret = false;
ret |= set(permission, description);
ret |= set(permission, defaultValue);
ret |= setPermissionDescription(permission, description);
ret |= setPermissionStandard(permission, standard);
return ret;
}
public static boolean set(Permission permission, String description, Map<String, Boolean> children)
public static boolean setPermissionDescriptionChildren(Permission permission, String description, Map<String, Boolean> children)
{
boolean ret = false;
ret |= set(permission, description);
ret |= set(permission, children);
ret |= setPermissionDescription(permission, description);
ret |= setPermissionChildren(permission, children);
return ret;
}
public static boolean set(Permission permission, PermissionDefault defaultValue, Map<String, Boolean> children)
public static boolean setPermissionStandardChildren(Permission permission, PermissionDefault standard, Map<String, Boolean> children)
{
boolean childrenChanged = false;
boolean defaultChanged = false;
boolean standardChanged = false;
if (children != null && ! children.equals(permission.getChildren()))
if (children != null && ! children.equals(getPermissionChildren(permission)))
{
// Recalculation need created: TRUE
// Recalculation auto-performed: FALSE
@ -218,69 +203,46 @@ public class PermissionUtil
childrenChanged = true;
}
if (defaultValue != null && defaultValue != permission.getDefault())
if (standard != null && standard != getPermissionStandard(permission))
{
// Recalculation need created: TRUE
// Recalculation auto-performed: TRUE
permission.setDefault(defaultValue);
defaultChanged = true;
permission.setDefault(standard);
standardChanged = true;
}
// Only recalculate if default wasn't changed since that would have caused a recalculation
if (childrenChanged && ! defaultChanged)
if (childrenChanged && ! standardChanged)
{
// Manual Recalculation
permission.recalculatePermissibles();
}
return childrenChanged || defaultChanged;
return childrenChanged || standardChanged;
}
// THREE FIELDS
public static boolean set(Permission permission, String description, PermissionDefault defaultValue, Map<String, Boolean> children)
public static boolean setPermissionDescriptionStandardChildren(Permission permission, String description, PermissionDefault standard, Map<String, Boolean> children)
{
boolean ret = false;
ret |= set(permission, defaultValue, children);
ret |= set(permission, description);
ret |= setPermissionStandardChildren(permission, standard, children);
ret |= setPermissionDescription(permission, description);
return ret;
}
// -------------------------------------------- //
// CONSTRUCT
// PERMISSION > GET (CREATE / UPDATE)
// -------------------------------------------- //
// 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)
public static Permission getPermission(boolean create, boolean update, String id, String description, PermissionDefault standard, 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
// -------------------------------------------- //
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(id);
if (ret == null)
{
if (create)
{
ret = construct(name, description, defaultValue, children);
ret = constructPermission(id, description, standard, children);
Bukkit.getPluginManager().addPermission(ret);
}
}
@ -288,7 +250,7 @@ public class PermissionUtil
{
if (update)
{
set(ret, description, defaultValue, children);
setPermissionDescriptionStandardChildren(ret, description, standard, children);
}
}
return ret;
@ -296,43 +258,230 @@ public class PermissionUtil
// ZERO FIELDS
public static Permission get(boolean create, String name)
public static Permission getPermission(boolean create, String id)
{
return get(create, false, name, null, null, null);
return getPermission(create, false, id, null, null, null);
}
// ONE FIELD
public static Permission get(boolean create, boolean update, String name, String description)
public static Permission getPermission(boolean create, boolean update, String id, String description)
{
return get(create, update, name, description, null, null);
return getPermission(create, update, id, description, null, null);
}
public static Permission get(boolean create, boolean update, String name, PermissionDefault defaultValue)
public static Permission getPermission(boolean create, boolean update, String id, PermissionDefault standard)
{
return get(create, update, name, null, defaultValue, null);
return getPermission(create, update, id, null, standard, null);
}
public static Permission get(boolean create, boolean update, String name, Map<String, Boolean> children)
public static Permission getPermission(boolean create, boolean update, String id, Map<String, Boolean> children)
{
return get(create, false, name, null, null, children);
return getPermission(create, false, id, null, null, children);
}
// TWO FIELDS
public static Permission get(boolean create, boolean update, String name, String description, PermissionDefault defaultValue)
public static Permission getPermission(boolean create, boolean update, String id, String description, PermissionDefault standard)
{
return get(create, false, name, description, defaultValue, null);
return getPermission(create, false, id, description, standard, null);
}
public static Permission get(boolean create, boolean update, String name, String description, Map<String, Boolean> children)
public static Permission getPermission(boolean create, boolean update, String id, String description, Map<String, Boolean> children)
{
return get(create, false, name, description, null, children);
return getPermission(create, false, id, description, null, children);
}
public static Permission get(boolean create, boolean update, String name, PermissionDefault defaultValue, Map<String, Boolean> children)
public static Permission getPermission(boolean create, boolean update, String id, PermissionDefault standard, Map<String, Boolean> children)
{
return get(create, false, name, null, defaultValue, children);
return getPermission(create, false, id, null, standard, children);
}
// -------------------------------------------- //
// PERMISSION > ACTION
// -------------------------------------------- //
// We declare a fake field called "action".
// The action is usually the description.
// It can however never be null.
// The action should fit into the format for a denied message such as:
// You don't have permission to FLY TO THE MOON.
public static String getPermissionAction(String permissionId)
{
if (permissionId == null) return Lang.PERM_DEFAULT_DESCRIPTION;
Permission permission = Bukkit.getPluginManager().getPermission(permissionId);
return getPermissionAction(permission);
}
public static String getPermissionAction(Permission permission)
{
if (permission == null) return Lang.PERM_DEFAULT_DESCRIPTION;
String ret = getPermissionDescription(permission);
if (ret == null || ret.isEmpty()) ret = Lang.PERM_DEFAULT_DESCRIPTION;
return ret;
}
// -------------------------------------------- //
// PERMISSION > DENIED FORMAT
// -------------------------------------------- //
public static String getPermissionDeniedFormat(String permissionId)
{
EventMassiveCorePermissionDeniedFormat event = new EventMassiveCorePermissionDeniedFormat(permissionId);
event.run();
String ret = event.getFormat();
if (ret == null) ret = Lang.PERM_DEFAULT_DENIED_FORMAT;
return ret;
}
public static String getPermissionDeniedFormat(Permission permission)
{
return getPermissionDeniedFormat(permission == null ? null : permission.getName());
}
// -------------------------------------------- //
// PERMISSION > DENIED MESSAGE
// -------------------------------------------- //
public static String getPermissionDeniedMessage(String permissionId)
{
String deniedFormat = getPermissionDeniedFormat(permissionId);
String action = getPermissionAction(permissionId);
return Txt.parse(deniedFormat, action);
}
public static String getPermissionDeniedMessage(Permission permission)
{
String deniedFormat = getPermissionDeniedFormat(permission);
String action = getPermissionAction(permission);
return Txt.parse(deniedFormat, action);
}
// -------------------------------------------- //
// PERMISSION > HAS
// -------------------------------------------- //
public static boolean hasPermission(Permissible permissable, Permission permission)
{
return hasPermission(permissable, permission.getName());
}
public static boolean hasPermission(Permissible permissable, String permissionId)
{
if (permissable == null) return false;
return permissable.hasPermission(permissionId);
}
public static boolean hasPermission(Permissible permissable, Permission permission, boolean verbose)
{
return hasPermission(permissable, permission.getName(), verbose);
}
public static boolean hasPermission(Permissible permissible, String permissionId, boolean verbose)
{
if (hasPermission(permissible, permissionId))
{
return true;
}
else if (verbose && permissible != null)
{
if (permissible instanceof CommandSender)
{
CommandSender sender = (CommandSender)permissible;
sender.sendMessage(getPermissionDeniedMessage(permissionId));
}
}
return false;
}
// -------------------------------------------- //
// PERMISSIBLE > BASE
// -------------------------------------------- //
public static PermissibleBase getPermissibleBase(Permissible permissible)
{
return NmsPermissions.get().getBase(permissible);
}
// -------------------------------------------- //
// PERMISSIBLE > ATTACHMENT
// -------------------------------------------- //
// The Bukkit version recalculates permissions which does not make any sense.
// An empty attachment is not going to affect the effective permissions.
// Thus we make use of our own attachment creator that does not waste CPU.
public static PermissionAttachment createPermissibleAttachment(Permissible permissible, Plugin plugin)
{
if (permissible == null) throw new NullPointerException("permissible");
if (plugin == null) throw new NullPointerException("plugin");
List<PermissionAttachment> attachments = getPermissibleAttachments(permissible);
if (attachments == null) return null;
PermissionAttachment ret = new PermissionAttachment(plugin, permissible);
attachments.add(ret);
return ret;
}
public static List<PermissionAttachment> getPermissibleAttachments(Permissible permissible)
{
PermissibleBase base = getPermissibleBase(permissible);
if (base == null) return null;
return getBaseAttachments(base);
}
// This method returns the first attachment belonging to the plugin.
// The thought process here is that plugins rarely need more than one attachment.
// With that in mind we offer this per plugin singleton getter.
public static PermissionAttachment getPermissibleAttachment(Permissible permissible, Plugin plugin, boolean creative)
{
List<PermissionAttachment> attachments = getPermissibleAttachments(permissible);
if (attachments == null) return null;
for (PermissionAttachment attachment : attachments)
{
if (MUtil.equals(attachment.getPlugin(), plugin)) return attachment;
}
if (creative) return createPermissibleAttachment(permissible, plugin);
return null;
}
// -------------------------------------------- //
// BASE > ATTACHMENT
// -------------------------------------------- //
public static List<PermissionAttachment> getBaseAttachments(PermissibleBase base)
{
return NmsPermissions.get().getAttachments(base);
}
// -------------------------------------------- //
// ATTACHMENT > PERMISSIONS
// -------------------------------------------- //
public static Map<String, Boolean> getAttachmentPermissions(PermissionAttachment attachment)
{
return NmsPermissions.get().getAttachmentPermissions(attachment);
}
public static boolean setAttachmentPermissions(PermissionAttachment attachment, Map<String, Boolean> permissions)
{
if (attachment == null) throw new NullPointerException("attachment");
if (permissions == null) return false;
Map<String, Boolean> before = getAttachmentPermissions(attachment);
if (before.equals(permissions)) return false;
before.clear();
before.putAll(permissions);
Permissible permissible = attachment.getPermissible();
if (permissible != null) permissible.recalculatePermissions();
return true;
}
}

View File

@ -126,7 +126,7 @@ public class SignUtil
if ( ! title.equalsIgnoreCase(lenientTitle)) return false;
// ... verify that the player has permission to create that type of sign ...
if ( ! PermissionUtil.has(player, permissionNode, true))
if ( ! PermissionUtil.hasPermission(player, permissionNode, true))
{
event.setCancelled(true);
return false;