From 1b117c31320fab8e66519040b08f41dd8dc2deac Mon Sep 17 00:00:00 2001 From: Steven Scott Date: Mon, 15 Aug 2011 23:13:11 -0700 Subject: [PATCH] Complete re-write of Permissions Far more advanced API for checking if people can do things Better implementation of personal gate creation that works alongside other network restrictions Debug option now hides 90% of output if disabled Better handling of undefined costs in .gate files Network/World filter option now removed SuperPerms support Probably a lot more stuff I don't remember --- README | 2 + src/net/TheDgtl/Stargate/Gate.java | 21 +- src/net/TheDgtl/Stargate/Portal.java | 330 +++++------ src/net/TheDgtl/Stargate/Stargate.java | 548 ++++++++++++------- src/net/TheDgtl/Stargate/iConomyHandler.java | 4 +- src/plugin.yml | 2 +- 6 files changed, 539 insertions(+), 368 deletions(-) diff --git a/README b/README index 95e92b9..1c9352d 100644 --- a/README +++ b/README @@ -139,6 +139,8 @@ maxgates - If non-zero, will define the maximum amount of gates allowed on a net ============= Changes ============= +[Version 0.6.0] + - Oh god the changes, hopefully I didn't fuck anything up, README updates coming later [Version 0.5.5] - Added 'B'ackwards option - Fixed opening of gates with a fixed gate as a destination diff --git a/src/net/TheDgtl/Stargate/Gate.java b/src/net/TheDgtl/Stargate/Gate.java index 14bd3ab..d5efaa0 100644 --- a/src/net/TheDgtl/Stargate/Gate.java +++ b/src/net/TheDgtl/Stargate/Gate.java @@ -43,9 +43,9 @@ public class Gate { private int portalBlockClosed = Material.AIR.getId(); // iConomy information - private int useCost = 0; - private int createCost = 0; - private int destroyCost = 0; + private int useCost = -1; + private int createCost = -1; + private int destroyCost = -1; private boolean toOwner = false; private Gate(String filename, Character[][] layout, HashMap types, HashMap metadata) { @@ -108,11 +108,11 @@ public class Gate { writeConfig(bw, "portal-open", portalBlockOpen); writeConfig(bw, "portal-closed", portalBlockClosed); - if (useCost != iConomyHandler.useCost) + if (useCost != -1) writeConfig(bw, "usecost", useCost); - if (createCost != iConomyHandler.createCost) + if (createCost != -1) writeConfig(bw, "createcost", createCost); - if (destroyCost != iConomyHandler.destroyCost) + if (destroyCost != -1) writeConfig(bw, "destroycost", destroyCost); writeConfig(bw, "toowner", toOwner); @@ -199,14 +199,17 @@ public class Gate { } public int getUseCost() { + if (useCost < 0) return iConomyHandler.useCost; return useCost; } public Integer getCreateCost() { + if (createCost < 0) return iConomyHandler.createCost; return createCost; } public Integer getDestroyCost() { + if (destroyCost < 0) return iConomyHandler.destroyCost; return destroyCost; } @@ -345,9 +348,9 @@ public class Gate { gate.portalBlockOpen = readConfig(config, gate, file, "portal-open", gate.portalBlockOpen); gate.portalBlockClosed = readConfig(config, gate, file, "portal-closed", gate.portalBlockClosed); - gate.useCost = readConfig(config, gate, file, "usecost", iConomyHandler.useCost); - gate.destroyCost = readConfig(config, gate, file, "destroycost", iConomyHandler.destroyCost); - gate.createCost = readConfig(config, gate, file, "createcost", iConomyHandler.createCost); + gate.useCost = readConfig(config, gate, file, "usecost", -1); + gate.destroyCost = readConfig(config, gate, file, "destroycost", -1); + gate.createCost = readConfig(config, gate, file, "createcost", -1); gate.toOwner = (config.containsKey("toowner") ? Boolean.valueOf(config.get("toowner")) : iConomyHandler.toOwner); if (gate.getControls().length != 2) { diff --git a/src/net/TheDgtl/Stargate/Portal.java b/src/net/TheDgtl/Stargate/Portal.java index 0765bd7..94eacc1 100644 --- a/src/net/TheDgtl/Stargate/Portal.java +++ b/src/net/TheDgtl/Stargate/Portal.java @@ -32,7 +32,7 @@ import org.bukkit.util.Vector; */ public class Portal { - // Variables used to store portal lists + // Static variables used to store portal lists private static final HashMap lookupBlocks = new HashMap(); private static final HashMap lookupEntrances = new HashMap(); private static final ArrayList allPortals = new ArrayList(); @@ -44,17 +44,23 @@ public class Portal { private int modX; private int modZ; private float rotX; + // Block references private SignPost id; private Blox button; private Blox[] frame; private Blox[] entrances; + // Gate information private String name; private String destination; private String network; private Gate gate; private String owner = ""; + private World world; + private boolean verified; + private boolean fixed; + // Options private boolean hidden = false; private boolean alwaysOn = false; @@ -62,10 +68,6 @@ public class Portal { private boolean free = false; private boolean backwards = false; - private World world; - // Gate options - private boolean verified; - private boolean fixed; // In-use information private Player player; private Player activePlayer; @@ -86,7 +88,6 @@ public class Portal { this.destination = dest; this.button = button; this.verified = verified; - this.fixed = dest.length() > 0; this.network = network; this.name = name; this.gate = gate; @@ -97,10 +98,11 @@ public class Portal { this.free = free; this.backwards = backwards; this.world = topLeft.getWorld(); + this.fixed = dest.length() > 0; - if (this.alwaysOn && !this.fixed) { + if (this.isAlwaysOn() && !this.isFixed()) { this.alwaysOn = false; - Stargate.log.log(Level.WARNING, "Can not create a non-fixed always-on gate."); + Stargate.debug("Portal", "Can not create a non-fixed always-on gate. Setting AlwaysOn = false"); } this.register(); @@ -108,13 +110,16 @@ public class Portal { this.drawSign(); } } - + + /** + * Option Check Functions + */ public boolean isOpen() { return isOpen || isAlwaysOn(); } public boolean isAlwaysOn() { - return alwaysOn && isFixed(); + return alwaysOn; } public boolean isHidden() { @@ -133,11 +138,76 @@ public class Portal { return backwards; } - public boolean isFree(Player player, Portal dest) { - // This gate is free, the player gets all gates free, or we don't charge for free dest and dest is free - boolean isFree = isFree() || Stargate.hasPerm(player, "stargate.free.use", player.isOp()) || - (!iConomyHandler.chargeFreeDestination && dest.isFree()); - return isFree; + /** + * Getters and Setters + */ + + public float getRotation() { + return rotX; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = filterName(name); + drawSign(); + } + + public Portal getDestination() { + return Portal.getByName(destination, getNetwork()); + } + + public void setDestination(Portal destination) { + setDestination(destination.getName()); + } + + public void setDestination(String destination) { + this.destination = destination; + } + + public String getDestinationName() { + return destination; + } + + public Gate getGate() { + return gate; + } + + public String getOwner() { + return owner; + } + + public Blox[] getEntrances() { + if (entrances == null) { + RelativeBlockVector[] space = gate.getEntrances(); + entrances = new Blox[space.length]; + int i = 0; + + for (RelativeBlockVector vector : space) { + entrances[i++] = getBlockAt(vector); + } + } + return entrances; + } + + public Blox[] getFrame() { + if (frame == null) { + RelativeBlockVector[] border = gate.getBorder(); + frame = new Blox[border.length]; + int i = 0; + + for (RelativeBlockVector vector : border) { + frame[i++] = getBlockAt(vector); + } + } + + return frame; + } + + public World getWorld() { + return world; } public boolean open(boolean force) { @@ -157,12 +227,14 @@ public class Portal { openTime = System.currentTimeMillis() / 1000; Stargate.openList.add(this); Stargate.activeList.remove(this); + // Open remote gate if (!isAlwaysOn()) { player = openFor; Portal end = getDestination(); - if (end != null && !end.isAlwaysOn() && !end.isOpen()) { + // Only open dest if it's not-fixed or points at this gate + if (end != null && (!end.isFixed() || end.getDestinationName().equalsIgnoreCase(getName())) && !end.isOpen()) { end.open(openFor, false); end.setDestination(this); if (end.isVerified()) end.drawSign(); @@ -303,36 +375,6 @@ public class Portal { } return traveller; } - - public float getRotation() { - return rotX; - } - - public void setName(String name) { - this.name = filterName(name); - - drawSign(); - } - - public String getName() { - return name; - } - - public void setDestination(Portal destination) { - setDestination(destination.getName()); - } - - public void setDestination(String destination) { - this.destination = destination; - } - - public Portal getDestination() { - return Portal.getByName(destination, getNetwork()); - } - - public String getDestinationName() { - return destination; - } public boolean isChunkLoaded() { return getWorld().isChunkLoaded(topLeft.getBlock().getChunk()); @@ -356,32 +398,25 @@ public class Portal { return gate.matches(topLeft, modX, modZ); } - public Gate getGate() { - return gate; - } - - public String getOwner() { - return owner; - } - public void activate(Player player) { destinations.clear(); destination = ""; drawSign(); Stargate.activeList.add(this); activePlayer = player; - for (String dest : allPortalsNet.get(getNetwork().toLowerCase())) { - Portal portal = getByName(dest, getNetwork()); + String network = getNetwork(); + for (String dest : allPortalsNet.get(network.toLowerCase())) { + Portal portal = getByName(dest, network); // Check if dest is always open (Don't show if so) if (portal.isAlwaysOn()) continue; // Check if this player can access the dest world - if (Stargate.worldFilter && !Stargate.hasPerm(player, "stargate.world." + portal.getWorld().getName(), player.isOp())) continue; + if (!Stargate.canAccessWorld(player, portal.getWorld().getName())) continue; // Check if dest is this portal if (dest.equalsIgnoreCase(getName())) continue; // Check if dest is a fixed gate not pointing to this gate if (portal.isFixed() && !portal.getDestinationName().equalsIgnoreCase(getName())) continue; // Visible to this player. - if (!portal.isHidden() || Stargate.hasPerm(player, "stargate.hidden", player.isOp()) || portal.getOwner().equals(player.getName())) { + if (Stargate.canSee(player, portal)) { destinations.add(portal.getName()); } } @@ -399,7 +434,7 @@ public class Portal { } public boolean isActive() { - return fixed || (destinations.size() > 0); + return isFixed() || (destinations.size() > 0); } public Player getActivePlayer() { @@ -417,6 +452,7 @@ public class Portal { public void cycleDestination(Player player) { cycleDestination(player, 1); } + public void cycleDestination(Player player, int dir) { if (!isActive() || getActivePlayer() != player) { activate(player); @@ -458,7 +494,7 @@ public class Portal { if ((index == max) && (max > 1) && (++done <= 3)) { if (iConomyHandler.useiConomy() && iConomyHandler.freeGatesGreen) { Portal dest = Portal.getByName(destinations.get(index - 2), network); - boolean green = isFree(activePlayer, dest); + boolean green = Stargate.isFree(activePlayer, this, dest); id.setText(done, (green ? ChatColor.DARK_GREEN : "") + destinations.get(index - 2)); } else { id.setText(done, destinations.get(index - 2)); @@ -467,7 +503,7 @@ public class Portal { if ((index > 0) && (++done <= 3)) { if (iConomyHandler.useiConomy() && iConomyHandler.freeGatesGreen) { Portal dest = Portal.getByName(destinations.get(index - 1), network); - boolean green = isFree(activePlayer, dest); + boolean green = Stargate.isFree(activePlayer, this, dest); id.setText(done, (green ? ChatColor.DARK_GREEN : "") + destinations.get(index - 1)); } else { id.setText(done, destinations.get(index - 1)); @@ -476,7 +512,7 @@ public class Portal { if (++done <= 3) { if (iConomyHandler.useiConomy() && iConomyHandler.freeGatesGreen) { Portal dest = Portal.getByName(destination, network); - boolean green = isFree(activePlayer, dest); + boolean green = Stargate.isFree(activePlayer, this, dest); id.setText(done, (green ? ChatColor.DARK_GREEN : "") + " >" + destination + "< "); } else { id.setText(done, " >" + destination + "< "); @@ -485,7 +521,7 @@ public class Portal { if ((max >= index + 1) && (++done <= 3)) { if (iConomyHandler.useiConomy() && iConomyHandler.freeGatesGreen) { Portal dest = Portal.getByName(destinations.get(index + 1), network); - boolean green = isFree(activePlayer, dest); + boolean green = Stargate.isFree(activePlayer, this, dest); id.setText(done, (green ? ChatColor.DARK_GREEN : "") + destinations.get(index + 1)); } else { id.setText(done, destinations.get(index + 1)); @@ -494,7 +530,7 @@ public class Portal { if ((max >= index + 2) && (++done <= 3)) { if (iConomyHandler.useiConomy() && iConomyHandler.freeGatesGreen) { Portal dest = Portal.getByName(destinations.get(index + 2), network); - boolean green = isFree(activePlayer, dest); + boolean green = Stargate.isFree(activePlayer, this, dest); id.setText(done, (green ? ChatColor.DARK_GREEN : "") + destinations.get(index + 2)); } else { id.setText(done, destinations.get(index + 2)); @@ -510,39 +546,8 @@ public class Portal { id.update(); } - public Blox[] getEntrances() { - if (entrances == null) { - RelativeBlockVector[] space = gate.getEntrances(); - entrances = new Blox[space.length]; - int i = 0; - - for (RelativeBlockVector vector : space) { - entrances[i++] = getBlockAt(vector); - } - } - return entrances; - } - - public Blox[] getFrame() { - if (frame == null) { - RelativeBlockVector[] border = gate.getBorder(); - frame = new Blox[border.length]; - int i = 0; - - for (RelativeBlockVector vector : border) { - frame[i++] = getBlockAt(vector); - } - } - - return frame; - } - - public World getWorld() { - return world; - } - public void unregister(boolean removeAll) { - Stargate.log.info("[Stargate] Unregistering gate " + getName()); + Stargate.debug("Unregister", "Unregistering gate " + getName()); close(true); lookupNamesNet.get(getNetwork().toLowerCase()).remove(getName().toLowerCase()); @@ -589,8 +594,10 @@ public class Portal { } private void register() { - if (!lookupNamesNet.containsKey(getNetwork().toLowerCase())) + if (!lookupNamesNet.containsKey(getNetwork().toLowerCase())) { + Stargate.debug("register", "Network not in lookupNamesNet, adding"); lookupNamesNet.put(getNetwork().toLowerCase(), new HashMap()); + } lookupNamesNet.get(getNetwork().toLowerCase()).put(getName().toLowerCase(), this); for (Blox block : getFrame()) { @@ -608,18 +615,21 @@ public class Portal { allPortals.add(this); // Check if this network exists - if (!allPortalsNet.containsKey(getNetwork().toLowerCase())) + if (!allPortalsNet.containsKey(getNetwork().toLowerCase())) { + Stargate.debug("register", "Network not in allPortalsNet, adding"); allPortalsNet.put(getNetwork().toLowerCase(), new ArrayList()); + } allPortalsNet.get(getNetwork().toLowerCase()).add(getName().toLowerCase()); } public static Portal createPortal(SignPost id, Player player) { Block idParent = id.getParent(); if (idParent == null) { - Stargate.debug("createPortal", "idParent == null"); return null; } + if (Gate.getGatesByControlBlock(idParent).length == 0) return null; + if (Portal.getByBlock(idParent) != null) { Stargate.debug("createPortal", "idParent belongs to existing gate"); return null; @@ -638,60 +648,18 @@ public class Portal { boolean backwards = (options.indexOf('b') != -1 || options.indexOf('B') != -1); // Check permissions for options. - if (!Stargate.hasPerm(player, "stargate.option.hidden", player.isOp())) hidden = false; - if (!Stargate.hasPerm(player, "stargate.option.alwayson", player.isOp())) alwaysOn = false; - if (!Stargate.hasPerm(player, "stargate.option.private", player.isOp())) priv = false; - if (!Stargate.hasPerm(player, "stargate.option.free", player.isOp())) free = false; - if (!Stargate.hasPerm(player, "stargate.option.backwards", player.isOp())) backwards = false; + if (!Stargate.canOption(player, "hidden")) hidden = false; + if (!Stargate.canOption(player, "alwayson")) alwaysOn = false; + if (!Stargate.canOption(player, "private")) priv = false; + if (!Stargate.canOption(player, "free")) free = false; + if (!Stargate.canOption(player, "backwards")) backwards = false; // Can not create a non-fixed always-on gate. if (alwaysOn && destName.length() == 0) { alwaysOn = false; } - // Debug - Stargate.debug("createPortal", "h = " + hidden + " a = " + alwaysOn + " p = " + priv + " f = " + free + " b = " + backwards); - - if ((network.length() < 1) || (network.length() > 11)) { - network = Stargate.getDefaultNetwork(); - } - - if ((name.length() < 1) || (name.length() > 11) || (getByName(name, network) != null)) { - Stargate.debug("createPortal", "Name Error"); - return null; - } - - // Check if the user can only create personal gates, set network if so - boolean createPersonal = false; - if (Stargate.hasPerm(player, "stargate.create.personal", false) && - !Stargate.hasPerm(player, "stargate.create", player.isOp()) ) { - network = player.getName(); - if (network.length() > 11) network = network.substring(0, 11); - createPersonal = true; - } - - // Check if there are too many gates in this network - ArrayList netList = allPortalsNet.get(network); - if (Stargate.maxGates > 0 && netList != null && netList.size() >= Stargate.maxGates) { - player.sendMessage(ChatColor.RED + "[Stargate]" + ChatColor.WHITE + " This network is full."); - return null; - } - - // Check if the user can create gates on this network. - if (!createPersonal && !Stargate.hasPerm(player, "stargate.network." + network, player.isOp())) { - player.sendMessage(ChatColor.RED + "[Stargate]" + ChatColor.WHITE + " You don't have access to that network"); - return null; - } - - // Check if the user can create gates to this world. - if (destName.length() != 0) { - Portal d = Portal.getByName(destName, network); - if (d != null && !Stargate.hasPerm(player, "stargate.world." + d.getWorld().getName(), player.isOp())) { - player.sendMessage(ChatColor.RED + "[Stargate]" + ChatColor.WHITE + " You don't have access to that world"); - return null; - } - } - + // Moved the layout check so as to avoid invalid messages when not making a gate int modX = 0; int modZ = 0; float rotX = 0f; @@ -750,6 +718,62 @@ public class Portal { return null; } + // Debug + Stargate.debug("createPortal", "h = " + hidden + " a = " + alwaysOn + " p = " + priv + " f = " + free + " b = " + backwards); + + if ((network.length() < 1) || (network.length() > 11)) { + Stargate.debug("createPortal", "Network name too long. Shortening"); + network = Stargate.getDefaultNetwork(); + } + + // Check if the player can create gates on this network + if (!Stargate.canCreate(player, network)) { + Stargate.debug("createPortal", "Player doesn't have create permissions on network. Trying personal"); + network = player.getName().substring(0, 11); + + // Check if we can create a gate on our own network + if (!Stargate.canCreate(player, network)) { + Stargate.debug("createPortal", "Player does not have access to network"); + Stargate.sendMessage(player, "You do not have access to that network"); + return null; + } else { + Stargate.debug("createPortal", "Creating personal portal"); + Stargate.sendMessage(player, "Creating gate on personal network"); + } + } + + if (name.length() < 1 || name.length() > 11) { + Stargate.debug("createPortal", "Name length error"); + Stargate.sendMessage(player, "Name too short or too long."); + return null; + } + + if (getByName(name, network) != null) { + Stargate.debug("createPortal", "Name Error"); + Stargate.sendMessage(player, "A gate by that name already exists!"); + return null; + } + + // Check if there are too many gates in this network + ArrayList netList = allPortalsNet.get(network.toLowerCase()); + if (Stargate.maxGates > 0 && netList != null && netList.size() >= Stargate.maxGates) { + Stargate.sendMessage(player, "This network is full."); + return null; + } + + // Check if the user can create gates to this world. + if (destName.length() > 0) { + Portal p = Portal.getByName(destName, network); + if (p != null) { + String world = p.getWorld().getName(); + if (!Stargate.canAccessWorld(player, world)) { + Stargate.debug("canCreate", "Player does not have access to destination world"); + Stargate.sendMessage(player, "You do not have access to that world."); + return null; + } + } + } + // Bleh, gotta check to make sure none of this gate belongs to another gate. Boo slow. for (RelativeBlockVector v : gate.getBorder()) { Blox b = topleft.modRelative(v.getRight(), v.getDepth(), v.getDistance(), modX, 1, modZ); @@ -759,15 +783,9 @@ public class Portal { } } - if (iConomyHandler.useiConomy() && !Stargate.hasPerm(player, "stargate.free.create", player.isOp())) { - if (!iConomyHandler.chargePlayer(player.getName(), null, gate.getCreateCost())) { - if (!iConomyHandler.inFundMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + iConomyHandler.inFundMsg); - } - return null; - } - if (gate.getCreateCost() > 0) - player.sendMessage(ChatColor.GREEN + "Deducted " + iConomyHandler.format(gate.getCreateCost())); + if (!Stargate.chargePlayer(player, null, "create", gate.getCreateCost())) { + Stargate.debug("createPortal", "Insufficient Funds"); + return null; } Portal portal = null; diff --git a/src/net/TheDgtl/Stargate/Stargate.java b/src/net/TheDgtl/Stargate/Stargate.java index 1b6b037..bf62d6b 100644 --- a/src/net/TheDgtl/Stargate/Stargate.java +++ b/src/net/TheDgtl/Stargate/Stargate.java @@ -83,11 +83,9 @@ public class Stargate extends JavaPlugin { private static String blockMsg = "Destination Blocked"; private static String defNetwork = "central"; private static boolean destroyExplosion = false; - public static boolean networkFilter = false; - public static boolean worldFilter = false; public static int maxGates = 0; - private static int activeLimit = 10; - private static int openLimit = 10; + private static int activeTime = 10; + private static int openTime = 10; // Used for debug private static boolean debug = false; @@ -162,8 +160,6 @@ public class Stargate extends JavaPlugin { blockMsg = config.getString("other-side-blocked-message", blockMsg); defNetwork = config.getString("default-gate-network", defNetwork).trim(); destroyExplosion = config.getBoolean("destroyexplosion", destroyExplosion); - networkFilter = config.getBoolean("networkfilter", networkFilter); - worldFilter = config.getBoolean("worldfilter", worldFilter); maxGates = config.getInt("maxgates", maxGates); // Debug debug = config.getBoolean("debug", debug); @@ -191,8 +187,6 @@ public class Stargate extends JavaPlugin { config.setProperty("other-side-blocked-message", blockMsg); config.setProperty("default-gate-network", defNetwork); config.setProperty("destroyexplosion", destroyExplosion); - config.setProperty("networkfilter", networkFilter); - config.setProperty("worldfilter", worldFilter); config.setProperty("maxgates", maxGates); // iConomy config.setProperty("useiconomy", iConomyHandler.useiConomy); @@ -255,6 +249,18 @@ public class Stargate extends JavaPlugin { log.log(Level.FINEST, "[Stargate::" + rout + "] " + msg); } } + + public static void sendMessage(Player player, String message) { + sendMessage(player, message, true); + } + + public static void sendMessage(Player player, String message, boolean error) { + if (message.isEmpty()) return; + if (error) + player.sendMessage(ChatColor.RED + "[Stargate] " + ChatColor.WHITE + message); + else + player.sendMessage(ChatColor.GREEN + "[Stargate] " + ChatColor.WHITE + message); + } public static String getSaveLocation() { return portalFolder; @@ -264,46 +270,197 @@ public class Stargate extends JavaPlugin { return defNetwork; } - private void onButtonPressed(Player player, Portal gate) { - Portal destination = gate.getDestination(); - - if (!gate.isOpen()) { - if ((!gate.isFixed()) && gate.isActive() && (gate.getActivePlayer() != player)) { - gate.deactivate(); - if (!denyMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + denyMsg); - } - } else if (gate.isPrivate() && !gate.getOwner().equals(player.getName()) && !hasPerm(player, "stargate.private", player.isOp())) { - if (!denyMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + denyMsg); - } - } else if ((destination == null) || (destination == gate)) { - if (!invMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + invMsg); - } - } else if ((destination.isOpen()) && (!destination.isAlwaysOn())) { - if (!blockMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + blockMsg); - } - } else { - gate.open(player, false); - } - } else { - gate.close(false); + private void onButtonPressed(Player player, Portal portal) { + Portal destination = portal.getDestination(); + + // Always-open gate -- Do nothing + if (portal.isAlwaysOn()) { + return; } + + // Invalid destination + if ((destination == null) || (destination == portal)) { + Stargate.sendMessage(player, invMsg); + return; + } + + // Gate is already open + if (portal.isOpen()) { + // Close if this player opened the gate + if (portal.getActivePlayer() == player) { + portal.close(false); + } + return; + } + + // Gate that someone else is using -- Deny access + if ((!portal.isFixed()) && portal.isActive() && (portal.getActivePlayer() != player)) { + Stargate.sendMessage(player, denyMsg); + return; + } + + // Check if the player can use the private gate + if (portal.isPrivate() && !Stargate.canPrivate(player, portal)) { + Stargate.sendMessage(player, denyMsg); + return; + } + + // Destination blocked + if ((destination.isOpen()) && (!destination.isAlwaysOn())) { + Stargate.sendMessage(player, blockMsg); + return; + } + + // Open gate + portal.open(player, false); } /* * Check whether the player has the given permissions. */ - public static boolean hasPerm(Player player, String perm, boolean def) { + public static boolean hasPerm(Player player, String perm) { if (permissions != null) { return permissions.getHandler().has(player, perm); } else { - return def; + return player.hasPermission(perm); } } + /* + * Check whether player can teleport to dest world + */ + public static boolean canAccessWorld(Player player, String world) { + // Can use all Stargate player features + if (hasPerm(player, "stargate.use")) return true; + // Can access all worlds + if (hasPerm(player, "stargate.world")) return true; + // Can access dest world + if (hasPerm(player, "stargate.world." + world)) return true; + return false; + } + + /* + * Check whether player can use network + */ + public static boolean canAccessNetwork(Player player, String network) { + // Can use all Stargate player features + if (hasPerm(player, "stargate.use")) return true; + // Can access all networks + if (hasPerm(player, "stargate.network")) return true; + // Can access this network + if (hasPerm(player, "stargate.network." + network)) return true; + return false; + } + + /* + * Return true if the portal is free for the player + */ + public static boolean isFree(Player player, Portal src, Portal dest) { + // This gate is free + if (src.isFree()) return true; + // Player gets free use + if (hasPerm(player, "stargate.free") || Stargate.hasPerm(player, "stargate.free.use")) return true; + // Don't charge for free destination gates + if (!iConomyHandler.chargeFreeDestination && dest.isFree()) return true; + return false; + } + + /* + * Check whether the player can see this gate (Hidden property check) + */ + public static boolean canSee(Player player, Portal portal) { + // The gate is not hidden + if (!portal.isHidden()) return true; + // The player is an admin with the ability to see hidden gates + if (hasPerm(player, "stargate.admin") || hasPerm(player, "stargate.admin.hidden")) return true; + // The player is the owner of the gate + if (portal.getOwner().equalsIgnoreCase(player.getName())) return true; + return false; + } + + /* + * Check if the player can use this private gate + */ + public static boolean canPrivate(Player player, Portal portal) { + // Check if the player is the owner of the gate + if (portal.getOwner().equalsIgnoreCase(player.getName())) return true; + // The player is an admin with the ability to use private gates + if (hasPerm(player, "stargate.admin") || hasPerm(player, "stargate.admin.private")) return true; + return false; + } + + /* + * Check if the player has access to {option} + */ + public static boolean canOption(Player player, String option) { + // Check if the player can use all options + if (hasPerm(player, "stargate.option")) return true; + // Check if they can use this specific option + if (hasPerm(player, "stargate.option." + option)) return true; + return false; + } + + /* + * Check if the player can create gates on {network} + */ + public static boolean canCreate(Player player, String network) { + // Check for general create + if (hasPerm(player, "stargate.create")) return true; + // Check for all network create permission + if (hasPerm(player, "stargate.create.network")) return true; + // Check for this specific network + if (hasPerm(player, "stargate.create.network." + network)) return true; + + // Check if this is a personal gate, and if the player has create.personal + if (player.getName().substring(0, 11).equalsIgnoreCase(network) && hasPerm(player, "stargate.create.personal")) return true; + return false; + } + + /* + * Check if the player can destroy this gate + */ + public static boolean canDestroy(Player player, Portal portal) { + // Check for general destroy + if (hasPerm(player, "stargate.destroy")) return true; + // Check for all network destroy permission + if (hasPerm(player, "stargate.destroy.network")) return true; + // Check for this specific network + if (hasPerm(player, "stargate.destroy.network." + portal.getNetwork())) return true; + // Check for personal gate + if (player.getName().equalsIgnoreCase(portal.getOwner()) && hasPerm(player, "stargate.destroy.personal")) return true; + return false; + } + + /* + * Charge player for {action} if required, true on success, false if can't afford + */ + public static boolean chargePlayer(Player player, String target, String action, int cost) { + // If cost is 0 + if (cost <= 0) return true; + // iConomy is disabled + if (!iConomyHandler.useiConomy()) return true; + // Player gets free {action} + if (hasPerm(player, "stargate.free") || hasPerm(player, "stargate.free." + action)) return true; + // Charge player + return iConomyHandler.chargePlayer(player.getName(), target, cost); + } + + /* + * Determine the cost of a gate + */ + public static int getUseCost(Player player, Portal src, Portal dest) { + // Not using iConomy + if (!iConomyHandler.useiConomy()) return 0; + // Portal is free + if (src.isFree()) return 0; + // Not charging for free destinations + if (!iConomyHandler.chargeFreeDestination && dest.isFree()) return 0; + // Cost is 0 if the player owns this gate and funds go to the owner + if (src.getGate().getToOwner() && src.getOwner().equalsIgnoreCase(player.getName())) return 0; + + return src.getGate().getUseCost(); + } + /* * Check if a plugin is loaded/enabled already. Returns the plugin if so, null otherwise */ @@ -332,46 +489,46 @@ public class Stargate extends JavaPlugin { if (passenger instanceof Player) { Player player = (Player)passenger; if (!portal.isOpenFor(player)) { - player.sendMessage(ChatColor.RED + denyMsg); + Stargate.sendMessage(player, denyMsg); return; } + Portal dest = portal.getDestination(); if (dest == null) return; - - if ((networkFilter && !hasPerm(player, "stargate.network." + portal.getNetwork(), player.isOp())) || - (worldFilter && !hasPerm(player, "stargate.world." + portal.getDestination().getWorld().getName(), player.isOp()))) { - if (!denyMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + denyMsg); - } + // Check if player has access to this network + if (!canAccessNetwork(player, portal.getNetwork())) { + Stargate.sendMessage(player, denyMsg); portal.close(false); return; } - boolean iConCharge = (iConomyHandler.useiConomy() && !portal.isFree() && !hasPerm(player, "stargate.free.use", player.isOp())); - if (!iConomyHandler.chargeFreeDestination) - iConCharge = iConCharge && !dest.isFree(); + // Check if player has access to destination world + if (!canAccessWorld(player, dest.getWorld().getName())) { + Stargate.sendMessage(player, denyMsg); + portal.close(false); + return; + } - String target = (portal.getGate().getToOwner() ? portal.getOwner() : null); - if (target != null) - iConCharge = iConCharge && !target.equals(player.getName()); - - if (!iConCharge || iConomyHandler.chargePlayer(player.getName(), target, portal.getGate().getUseCost())) { - if (iConCharge && portal.getGate().getUseCost() > 0) { - player.sendMessage(ChatColor.GREEN + "Deducted " + iConomyHandler.format(portal.getGate().getUseCost())); - - Player p = server.getPlayer(portal.getOwner()); - if (portal.getGate().getToOwner() && p != null) - p.sendMessage(ChatColor.GREEN + "Obtained " + iConomyHandler.format(portal.getGate().getUseCost()) + " from Stargate " + portal.getName()); + int cost = Stargate.getUseCost(player, portal, dest); + if (cost > 0) { + String target = portal.getGate().getToOwner() ? portal.getOwner() : null; + if (!Stargate.chargePlayer(player, target, "use", cost)) { + // Insufficient Funds + Stargate.sendMessage(player, "Insufficient Funds"); + portal.close(false); + return; } - if (!teleMsg.isEmpty()) { - player.sendMessage(ChatColor.BLUE + teleMsg); - } - dest.teleport(vehicle); - } else { - if (!iConomyHandler.inFundMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + iConomyHandler.inFundMsg); + sendMessage(player, "Deducted " + iConomyHandler.format(cost), false); + if (target != null) { + Player p = server.getPlayer(target); + if (p != null) { + Stargate.sendMessage(p, "Obtained " + iConomyHandler.format(cost) + " from Stargate " + portal.getName(), false); + } } } + + Stargate.sendMessage(player, teleMsg, false); + dest.teleport(vehicle); portal.close(false); } else { Portal dest = portal.getDestination(); @@ -403,62 +560,64 @@ public class Stargate extends JavaPlugin { } } } + @Override public void onPlayerMove(PlayerMoveEvent event) { Player player = event.getPlayer(); Portal portal = Portal.getByEntrance(event.getTo()); + + // No portal or not open + if (portal == null || !portal.isOpen()) return; - if ((portal != null) && (portal.isOpen())) { - if (!portal.isOpenFor(player)) { - if (!denyMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + denyMsg); - } - portal.teleport(player, portal, event); - return; + // Not open for this player + if (!portal.isOpenFor(player)) { + if (!denyMsg.isEmpty()) { + Stargate.sendMessage(player, denyMsg); } - - Portal destination = portal.getDestination(); - if (destination == null) return; - - if ((networkFilter && !hasPerm(player, "stargate.network." + portal.getNetwork(), player.isOp())) || - (worldFilter && !hasPerm(player, "stargate.world." + portal.getDestination().getWorld().getName(), player.isOp()))) { - if (!denyMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + denyMsg); - } - portal.teleport(player, portal, event); + portal.teleport(player, portal, event); + return; + } + + Portal destination = portal.getDestination(); + if (destination == null) return; + + // Check if player has access to this network + if (!canAccessNetwork(player, portal.getNetwork())) { + Stargate.sendMessage(player, denyMsg); + portal.teleport(player, portal, event); + portal.close(false); + return; + } + + // Check if player has access to destination world + if (!canAccessWorld(player, destination.getWorld().getName())) { + Stargate.sendMessage(player, denyMsg); + portal.teleport(player, portal, event); + portal.close(false); + return; + } + + int cost = Stargate.getUseCost(player, portal, destination); + if (cost > 0) { + String target = portal.getGate().getToOwner() ? portal.getOwner() : null; + if (!Stargate.chargePlayer(player, target, "use", cost)) { + // Insufficient Funds + Stargate.sendMessage(player, "Insufficient Funds"); portal.close(false); return; } - - boolean iConCharge = (iConomyHandler.useiConomy() && !portal.isFree() && !hasPerm(player, "stargate.free.use", player.isOp())); - if (!iConomyHandler.chargeFreeDestination) - iConCharge = iConCharge && !destination.isFree(); - - String target = (portal.getGate().getToOwner() ? portal.getOwner() : null); - if (target != null) - iConCharge = iConCharge && !target.equals(player.getName()); - - if (!iConCharge || iConomyHandler.chargePlayer(player.getName(), target, portal.getGate().getUseCost())) { - if (iConCharge && portal.getGate().getUseCost() > 0) { - player.sendMessage(ChatColor.GREEN + "Deducted " + iConomyHandler.format(portal.getGate().getUseCost())); - - Player p = server.getPlayer(portal.getOwner()); - if (portal.getGate().getToOwner() && p != null) { - p.sendMessage(ChatColor.GREEN + "Obtained " + iConomyHandler.format(portal.getGate().getUseCost()) + " from Stargate " + portal.getName()); - } - } - if (!teleMsg.isEmpty()) { - player.sendMessage(ChatColor.BLUE + teleMsg); - } - - destination.teleport(player, portal, event); - } else { - if (!iConomyHandler.inFundMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + iConomyHandler.inFundMsg); + sendMessage(player, "Deducted " + iConomyHandler.format(cost), false); + if (target != null) { + Player p = server.getPlayer(target); + if (p != null) { + Stargate.sendMessage(p, "Obtained " + iConomyHandler.format(cost) + " from Stargate " + portal.getName(), false); } } - portal.close(false); } + + Stargate.sendMessage(player, teleMsg); + destination.teleport(player, portal, event); + portal.close(false); } @Override @@ -470,33 +629,33 @@ public class Stargate extends JavaPlugin { if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (block.getType() == Material.WALL_SIGN) { Portal portal = Portal.getByBlock(block); - // Cycle through a stargates locations - if (portal != null) { - event.setUseItemInHand(Result.DENY); - event.setUseInteractedBlock(Result.DENY); - if (!hasPerm(player, "stargate.use", true) || - (networkFilter && !hasPerm(player, "stargate.network." + portal.getNetwork(), player.isOp()))) { - if (!denyMsg.isEmpty()) { - player.sendMessage(denyMsg); - } - return; - } - - if ((!portal.isOpen()) && (!portal.isFixed())) { - portal.cycleDestination(player); - } + if (portal == null) return; + // Cancel item use + event.setUseItemInHand(Result.DENY); + event.setUseInteractedBlock(Result.DENY); + + if (!Stargate.canAccessNetwork(player, portal.getNetwork())) { + Stargate.sendMessage(player, denyMsg); + return; } + + if ((!portal.isOpen()) && (!portal.isFixed())) { + portal.cycleDestination(player); + } + return; } // Implement right-click to toggle a stargate, gets around spawn protection problem. if ((block.getType() == Material.STONE_BUTTON)) { - if (hasPerm(player, "stargate.use", true)) { - Portal portal = Portal.getByBlock(block); - if (portal != null) { - onButtonPressed(player, portal); - } + Portal portal = Portal.getByBlock(block); + if (portal == null) return; + if (!Stargate.canAccessNetwork(player, portal.getNetwork())) { + Stargate.sendMessage(player, denyMsg); + return; } + onButtonPressed(player, portal); } + return; } // Left click @@ -504,29 +663,31 @@ public class Stargate extends JavaPlugin { // Check if we're scrolling a sign if (block.getType() == Material.WALL_SIGN) { Portal portal = Portal.getByBlock(block); - // Cycle through a stargates locations - if (portal != null) { - if (!hasPerm(player, "stargate.use", true) || - (networkFilter && !hasPerm(player, "stargate.network." + portal.getNetwork(), player.isOp()))) { - if (!denyMsg.isEmpty()) { - player.sendMessage(denyMsg); - } - return; - } - - if ((!portal.isOpen()) && (!portal.isFixed())) { - portal.cycleDestination(player, -1); - } + if (portal == null) return; + // Cancel item use + event.setUseItemInHand(Result.DENY); + event.setUseInteractedBlock(Result.DENY); + + if (!Stargate.canAccessNetwork(player, portal.getNetwork())) { + Stargate.sendMessage(player, denyMsg); + return; } + + if ((!portal.isOpen()) && (!portal.isFixed())) { + portal.cycleDestination(player, -1); + } + return; } + // Check if we're pushing a button. if (block.getType() == Material.STONE_BUTTON) { - if (hasPerm(player, "stargate.use", true)) { - Portal portal = Portal.getByBlock(block); - if (portal != null) { - onButtonPressed(player, portal); - } + Portal portal = Portal.getByBlock(block); + if (portal == null) return; + if (!Stargate.canAccessNetwork(player, portal.getNetwork())) { + Stargate.sendMessage(player, denyMsg); + return; } + onButtonPressed(player, portal); } } } @@ -539,34 +700,25 @@ public class Stargate extends JavaPlugin { Block block = event.getBlock(); if (block.getType() != Material.WALL_SIGN) return; - // Initialize a stargate - if (hasPerm(player, "stargate.create", player.isOp()) || - hasPerm(player, "stargate.create.personal", false)) { - SignPost sign = new SignPost(new Blox(block)); - // Set sign text so we can create a gate with it. - sign.setText(0, event.getLine(0)); - sign.setText(1, event.getLine(1)); - sign.setText(2, event.getLine(2)); - sign.setText(3, event.getLine(3)); - Portal portal = Portal.createPortal(sign, player); - if (portal == null) { - Stargate.debug("SignChange", "createPortal returned null"); - return; - } + // Initialize a stargate -- Permission check is done in createPortal + SignPost sign = new SignPost(new Blox(block)); + // Set sign text so we can create a gate with it. + sign.setText(0, event.getLine(0)); + sign.setText(1, event.getLine(1)); + sign.setText(2, event.getLine(2)); + sign.setText(3, event.getLine(3)); + Portal portal = Portal.createPortal(sign, player); + // Not creating a gate, just placing a sign + if (portal == null) return; - if (!regMsg.isEmpty()) { - player.sendMessage(ChatColor.GREEN + regMsg); - } - log.info("[Stargate] Initialized stargate: " + portal.getName()); - portal.drawSign(); - // Set event text so our new sign is instantly initialized - event.setLine(0, sign.getText(0)); - event.setLine(1, sign.getText(1)); - event.setLine(2, sign.getText(2)); - event.setLine(3, sign.getText(3)); - } else { - Stargate.debug("SignChange", player.getName() + " tried to create gate without permissions"); - } + Stargate.sendMessage(player, regMsg, false); + Stargate.debug("onSignChange", "Initialized stargate: " + portal.getName()); + portal.drawSign(); + // Set event text so our new sign is instantly initialized + event.setLine(0, sign.getText(0)); + event.setLine(1, sign.getText(1)); + event.setLine(2, sign.getText(2)); + event.setLine(3, sign.getText(3)); } @Override @@ -581,33 +733,27 @@ public class Stargate extends JavaPlugin { Portal portal = Portal.getByBlock(block); if (portal == null) return; - if (hasPerm(player, "stargate.destroy", player.isOp()) || hasPerm(player, "stargate.destroy.all", player.isOp()) || - ( portal.getOwner().equalsIgnoreCase(player.getName()) && hasPerm(player, "stargate.destroy.owner", false) )) { - // Can't afford - if (iConomyHandler.useiConomy() && !hasPerm(player, "stargate.free.destroy", player.isOp())) { - if (!iConomyHandler.chargePlayer(player.getName(), null, portal.getGate().getDestroyCost())) { - if (!iConomyHandler.inFundMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + iConomyHandler.inFundMsg); - } - event.setCancelled(true); - return; - } - - if (portal.getGate().getDestroyCost() > 0) { - player.sendMessage(ChatColor.GREEN + "Deducted " + iConomyHandler.format(portal.getGate().getDestroyCost())); - } else if (portal.getGate().getDestroyCost() < 0) { - player.sendMessage(ChatColor.GREEN + "Refunded " + iConomyHandler.format(-portal.getGate().getDestroyCost())); - } - } - - portal.unregister(true); - if (!dmgMsg.isEmpty()) { - player.sendMessage(ChatColor.RED + dmgMsg); - } + if (!Stargate.canDestroy(player, portal)) { + Stargate.sendMessage(player, "Permission Denied"); + event.setCancelled(true); return; } - event.setCancelled(true); + if (!Stargate.chargePlayer(player, null, "destroy", portal.getGate().getDestroyCost())) { + Stargate.debug("onBlockBreak", "Insufficient Funds"); + Stargate.sendMessage(player, iConomyHandler.inFundMsg); + event.setCancelled(true); + return; + } + + if (portal.getGate().getDestroyCost() > 0) { + Stargate.sendMessage(player, "Deducted " + iConomyHandler.format(portal.getGate().getDestroyCost()), false); + } else if (portal.getGate().getDestroyCost() < 0) { + Stargate.sendMessage(player, "Refunded " + iConomyHandler.format(-portal.getGate().getDestroyCost()), false); + } + + portal.unregister(true); + Stargate.sendMessage(player, dmgMsg, false); } @Override @@ -773,7 +919,9 @@ public class Stargate extends JavaPlugin { // Close open portals for (Iterator iter = Stargate.openList.iterator(); iter.hasNext();) { Portal p = iter.next(); - if (time > p.getOpenTime() + Stargate.openLimit) { + // Skip always open gates + if (p.isAlwaysOn()) continue; + if (time > p.getOpenTime() + Stargate.openTime) { p.close(false); iter.remove(); } @@ -781,7 +929,7 @@ public class Stargate extends JavaPlugin { // Deactivate active portals for (Iterator iter = Stargate.activeList.iterator(); iter.hasNext();) { Portal p = iter.next(); - if (time > p.getOpenTime() + Stargate.activeLimit) { + if (time > p.getOpenTime() + Stargate.activeTime) { p.deactivate(); iter.remove(); } @@ -792,7 +940,7 @@ public class Stargate extends JavaPlugin { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (sender instanceof Player) { - sender.sendMessage("Permission Denied"); + Stargate.sendMessage((Player)sender, "Permission Denied"); return true; } String cmd = command.getName(); diff --git a/src/net/TheDgtl/Stargate/iConomyHandler.java b/src/net/TheDgtl/Stargate/iConomyHandler.java index 11c7f57..cfca795 100644 --- a/src/net/TheDgtl/Stargate/iConomyHandler.java +++ b/src/net/TheDgtl/Stargate/iConomyHandler.java @@ -26,7 +26,7 @@ public class iConomyHandler { if (useiConomy && iconomy != null) { Account acc = iConomy.getAccount(player); if (acc == null) { - Stargate.log.info("[" + pName + "::ich::getBalance] Error fetching iConomy account for " + player); + Stargate.debug("ich::getBalance", "Error fetching iConomy account for " + player); return 0; } return acc.getHoldings().balance(); @@ -41,7 +41,7 @@ public class iConomyHandler { Account acc = iConomy.getAccount(player); if (acc == null) { - Stargate.log.info("[" + pName + "::ich::chargePlayer] Error fetching iConomy account for " + player); + Stargate.debug("ich::chargePlayer", "Error fetching iConomy account for " + player); return false; } Holdings hold = acc.getHoldings(); diff --git a/src/plugin.yml b/src/plugin.yml index cb75502..cdb3247 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: Stargate main: net.TheDgtl.Stargate.Stargate -version: 0.5.5 +version: 0.6.0 description: Stargate mod for Bukkit author: Drakia website: http://www.thedgtl.net