From 1cc47ff1ba1f4fca881a1c02075cba1b9748cc6c Mon Sep 17 00:00:00 2001 From: Eric Stokes Date: Wed, 3 Aug 2011 18:59:21 -0600 Subject: [PATCH] Fix "Blah, Blah" in delete command, abs class Dest -> interface MVDest, restrict teleports based on MVDest.getPermissionString. --- .../MultiverseCore/MVPermissions.java | 28 ++++++++++++++++--- .../commands/DeleteCommand.java | 4 +-- .../commands/TeleportCommand.java | 6 ++-- .../MultiverseCore/event/MVTeleportEvent.java | 8 +++--- .../utils/DestinationFactory.java | 12 ++++---- .../onarandombox/utils/ExactDestination.java | 13 ++++++--- .../utils/InvalidDestination.java | 7 ++++- .../com/onarandombox/utils/MVDestination.java | 16 +++++++++++ .../onarandombox/utils/WorldDestination.java | 7 ++++- 9 files changed, 76 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/onarandombox/utils/MVDestination.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVPermissions.java b/src/main/java/com/onarandombox/MultiverseCore/MVPermissions.java index 448fa8ec..c89018a9 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVPermissions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVPermissions.java @@ -9,6 +9,7 @@ import org.bukkit.entity.Player; import com.nijiko.permissions.PermissionHandler; import com.nijikokun.bukkit.Permissions.Permissions; +import com.onarandombox.utils.MVDestination; import com.pneumaticraft.commandhandler.PermissionsInterface; public class MVPermissions implements PermissionsInterface { @@ -81,6 +82,20 @@ public class MVPermissions implements PermissionsInterface { return this.hasPermission(p, "multiverse.access." + worldName, false); } + public Boolean canEnterDestination(Player p, MVDestination d) { + if (d == null || d.getLocation() == null) { + return false; + } + String worldName = d.getLocation().getWorld().getName(); + if (!this.plugin.isMVWorld(worldName)) { + return false; + } + if(!canEnterLocation(p, d.getLocation())) { + return false; + } + return this.hasPermission(p, d.getRequiredPermission(), false); + } + public void setPermissions(PermissionHandler handler) { this.permissions = handler; } @@ -90,24 +105,29 @@ public class MVPermissions implements PermissionsInterface { if (!(sender instanceof Player)) { return true; } + + // NO one can access a null permission (mainly used for destinations):w + if(node == null) { + return false; + } Player player = (Player) sender; boolean opFallback = this.plugin.getConfig().getBoolean("opfallback", true); if (this.permissions != null && this.permissions.has(player, node)) { // If Permissions is enabled we check against them. - //this.plugin.log(Level.WARNING, "Allowed by P3/P2 "); + // this.plugin.log(Level.WARNING, "Allowed by P3/P2 "); return true; } else if (sender.hasPermission(node)) { // If Now check the bukkit permissions - //this.plugin.log(Level.WARNING, "Allowed by BukkitPerms"); + // this.plugin.log(Level.WARNING, "Allowed by BukkitPerms"); return true; } else if (player.isOp() && opFallback) { // If Player is Op we always let them use it if they have the fallback enabled! - //this.plugin.log(Level.WARNING, "Allowed by OP"); + // this.plugin.log(Level.WARNING, "Allowed by OP"); return true; } - + // If the Player doesn't have Permissions and isn't an Op then // we return true if OP is not required, otherwise we return false // This allows us to act as a default permission guidance diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java index 4a6ec853..3868606c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java @@ -25,7 +25,7 @@ public class DeleteCommand extends MultiverseCommand { public void runCommand(CommandSender sender, List args) { Class paramTypes[] = { String.class }; List objectArgs = new ArrayList(args); - //this.plugin.getCommandHandler().queueCommand(sender, "mvdelete", "deleteWorld", objectArgs, paramTypes, "World Deleted!", "World was not deleted!"); - this.plugin.getCommandHandler().queueCommand(sender, "mvteleport", "deleteWorld", objectArgs, paramTypes, "Blah", "Blah", "Success", "Fail", 15); + this.plugin.getCommandHandler().queueCommand(sender, "mvdelete", "deleteWorld", objectArgs, paramTypes, "World Deleted!", "World was not deleted!"); + //this.plugin.getCommandHandler().queueCommand(sender, "mvteleport", "deleteWorld", objectArgs, paramTypes, "Blah", "Blah", "Success", "Fail", 15); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java index de1ecb17..33d4c009 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java @@ -12,7 +12,7 @@ import org.bukkit.permissions.PermissionDefault; import com.onarandombox.MultiverseCore.MVTeleport; import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.utils.Destination; +import com.onarandombox.utils.MVDestination; import com.onarandombox.utils.DestinationFactory; import com.onarandombox.utils.InvalidDestination; @@ -74,13 +74,13 @@ public class TeleportCommand extends MultiverseCommand { } DestinationFactory df = this.plugin.getDestinationFactory();// .parseDestination(worldName, (MultiverseCore) this.plugin); - Destination d = df.getDestination(worldName); + MVDestination d = df.getDestination(worldName); if (d != null && d instanceof InvalidDestination) { sender.sendMessage("Multiverse does not know how to take you to: " + ChatColor.RED + worldName); return; } - if (teleporter != null && !this.plugin.getPermissions().canEnterLocation(teleporter, d.getLocation())) { + if (teleporter != null && !this.plugin.getPermissions().canEnterDestination(teleporter, d)) { if (teleportee.equals(teleporter)) { teleporter.sendMessage("Doesn't look like you're allowed to go " + ChatColor.RED + "there..."); } else { diff --git a/src/main/java/com/onarandombox/MultiverseCore/event/MVTeleportEvent.java b/src/main/java/com/onarandombox/MultiverseCore/event/MVTeleportEvent.java index c026d3d7..f258c388 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/event/MVTeleportEvent.java +++ b/src/main/java/com/onarandombox/MultiverseCore/event/MVTeleportEvent.java @@ -3,16 +3,16 @@ package com.onarandombox.MultiverseCore.event; import org.bukkit.entity.Player; import org.bukkit.event.Event; -import com.onarandombox.utils.Destination; +import com.onarandombox.utils.MVDestination; public class MVTeleportEvent extends Event { private static final long serialVersionUID = 854826818438649269L; private Player player; - private Destination dest; + private MVDestination dest; private String teleportString; - public MVTeleportEvent(Destination dest, Player p, String teleportString) { + public MVTeleportEvent(MVDestination dest, Player p, String teleportString) { super("MVTeleport"); this.player = p; this.dest = dest; @@ -27,7 +27,7 @@ public class MVTeleportEvent extends Event { return this.teleportString; } - public Class getDestType() { + public Class getDestType() { return this.dest.getClass(); } } diff --git a/src/main/java/com/onarandombox/utils/DestinationFactory.java b/src/main/java/com/onarandombox/utils/DestinationFactory.java index dc4ce28d..8f962f31 100644 --- a/src/main/java/com/onarandombox/utils/DestinationFactory.java +++ b/src/main/java/com/onarandombox/utils/DestinationFactory.java @@ -7,23 +7,23 @@ import com.onarandombox.MultiverseCore.MultiverseCore; public class DestinationFactory { private MultiverseCore plugin; - private Map> destList; + private Map> destList; public DestinationFactory(MultiverseCore plugin) { this.plugin = plugin; - this.destList = new HashMap>(); + this.destList = new HashMap>(); } - public Destination getDestination(String dest) { + public MVDestination getDestination(String dest) { String idenChar = ""; if(dest.split(":").length > 1) { idenChar = dest.substring(0, 1); } if (this.destList.containsKey(idenChar)) { - Class myClass = this.destList.get(idenChar); + Class myClass = this.destList.get(idenChar); try { - Destination mydest = myClass.newInstance(); + MVDestination mydest = myClass.newInstance(); if(!mydest.isThisType((MultiverseCore) this.plugin, dest)) { return new InvalidDestination(); } @@ -36,7 +36,7 @@ public class DestinationFactory { return new InvalidDestination(); } - public boolean registerDestinationType(Class c, String identifier) { + public boolean registerDestinationType(Class c, String identifier) { if (this.destList.containsKey(identifier)) { return false; } diff --git a/src/main/java/com/onarandombox/utils/ExactDestination.java b/src/main/java/com/onarandombox/utils/ExactDestination.java index 7a5096b2..e6bb4efd 100644 --- a/src/main/java/com/onarandombox/utils/ExactDestination.java +++ b/src/main/java/com/onarandombox/utils/ExactDestination.java @@ -8,7 +8,7 @@ import org.bukkit.plugin.java.JavaPlugin; import com.onarandombox.MultiverseCore.MultiverseCore; -public class ExactDestination extends Destination { +public class ExactDestination implements MVDestination { private final String coordRegex = "(-?[\\d]+\\.?[\\d]*),(-?[\\d]+\\.?[\\d]*),(-?[\\d]+\\.?[\\d]*)"; private boolean isValid; private Location location; @@ -36,7 +36,7 @@ public class ExactDestination extends Destination { } // If it's not a MV world - if (!((MultiverseCore)plugin).isMVWorld(parsed.get(1))) { + if (!((MultiverseCore) plugin).isMVWorld(parsed.get(1))) { return false; } @@ -92,11 +92,11 @@ public class ExactDestination extends Destination { return; } - if (!((MultiverseCore)plugin).isMVWorld(parsed.get(1))) { + if (!((MultiverseCore) plugin).isMVWorld(parsed.get(1))) { this.isValid = false; return; } - this.location = new Location(((MultiverseCore)plugin).getMVWorld(parsed.get(1)).getCBWorld(), 0, 0, 0); + this.location = new Location(((MultiverseCore) plugin).getMVWorld(parsed.get(1)).getCBWorld(), 0, 0, 0); if (!parsed.get(2).matches(this.coordRegex)) { this.isValid = false; @@ -157,4 +157,9 @@ public class ExactDestination extends Destination { } return "i:Invalid Destination"; } + + @Override + public String getRequiredPermission() { + return "multiverse.access." + this.location.getWorld().getName(); + } } diff --git a/src/main/java/com/onarandombox/utils/InvalidDestination.java b/src/main/java/com/onarandombox/utils/InvalidDestination.java index b10fbead..9d41245e 100644 --- a/src/main/java/com/onarandombox/utils/InvalidDestination.java +++ b/src/main/java/com/onarandombox/utils/InvalidDestination.java @@ -4,7 +4,7 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.plugin.java.JavaPlugin; -public class InvalidDestination extends Destination { +public class InvalidDestination implements MVDestination { @Override public String getIdentifer() { @@ -46,4 +46,9 @@ public class InvalidDestination extends Destination { return "i:Invalid Destination"; } + @Override + public String getRequiredPermission() { + return null; + } + } diff --git a/src/main/java/com/onarandombox/utils/MVDestination.java b/src/main/java/com/onarandombox/utils/MVDestination.java new file mode 100644 index 00000000..7fcc4323 --- /dev/null +++ b/src/main/java/com/onarandombox/utils/MVDestination.java @@ -0,0 +1,16 @@ +package com.onarandombox.utils; + +import org.bukkit.Location; +import org.bukkit.plugin.java.JavaPlugin; + +public interface MVDestination { + public String getIdentifer(); + public boolean isThisType(JavaPlugin plugin, String dest); + public Location getLocation(); + public boolean isValid(); + public void setDestination(JavaPlugin plugin, String dest); + public String getType(); + public String getName(); + public String toString(); + public String getRequiredPermission(); +} diff --git a/src/main/java/com/onarandombox/utils/WorldDestination.java b/src/main/java/com/onarandombox/utils/WorldDestination.java index 166cc795..cf701af3 100644 --- a/src/main/java/com/onarandombox/utils/WorldDestination.java +++ b/src/main/java/com/onarandombox/utils/WorldDestination.java @@ -6,7 +6,7 @@ import org.bukkit.plugin.java.JavaPlugin; import com.onarandombox.MultiverseCore.MVWorld; import com.onarandombox.MultiverseCore.MultiverseCore; -public class WorldDestination extends Destination { +public class WorldDestination implements MVDestination { private boolean isValid; private MVWorld world; float yaw = -1; @@ -99,4 +99,9 @@ public class WorldDestination extends Destination { return this.world.getCBWorld().getName(); } + @Override + public String getRequiredPermission() { + return this.world.getName(); + } + }