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,12 +10,11 @@ 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)
{
@ -82,11 +79,6 @@ public class Command_cage extends TFM_Command
p.setGameMode(GameMode.SURVIVAL);
TFM_Util.bcastMsg(sender.getName() + " caged " + p.getName() + "!", ChatColor.YELLOW);
}
else
{
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
}
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;
@ -31,6 +33,76 @@ public class TFM_Command
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

@ -148,9 +148,16 @@ public class TotalFreedomMod extends JavaPlugin
}
try
{
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)
{
sender.sendMessage(ChatColor.RED + "Command Error: " + ex.getMessage());