Better permission nodes, player references and ensureHas utility in Perm.

This commit is contained in:
Olof Larsson 2013-01-05 14:25:35 +01:00
parent b13f8f2df0
commit e4da009bfc
25 changed files with 230 additions and 100 deletions

View File

@ -6,51 +6,77 @@ permissions:
# -------------------------------------------- #
# THE REAL NODES
# -------------------------------------------- #
mcore.usys: {description: manage the universe system, default: op}
mcore.usys.multiverse: {description: manage multiverses, default: op}
mcore.usys.multiverse.list: {description: list multiverses, default: op}
mcore.usys.multiverse.show: {description: show multiverse, default: op}
mcore.usys.multiverse.new: {description: create new multiverse, default: op}
mcore.usys.multiverse.del: {description: delete multiverse, default: op}
mcore.usys.universe: {description: manage universes, default: op}
mcore.usys.universe.new: {description: create new universe in multiverse, default: op}
mcore.usys.universe.del: {description: delete universe in multiverse, default: op}
mcore.usys.universe.clear: {description: clear universe in multiverse, default: op}
mcore.usys.world: {description: set a worlds universe in a multiverse, default: op}
mcore.usys.aspect: {description: manage aspects, default: op}
mcore.usys.aspect.list: {description: list aspects, default: op}
mcore.usys.aspect.show: {description: show aspect, default: op}
mcore.usys.aspect.use: {description: set multiverse for aspect, default: op}
mcore.cmd.usys: {description: manage the universe system, default: false}
mcore.cmd.usys.multiverse: {description: manage multiverses, default: false}
mcore.cmd.usys.multiverse.list: {description: list multiverses, default: false}
mcore.cmd.usys.multiverse.show: {description: show multiverse, default: false}
mcore.cmd.usys.multiverse.new: {description: create new multiverse, default: false}
mcore.cmd.usys.multiverse.del: {description: delete multiverse, default: false}
mcore.cmd.usys.universe: {description: manage universes, default: false}
mcore.cmd.usys.universe.new: {description: create new universe in multiverse, default: false}
mcore.cmd.usys.universe.del: {description: delete universe in multiverse, default: false}
mcore.cmd.usys.universe.clear: {description: clear universe in multiverse, default: false}
mcore.cmd.usys.world: {description: set a worlds universe in a multiverse, default: false}
mcore.cmd.usys.aspect: {description: manage aspects, default: false}
mcore.cmd.usys.aspect.list: {description: list aspects, default: false}
mcore.cmd.usys.aspect.show: {description: show aspect, default: false}
mcore.cmd.usys.aspect.use: {description: set multiverse for aspect, default: false}
# -------------------------------------------- #
# STAR NOTATION
# -------------------------------------------- #
mcore.usys.multiverse.*:
mcore.cmd.usys.multiverse.*:
default: false
children:
mcore.cmd.usys.multiverse.list: true
mcore.cmd.usys.multiverse.show: true
mcore.cmd.usys.multiverse.new: true
mcore.cmd.usys.multiverse.del: true
mcore.cmd.usys.universe.*:
default: false
children:
mcore.cmd.usys.universe.new: true
mcore.cmd.usys.universe.del: true
mcore.cmd.usys.universe.clear: true
mcore.cmd.usys.aspect.*:
default: false
children:
mcore.cmd.usys.aspect.list: true
mcore.cmd.usys.aspect.show: true
mcore.cmd.usys.aspect.use: true
mcore.cmd.usys.*:
default: false
children:
mcore.cmd.usys.multiverse: true
mcore.cmd.usys.multiverse.*: true
mcore.cmd.usys.universe: true
mcore.cmd.usys.universe.*: true
mcore.cmd.usys.world: true
mcore.cmd.usys.aspect: true
mcore.cmd.usys.aspect.*: true
mcore.cmd.*:
default: false
children:
mcore.cmd.usys: true
mcore.cmd.usys.*: true
mcore.*:
default: false
children:
mcore.cmd.*: true
# -------------------------------------------- #
# KITS
# -------------------------------------------- #
mcore.kit.operator:
default: op
children:
mcore.usys.multiverse.list: true
mcore.usys.multiverse.show: true
mcore.usys.multiverse.new: true
mcore.usys.multiverse.del: true
mcore.usys.universe.*:
default: op
mcore.*: true
mcore.kit.readonly:
default: false
children:
mcore.usys.universe.new: true
mcore.usys.universe.del: true
mcore.usys.universe.clear: true
mcore.usys.world.*:
default: op
children:
mcore.usys.world: true
mcore.usys.aspect.*:
default: op
children:
mcore.usys.aspect.list: true
mcore.usys.aspect.show: true
mcore.usys.aspect.use: true
mcore.usys.*:
default: op
children:
mcore.usys.multiverse.*: true
mcore.usys.universe.*: true
mcore.usys.world.*: true
mcore.usys.aspect.*: true
mcore.cmd.usys: true
mcore.cmd.usys.multiverse: true
mcore.cmd.usys.multiverse.list: true
mcore.cmd.usys.multiverse.show: true
mcore.cmd.usys.universe: true
mcore.cmd.usys.aspect: true
mcore.cmd.usys.aspect.list: true
mcore.cmd.usys.aspect.show: true

