From 918d81198f77ae856c3b2c210221edfe06ef8cd5 Mon Sep 17 00:00:00 2001 From: GJ Date: Wed, 20 Feb 2013 11:43:23 -0500 Subject: [PATCH] Cleaner way to handle per-world teleport permissions. Also provides a nice error message now to explain WHY a player can't teleport. --- src/main/java/com/gmail/nossr50/mcMMO.java | 5 +++ .../nossr50/party/commands/PtpCommand.java | 13 ++++--- .../com/gmail/nossr50/util/Permissions.java | 34 ++++++++++--------- .../resources/locale/locale_en_US.properties | 1 + 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 194d27948..8aa9d1011 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -50,6 +50,7 @@ import com.gmail.nossr50.skills.runnables.BleedTimer; import com.gmail.nossr50.skills.runnables.SkillMonitor; import com.gmail.nossr50.spout.SpoutConfig; import com.gmail.nossr50.spout.SpoutTools; +import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.UpdateCheck; import com.gmail.nossr50.util.Users; @@ -131,6 +132,10 @@ public class mcMMO extends JavaPlugin { placeStore = ChunkManagerFactory.getChunkManager(); // Get our ChunkletManager checkForUpdates(); + + if (Config.getInstance().getPTPCommandWorldPermissions()) { + Permissions.generateWorldTeleportPermissions(); + } } /** diff --git a/src/main/java/com/gmail/nossr50/party/commands/PtpCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PtpCommand.java index cdcb0f024..6d943dae2 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PtpCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PtpCommand.java @@ -1,5 +1,6 @@ package com.gmail.nossr50.party.commands; +import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -118,15 +119,17 @@ public class PtpCommand implements CommandExecutor { return true; } - //TODO: Someone want to clarify what's going on with these dynamic permissions? if (Config.getInstance().getPTPCommandWorldPermissions()) { - String perm = "mcmmo.commands.ptp.world."; + World targetWorld = target.getWorld(); + World playerWorld = player.getWorld(); - if (!Permissions.hasDynamicPermission(target, perm + "all", "op")) { - if (!Permissions.hasDynamicPermission(target, perm + target.getWorld().getName(), "op")) { + if (!Permissions.partyTeleportAllWorlds(target)) { + if (!Permissions.partyTeleportWorld(target, targetWorld)) { + target.sendMessage(LocaleLoader.getString("Commands.ptp.NoWorldPermissions", targetWorld.getName())); return true; } - else if (target.getWorld() != player.getWorld() && !Permissions.hasDynamicPermission(target, perm + player.getWorld().getName(), "op")) { + else if (targetWorld != playerWorld && !Permissions.partyTeleportWorld(target, playerWorld)) { + target.sendMessage(LocaleLoader.getString("Commands.ptp.NoWorldPermissions", playerWorld.getName())); return true; } } diff --git a/src/main/java/com/gmail/nossr50/util/Permissions.java b/src/main/java/com/gmail/nossr50/util/Permissions.java index e8acd21fc..ec55c01a6 100644 --- a/src/main/java/com/gmail/nossr50/util/Permissions.java +++ b/src/main/java/com/gmail/nossr50/util/Permissions.java @@ -1,15 +1,14 @@ package com.gmail.nossr50.util; -import java.util.HashMap; -import java.util.Map; - -import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.command.CommandSender; +import org.bukkit.Server; +import org.bukkit.World; import org.bukkit.permissions.Permissible; import org.bukkit.permissions.Permission; +import org.bukkit.permissions.PermissionDefault; import org.bukkit.plugin.PluginManager; +import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.party.commands.PartySubcommandType; import com.gmail.nossr50.skills.utilities.SkillType; @@ -216,20 +215,23 @@ public final class Permissions { public static boolean partyTeleportAcceptAll(Permissible permissible) { return permissible.hasPermission("mcmmo.commands.ptp.acceptall"); } public static boolean partyTeleportToggle(Permissible permissible) { return permissible.hasPermission("mcmmo.commands.ptp.toggle"); } - // TODO: Still think there's a better way to handle this - public static boolean hasDynamicPermission(CommandSender sender, String perm, String defaultType) { - Map m = new HashMap(); + public static boolean partyTeleportAllWorlds(Permissible permissible) { return permissible.hasPermission("mcmmo.commands.ptp.world.all"); } + public static boolean partyTeleportWorld(Permissible permissible, World world) { return permissible.hasPermission("mcmmo.commands.ptp.world." + world.getName()); } - if(defaultType != null) { - m.put("default", defaultType); + public static void generateWorldTeleportPermissions() { + Server server = mcMMO.p.getServer(); + PluginManager pluginManager = server.getPluginManager(); + + for (World world : server.getWorlds()) { + addDynamicPermission("mcmmo.commands.ptp.world." + world.getName(), PermissionDefault.OP, pluginManager); } - PluginManager manager = Bukkit.getPluginManager(); + addDynamicPermission("mcmmo.commands.ptp.world.all", PermissionDefault.OP, pluginManager); + } - if (manager.getPermission(perm) == null) { - Permission.loadPermission(perm, m); - } - - return sender.hasPermission(perm); + private static void addDynamicPermission(String permissionName, PermissionDefault permissionDefault, PluginManager pluginManager) { + Permission permission = new Permission(permissionName); + permission.setDefault(permissionDefault); + pluginManager.addPermission(permission); } } diff --git a/src/main/resources/locale/locale_en_US.properties b/src/main/resources/locale/locale_en_US.properties index c58a43644..50cefc831 100644 --- a/src/main/resources/locale/locale_en_US.properties +++ b/src/main/resources/locale/locale_en_US.properties @@ -470,6 +470,7 @@ Commands.Party2=[[RED]]- Join a players party Commands.ptp.Enabled=Party teleporting [[GREEN]]enabled Commands.ptp.Disabled=Party teleporting [[RED]]disabled Commands.ptp.NoRequests=[[RED]]You have no teleport requests at this time +Commands.ptp.NoWorldPermissions=[[RED]][mcMMO] You do not have permission to teleport to the world {0}. Commands.ptp.Request1=[[YELLOW]]{0} [[GREEN]]has requested to teleport to you. Commands.ptp.Request2=[[GREEN]]To teleport, type [[YELLOW]]/ptp accept. [[GREEN]]Request expires in [[RED]]{0} [[GREEN]]seconds. Commands.ptp.AcceptAny.Enabled=Party teleport request confirmation [[GREEN]]enabled