Permission System Improvements. TypeEnchantment Aliases. TypeMap settings.

This commit is contained in:
Olof Larsson 2016-06-16 19:28:15 +02:00
parent 9d85db13b1
commit baa090b39a
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
9 changed files with 198 additions and 117 deletions

View File

@ -72,13 +72,14 @@ public enum MassiveCorePerm implements Identified
// HAS // HAS
// -------------------------------------------- // // -------------------------------------------- //
public boolean has(Permissible permissible, boolean informSenderIfNot) public boolean has(Permissible permissible, boolean verboose)
{ {
return PermissionUtil.hasPermission(permissible, this.id, informSenderIfNot); return PermissionUtil.hasPermission(permissible, this, verboose);
} }
public boolean has(Permissible permissible) public boolean has(Permissible permissible)
{ {
return has(permissible, false); return PermissionUtil.hasPermission(permissible, this);
} }
} }

View File

@ -2,7 +2,6 @@ package com.massivecraft.massivecore.command.requirement;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.Identified;
import com.massivecraft.massivecore.command.MassiveCommand; import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.util.PermissionUtil; import com.massivecraft.massivecore.util.PermissionUtil;
@ -14,21 +13,15 @@ public class RequirementHasPerm extends RequirementAbstract
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public static RequirementHasPerm get(String permissionId) { return new RequirementHasPerm(permissionId); } public static RequirementHasPerm get(Object permission) { return new RequirementHasPerm(permission); }
public static RequirementHasPerm get(Identified identified) { return new RequirementHasPerm(identified); }
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public RequirementHasPerm(String permissionId) public RequirementHasPerm(Object permission)
{ {
this.permissionId = permissionId; this.permissionId = PermissionUtil.asPermissionId(permission);
}
public RequirementHasPerm(Identified identified)
{
this(identified.getId());
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -1,16 +1,57 @@
package com.massivecraft.massivecore.command.type; package com.massivecraft.massivecore.command.type;
import java.util.Collections; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
public class TypeEnchantment extends TypeAbstractChoice<Enchantment> public class TypeEnchantment extends TypeAbstractChoice<Enchantment>
{ {
// -------------------------------------------- //
// DATA
// -------------------------------------------- //
// http://minecraft.gamepedia.com/Enchanting#Enchantments
// The first name is taken from the wiki. Those names are those people think of.
// The second name is the Bukkit enum name.
// Thereafter comes assorted extras
public static Map<Integer, List<String>> ID_TO_RAWNAMES = new MassiveMap<Integer, List<String>>(
0, new MassiveList<>("Protection", "PROTECTION_ENVIRONMENTAL"),
1, new MassiveList<>("Fire Protection", "PROTECTION_FIRE"),
2, new MassiveList<>("Feather Falling", "PROTECTION_FALL", "FallProtection"),
3, new MassiveList<>("Blast Protection", "PROTECTION_EXPLOSIONS", "ExplosionProtection"),
4, new MassiveList<>("Projectile Protection", "PROTECTION_PROJECTILE", "ProjectileProtection"),
5, new MassiveList<>("Respiration", "OXYGEN", "Breathing"),
6, new MassiveList<>("Aqua Affinity", "WATER_WORKER"),
7, new MassiveList<>("Thorns", "THORNS"),
8, new MassiveList<>("Depth Strider", "DEPTH_STRIDER"),
9, new MassiveList<>("Frost Walker", "FROST_WALKER"),
16, new MassiveList<>("Sharpness", "DAMAGE_ALL"),
17, new MassiveList<>("Smite", "DAMAGE_UNDEAD"),
18, new MassiveList<>("Bane of Arthropods", "DAMAGE_ARTHROPODS", "BaneArthropods", "Arthropods"),
19, new MassiveList<>("Knockback", "KNOCKBACK"),
20, new MassiveList<>("Fire Aspect", "FIRE_ASPECT"),
21, new MassiveList<>("Looting", "LOOT_BONUS_MOBS"),
32, new MassiveList<>("Efficiency", "DIG_SPEED"),
33, new MassiveList<>("Silk Touch", "SILK_TOUCH"),
34, new MassiveList<>("Unbreaking", "DURABILITY"),
35, new MassiveList<>("Fortune", "LOOT_BONUS_BLOCKS"),
48, new MassiveList<>("Power", "ARROW_DAMAGE"),
49, new MassiveList<>("Punch", "ARROW_KNOCKBACK"),
50, new MassiveList<>("Flame", "ARROW_FIRE"),
51, new MassiveList<>("Infinity", "ARROW_INFINITE", "ArrowInfinity"),
61, new MassiveList<>("Luck of the Sea", "LUCK", "LuckOfSea", "LuckTheSea", "LuckSea"),
62, new MassiveList<>("Lure", "LURE"),
70, new MassiveList<>("Mending", "MENDING")
);
// -------------------------------------------- // // -------------------------------------------- //
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
@ -28,50 +69,42 @@ public class TypeEnchantment extends TypeAbstractChoice<Enchantment>
// OVERRIDE // OVERRIDE
// -------------------------------------------- // // -------------------------------------------- //
@SuppressWarnings("deprecation")
@Override @Override
public String getNameInner(Enchantment enchantment) public String getNameInner(Enchantment enchantment)
{ {
return Txt.getNicedEnumString(enchantment.getName()); String rawname = enchantment.getName();
List<String> rawnames = ID_TO_RAWNAMES.get(enchantment.getId());
if (rawnames != null) rawname = rawnames.get(0);
return Txt.getNicedEnumString(rawname);
} }
@SuppressWarnings("deprecation")
@Override @Override
public Set<String> getNamesInner(Enchantment enchantment) public Set<String> getNamesInner(Enchantment enchantment)
{ {
// Create
Set<String> ret = new MassiveSet<>(); Set<String> ret = new MassiveSet<>();
ret.add(this.getName(enchantment));
if (Enchantment.DAMAGE_ALL.equals(enchantment)) Collections.addAll(ret, "Sharpness"); // Fill
if (Enchantment.DAMAGE_ARTHROPODS.equals(enchantment)) Collections.addAll(ret, "BaneOfArthropdos"); List<String> raws = new MassiveList<>();
if (Enchantment.DAMAGE_UNDEAD.equals(enchantment)) Collections.addAll(ret, "Smite"); List<String> rawnames = ID_TO_RAWNAMES.get(enchantment.getId());
if (Enchantment.DIG_SPEED.equals(enchantment)) Collections.addAll(ret, "Efficiency"); if (rawnames != null) raws.addAll(rawnames);
if (Enchantment.DURABILITY.equals(enchantment)) Collections.addAll(ret, "Unbreaking"); raws.add(enchantment.getName());
if (Enchantment.THORNS.equals(enchantment)) Collections.addAll(ret); for (String raw : raws)
if (Enchantment.FIRE_ASPECT.equals(enchantment)) Collections.addAll(ret); {
if (Enchantment.KNOCKBACK.equals(enchantment)) Collections.addAll(ret); ret.add(Txt.getNicedEnumString(raw));
if (Enchantment.LOOT_BONUS_BLOCKS.equals(enchantment)) Collections.addAll(ret, "Fortune"); }
if (Enchantment.LOOT_BONUS_MOBS.equals(enchantment)) Collections.addAll(ret, "Looting");
if (Enchantment.OXYGEN.equals(enchantment)) Collections.addAll(ret, "Respiration", "Breathing");
if (Enchantment.PROTECTION_ENVIRONMENTAL.equals(enchantment)) Collections.addAll(ret, "Protection");
if (Enchantment.PROTECTION_EXPLOSIONS.equals(enchantment)) Collections.addAll(ret, "BlastProtection", "ExplosionProtection");
if (Enchantment.PROTECTION_FALL.equals(enchantment)) Collections.addAll(ret, "FeatherFalling", "FallProtection");
if (Enchantment.PROTECTION_FIRE.equals(enchantment)) Collections.addAll(ret, "FireProtection");
if (Enchantment.PROTECTION_PROJECTILE.equals(enchantment)) Collections.addAll(ret, "ProectileProtection");
if (Enchantment.SILK_TOUCH.equals(enchantment)) Collections.addAll(ret);
if (Enchantment.WATER_WORKER.equals(enchantment)) Collections.addAll(ret, "AquaAffinity");
if (Enchantment.ARROW_FIRE.equals(enchantment)) Collections.addAll(ret, "Flame");
if (Enchantment.ARROW_DAMAGE.equals(enchantment)) Collections.addAll(ret, "Power");
if (Enchantment.ARROW_KNOCKBACK.equals(enchantment)) Collections.addAll(ret, "Punch");
if (Enchantment.ARROW_INFINITE.equals(enchantment)) Collections.addAll(ret, "Infinity");
if (Enchantment.LUCK.equals(enchantment)) Collections.addAll(ret, "LuckOfSea", "LuckOfTheSea");
if (Enchantment.LURE.equals(enchantment)) Collections.addAll(ret);
// Return
return ret; return ret;
} }
@SuppressWarnings("deprecation")
@Override @Override
public String getIdInner(Enchantment enchantment) public String getIdInner(Enchantment enchantment)
{ {
return enchantment.getName(); return String.valueOf(enchantment.getId());
} }
} }

View File

@ -46,6 +46,20 @@ public abstract class TypeContainer<C extends Object, E> extends TypeAbstract<C>
return super.getName(); return super.getName();
} }
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private int indexStart = 0;
public int getIndexStart() { return this.indexStart; }
@SuppressWarnings("unchecked")
public <X> X setIndexStart(int indexStart) { this.indexStart = indexStart; return (X)this; }
private boolean indexVisible = true;
public boolean isIndexVisible() { return this.indexVisible; }
@SuppressWarnings("unchecked")
public <X> X setIndexVisible(boolean indexVisible) { this.indexVisible = indexVisible; return (X)this; }
// -------------------------------------------- // // -------------------------------------------- //
// WRITE VISUAL // WRITE VISUAL
// -------------------------------------------- // // -------------------------------------------- //
@ -62,16 +76,20 @@ public abstract class TypeContainer<C extends Object, E> extends TypeAbstract<C>
// Fill // Fill
List<E> elements = this.getContainerElementsOrdered(container); List<E> elements = this.getContainerElementsOrdered(container);
Type<E> innerType = this.getInnerType(); Type<E> innerType = this.getInnerType();
int index = -1; int index = this.getIndexStart();
for (E element : elements) for (E element : elements)
{ {
index++; Mson part = innerType.getVisualMson(element, sender);
Mson part = Mson.mson( if (this.isIndexVisible())
{
part = Mson.mson(
Mson.mson(String.valueOf(index)).color(ChatColor.WHITE), Mson.mson(String.valueOf(index)).color(ChatColor.WHITE),
Mson.SPACE, Mson.SPACE,
innerType.getVisualMson(element, sender) part
); );
}
parts.add(part); parts.add(part);
index++;
} }
// Return // Return
@ -94,12 +112,13 @@ public abstract class TypeContainer<C extends Object, E> extends TypeAbstract<C>
// Fill // Fill
List<E> elements = this.getContainerElementsOrdered(container); List<E> elements = this.getContainerElementsOrdered(container);
Type<E> innerType = this.getInnerType(); Type<E> innerType = this.getInnerType();
int index = -1; int index = this.getIndexStart();
for (E element : elements) for (E element : elements)
{ {
index++; String part = innerType.getVisual(element, sender);
String part = Txt.parse("<white>%d <yellow>%s", index, innerType.getVisual(element, sender)); if (this.isIndexVisible()) part = Txt.parse("<white>%d <yellow>%s", index, part);
parts.add(part); parts.add(part);
index++;
} }
// Return // Return

