Work on the new command system, this is not ready at all.
This commit is only so I can work on it more at another computer.
This commit is contained in:
parent
cb8de04a91
commit
691d59f0f7
@ -7,6 +7,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.graywolf336.jail.beans.Jail;
|
||||
import com.graywolf336.jail.command.CommandHandler;
|
||||
import com.graywolf336.jail.command.JailHandler;
|
||||
import com.graywolf336.jail.enums.Settings;
|
||||
import com.graywolf336.jail.listeners.BlockListener;
|
||||
import com.graywolf336.jail.listeners.EntityListener;
|
||||
@ -24,6 +25,7 @@ import com.graywolf336.jail.listeners.ProtectionListener;
|
||||
public class JailMain extends JavaPlugin {
|
||||
private CommandHandler cmdHand;
|
||||
private HandCuffManager hcm;
|
||||
private JailHandler jh;
|
||||
private JailIO io;
|
||||
private JailManager jm;
|
||||
private JailTimer jt;
|
||||
@ -41,6 +43,7 @@ public class JailMain extends JavaPlugin {
|
||||
io.loadJails();
|
||||
|
||||
cmdHand = new CommandHandler(this);
|
||||
jh = new JailHandler(this);
|
||||
pm = new PrisonerManager(this);
|
||||
|
||||
PluginManager plm = this.getServer().getPluginManager();
|
||||
@ -103,7 +106,12 @@ public class JailMain extends JavaPlugin {
|
||||
* Send the command off to the CommandHandler class, that way this main class doesn't get clogged up.
|
||||
*/
|
||||
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
|
||||
cmdHand.handleCommand(jm, sender, command.getName().toLowerCase(), args);
|
||||
if(command.getName().equalsIgnoreCase("jail")) {
|
||||
jh.handleCommand(jm, sender, args);
|
||||
}else {
|
||||
cmdHand.handleCommand(jm, sender, command.getName().toLowerCase(), args);
|
||||
}
|
||||
|
||||
return true;//Always return true here, that way we can handle the help and command usage ourself.
|
||||
}
|
||||
|
||||
|
@ -15,10 +15,8 @@ import com.graywolf336.jail.command.commands.HandCuffCommand;
|
||||
import com.graywolf336.jail.command.commands.JailCheckCommand;
|
||||
import com.graywolf336.jail.command.commands.JailClearCommand;
|
||||
import com.graywolf336.jail.command.commands.JailClearForceCommand;
|
||||
import com.graywolf336.jail.command.commands.JailCommand;
|
||||
import com.graywolf336.jail.command.commands.JailCreateCommand;
|
||||
import com.graywolf336.jail.command.commands.JailListCellsCommand;
|
||||
import com.graywolf336.jail.command.commands.JailListCommand;
|
||||
import com.graywolf336.jail.command.commands.JailMuteCommand;
|
||||
import com.graywolf336.jail.command.commands.JailReloadCommand;
|
||||
import com.graywolf336.jail.command.commands.JailRemoveCellCommand;
|
||||
@ -156,10 +154,8 @@ public class CommandHandler {
|
||||
load(JailCheckCommand.class);
|
||||
load(JailClearCommand.class);
|
||||
load(JailClearForceCommand.class);
|
||||
load(JailCommand.class);
|
||||
load(JailCreateCommand.class);
|
||||
load(JailListCellsCommand.class);
|
||||
load(JailListCommand.class);
|
||||
load(JailMuteCommand.class);
|
||||
load(JailReloadCommand.class);
|
||||
load(JailRemoveCellCommand.class);
|
||||
|
160
src/main/java/com/graywolf336/jail/command/JailHandler.java
Normal file
160
src/main/java/com/graywolf336/jail/command/JailHandler.java
Normal file
@ -0,0 +1,160 @@
|
||||
package com.graywolf336.jail.command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.beust.jcommander.JCommander;
|
||||
import com.beust.jcommander.ParameterException;
|
||||
import com.graywolf336.jail.JailMain;
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.command.jcommands.JailFoundation;
|
||||
import com.graywolf336.jail.command.jcommands.ListJails;
|
||||
import com.graywolf336.jail.command.subcommands.JailCommand;
|
||||
import com.graywolf336.jail.command.subcommands.JailListCommand;
|
||||
import com.graywolf336.jail.enums.LangString;
|
||||
|
||||
public class JailHandler {
|
||||
private LinkedHashMap<String, Command> commands;
|
||||
|
||||
public JailHandler(JailMain plugin) {
|
||||
commands = new LinkedHashMap<String, Command>();
|
||||
loadCommands();
|
||||
|
||||
plugin.getLogger().info("Loaded " + commands.size() + " sub-commands of /jail.");
|
||||
}
|
||||
|
||||
public void handleCommand(JailManager jm, CommandSender sender, String... args) {
|
||||
JailFoundation foundation = new JailFoundation();
|
||||
JCommander jc = new JCommander(foundation);
|
||||
|
||||
//Now let's add the subcommands
|
||||
jc.addCommand("list", new ListJails());
|
||||
|
||||
try {
|
||||
jc.parse(args);
|
||||
|
||||
List<Command> matches = getMatches(jc.getParsedCommand());
|
||||
|
||||
if(matches.size() == 0) {
|
||||
//There should only be one for /jail
|
||||
parseCommand(jm, sender, getMatches("jail").get(0), args);
|
||||
} else if(matches.size() > 1) {
|
||||
for(Command c : matches)
|
||||
showUsage(sender, c);
|
||||
}else {
|
||||
parseCommand(jm, sender, matches.get(0), args);
|
||||
}
|
||||
}catch(ParameterException e) {
|
||||
parseCommand(jm, sender, getMatches("jail").get(0), args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the given command and checks that the command is in valid form.
|
||||
*
|
||||
* <p>
|
||||
*
|
||||
* It checks in the following order:
|
||||
* <ol>
|
||||
* <li>If they have permission for it, if they don't then we send them a message stating so.</li>
|
||||
* <li>If the command needs a player instance, if so we send a message stating that.</li>
|
||||
* <li>If the required minimum arguments have been passed, if not sends the usage.</li>
|
||||
* <li>If the required maximum arguments have been passed (if there is a max, -1 if no max), if not sends the usage.</li>
|
||||
* <li>Then executes, upon failed execution it sends the usage command.</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param jailmanager The instance of {@link JailManager}.
|
||||
* @param sender The sender of the command.
|
||||
* @param command The name of the command.
|
||||
* @param args The arguments passed to the command.
|
||||
*/
|
||||
public boolean parseCommand(JailManager jailmanager, CommandSender sender, Command c, String[] args) {
|
||||
CommandInfo i = c.getClass().getAnnotation(CommandInfo.class);
|
||||
|
||||
// First, let's check if the sender has permission for the command.
|
||||
if(!sender.hasPermission(i.permission())) {
|
||||
sender.sendMessage(jailmanager.getPlugin().getJailIO().getLanguageString(LangString.NOPERMISSION));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Next, let's check if we need a player and then if the sender is actually a player
|
||||
if(i.needsPlayer() && !(sender instanceof Player)) {
|
||||
sender.sendMessage(jailmanager.getPlugin().getJailIO().getLanguageString(LangString.PLAYERCONTEXTREQUIRED));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now, let's check the size of the arguments passed. If it is shorter than the minimum required args, let's show the usage.
|
||||
if(args.length < i.minimumArgs()) {
|
||||
showUsage(sender, c);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Then, if the maximumArgs doesn't equal -1, we need to check if the size of the arguments passed is greater than the maximum args.
|
||||
if(i.maxArgs() != -1 && i.maxArgs() < args.length) {
|
||||
showUsage(sender, c);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Since everything has been checked and we're all clear, let's execute it.
|
||||
// But if get back false, let's show the usage message.
|
||||
try {
|
||||
if(!c.execute(jailmanager, sender, args)) {
|
||||
showUsage(sender, c);
|
||||
return false;
|
||||
}else {
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
jailmanager.getPlugin().getLogger().severe("An error occured while handling the command: " + i.usage());
|
||||
showUsage(sender, c);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private List<Command> getMatches(String command) {
|
||||
List<Command> result = new ArrayList<Command>();
|
||||
|
||||
for(Entry<String, Command> entry : commands.entrySet()) {
|
||||
if(command.matches(entry.getKey())) {
|
||||
result.add(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the usage information to the sender, if they have permission.
|
||||
*
|
||||
* @param sender The sender of the command
|
||||
* @param command The command to send usage of.
|
||||
*/
|
||||
private void showUsage(CommandSender sender, Command command) {
|
||||
CommandInfo info = command.getClass().getAnnotation(CommandInfo.class);
|
||||
if(!sender.hasPermission(info.permission())) return;
|
||||
|
||||
sender.sendMessage(info.usage());
|
||||
}
|
||||
|
||||
private void loadCommands() {
|
||||
load(JailCommand.class);
|
||||
load(JailListCommand.class);
|
||||
}
|
||||
|
||||
private void load(Class<? extends Command> c) {
|
||||
CommandInfo info = c.getAnnotation(CommandInfo.class);
|
||||
if(info == null) return;
|
||||
|
||||
try {
|
||||
commands.put(info.pattern(), c.newInstance());
|
||||
}catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.graywolf336.jail.command.jcommands;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
|
||||
public class JailFoundation {
|
||||
|
||||
@Parameter(names = { "-v", "-verbose" })
|
||||
public boolean verbose = false;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.graywolf336.jail.command.parameters;
|
||||
package com.graywolf336.jail.command.jcommands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -12,26 +12,26 @@ import com.beust.jcommander.Parameter;
|
||||
* @version 1.0.1
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public class JailParameters {
|
||||
public class Jailing {
|
||||
@Parameter
|
||||
private List<String> parameters = new ArrayList<String>();
|
||||
|
||||
@Parameter(names = { "-p", "-player", "-prisoner" }, description = "The name of the player we are jailing.", required = true)
|
||||
@Parameter(names = { "-player", "-p", "-prisoner" }, description = "The name of the player we are jailing.")
|
||||
private String player = "";
|
||||
|
||||
@Parameter(names = { "-t", "-time", "-length" }, description = "The length of the jailing sentence.")
|
||||
@Parameter(names = { "-time", "-t", "-length" }, description = "The length of the jailing sentence.")
|
||||
private String time = "";
|
||||
|
||||
@Parameter(names = { "-j", "-jail", "-prison" }, description = "The jail we are sending the player to.")
|
||||
@Parameter(names = { "-jail", "-j", "-prison" }, description = "The jail we are sending the player to.")
|
||||
private String jail = "";
|
||||
|
||||
@Parameter(names = { "-c", "-cell" }, description = "The cell in the jail we are sending them to.")
|
||||
@Parameter(names = { "-cell", "-c"}, description = "The cell in the jail we are sending them to.")
|
||||
private String cell = "";
|
||||
|
||||
@Parameter(names = { "-m", "-muted" }, description = "Whether they can talk or not.")
|
||||
@Parameter(names = { "-muted", "-m" }, description = "Whether they can talk or not.")
|
||||
private boolean muted = false;
|
||||
|
||||
@Parameter(names = { "-r", "-reason" }, description = "The reason this player is being jailed for.", variableArity = true)
|
||||
@Parameter(names = { "-reason", "-r" }, description = "The reason this player is being jailed for.", variableArity = true)
|
||||
private List<String> reason = new ArrayList<String>();
|
||||
|
||||
/** Returns the parameters. */
|
@ -0,0 +1,13 @@
|
||||
package com.graywolf336.jail.command.jcommands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.Parameters;
|
||||
|
||||
@Parameters(commandDescription = "Lists all the jails in the system.")
|
||||
public class ListJails {
|
||||
@Parameter
|
||||
private List<String> parameters = new ArrayList<String>();
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
package com.graywolf336.jail.command.commands;
|
||||
package com.graywolf336.jail.command.subcommands;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -13,7 +17,7 @@ import com.graywolf336.jail.beans.Jail;
|
||||
import com.graywolf336.jail.beans.Prisoner;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
import com.graywolf336.jail.command.parameters.JailParameters;
|
||||
import com.graywolf336.jail.command.jcommands.Jailing;
|
||||
import com.graywolf336.jail.enums.LangString;
|
||||
import com.graywolf336.jail.enums.Settings;
|
||||
import com.graywolf336.jail.events.PrePrisonerJailedEvent;
|
||||
@ -24,7 +28,7 @@ import com.graywolf336.jail.events.PrePrisonerJailedEvent;
|
||||
needsPlayer = false,
|
||||
pattern = "jail|j",
|
||||
permission = "jail.command.jail",
|
||||
usage = "/jail [-p name] (-t time) (-j JailName) (-c CellName) (-m Muted) (-r A reason for jailing)"
|
||||
usage = "/jail [name] (-t time) (-j JailName) (-c CellName) (-m Muted) (-r A reason for jailing)"
|
||||
)
|
||||
public class JailCommand implements Command {
|
||||
|
||||
@ -46,10 +50,14 @@ public class JailCommand implements Command {
|
||||
return true;
|
||||
}
|
||||
|
||||
JailParameters params = new JailParameters();
|
||||
//This is just to add the -p param so jCommander doesn't blow up
|
||||
List<String> arguments = new LinkedList<String>(Arrays.asList(args));
|
||||
arguments.add(0, "-p");
|
||||
|
||||
Jailing params = new Jailing();
|
||||
|
||||
try {
|
||||
new JCommander(params, args);
|
||||
new JCommander(params, arguments.toArray(new String[arguments.size()]));
|
||||
}catch(ParameterException e) {
|
||||
sender.sendMessage(ChatColor.RED + e.getMessage());
|
||||
return true;
|
@ -1,4 +1,4 @@
|
||||
package com.graywolf336.jail.command.commands;
|
||||
package com.graywolf336.jail.command.subcommands;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -13,9 +13,9 @@ import com.graywolf336.jail.enums.LangString;
|
||||
maxArgs = 0,
|
||||
minimumArgs = 0,
|
||||
needsPlayer = false,
|
||||
pattern = "jaillist|jl",
|
||||
pattern = "list|l",
|
||||
permission = "jail.command.jaillist",
|
||||
usage = "/jaillist"
|
||||
usage = "/jail list"
|
||||
)
|
||||
public class JailListCommand implements Command {
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
@ -6,14 +6,13 @@ import org.junit.Test;
|
||||
|
||||
import com.beust.jcommander.JCommander;
|
||||
import com.beust.jcommander.ParameterException;
|
||||
|
||||
import com.graywolf336.jail.command.parameters.JailParameters;
|
||||
import com.graywolf336.jail.command.jcommands.Jailing;
|
||||
|
||||
public class TestCommandParams {
|
||||
|
||||
@Test
|
||||
public void TestJailCommand() {
|
||||
JailParameters jail = new JailParameters();
|
||||
Jailing jail = new Jailing();
|
||||
//"/jail [-p name] (-t time) (-j JailName) (-c CellName) (-m Muted) (-r A reason for jailing)"
|
||||
String[] params = { "-p", "graywolf336", "-t", "30", "-j", "den", "-c", "cell_01", "-m", "-r", "He", "was", "a", "very", "bad", "boy." };
|
||||
new JCommander(jail, params);
|
||||
@ -28,8 +27,8 @@ public class TestCommandParams {
|
||||
|
||||
@Test(expected=ParameterException.class)
|
||||
public void TestFailedJailCommand() {
|
||||
JailParameters jail = new JailParameters();
|
||||
String[] params = { "-t", "30", "-j", "den", "-c", "cell_01", "-m", "-r", "He", "was", "a", "very", "bad", "boy." };
|
||||
Jailing jail = new Jailing();
|
||||
String[] params = { "-t", "30", "-j", "den", "-c", "cell_01", "-m true", "-r", "He", "was", "a", "very", "bad", "boy." };
|
||||
|
||||
new JCommander(jail, params);
|
||||
}
|
||||
|
@ -99,4 +99,15 @@ public class TestJailCommandInfo {
|
||||
verify(sender).sendMessage(ChatColor.GREEN + "First, you must select jail cuboid. Select the first point of the cuboid by right clicking on the block with your wooden sword. DO NOT FORGET TO MARK THE FLOOR AND CEILING TOO!");
|
||||
verify(sender).sendMessage(ChatColor.AQUA + "--------------------------------------");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJailingCommand() {
|
||||
Command command = mock(Command.class);
|
||||
when(command.getName()).thenReturn("jail");
|
||||
String[] args = { "graywolf336", "-t", "30", "-j", "den", "-c", "cell_01", "-m", "-r", "He", "was", "a", "very", "bad", "boy." };
|
||||
|
||||
CommandSender sender = creator.getPlayerCommandSender();
|
||||
assertTrue(main.onCommand(sender, command, "jail", args));
|
||||
verify(sender).sendMessage(ChatColor.RED + "There are currently no jails.");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user