Rename Perm --> PermUtil and improved permission management utilities.

This commit is contained in:
Olof Larsson 2013-01-15 16:08:43 +01:00
parent cfd3ff0d94
commit 0185870205
5 changed files with 383 additions and 166 deletions

View File

@ -2,7 +2,7 @@ package com.massivecraft.mcore5;
import org.bukkit.command.CommandSender;
import com.massivecraft.mcore5.util.Perm;
import com.massivecraft.mcore5.util.PermUtil;
public enum Permission
{
@ -33,7 +33,7 @@ public enum Permission
public boolean has(CommandSender sender, boolean informSenderIfNot)
{
return Perm.has(sender, this.node, informSenderIfNot);
return PermUtil.has(sender, this.node, informSenderIfNot);
}
public boolean has(CommandSender sender)

View File

@ -18,7 +18,7 @@ import com.massivecraft.mcore5.cmd.arg.ArgResult;
import com.massivecraft.mcore5.cmd.req.IReq;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.util.BukkitCommandUtil;
import com.massivecraft.mcore5.util.Perm;
import com.massivecraft.mcore5.util.PermUtil;
import com.massivecraft.mcore5.util.Txt;
public abstract class MCommand
@ -77,7 +77,7 @@ public abstract class MCommand
String perm = this.getDescPermission();
if (perm != null)
{
String pdesc = Perm.getPermissionDescription(this.getDescPermission());
String pdesc = PermUtil.getPermissionDescription(this.getDescPermission());
if (pdesc != null)
{
return pdesc;

View File

@ -6,7 +6,7 @@ import lombok.Setter;
import org.bukkit.command.CommandSender;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.util.Perm;
import com.massivecraft.mcore5.util.PermUtil;
public class ReqHasPerm implements IReq
{
@ -26,7 +26,7 @@ public class ReqHasPerm implements IReq
@Override
public String createErrorMessage(CommandSender sender, MCommand command)
{
return Perm.getForbiddenMessage(this.perm);
return PermUtil.getForbiddenMessage(this.perm);
}
public static ReqHasPerm get(String perm)

View File

@ -1,160 +0,0 @@
package com.massivecraft.mcore5.util;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import com.massivecraft.mcore5.Lang;
import com.massivecraft.mcore5.MCore;
public class Perm
{
// -------------------------------------------- //
// HAS
// -------------------------------------------- //
public static boolean has(CommandSender sender, Permission permission)
{
return has(sender, permission.getName());
}
public static boolean has(CommandSender sender, String perm)
{
if (sender == null) return false;
return sender.hasPermission(perm);
}
public static boolean has(CommandSender sender, Permission permission, boolean verbose)
{
return has(sender, permission.getName(), verbose);
}
public static boolean has(CommandSender sender, String perm, boolean verbose)
{
if (has(sender, perm))
{
return true;
}
else if (verbose && sender != null)
{
sender.sendMessage(getForbiddenMessage(perm));
}
return false;
}
// -------------------------------------------- //
// DESCRIPTIONS AND MESSAGES
// -------------------------------------------- //
public static String getPermissionDescription(String perm)
{
if (perm == null) return Lang.permDoThat;
Permission permission = Bukkit.getPluginManager().getPermission(perm);
return getPermissionDescription(permission);
}
public static String getPermissionDescription(Permission perm)
{
if (perm == null) return Lang.permDoThat;
String desc = perm.getDescription();
if (desc == null || desc.length() == 0) return Lang.permDoThat;
return desc;
}
public static String getForbiddenMessage(String perm)
{
return Txt.parse(Lang.permForbidden, getPermissionDescription(perm));
}
// -------------------------------------------- //
// RANDOM UTILS
// -------------------------------------------- //
public static <T> T pickFirstVal(CommandSender me, Map<String, T> perm2val)
{
if (perm2val == null) return null;
T ret = null;
for ( Entry<String, T> entry : perm2val.entrySet())
{
ret = entry.getValue();
if (has(me, entry.getKey())) break;
}
return ret;
}
// -------------------------------------------- //
// ENSURE HAS
// -------------------------------------------- //
public static void ensureHas(Player player, String permissionName)
{
if (player.hasPermission(permissionName))
{
return;
}
else
{
player.addAttachment(MCore.p, permissionName, true);
}
}
public static void ensureHas(Player player, Permission permission)
{
ensureHas(player, permission.getName());
}
// -------------------------------------------- //
// GET CREATIVE
// -------------------------------------------- //
public static Permission getCreative(String name)
{
return getCreative(name, null, null, null);
}
public static Permission getCreative(String name, String description)
{
return getCreative(name, description, null, null);
}
public static Permission getCreative(String name, PermissionDefault defaultValue)
{
return getCreative(name, null, defaultValue, null);
}
public static Permission getCreative(String name, String description, PermissionDefault defaultValue)
{
return getCreative(name, description, defaultValue, null);
}
public static Permission getCreative(String name, Map<String, Boolean> children)
{
return getCreative(name, null, null, children);
}
public static Permission getCreative(String name, String description, Map<String, Boolean> children)
{
return getCreative(name, description, null, children);
}
public static Permission getCreative(String name, PermissionDefault defaultValue, Map<String, Boolean> children)
{
return getCreative(name, null, defaultValue, children);
}
public static Permission getCreative(String name, String description, PermissionDefault defaultValue, Map<String, Boolean> children)
{
Permission ret = Bukkit.getPluginManager().getPermission(name);
if (ret == null)
{
ret = new Permission(name, description, defaultValue, children);
Bukkit.getPluginManager().addPermission(ret);
}
return ret;
}
}

View File

@ -0,0 +1,377 @@
package com.massivecraft.mcore5.util;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import com.massivecraft.mcore5.Lang;
import com.massivecraft.mcore5.MCore;
public class PermUtil
{
// -------------------------------------------- //
// HAS
// -------------------------------------------- //
public static boolean has(CommandSender sender, Permission permission)
{
return has(sender, permission.getName());
}
public static boolean has(CommandSender sender, String perm)
{
if (sender == null) return false;
return sender.hasPermission(perm);
}
public static boolean has(CommandSender sender, Permission permission, boolean verbose)
{
return has(sender, permission.getName(), verbose);
}
public static boolean has(CommandSender sender, String perm, boolean verbose)
{
if (has(sender, perm))
{
return true;
}
else if (verbose && sender != null)
{
sender.sendMessage(getForbiddenMessage(perm));
}
return false;
}
// -------------------------------------------- //
// DESCRIPTIONS AND MESSAGES
// -------------------------------------------- //
public static String getPermissionDescription(String perm)
{
if (perm == null) return Lang.permDoThat;
Permission permission = Bukkit.getPluginManager().getPermission(perm);
return getPermissionDescription(permission);
}
public static String getPermissionDescription(Permission perm)
{
if (perm == null) return Lang.permDoThat;
String desc = perm.getDescription();
if (desc == null || desc.length() == 0) return Lang.permDoThat;
return desc;
}
public static String getForbiddenMessage(String perm)
{
return Txt.parse(Lang.permForbidden, getPermissionDescription(perm));
}
// -------------------------------------------- //
// RANDOM UTILS
// -------------------------------------------- //
public static <T> T pickFirstVal(CommandSender me, Map<String, T> perm2val)
{
if (perm2val == null) return null;
T ret = null;
for ( Entry<String, T> entry : perm2val.entrySet())
{
ret = entry.getValue();
if (has(me, entry.getKey())) break;
}
return ret;
}
// -------------------------------------------- //
// ENSURE HAS
// -------------------------------------------- //
public static void ensureHas(Player player, String permissionName)
{
if (player.hasPermission(permissionName))
{
return;
}
else
{
player.addAttachment(MCore.p, permissionName, true);
}
}
public static void ensureHas(Player player, Permission permission)
{
ensureHas(player, permission.getName());
}
// -------------------------------------------- //
// EFFICIENT UPDATERS
// -------------------------------------------- //
// 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.
// ONE FIELD
public static void set(Permission permission, String description)
{
// Recalculation need created: FALSE
// Recalculation auto-performed: FALSE
permission.setDescription(description);
}
public static void set(Permission permission, PermissionDefault defaultValue)
{
if (defaultValue == null) return;
if (permission.getDefault() == defaultValue) return;
// Recalculation need created: TRUE
// Recalculation auto-performed: TRUE
permission.setDefault(defaultValue);
}
public static void set(Permission permission, Map<String, Boolean> children)
{
if (children == null) return;
if (permission.getChildren().equals(children)) return;
// Recalculation need created: TRUE
// Recalculation auto-performed: FALSE
permission.getChildren().clear();
permission.getChildren().putAll(children);
// Manual Recalculation
permission.recalculatePermissibles();
}
// TWO FIELDS
public static void set(Permission permission, String description, PermissionDefault defaultValue)
{
set(permission, defaultValue);
set(permission, description);
}
public static void set(Permission permission, String description, Map<String, Boolean> children)
{
set(permission, children);
set(permission, description);
}
public static void set(Permission permission, PermissionDefault defaultValue, Map<String, Boolean> children)
{
boolean childrenChanged = false;
boolean defaultChanged = false;
if ( ! permission.getChildren().equals(children))
{
// Recalculation need created: TRUE
// Recalculation auto-performed: FALSE
permission.getChildren().clear();
permission.getChildren().putAll(children);
childrenChanged = true;
}
if (permission.getDefault() != defaultValue)
{
// Recalculation need created: TRUE
// Recalculation auto-performed: TRUE
permission.setDefault(defaultValue);
defaultChanged = true;
}
// Only recalculate if default wasn't changed since that would have caused a recalculation
if (childrenChanged && ! defaultChanged)
{
// Manual Recalculation
permission.recalculatePermissibles();
}
}
// THREE FIELDS
public static void set(Permission permission, String description, PermissionDefault defaultValue, Map<String, Boolean> children)
{
set(permission, defaultValue, children);
set(permission, description);
}
// -------------------------------------------- //
// 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)
{
Permission ret = Bukkit.getPluginManager().getPermission(name);
if (ret == null)
{
if (create)
{
ret = new Permission(name, description, defaultValue, children);
Bukkit.getPluginManager().addPermission(ret);
}
}
else
{
if (update)
{
set(ret, description, defaultValue, children);
}
}
return ret;
}
// ZERO FIELDS
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;
}
// 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;
}
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;
}
public static Permission get(boolean create, boolean update, String name, Map<String, Boolean> 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;
}
// 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;
}
public static Permission get(boolean create, boolean update, String name, String description, Map<String, Boolean> 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;
}
public static Permission get(boolean create, boolean update, String name, PermissionDefault defaultValue, Map<String, Boolean> 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;
}
}