View File

@ -98,7 +98,7 @@ public class EngineMassiveCoreMain extends Engine
if (event.hasFormat()) return; if (event.hasFormat()) return;
// ... and we have a custom format in the config ... // ... and we have a custom format in the config ...
String customFormat = MassiveCoreMConf.get().getPermissionDeniedFormat(event.getPermissionName()); String customFormat = MassiveCoreMConf.get().getPermissionDeniedFormat(event.getPermissionId());
if (customFormat == null) return; if (customFormat == null) return;
// ... then make use of that format. // ... then make use of that format.

View File

@ -16,8 +16,8 @@ public class EventMassiveCorePermissionDeniedFormat extends EventMassiveCore
// FIELD // FIELD
// -------------------------------------------- // // -------------------------------------------- //
private final String permissionName; private final String permissionId;
public String getPermissionName() { return this.permissionName; } public String getPermissionId() { return this.permissionId; }
private String format; private String format;
public String getFormat() { return this.format; } public String getFormat() { return this.format; }
@ -28,9 +28,9 @@ public class EventMassiveCorePermissionDeniedFormat extends EventMassiveCore
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public EventMassiveCorePermissionDeniedFormat(String permissionName) public EventMassiveCorePermissionDeniedFormat(String permissionId)
{ {
this.permissionName = permissionName; this.permissionId = permissionId;
this.format = null; this.format = null;
} }