View File

@ -12,6 +12,7 @@ import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
@ -69,6 +70,29 @@ public class InternalListener implements Listener
event.setCancelled(true);
}
// -------------------------------------------- //
// PLAYER REFERENCES
// -------------------------------------------- //
@EventHandler(priority = EventPriority.LOWEST)
public void playerReferencesLoginLowest(PlayerLoginEvent event)
{
PlayerColl.setPlayerRefferences(event.getPlayer().getName(), event.getPlayer());
}
@EventHandler(priority = EventPriority.MONITOR)
public void playerReferencesLoginMonitor(PlayerLoginEvent event)
{
if (event.getResult() == Result.ALLOWED) return;
PlayerColl.setPlayerRefferences(event.getPlayer().getName(), null);
}
@EventHandler(priority = EventPriority.MONITOR)
public void playerReferencesQuitMonitor(PlayerQuitEvent event)
{
PlayerColl.setPlayerRefferences(event.getPlayer().getName(), null);
}
// -------------------------------------------- //
// AFTER EVENTS
// -------------------------------------------- //

View File

@ -6,21 +6,21 @@ import com.massivecraft.mcore5.util.Perm;
public enum Permission
{
USYS("usys"),
USYS_MULTIVERSE("usys.multiverse"),
USYS_MULTIVERSE_LIST("usys.multiverse.list"),
USYS_MULTIVERSE_SHOW("usys.multiverse.show"),
USYS_MULTIVERSE_NEW("usys.multiverse.new"),
USYS_MULTIVERSE_DEL("usys.multiverse.del"),
USYS_UNIVERSE("usys.universe"),
USYS_UNIVERSE_NEW("usys.universe.new"),
USYS_UNIVERSE_DEL("usys.universe.del"),
USYS_UNIVERSE_CLEAR("usys.universe.clear"),
USYS_WORLD("usys.world"),
USYS_ASPECT("usys.aspect"),
USYS_ASPECT_LIST("usys.aspect.list"),
USYS_ASPECT_SHOW("usys.aspect.show"),
USYS_ASPECT_USE("usys.aspect.use"),
CMD_USYS("cmd.usys"),
CMD_USYS_MULTIVERSE("cmd.usys.multiverse"),
CMD_USYS_MULTIVERSE_LIST("cmd.usys.multiverse.list"),
CMD_USYS_MULTIVERSE_SHOW("cmd.usys.multiverse.show"),
CMD_USYS_MULTIVERSE_NEW("cmd.usys.multiverse.new"),
CMD_USYS_MULTIVERSE_DEL("cmd.usys.multiverse.del"),
CMD_USYS_UNIVERSE("cmd.usys.universe"),
CMD_USYS_UNIVERSE_NEW("cmd.usys.universe.new"),
CMD_USYS_UNIVERSE_DEL("cmd.usys.universe.del"),
CMD_USYS_UNIVERSE_CLEAR("cmd.usys.universe.clear"),
CMD_USYS_WORLD("cmd.usys.world"),
CMD_USYS_ASPECT("cmd.usys.aspect"),
CMD_USYS_ASPECT_LIST("cmd.usys.aspect.list"),
CMD_USYS_ASPECT_SHOW("cmd.usys.aspect.show"),
CMD_USYS_ASPECT_USE("cmd.usys.aspect.use"),
;
public final String node;

View File

@ -24,7 +24,7 @@ public class ARAspect extends ARAbstractSelect<Aspect>
@Override
public boolean canList(MCommand mcommand)
{
return Permission.USYS_ASPECT_LIST.has(mcommand.sender, false);
return Permission.CMD_USYS_ASPECT_LIST.has(mcommand.sender, false);
}
@Override

View File

@ -24,7 +24,7 @@ public class ARMultiverse extends ARAbstractSelect<Multiverse>
@Override
public boolean canList(MCommand mcommand)
{
return Permission.USYS_MULTIVERSE_LIST.has(mcommand.sender, false);
return Permission.CMD_USYS_MULTIVERSE_LIST.has(mcommand.sender, false);
}
@Override

View File

@ -98,6 +98,28 @@ public class PlayerColl<E extends PlayerEntity<E>> extends Coll<E, String>
});
}
// -------------------------------------------- //
// PLAYER REFFERENCE MANAGEMENT
// -------------------------------------------- //
protected void setPlayerRefference(String playerName, Player player)
{
E pentity = this.get(playerName, false);
if (pentity == null) return;
pentity.player = player;
pentity.playerInitiated = true;
}
public static void setPlayerRefferences(String playerName, Player player)
{
for (Coll<?, ?> coll : Coll.instances)
{
if (!(coll instanceof PlayerColl)) continue;
PlayerColl<?> pcoll = (PlayerColl<?>)coll;
pcoll.setPlayerRefference(playerName, player);
}
}
// -------------------------------------------- //
// ARGUMENT READERS
// -------------------------------------------- //

