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.
This commit is contained in:
Steven Lawson 2012-11-22 22:48:28 -05:00
parent 502b055265
commit 13af6cb6a3
8 changed files with 166 additions and 92 deletions

View File

@ -0,0 +1,6 @@
package me.StevenLawson.TotalFreedomMod.Commands;
public enum ADMIN_LEVEL
{
ALL, OP, SUPER, SENIOR
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;

View File

@ -0,0 +1,6 @@
package me.StevenLawson.TotalFreedomMod.Commands;
public enum SOURCE_TYPE_ALLOWED
{
ONLY_IN_GAME, ONLY_CONSOLE, BOTH
}

View File

@ -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<Player> matches = server.matchPlayer(partialname);

View File

@ -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)
{