View File

@ -176,15 +176,15 @@ public class Entity<E extends Entity<E>> implements Identified
// -------------------------------------------- // // -------------------------------------------- //
// GENERIC // GENERIC
public <T> T convertGet(T value, T defaultValue) public <T> T convertGet(T value, T standard)
{ {
return value != null ? value : defaultValue; return value != null ? value : standard;
} }
public <T> T convertSet(T value, T defaultValue) public <T> T convertSet(T value, T standard)
{ {
this.changed(); this.changed();
return Objects.equals(value, defaultValue) ? null : value; return Objects.equals(value, standard) ? null : value;
} }
// BOOLEAN // BOOLEAN

View File

@ -10,6 +10,7 @@ import org.bukkit.entity.Player;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.massivecraft.massivecore.Named; import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.PlayerState;
import com.massivecraft.massivecore.event.EventMassiveCoreAknowledge; import com.massivecraft.massivecore.event.EventMassiveCoreAknowledge;
import com.massivecraft.massivecore.mixin.MixinDisplayName; import com.massivecraft.massivecore.mixin.MixinDisplayName;
import com.massivecraft.massivecore.mixin.MixinMessage; import com.massivecraft.massivecore.mixin.MixinMessage;
@ -70,22 +71,53 @@ public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E>
// -------------------------------------------- // // -------------------------------------------- //
// GENERIC // GENERIC
public <T> T convertGet(T value, T defaultValue, String permissionId) public <T> T convertGet(T value, T standard, Object permission)
{ {
// Create // Create
T ret = super.convertGet(value, defaultValue); T ret = this.convertGet(value, standard);
// Permission Requirement // Permission Requirement
if ( ! Objects.equal(value, defaultValue) && ! PermissionUtil.hasPermission(this.getSender(), permissionId)) return defaultValue; if ( ! Objects.equal(value, standard) && !this.hasPermission(permission, true)) return standard;
// Return // Return
return ret; return ret;
} }
// BOOLEAN // BOOLEAN
public boolean convertGet(Boolean value, String permissionId) public boolean convertGet(Boolean value, Object permission)
{ {
return this.convertGet(value, false, permissionId); return this.convertGet(value, false, permission);
}
// -------------------------------------------- //
// CONVENIENCE: PERMISSON
// -------------------------------------------- //
public Boolean hasPermission(Object permission, Boolean standard, boolean verboose)
{
// Null
if (permission == null) throw new NullPointerException("permission");
// Sender
CommandSender sender = this.getSender();
if (sender == null) return standard;
// Players must be fully joined.
// The permission manager may not have updated permissions during the early login/connect stages.
if (sender instanceof Player)
{
Player player = (Player)sender;
PlayerState state = PlayerState.get(player);
if (state != PlayerState.JOINED) return standard;
}
// Check
return PermissionUtil.hasPermission(sender, permission, verboose);
}
public Boolean hasPermission(Object permission, Boolean standard)
{
return this.hasPermission(permission, standard, false);
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -13,6 +13,7 @@ import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.massivecraft.massivecore.Identified;
import com.massivecraft.massivecore.Lang; import com.massivecraft.massivecore.Lang;
import com.massivecraft.massivecore.MassiveCore; import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.event.EventMassiveCorePermissionDeniedFormat; import com.massivecraft.massivecore.event.EventMassiveCorePermissionDeniedFormat;
@ -70,6 +71,32 @@ public class PermissionUtil
ensureHas(permissible, permission.getName()); ensureHas(permissible, permission.getName());
} }
// -------------------------------------------- //
// AS
// -------------------------------------------- //
public static String asPermissionId(Object object)
{
if (object == null) return null;
if (object instanceof String) return (String)object;
if (object instanceof Identified) return ((Identified)object).getId();
if (object instanceof Permission) return getPermissionId((Permission)object);
throw new IllegalArgumentException(object.toString());
}
public static Permission asPermission(Object object)
{
if (object == null) return null;
if (object instanceof Permission) return (Permission)object;
String permissionId = asPermissionId(object);
Permission permission = getPermission(false, permissionId);
return permission;
}
// -------------------------------------------- // // -------------------------------------------- //
// PERMISSION > CONSTRUCT // PERMISSION > CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
@ -311,17 +338,11 @@ public class PermissionUtil
// The action should fit into the format for a denied message such as: // The action should fit into the format for a denied message such as:
// You don't have permission to FLY TO THE MOON. // You don't have permission to FLY TO THE MOON.
public static String getPermissionAction(String permissionId) public static String getPermissionAction(Object permission)
{ {
if (permissionId == null) return Lang.PERM_DEFAULT_DESCRIPTION; Permission permissionBukkit = asPermission(permission);
Permission permission = Bukkit.getPluginManager().getPermission(permissionId); if (permissionBukkit == null) return Lang.PERM_DEFAULT_DESCRIPTION;
return getPermissionAction(permission); String ret = getPermissionDescription(permissionBukkit);
}
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; if (ret == null || ret.isEmpty()) ret = Lang.PERM_DEFAULT_DESCRIPTION;
return ret; return ret;
} }
@ -330,8 +351,9 @@ public class PermissionUtil
// PERMISSION > DENIED FORMAT // PERMISSION > DENIED FORMAT
// -------------------------------------------- // // -------------------------------------------- //
public static String getPermissionDeniedFormat(String permissionId) public static String getPermissionDeniedFormat(Object permission)
{ {
String permissionId = asPermissionId(permission);
EventMassiveCorePermissionDeniedFormat event = new EventMassiveCorePermissionDeniedFormat(permissionId); EventMassiveCorePermissionDeniedFormat event = new EventMassiveCorePermissionDeniedFormat(permissionId);
event.run(); event.run();
String ret = event.getFormat(); String ret = event.getFormat();
@ -339,23 +361,11 @@ public class PermissionUtil
return ret; return ret;
} }
public static String getPermissionDeniedFormat(Permission permission)
{
return getPermissionDeniedFormat(permission == null ? null : permission.getName());
}
// -------------------------------------------- // // -------------------------------------------- //
// PERMISSION > DENIED MESSAGE // PERMISSION > DENIED MESSAGE
// -------------------------------------------- // // -------------------------------------------- //
public static String getPermissionDeniedMessage(String permissionId) public static String getPermissionDeniedMessage(Object permission)
{
String deniedFormat = getPermissionDeniedFormat(permissionId);
String action = getPermissionAction(permissionId);
return Txt.parse(deniedFormat, action);
}
public static String getPermissionDeniedMessage(Permission permission)
{ {
String deniedFormat = getPermissionDeniedFormat(permission); String deniedFormat = getPermissionDeniedFormat(permission);
String action = getPermissionAction(permission); String action = getPermissionAction(permission);
@ -366,39 +376,32 @@ public class PermissionUtil
// PERMISSION > HAS // PERMISSION > HAS
// -------------------------------------------- // // -------------------------------------------- //
public static boolean hasPermission(Permissible permissable, Permission permission) public static boolean hasPermission(Permissible permissible, Object permission, boolean verbose)
{ {
return hasPermission(permissable, permission.getName()); // Fail Fast
} if (permissible == null) throw new NullPointerException("permissible");
if (permission == null) throw new NullPointerException("permission");
public static boolean hasPermission(Permissible permissable, String permissionId) String permissionId = asPermissionId(permission);
{ if (permissionId == null) throw new NullPointerException("permissionId");
if (permissable == null) return false;
return permissable.hasPermission(permissionId);
}
public static boolean hasPermission(Permissible permissable, Permission permission, boolean verbose) if (permissible.hasPermission(permissionId)) return true;
{
return hasPermission(permissable, permission.getName(), verbose);
}
public static boolean hasPermission(Permissible permissible, String permissionId, boolean verbose) if (verbose && permissible instanceof CommandSender)
{
if (hasPermission(permissible, permissionId))
{
return true;
}
else if (verbose && permissible != null)
{
if (permissible instanceof CommandSender)
{ {
CommandSender sender = (CommandSender)permissible; CommandSender sender = (CommandSender)permissible;
sender.sendMessage(getPermissionDeniedMessage(permissionId)); String message = getPermissionDeniedMessage(permission);
} sender.sendMessage(message);
} }
return false; return false;
} }
public static boolean hasPermission(Permissible permissible, Object permission)
{
return hasPermission(permissible, permission, false);
}
// -------------------------------------------- // // -------------------------------------------- //
// PERMISSIBLE > BASE // PERMISSIBLE > BASE
// -------------------------------------------- // // -------------------------------------------- //