View File

@ -6,23 +6,22 @@ import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import com.massivecraft.mcore5.util.PlayerUtil;
import com.massivecraft.mcore5.util.Txt;
public abstract class PlayerEntity<E extends PlayerEntity<E>> extends Entity<E, String>
{
// The Bukkit player reference is initiated here
// It's kept updated using the InternalListener
protected transient Player player = null;
protected transient boolean playerInitiated = false;
public Player getPlayer()
{
if (this.getColl() == null) return null;
if (this.getColl().isLowercasing())
if ( ! this.playerInitiated)
{
return Bukkit.getPlayerExact(this.getId());
}
else
{
return PlayerUtil.getPlayerExact(this.getId());
this.player = Bukkit.getPlayerExact(this.getId());
this.playerInitiated = true;
}
return this.player;
}
@Override

View File

@ -25,7 +25,7 @@ public class CmdUsys extends UsysCommand
this.addSubCommand(this.cmdUsysWorld);
this.addSubCommand(this.cmdUsysAspect);
this.addRequirements(ReqHasPerm.get(Permission.USYS.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS.node));
}
@Override

View File

@ -19,7 +19,7 @@ public class CmdUsysAspect extends UsysCommand
this.addSubCommand(this.cmdUsysAspectShow);
this.addSubCommand(this.cmdUsysAspectUse);
this.addRequirements(ReqHasPerm.get(Permission.USYS_ASPECT.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_ASPECT.node));
}
@Override

View File

