Implement an option to jail in the first open cell, -a. Closes #29

This feature adds a new option to the jail command `-a` which can be
used in place of `-c` if you don't want a specific cell. This option
will jail the player in the first open cell in the jail, thus
eliminating the need to know cell names.
This commit is contained in:
graywolf336 2014-07-27 14:37:46 -05:00
parent 57e304f7c4
commit f89de50d75
3 changed files with 68 additions and 23 deletions

View File

@ -18,6 +18,9 @@ public interface Jailing {
@Option(longName={"cell"}, shortName="c", description = "the cell")
public String getCell();
@Option(longName={"anycell"}, shortName="a", description = "decides whether the plugin will pick any open cell")
public boolean getAnyCell();
@Option(longName={"muted", "canttalk"}, shortName="m", description = "whether the prisoner is muted or not")
public boolean getMuted();
@ -27,6 +30,7 @@ public interface Jailing {
public boolean isTime();
public boolean isJail();
public boolean isCell();
public boolean isAnyCell();
public boolean isMuted();
public boolean isReason();
}

View File

@ -28,7 +28,7 @@ import com.lexicalscope.jewel.cli.CliFactory;
needsPlayer = false,
pattern = "jail|j",
permission = "jail.command.jail",
usage = "/jail [name] (-t time) (-j JailName) (-c CellName) (-m Muted) (-r A reason for jailing)"
usage = "/jail [name] (-t time) (-j JailName) (-c CellName) (-a AnyCell) (-m Muted) (-r A reason for jailing)"
)
public class JailCommand implements Command {
@ -83,7 +83,7 @@ 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.getTime() == null) {
if(!params.isTime()) {
time = Util.getTime(jm.getPlugin().getConfig().getString(Settings.DEFAULTTIME.getPath(), "30m"));
}else if(params.getTime() == String.valueOf(-1)) {
time = -1L;
@ -100,44 +100,65 @@ public class JailCommand implements Command {
//the sender but otherwise if it isn't nearest then let's set it to the default jail
//which is defined in the config.
String jailName = "";
if(params.getJail() == null) {
if(!params.isJail()) {
String dJail = jm.getPlugin().getConfig().getString(Settings.DEFAULTJAIL.getPath());
if(dJail.equalsIgnoreCase("nearest")) {
jailName = jm.getNearestJail(sender).getName();
}else {
jailName = jm.getPlugin().getConfig().getString(Settings.DEFAULTJAIL.getPath());
jailName = dJail;
}
}else if(jm.getJail(params.getJail()) == null) {
}else if(!jm.isValidJail(params.getJail())) {
sender.sendMessage(Lang.NOJAIL.get(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.getCell() != null) {
if(jm.getJail(params.getJail()).getCell(params.getCell()) == null) {
//There is no cell by that name
sender.sendMessage(Lang.NOCELL.get(new String[] { params.getCell(), params.getJail() }));
//Get the jail instance from the name of jail in the params.
Jail j = jm.getJail(jailName);
if(!j.isEnabled()) {
sender.sendMessage(Lang.WORLDUNLOADED.get(j.getName()));
return true;
}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(Lang.CELLNOTEMPTY.get(params.getCell()));
Cell suggestedCell = jm.getJail(params.getJail()).getFirstEmptyCell();
if(suggestedCell != null) {
sender.sendMessage(Lang.SUGGESTEDCELL.get(new String[] { params.getJail(), suggestedCell.getName() }));
}else {
sender.sendMessage(Lang.NOEMPTYCELLS.get(params.getJail()));
}
Cell c = null;
//Check if the cell is defined
if(params.isCell()) {
//Check if it is a valid cell
if(!jm.getJail(jailName).isValidCell(params.getCell())) {
//There is no cell by that name
sender.sendMessage(Lang.NOCELL.get(new String[] { params.getCell(), jailName }));
return true;
}else if(jm.getJail(jailName).getCell(params.getCell()).hasPrisoner()) {
//If the cell has a prisoner, don't allow jailing them to that particular cell but suggest another one
sender.sendMessage(Lang.CELLNOTEMPTY.get(params.getCell()));
Cell suggestedCell = jm.getJail(jailName).getFirstEmptyCell();
if(suggestedCell != null) {
sender.sendMessage(Lang.SUGGESTEDCELL.get(new String[] { jailName, suggestedCell.getName() }));
}else {
sender.sendMessage(Lang.NOEMPTYCELLS.get(jailName));
}
return true;
}else {
c = jm.getJail(jailName).getCell(params.getCell());
}
}
//If they want just any open cell, then let's find the first empty one
if(params.isAnyCell()) {
c = jm.getJail(jailName).getFirstEmptyCell();
if(c == null) {
//If there wasn't an empty cell, then tell them so.
sender.sendMessage(Lang.NOEMPTYCELLS.get(jailName));
return true;
}
}
//If the jailer gave no reason, then let's get the default reason
String reason = "";
if(params.getReason() == null) {
if(!params.isReason()) {
reason = Lang.DEFAULTJAILEDREASON.get();
}else {
StringBuilder sb = new StringBuilder();
@ -172,14 +193,6 @@ public class JailCommand implements Command {
uuid = p.getUniqueId().toString();
}
//Get the jail instance from the name of jail in the params.
Jail j = jm.getJail(jailName);
if(!j.isEnabled()) {
sender.sendMessage(Lang.WORLDUNLOADED.get(j.getName()));
return true;
}
Cell c = j.getCell(params.getCell());
Prisoner pris = new Prisoner(uuid, params.getPlayer(), muted, time, sender.getName(), reason);
//call the event

View File

@ -124,8 +124,30 @@ public class TestJailCommandInfo {
Jailing j = CliFactory.parseArguments(Jailing.class, args);
assertEquals("graywolf336", j.getPlayer());
assertTrue("The cell wasn't provided.", j.isCell());
assertEquals("testing", j.getCell());
assertTrue("A reason wasn't provided.", j.isReason());
StringBuilder sb = new StringBuilder();
for(String s : j.getReason()) {
sb.append(s).append(' ');
}
sb.deleteCharAt(sb.length() - 1);
assertEquals("This is a reason", sb.toString());
}
@Test
public void testJailWithAnyCellJewel() {
String[] args = { "--player", "graywolf336", "-a", "-r", "This", "is", "a", "reason" };
Jailing j = CliFactory.parseArguments(Jailing.class, args);
assertEquals("graywolf336", j.getPlayer());
assertTrue("The any cell option wasn't provided.", j.isAnyCell());
assertTrue("The any cell is false when it should be true.", j.getAnyCell());
assertTrue("A reason wasn't provided.", j.isReason());
StringBuilder sb = new StringBuilder();
for(String s : j.getReason()) {
sb.append(s).append(' ');
@ -141,8 +163,11 @@ public class TestJailCommandInfo {
String[] args = { "-p", "graywolf336", "-j", "hardcore", "-c", "cell_n01" };
Transfer t = CliFactory.parseArguments(Transfer.class, args);
assertTrue("The player wasn't provided.", t.isPlayer());
assertEquals("The player parsed is not what we expected.", "graywolf336", t.getPlayer());
assertTrue("The jail wasn't provided.", t.isJail());
assertEquals("The jail parsed is not what we expected.", "hardcore", t.getJail());
assertTrue("The cell wasn't provided.", t.isCell());
assertEquals("The cell parsed is not what we expected.", "cell_n01", t.getCell());
}
@ -151,8 +176,11 @@ public class TestJailCommandInfo {
String[] args = { "-p", "graywolf336", "-j", "hardcore" };
Transfer t = CliFactory.parseArguments(Transfer.class, args);
assertTrue("The player wasn't provided.", t.isPlayer());
assertEquals("The player parsed is not what we expected.", "graywolf336", t.getPlayer());
assertTrue("The jail wasn't provided.", t.isJail());
assertEquals("The jail parsed is not what we expected.", "hardcore", t.getJail());
assertFalse("The cell was provided?", t.isCell());
assertNull("The cell is not null?", t.getCell());
}
}