Merge pull request #6 from graywolf336/CommandCleanUp

Command Clean Up
This commit is contained in:
Bradley Hilton 2014-02-11 20:49:02 -06:00
commit 0b69133114
30 changed files with 165 additions and 601 deletions

View File

@ -67,12 +67,6 @@
<type>jar</type>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.32</version>
</dependency>
<dependency>
<groupId>com.lexicalscope.jewelcli</groupId>
<artifactId>jewelcli</artifactId>

View File

@ -118,7 +118,7 @@ public class JailMain extends JavaPlugin {
*/
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
if(command.getName().equalsIgnoreCase("jail")) {
jh.handleCommand(jm, sender, args);
jh.parseCommand(jm, sender, args);
}else {
cmdHand.handleCommand(jm, sender, command.getName().toLowerCase(), args);
}

View File

@ -58,11 +58,13 @@ public class CommandHandler {
public void handleCommand(JailManager jailmanager, CommandSender sender, String command, String[] args) {
List<Command> matches = getMatches(command);
//If no matches were found, send them the unknown command message.
if(matches.size() == 0) {
sender.sendMessage(jailmanager.getPlugin().getJailIO().getLanguageString(LangString.UNKNOWNCOMMAND, command));
return;
}
//If more than one command was found, send them each command's help message.
if(matches.size() > 1) {
for(Command c : matches)
showUsage(sender, c);

View File

@ -1,7 +1,6 @@
package com.graywolf336.jail.command;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
@ -9,26 +8,8 @@ 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.CellCreate;
import com.graywolf336.jail.command.jcommands.Check;
import com.graywolf336.jail.command.jcommands.Clear;
import com.graywolf336.jail.command.jcommands.ClearForce;
import com.graywolf336.jail.command.jcommands.Create;
import com.graywolf336.jail.command.jcommands.JailFoundation;
import com.graywolf336.jail.command.jcommands.JailList;
import com.graywolf336.jail.command.jcommands.ListCells;
import com.graywolf336.jail.command.jcommands.Mute;
import com.graywolf336.jail.command.jcommands.Reload;
import com.graywolf336.jail.command.jcommands.RemoveCell;
import com.graywolf336.jail.command.jcommands.Stop;
import com.graywolf336.jail.command.jcommands.TeleIn;
import com.graywolf336.jail.command.jcommands.TeleOut;
import com.graywolf336.jail.command.jcommands.Transfer;
import com.graywolf336.jail.command.jcommands.Version;
import com.graywolf336.jail.command.subcommands.JailCellCreateCommand;
import com.graywolf336.jail.command.subcommands.JailCheckCommand;
import com.graywolf336.jail.command.subcommands.JailClearCommand;
@ -49,46 +30,13 @@ import com.graywolf336.jail.enums.LangString;
public class JailHandler {
private LinkedHashMap<String, Command> commands;
private HashMap<String, Object> addCmds;
public JailHandler(JailMain plugin) {
commands = new LinkedHashMap<String, Command>();
addCmds = new HashMap<String, Object>();
loadCommands();
plugin.getLogger().info("Loaded " + commands.size() + " sub-commands of /jail.");
}
public void handleCommand(JailManager jm, CommandSender sender, String... args) {
if(args.length == 0) {
parseCommand(jm, sender, getMatches("jail").get(0), args);
}else {
JailFoundation foundation = new JailFoundation();
JCommander jc = new JCommander(foundation);
for(Entry<String, Object> e : addCmds.entrySet()) {
jc.addCommand(e.getKey(), e.getValue());
}
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.
@ -109,21 +57,49 @@ public class JailHandler {
* @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) {
public boolean parseCommand(JailManager jailmanager, CommandSender sender, String[] args) {
Command c = null;
//If they didn't provide any arguments (aka just: /jail) then we will need to send them some help
if(args.length == 0) {
//TODO: Create the help page(s)
c = getMatches("jail").get(0);
}else {
//Get the matches from the first argument passed
List<Command> matches = getMatches(args[0]);
if(matches.size() == 0) {
//No matches found, thus it is more likely than not they are trying to jail someone
c = getMatches("jail").get(0);
} else if(matches.size() > 1) {
//If there was found more than one match
//then let's send the usage of each match to the sender
for(Command cmd : matches)
showUsage(sender, cmd);
return true;
}else {
//Only one match was found, so let's continue
c = matches.get(0);
}
}
CommandInfo i = c.getClass().getAnnotation(CommandInfo.class);
// First, let's check if the sender has permission for the command.
if(!sender.hasPermission(i.permission())) {
jailmanager.getPlugin().debug("Sender has no permission.");
sender.sendMessage(jailmanager.getPlugin().getJailIO().getLanguageString(LangString.NOPERMISSION));
return false;
return true;
}
// Next, let's check if we need a player and then if the sender is actually a player
if(i.needsPlayer() && !(sender instanceof Player)) {
jailmanager.getPlugin().debug("Sender is not a player.");
sender.sendMessage(jailmanager.getPlugin().getJailIO().getLanguageString(LangString.PLAYERCONTEXTREQUIRED));
return false;
return true;
}
// Now, let's check the size of the arguments passed. If it is shorter than the minimum required args, let's show the usage.
@ -131,7 +107,7 @@ public class JailHandler {
if(args.length - 1 < i.minimumArgs()) {
jailmanager.getPlugin().debug("Sender didn't provide enough arguments.");
showUsage(sender, c);
return false;
return true;
}
// 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.
@ -139,7 +115,7 @@ public class JailHandler {
if(i.maxArgs() != -1 && i.maxArgs() < args.length - 1) {
jailmanager.getPlugin().debug("Sender provided too many arguments.");
showUsage(sender, c);
return false;
return true;
}
// Since everything has been checked and we're all clear, let's execute it.
@ -147,7 +123,7 @@ public class JailHandler {
try {
if(!c.execute(jailmanager, sender, args)) {
showUsage(sender, c);
return false;
return true;
}else {
return true;
}
@ -155,7 +131,7 @@ public class JailHandler {
e.printStackTrace();
jailmanager.getPlugin().getLogger().severe("An error occured while handling the command: " + i.usage());
showUsage(sender, c);
return false;
return true;
}
}
@ -201,37 +177,6 @@ public class JailHandler {
load(JailTeleOutCommand.class);
load(JailTransferCommand.class);
load(JailVersionCommand.class);
//Puts the commands in the HashMap
addCmds.put("cellcreate", new CellCreate());
addCmds.put("cc", new CellCreate());
addCmds.put("check", new Check());
addCmds.put("clear", new Clear());
addCmds.put("clearforce", new ClearForce());
addCmds.put("cf", new ClearForce());
addCmds.put("create", new Create());
addCmds.put("list", new JailList());
addCmds.put("l", new JailList());
addCmds.put("listcells", new ListCells());
addCmds.put("lc", new ListCells());
addCmds.put("mute", new Mute());
addCmds.put("m", new Mute());
addCmds.put("reload", new Reload());
addCmds.put("r", new Reload());
addCmds.put("removecell", new RemoveCell());
addCmds.put("rcell", new RemoveCell());
addCmds.put("rc", new RemoveCell());
addCmds.put("stop", new Stop());
addCmds.put("s", new Stop());
addCmds.put("telein", new TeleIn());
addCmds.put("teleportin", new TeleIn());
addCmds.put("teleout", new TeleOut());
addCmds.put("teleportout", new TeleOut());
addCmds.put("transfer", new Transfer());
addCmds.put("trans", new Transfer());
addCmds.put("version", new Version());
addCmds.put("ver", new Version());
addCmds.put("v", new Version());
}
private void load(Class<? extends Command> c) {

View File

@ -0,0 +1,32 @@
package com.graywolf336.jail.command.commands.jewels;
import java.util.List;
import com.lexicalscope.jewel.cli.Option;
public interface Jailing {
@Option(longName={"player", "pl"}, shortName="p", description = "the player's name")
public String getPlayer();
@Option(longName={"time", "length"}, shortName="t", description = "the amount of time")
public String getTime();
@Option(longName={"jail", "prison"}, shortName="j", description = "the jail")
public String getJail();
@Option(longName={"cell"}, shortName="c", description = "the cell")
public String getCell();
@Option(longName={"muted", "canttalk"}, shortName="m", description = "whether the prisoner is muted or not")
public boolean getMuted();
@Option(longName={"reason"}, shortName="r", description = "the reason this player is being jailed")
public List<String> getReason();
public boolean isTime();
public boolean isJail();
public boolean isCell();
public boolean isMuted();
public boolean isReason();
}

View File

@ -0,0 +1,19 @@
package com.graywolf336.jail.command.commands.jewels;
import com.lexicalscope.jewel.cli.Option;
public interface Transfer {
@Option(longName={"player", "pl"}, shortName="p", description = "the player's name")
public String getPlayer();
@Option(longName={"jail", "prison"}, shortName="j", description = "the jail")
public String getJail();
@Option(longName={"cell"}, shortName="c", description = "the cell")
public String getCell();
public boolean isPlayer();
public boolean isJail();
public boolean isCell();
}

View File

@ -1,105 +0,0 @@
package com.graywolf336.jail.command.commands.params;
import java.util.ArrayList;
import java.util.List;
import com.beust.jcommander.Parameter;
/**
* Contains all the parameters from the jail command.
*
* @author graywolf336
* @version 1.0.1
* @since 3.0.0
*/
public class Jailing {
@Parameter
private List<String> parameters = new ArrayList<String>();
@Parameter(names = { "-player", "-p", "-prisoner" }, description = "The name of the player we are jailing.")
private String player = "";
@Parameter(names = { "-time", "-t", "-length" }, description = "The length of the jailing sentence.")
private String time = "";
@Parameter(names = { "-jail", "-j", "-prison" }, description = "The jail we are sending the player to.")
private String jail = "";
@Parameter(names = { "-cell", "-c"}, description = "The cell in the jail we are sending them to.")
private String cell = "";
@Parameter(names = { "-muted", "-m" }, description = "Whether they can talk or not.")
private boolean muted = false;
@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. */
public List<String> parameters() {
return parameters;
}
/** Returns the player parameter. */
public String player() {
return player;
}
/** Returns the time parameter. */
public String time() {
return time;
}
/** Sets the time parameter. */
public void setTime(String time) {
this.time = time;
}
/** Returns the jail parameter. */
public String jail() {
return jail;
}
/** Sets the jail parameter. */
public void setJail(String jail) {
this.jail = jail;
}
/** Returns the cell parameter. */
public String cell() {
return cell;
}
/** Returns the muted parameter. */
public boolean muted() {
return muted;
}
/** Sets the muted parameter. */
public void setMuted(boolean muted) {
this.muted = muted;
}
/** Returns the reason compressed into one string. */
public String reason() {
String r = "";
for(String s : reason) {
if(r.isEmpty()) {
r = s;
}else {
r += " " + s;
}
}
return r;
}
/** Sets the reason. */
public void setReason(String r) {
String[] rs = r.split(" ");
for(String s : rs) {
reason.add(s);
}
}
}

View File

@ -1,45 +0,0 @@
package com.graywolf336.jail.command.commands.params;
import java.util.ArrayList;
import java.util.List;
import com.beust.jcommander.Parameter;
public class Transferring {
@Parameter
private List<String> parameters = new ArrayList<String>();
@Parameter(names = { "-player", "-p", "-prisoner" }, description = "The name of the player we are jailing.")
private String player = "";
@Parameter(names = { "-jail", "-j", "-prison" }, description = "The jail we are sending the player to.")
private String jail = "";
@Parameter(names = { "-cell", "-c"}, description = "The cell in the jail we are sending them to.")
private String cell = "";
/** Returns the parameters. */
public List<String> parameters() {
return parameters;
}
/** Returns the player parameter. */
public String player() {
return player;
}
/** Returns the jail parameter. */
public String jail() {
return jail;
}
/** Sets the jail parameter. */
public void setJail(String jail) {
this.jail = jail;
}
/** Returns the cell parameter. */
public String cell() {
return cell;
}
}

View File

@ -1,13 +0,0 @@
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 = "Starts the creation of cells in the specified jail.")
public class CellCreate {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Checks if a player is jailed and if so gives the sender information about the jailing.")
public class Check {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Unjails all the prisoners from all the jails or the specified jail.")
public class Clear {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Starts the creation process of creating a jail.")
public class ClearForce {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Starts the creation process of creating a jail.")
public class Create {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,8 +0,0 @@
package com.graywolf336.jail.command.jcommands;
import com.beust.jcommander.Parameter;
public class JailFoundation {
@Parameter(names = { "-v", "-verbose" })
public boolean verbose = false;
}

View File

@ -1,13 +0,0 @@
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 JailList {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 cells in a certain jail.")
public class ListCells {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Mutes a player who is in jail.")
public class Mute {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Reloads the plugin and data and all.")
public class Reload {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Removes the cell from the jail.")
public class RemoveCell {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Stop the creations of anything.")
public class Stop {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Teleports a player to the in of jail.")
public class TeleIn {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Teleports the player to the teleport out location.")
public class TeleOut {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Transfers a player to another jail.")
public class Transfer {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,13 +0,0 @@
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 = "Shows the version of the plugin.")
public class Version {
@Parameter
private List<String> parameters = new ArrayList<String>();
}

View File

@ -1,16 +0,0 @@
package com.graywolf336.jail.command.jewel;
import java.util.List;
import com.lexicalscope.jewel.cli.Option;
public interface Jailing {
@Option(shortName={"player", "pl", "p"})
String getPlayer();
@Option(shortName="c")
String getCell();
@Option(shortName="r")
List<String> getReason();
}

View File

@ -8,8 +8,6 @@ import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import com.graywolf336.jail.JailManager;
import com.graywolf336.jail.Util;
import com.graywolf336.jail.beans.Cell;
@ -17,10 +15,12 @@ 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.commands.params.Jailing;
import com.graywolf336.jail.command.commands.jewels.Jailing;
import com.graywolf336.jail.enums.LangString;
import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.events.PrePrisonerJailedEvent;
import com.lexicalscope.jewel.cli.ArgumentValidationException;
import com.lexicalscope.jewel.cli.CliFactory;
@CommandInfo(
maxArgs = -1,
@ -56,26 +56,26 @@ public class JailCommand implements Command {
//has a name which is one of our subcommands
if(!arguments.contains("-p")) arguments.add(0, "-p");
Jailing params = new Jailing();
Jailing params = null;
try {
new JCommander(params, arguments.toArray(new String[arguments.size()]));
}catch(ParameterException e) {
params = CliFactory.parseArguments(Jailing.class, args);
}catch(ArgumentValidationException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
return true;
}
//Check if they've actually given us a player to jail
if(params.player().isEmpty()) {
if(params.getPlayer().isEmpty()) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PROVIDEAPLAYER, LangString.JAILING));
return true;
}else {
jm.getPlugin().debug("We are getting ready to handle jailing: " + params.player());
jm.getPlugin().debug("We are getting ready to handle jailing: " + params.getPlayer());
}
//Check if the given player is already jailed or not
if(jm.isPlayerJailed(params.player())) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.ALREADYJAILED, params.player()));
if(jm.isPlayerJailed(params.getPlayer())) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.ALREADYJAILED, params.getPlayer()));
return true;
}
@ -83,12 +83,12 @@ public class JailCommand implements Command {
//from the config and if that isn't there then we default to thirty minutes.
Long time = 10L;
try {
if(params.time().isEmpty()) {
if(params.getTime().isEmpty()) {
time = Util.getTime(jm.getPlugin().getConfig().getString(Settings.JAILDEFAULTTIME.getPath(), "30m"));
}else if(params.time() == String.valueOf(-1)) {
}else if(params.getTime() == String.valueOf(-1)) {
time = -1L;
}else {
time = Util.getTime(params.time());
time = Util.getTime(params.getTime());
}
}catch(Exception e) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NUMBERFORMATINCORRECT));
@ -98,35 +98,37 @@ public class JailCommand implements Command {
//Check the jail params. If it is empty, let's get the default jail
//from the config. If that is nearest, let's make a call to getting the nearest jail to
//the sender but otherwise if it isn't nearest then let's set it to the default jail
//which is defined in the config. After that is done, we set the name of it in the params
//so that we can keep consistency.
if(params.jail().isEmpty()) {
//which is defined in the config.
String jailName = "";
if(params.getJail().isEmpty()) {
String dJail = jm.getPlugin().getConfig().getString(Settings.DEFAULTJAIL.getPath());
if(dJail.equalsIgnoreCase("nearest")) {
params.setJail(jm.getNearestJail(sender).getName());
jailName = jm.getNearestJail(sender).getName();
}else {
params.setJail(jm.getPlugin().getConfig().getString(Settings.DEFAULTJAIL.getPath()));
jailName = jm.getPlugin().getConfig().getString(Settings.DEFAULTJAIL.getPath());
}
}else if(jm.getJail(params.jail()) == null) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAIL, params.jail()));
}else if(jm.getJail(params.getJail()) == null) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAIL, params.getJail()));
return true;
}else {
jailName = params.getJail();
}
//Check if the cell is defined, and if so check to be sure it exists.
if(!params.cell().isEmpty()) {
if(jm.getJail(params.jail()).getCell(params.cell()) == null) {
if(!params.getCell().isEmpty()) {
if(jm.getJail(params.getJail()).getCell(params.getCell()) == null) {
//There is no cell by that name
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOCELL, new String[] { params.cell(), params.jail() }));
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOCELL, new String[] { params.getCell(), params.getJail() }));
return true;
}else if(jm.getJail(params.jail()).getCell(params.cell()).hasPrisoner()) {
}else if(jm.getJail(params.getJail()).getCell(params.getCell()).hasPrisoner()) {
//If the cell has a prisoner, don't allow jailing them to that particular cell
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.CELLNOTEMPTY, params.cell()));
Cell suggestedCell = jm.getJail(params.jail()).getFirstEmptyCell();
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.CELLNOTEMPTY, params.getCell()));
Cell suggestedCell = jm.getJail(params.getJail()).getFirstEmptyCell();
if(suggestedCell != null) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.SUGGESTEDCELL, new String[] { params.jail(), suggestedCell.getName() }));
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.SUGGESTEDCELL, new String[] { params.getJail(), suggestedCell.getName() }));
}else {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOEMPTYCELLS, params.jail()));
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOEMPTYCELLS, params.getJail()));
}
return true;
@ -134,16 +136,26 @@ public class JailCommand implements Command {
}
//If the jailer gave no reason, then let's get the default reason
if(params.reason().isEmpty()) {
params.setReason(jm.getPlugin().getJailIO().getLanguageString(LangString.DEFAULTJAILEDREASON));
String reason = "";
if(params.getReason().isEmpty()) {
reason = jm.getPlugin().getJailIO().getLanguageString(LangString.DEFAULTJAILEDREASON);
}else {
StringBuilder sb = new StringBuilder();
for(String s : params.getReason()) {
sb.append(s).append(' ');
}
sb.deleteCharAt(sb.length() - 1);
reason = sb.toString();
}
//If the config has automatic muting, then let's set them as muted
boolean muted = params.getMuted();
if(jm.getPlugin().getConfig().getBoolean(Settings.AUTOMATICMUTE.getPath())) {
params.setMuted(true);
muted = true;
}
Player p = jm.getPlugin().getServer().getPlayer(params.player());
Player p = jm.getPlugin().getServer().getPlayer(params.getPlayer());
//If the player instance is not null and the player has the permission
//'jail.cantbejailed' then don't allow this to happen
@ -153,9 +165,9 @@ public class JailCommand implements Command {
}
//Get the jail instance from the name of jail in the params.
Jail j = jm.getJail(params.jail());
Cell c = j.getCell(params.cell());
Prisoner pris = new Prisoner(params.player(), params.muted(), time, sender.getName(), params.reason());
Jail j = jm.getJail(jailName);
Cell c = j.getCell(params.getCell());
Prisoner pris = new Prisoner(params.getPlayer(), muted, time, sender.getName(), reason);
//call the event
PrePrisonerJailedEvent event = new PrePrisonerJailedEvent(j, c, pris, p, p == null, pris.getJailer());
@ -164,7 +176,7 @@ public class JailCommand implements Command {
//check if the event is cancelled
if(event.isCancelled()) {
if(event.getCancelledMessage().isEmpty())
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.CANCELLEDBYANOTHERPLUGIN, params.player()));
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.CANCELLEDBYANOTHERPLUGIN, params.getPlayer()));
else
sender.sendMessage(event.getCancelledMessage());

View File

@ -3,15 +3,15 @@ package com.graywolf336.jail.command.subcommands;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import com.graywolf336.jail.JailManager;
import com.graywolf336.jail.beans.Cell;
import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.command.Command;
import com.graywolf336.jail.command.CommandInfo;
import com.graywolf336.jail.command.commands.params.Transferring;
import com.graywolf336.jail.command.commands.jewels.Transfer;
import com.graywolf336.jail.enums.LangString;
import com.lexicalscope.jewel.cli.ArgumentValidationException;
import com.lexicalscope.jewel.cli.CliFactory;
@CommandInfo(
maxArgs = 6,
@ -29,59 +29,59 @@ public class JailTransferCommand implements Command {
}
//Parse the command
Transferring params = new Transferring();
Transfer params = null;
try {
new JCommander(params, args);
}catch(ParameterException e) {
params = CliFactory.parseArguments(Transfer.class, args);
}catch(ArgumentValidationException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
return true;
}
//Verify they gave us a player and if so check if they're jailed
if(params.player().isEmpty()) {
if(params.getPlayer().isEmpty()) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PROVIDEAPLAYER, LangString.TRANSFERRING));
return true;
}else if(!jm.isPlayerJailed(params.player())) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOTJAILED, params.player()));
}else if(!jm.isPlayerJailed(params.getPlayer())) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOTJAILED, params.getPlayer()));
return true;
}
jm.getPlugin().debug("Checking everything before we transfer: " + params.player());
jm.getPlugin().debug("Checking everything before we transfer: " + params.getPlayer());
//If they didn't provide a jail, tell them we need one
if(params.jail().isEmpty()) {
if(params.getJail().isEmpty()) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PROVIDEAJAIL, LangString.TRANSFERRING));
return true;
}else {
//Check if the jail they did provide exists
if(jm.getJail(params.jail()) == null) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAIL, params.jail()));
if(jm.getJail(params.getJail()) == null) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAIL, params.getJail()));
return true;
}
}
Jail target = jm.getJail(params.jail());
Jail target = jm.getJail(params.getJail());
Cell targetCell = null;
//Check if they provided a cell and if so does it exist
if(!params.cell().isEmpty()) {
if(target.getCell(params.cell()) == null) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOCELL, params.cell()));
if(!params.getCell().isEmpty()) {
if(target.getCell(params.getCell()) == null) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOCELL, params.getCell()));
return true;
}else {
//Store the cell for easy of access and also check if it already is full
targetCell = target.getCell(params.cell());
targetCell = target.getCell(params.getCell());
if(targetCell.hasPrisoner()) {
//If the cell has a prisoner, don't allow jailing them to that particular cell
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.CELLNOTEMPTY, params.cell()));
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.CELLNOTEMPTY, params.getCell()));
//But suggest the first empty cell we find
Cell suggestedCell = jm.getJail(params.jail()).getFirstEmptyCell();
Cell suggestedCell = jm.getJail(params.getCell()).getFirstEmptyCell();
if(suggestedCell != null) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.SUGGESTEDCELL, new String[] { params.jail(), suggestedCell.getName() }));
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.SUGGESTEDCELL, new String[] { params.getCell(), suggestedCell.getName() }));
}else {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOEMPTYCELLS, params.jail()));
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOEMPTYCELLS, params.getCell()));
}
return true;
@ -92,9 +92,9 @@ public class JailTransferCommand implements Command {
jm.getPlugin().debug("Sending the transferring off, jail and cell check all came out clean.");
//Start the transferring of the prisoner
jm.getPlugin().getPrisonerManager().transferPrisoner(jm.getJailPlayerIsIn(params.player()),
jm.getJailPlayerIsIn(params.player()).getCellPrisonerIsIn(params.player()),
target, targetCell, jm.getPrisoner(params.player()));
jm.getPlugin().getPrisonerManager().transferPrisoner(jm.getJailPlayerIsIn(params.getPlayer()),
jm.getJailPlayerIsIn(params.getPlayer()).getCellPrisonerIsIn(params.getPlayer()),
target, targetCell, jm.getPrisoner(params.getPlayer()));
return true;
}