@ -17,7 +17,7 @@ public class CmdUsysAspectList extends UsysCommand
this.addAliases("l", "list");
this.addOptionalArg("page", "1");
this.addRequirements(ReqHasPerm.get(Permission.USYS_ASPECT_LIST.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_ASPECT_LIST.node));
}
@Override

View File

@ -13,7 +13,7 @@ public class CmdUsysAspectShow extends UsysCommand
this.addAliases("s", "show");
this.addRequiredArg("aspect");
this.addRequirements(ReqHasPerm.get(Permission.USYS_ASPECT_SHOW.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_ASPECT_SHOW.node));
}
@Override

View File

@ -15,7 +15,7 @@ public class CmdUsysAspectUse extends UsysCommand
this.addRequiredArg("aspect");
this.addRequiredArg("multiverse");
this.addRequirements(ReqHasPerm.get(Permission.USYS_ASPECT_USE.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_ASPECT_USE.node));
}
@Override

View File

@ -21,7 +21,7 @@ public class CmdUsysMultiverse extends UsysCommand
this.addSubCommand(this.cmdUsysMultiverseNew);
this.addSubCommand(this.cmdUsysMultiverseDel);
this.addRequirements(ReqHasPerm.get(Permission.USYS_MULTIVERSE.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_MULTIVERSE.node));
}
@Override

View File

@ -13,7 +13,7 @@ public class CmdUsysMultiverseDel extends UsysCommand
this.addAliases("d", "del");
this.addRequiredArg("multiverse");
this.addRequirements(ReqHasPerm.get(Permission.USYS_MULTIVERSE_DEL.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_MULTIVERSE_DEL.node));
}
@Override

View File

@ -17,7 +17,7 @@ public class CmdUsysMultiverseList extends UsysCommand
this.addAliases("l", "list");
this.addOptionalArg("page", "1");
this.addRequirements(ReqHasPerm.get(Permission.USYS_MULTIVERSE_LIST.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_MULTIVERSE_LIST.node));
}
@Override

View File

@ -11,7 +11,7 @@ public class CmdUsysMultiverseNew extends UsysCommand
this.addAliases("n", "new");
this.addRequiredArg("multiverse");
this.addRequirements(ReqHasPerm.get(Permission.USYS_MULTIVERSE_NEW.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_MULTIVERSE_NEW.node));
}
@Override

View File

@ -18,7 +18,7 @@ public class CmdUsysMultiverseShow extends UsysCommand
this.addAliases("s", "show");
this.addRequiredArg("multiverse");
this.addRequirements(ReqHasPerm.get(Permission.USYS_MULTIVERSE_SHOW.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_MULTIVERSE_SHOW.node));
}
@Override

View File

@ -19,7 +19,7 @@ public class CmdUsysUniverse extends UsysCommand
this.addSubCommand(this.cmdUsysUniverseDel);
this.addSubCommand(this.cmdUsysUniverseClear);
this.addRequirements(ReqHasPerm.get(Permission.USYS_UNIVERSE.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_UNIVERSE.node));
}
@Override

View File

@ -14,7 +14,7 @@ public class CmdUsysUniverseClear extends UsysCommand
this.addRequiredArg("universe");
this.addRequiredArg("multiverse");
this.addRequirements(ReqHasPerm.get(Permission.USYS_UNIVERSE_CLEAR.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_UNIVERSE_CLEAR.node));
}
@Override

View File

@ -14,7 +14,7 @@ public class CmdUsysUniverseDel extends UsysCommand
this.addRequiredArg("universe");
this.addRequiredArg("multiverse");
this.addRequirements(ReqHasPerm.get(Permission.USYS_MULTIVERSE_DEL.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_MULTIVERSE_DEL.node));
}
@Override

View File

@ -13,7 +13,7 @@ public class CmdUsysUniverseNew extends UsysCommand
this.addRequiredArg("universe");
this.addRequiredArg("multiverse");
this.addRequirements(ReqHasPerm.get(Permission.USYS_UNIVERSE_NEW.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_UNIVERSE_NEW.node));
}
@Override

