From 13af6cb6a3bf03d6b9920e0f8d459058952a2c51 Mon Sep 17 00:00:00 2001 From: Steven Lawson Date: Thu, 22 Nov 2012 22:48:28 -0500 Subject: [PATCH] Implemented support for annotation based command permission setting. Example usages in adminmode, cage, and fuckoff. This feature can be "ignored" for commands that want to check perms for themselves. This means that we can gradually implement this, and not all commands have to be on the new system. --- .../TotalFreedomMod/Commands/ADMIN_LEVEL.java | 6 + .../Commands/CommandPermissions.java | 16 +++ .../Commands/Command_adminmode.java | 8 +- .../Commands/Command_cage.java | 112 ++++++++---------- .../Commands/Command_fuckoff.java | 21 +--- .../Commands/SOURCE_TYPE_ALLOWED.java | 6 + .../TotalFreedomMod/Commands/TFM_Command.java | 80 ++++++++++++- .../TotalFreedomMod/TotalFreedomMod.java | 9 +- 8 files changed, 166 insertions(+), 92 deletions(-) create mode 100644 src/me/StevenLawson/TotalFreedomMod/Commands/ADMIN_LEVEL.java create mode 100644 src/me/StevenLawson/TotalFreedomMod/Commands/CommandPermissions.java create mode 100644 src/me/StevenLawson/TotalFreedomMod/Commands/SOURCE_TYPE_ALLOWED.java diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/ADMIN_LEVEL.java b/src/me/StevenLawson/TotalFreedomMod/Commands/ADMIN_LEVEL.java new file mode 100644 index 00000000..c8b5d318 --- /dev/null +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/ADMIN_LEVEL.java @@ -0,0 +1,6 @@ +package me.StevenLawson.TotalFreedomMod.Commands; + +public enum ADMIN_LEVEL +{ + ALL, OP, SUPER, SENIOR +} diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/CommandPermissions.java b/src/me/StevenLawson/TotalFreedomMod/Commands/CommandPermissions.java new file mode 100644 index 00000000..9206e353 --- /dev/null +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/CommandPermissions.java @@ -0,0 +1,16 @@ +package me.StevenLawson.TotalFreedomMod.Commands; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface CommandPermissions +{ + ADMIN_LEVEL level(); + + SOURCE_TYPE_ALLOWED source(); + + boolean block_web_console() default false; + + boolean ignore_permissions() default true; +} diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_adminmode.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_adminmode.java index 6eefdf1f..30b6377e 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_adminmode.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_adminmode.java @@ -3,22 +3,16 @@ package me.StevenLawson.TotalFreedomMod.Commands; import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList; import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; -import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +@CommandPermissions(level = ADMIN_LEVEL.SUPER, source = SOURCE_TYPE_ALLOWED.ONLY_CONSOLE, block_web_console = true, ignore_permissions = false) public class Command_adminmode extends TFM_Command { @Override public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { - if (!senderIsConsole || TFM_Util.isFromClanforge(sender.getName())) - { - sender.sendMessage(ChatColor.GRAY + "This command may only be used from the Telnet or BukkitHttpd console."); - return true; - } - if (args[0].equalsIgnoreCase("off")) { TotalFreedomMod.adminOnlyMode = false; diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_cage.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_cage.java index 3bf3cf20..d4cc4d6f 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_cage.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_cage.java @@ -1,9 +1,7 @@ package me.StevenLawson.TotalFreedomMod.Commands; -import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList; import me.StevenLawson.TotalFreedomMod.TFM_UserInfo; import me.StevenLawson.TotalFreedomMod.TFM_Util; -import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Location; @@ -12,82 +10,76 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +@CommandPermissions(level = ADMIN_LEVEL.SUPER, source = SOURCE_TYPE_ALLOWED.BOTH, ignore_permissions = false) public class Command_cage extends TFM_Command { @Override public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { - if (senderIsConsole || TFM_SuperadminList.isUserSuperadmin(sender)) + if (args.length == 0) { - if (args.length == 0) - { - return false; - } + return false; + } - Player p; - try + Player p; + try + { + p = getPlayer(args[0]); + } + catch (CantFindPlayerException ex) + { + sender.sendMessage(ex.getMessage()); + return true; + } + + TFM_UserInfo playerdata = TFM_UserInfo.getPlayerData(p); + + Material cage_material_outer = Material.GLASS; + Material cage_material_inner = Material.AIR; + if (args.length >= 2) + { + if (TFM_Util.isStopCommand(args[1])) { - p = getPlayer(args[0]); - } - catch (CantFindPlayerException ex) - { - sender.sendMessage(ex.getMessage()); + playerdata.setCaged(false); + playerdata.regenerateHistory(); + playerdata.clearHistory(); + sender.sendMessage(ChatColor.GREEN + p.getName() + " uncaged."); return true; } - - TFM_UserInfo playerdata = TFM_UserInfo.getPlayerData(p); - - Material cage_material_outer = Material.GLASS; - Material cage_material_inner = Material.AIR; - if (args.length >= 2) + else { - if (TFM_Util.isStopCommand(args[1])) + cage_material_outer = Material.matchMaterial(args[1]); + if (cage_material_outer == null) { - playerdata.setCaged(false); - playerdata.regenerateHistory(); - playerdata.clearHistory(); - sender.sendMessage(ChatColor.GREEN + p.getName() + " uncaged."); - return true; - } - else - { - cage_material_outer = Material.matchMaterial(args[1]); - if (cage_material_outer == null) - { - cage_material_outer = Material.GLASS; - } + cage_material_outer = Material.GLASS; } } - - if (args.length >= 3) - { - if (args[2].equalsIgnoreCase("water")) - { - cage_material_inner = Material.STATIONARY_WATER; - } - else if (args[2].equalsIgnoreCase("lava")) - { - cage_material_inner = Material.STATIONARY_LAVA; - } - } - - Location target_pos = p.getLocation().add(0, 1, 0); - playerdata.setCaged(true, target_pos, cage_material_outer, cage_material_inner); - playerdata.regenerateHistory(); - playerdata.clearHistory(); - TFM_Util.buildHistory(target_pos, 2, playerdata); - TFM_Util.generateCube(target_pos, 2, playerdata.getCageMaterial(TFM_UserInfo.CageLayer.OUTER)); - TFM_Util.generateCube(target_pos, 1, playerdata.getCageMaterial(TFM_UserInfo.CageLayer.INNER)); - - p.setGameMode(GameMode.SURVIVAL); - - TFM_Util.bcastMsg(sender.getName() + " caged " + p.getName() + "!", ChatColor.YELLOW); } - else + + if (args.length >= 3) { - sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); + if (args[2].equalsIgnoreCase("water")) + { + cage_material_inner = Material.STATIONARY_WATER; + } + else if (args[2].equalsIgnoreCase("lava")) + { + cage_material_inner = Material.STATIONARY_LAVA; + } } + Location target_pos = p.getLocation().add(0, 1, 0); + playerdata.setCaged(true, target_pos, cage_material_outer, cage_material_inner); + playerdata.regenerateHistory(); + playerdata.clearHistory(); + TFM_Util.buildHistory(target_pos, 2, playerdata); + TFM_Util.generateCube(target_pos, 2, playerdata.getCageMaterial(TFM_UserInfo.CageLayer.OUTER)); + TFM_Util.generateCube(target_pos, 1, playerdata.getCageMaterial(TFM_UserInfo.CageLayer.INNER)); + + p.setGameMode(GameMode.SURVIVAL); + + TFM_Util.bcastMsg(sender.getName() + " caged " + p.getName() + "!", ChatColor.YELLOW); + return true; } } diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_fuckoff.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_fuckoff.java index 90ef2e4b..1b6bdfe6 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_fuckoff.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_fuckoff.java @@ -1,35 +1,16 @@ package me.StevenLawson.TotalFreedomMod.Commands; -import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList; -import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +@CommandPermissions(level = ADMIN_LEVEL.SENIOR, source = SOURCE_TYPE_ALLOWED.ONLY_IN_GAME, ignore_permissions = false) public class Command_fuckoff extends TFM_Command { @Override public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { - if (senderIsConsole) - { - sender.sendMessage(TotalFreedomMod.NOT_FROM_CONSOLE); - return true; - } - - if (!TFM_SuperadminList.isUserSuperadmin(sender)) - { - sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); - return true; - } - - if (!TFM_SuperadminList.isSeniorAdmin(sender_p)) - { - sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); - return true; - } - if (args.length < 1) { return false; diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/SOURCE_TYPE_ALLOWED.java b/src/me/StevenLawson/TotalFreedomMod/Commands/SOURCE_TYPE_ALLOWED.java new file mode 100644 index 00000000..376c22c9 --- /dev/null +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/SOURCE_TYPE_ALLOWED.java @@ -0,0 +1,6 @@ +package me.StevenLawson.TotalFreedomMod.Commands; + +public enum SOURCE_TYPE_ALLOWED +{ + ONLY_IN_GAME, ONLY_CONSOLE, BOTH +} diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_Command.java b/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_Command.java index b493e3e6..e5ead5f3 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_Command.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_Command.java @@ -2,6 +2,8 @@ package me.StevenLawson.TotalFreedomMod.Commands; import java.util.List; import me.StevenLawson.TotalFreedomMod.TFM_Log; +import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList; +import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import org.bukkit.ChatColor; import org.bukkit.Server; @@ -13,24 +15,94 @@ public class TFM_Command { protected TotalFreedomMod plugin; protected Server server; - + public TFM_Command() { } - + public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { TFM_Log.severe("Command Error: Command not implemented: " + cmd.getName()); sender.sendMessage(ChatColor.RED + "Command Error: Command not implemented: " + cmd.getName()); return false; } - + public void setPlugin(TotalFreedomMod plugin) { this.plugin = plugin; this.server = plugin.getServer(); } - + + public boolean senderHasPermission(Class cmd_class, CommandSender sender) + { + CommandPermissions permissions = cmd_class.getAnnotation(CommandPermissions.class); + if (permissions != null) + { + if (permissions.ignore_permissions()) + { + return true; + } + else + { + boolean is_super = TFM_SuperadminList.isUserSuperadmin(sender); + boolean is_senior = false; + if (is_super) + { + is_senior = TFM_SuperadminList.isSeniorAdmin(sender); + } + + ADMIN_LEVEL level = permissions.level(); + SOURCE_TYPE_ALLOWED source = permissions.source(); + boolean block_web_console = permissions.block_web_console(); + + //TFM_Log.info("Level: " + level + ", Source: " + source + ", BWC: " + block_web_console); + + Player sender_p = null; + if (sender instanceof Player) + { + sender_p = (Player) sender; + } + + if (sender_p == null) + { + if (source == SOURCE_TYPE_ALLOWED.ONLY_IN_GAME) + { + return false; + } + else if (level == ADMIN_LEVEL.SENIOR && !is_senior) + { + return false; + } + else if (block_web_console && TFM_Util.isFromClanforge(sender.getName())) + { + return false; + } + } + else + { + if (source == SOURCE_TYPE_ALLOWED.ONLY_CONSOLE) + { + return false; + } + else if (level == ADMIN_LEVEL.SENIOR && !is_senior) + { + return false; + } + else if (level == ADMIN_LEVEL.SUPER && !is_super) + { + return false; + } + else if (level == ADMIN_LEVEL.OP && !sender_p.isOp()) + { + return false; + } + } + return true; + } + } + return true; + } + public Player getPlayer(String partialname) throws CantFindPlayerException { List matches = server.matchPlayer(partialname); diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java index 71ad4c7c..a51a6a0e 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java @@ -149,7 +149,14 @@ public class TotalFreedomMod extends JavaPlugin try { - return dispatcher.run(sender, sender_p, cmd, commandLabel, args, senderIsConsole); + if (dispatcher.senderHasPermission(dispatcher.getClass(), sender)) + { + return dispatcher.run(sender, sender_p, cmd, commandLabel, args, senderIsConsole); + } + else + { + sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); + } } catch (Throwable ex) {