View File

@ -4,10 +4,10 @@ import junit.framework.Assert;
import org.junit.Test;
import com.graywolf336.jail.command.jewel.Jailing;
import com.graywolf336.jail.command.commands.jewels.Jailing;
import com.lexicalscope.jewel.cli.CliFactory;
public class TestJewel {
public class JewelCommands {
@Test
public void testJewel() {
String[] args = { "--player", "graywolf336", "-c", "testing", "-r", "This", "is", "a", "reason" };
@ -25,4 +25,4 @@ public class TestJewel {
Assert.assertEquals("This is a reason", sb.toString());
}
}
}

View File

@ -1,35 +0,0 @@
package test.java.com.graywolf336.jail;
import junit.framework.Assert;
import org.junit.Test;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import com.graywolf336.jail.command.commands.params.Jailing;
public class TestCommandParams {
@Test
public void TestJailCommand() {
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);
Assert.assertEquals("The player is not the one we provided.", "graywolf336", jail.player());
Assert.assertEquals("The time doesn't match what we gave.", "30", jail.time());
Assert.assertEquals("The jail is not the one we specified.", "den", jail.jail());
Assert.assertEquals("The cell doesn't match up.", "cell_01", jail.cell());
Assert.assertEquals("The muted is false.", true, jail.muted());
Assert.assertEquals("Jailed reason didn't match up.", "He was a very bad boy.", jail.reason());
}
@Test(expected=ParameterException.class)
public void TestFailedJailCommand() {
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);
}
}

View File

@ -1,36 +0,0 @@
package test.java.com.graywolf336.jail;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.junit.Test;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
@Parameters(separators = ":")
public class TestJCommander {
@Parameter
public List<String> parameters = new ArrayList<String>();
@Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
public Integer verbose = 1;
@Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
public String groups;
@Parameter(names = "-debug", description = "Debug mode")
public boolean debug = false;
@Test
public void testJCommander() {
String[] args = { "-log:2", "-groups:unit" };
new JCommander(this, args);
Assert.assertEquals(this.verbose.intValue(), 2);
Assert.assertEquals(this.groups.toLowerCase(), "unit");
}
}