View File

@ -14,7 +14,7 @@ public class CmdUsysWorld extends UsysCommand
this.addRequiredArg("universe");
this.addRequiredArg("multiverse");
this.addRequirements(ReqHasPerm.get(Permission.USYS_WORLD.node));
this.addRequirements(ReqHasPerm.get(Permission.CMD_USYS_WORLD.node));
}
@Override

View File

@ -25,8 +25,12 @@ import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import com.massivecraft.mcore5.InternalListener;
import com.massivecraft.mcore5.MCore;
@ -65,6 +69,7 @@ public class MUtil
// EVENT DERP
// -------------------------------------------- //
// Note that this one is unstable and invalid. It cannot catch all cases.
public static Material getEatenMaterial(PlayerInteractEvent event)
{
Action action = event.getAction();
@ -84,6 +89,53 @@ public class MUtil
return ret;
}
/**
* This method will return the ItemStack the player is trying to equip.
* If the click event would not result in equipping something null will be returned.
* Note that this algorithm is not perfect. It's an adequate guess.
*
* @param event The InventoryClickEvent to analyze.
* @return The ItemStack the player is trying to equip.
*/
public static ItemStack getEquipping(InventoryClickEvent event)
{
/*
System.out.println("---");
System.out.println("getInventory().getType() "+event.getInventory().getType());
System.out.println("getView().getTopInventory().getType() "+event.getView().getTopInventory().getType());
System.out.println("getView().getType() "+event.getView().getType());
System.out.println("getView().getBottomInventory().getType() "+event.getView().getBottomInventory().getType());
System.out.println("event.getSlotType() "+event.getSlotType());
System.out.println("event.getRawSlot() "+event.getRawSlot());
System.out.println("event.getSlot() "+event.getSlot());
*/
boolean isShiftClick = event.isShiftClick();
InventoryType inventoryType = event.getInventory().getType();
SlotType slotType = event.getSlotType();
ItemStack cursor = event.getCursor();
ItemStack currentItem = event.getCurrentItem();
if (isShiftClick)
{
if (inventoryType != InventoryType.CRAFTING) return null;
if (slotType == SlotType.CRAFTING) return null;
if (slotType == SlotType.ARMOR) return null;
if (slotType == SlotType.RESULT) return null;
if (currentItem.getType() == Material.AIR) return null;
return currentItem;
}
else
{
if (slotType == SlotType.ARMOR)
{
return cursor;
}
return null;
}
}
public static boolean isCombatEvent(EntityDamageEvent event)
{
if (event.getCause() != DamageCause.ENTITY_ATTACK && event.getCause() != DamageCause.PROJECTILE) return false;

View File

@ -5,10 +5,12 @@ 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
{
@ -85,6 +87,27 @@ public class Perm
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
// -------------------------------------------- //

View File

@ -45,6 +45,7 @@ public class PlayerUtil implements Listener
*/
protected static Map<String, Set<String>> lowerCaseStartOfNameToCorrectNames = new ConcurrentSkipListMap<String, Set<String>>();
// -------------------------------------------- //
// CONSTRUCTOR AND EVENT LISTENER
// -------------------------------------------- //
@ -78,23 +79,6 @@ public class PlayerUtil implements Listener
// PUBLIC METHODS
// -------------------------------------------- //
/**
* This is a faster version of the getPlayerExact method since this one is exact for real (no to lower case stuff).
*/
public static Player getPlayerExact(String exactPlayerName)
{
if (exactPlayerName == null) return null;
for (Player player : Bukkit.getOnlinePlayers())
{
if (player.getName().equals(exactPlayerName))
{
return player;
}
}
return null;
}
/**
* This method simply checks if the playerName is a valid one.
* Mojangs rules for Minecraft character registration is used.