Switch to using JCommander for the params/arguments system.
This system is very complex and we can make it as complex or as simple as we want. This will save us time when we want to parse commands and since it takes a while to write the code to parse the commands, this way provides a lot more options. I'm excited for this.
This commit is contained in:
parent
6158aae5b5
commit
500c2abd51
@ -3,9 +3,13 @@ package com.graywolf336.jail.command.commands;
|
|||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import com.beust.jcommander.JCommander;
|
||||||
|
import com.beust.jcommander.ParameterException;
|
||||||
|
|
||||||
import com.graywolf336.jail.JailManager;
|
import com.graywolf336.jail.JailManager;
|
||||||
import com.graywolf336.jail.command.Command;
|
import com.graywolf336.jail.command.Command;
|
||||||
import com.graywolf336.jail.command.CommandInfo;
|
import com.graywolf336.jail.command.CommandInfo;
|
||||||
|
import com.graywolf336.jail.command.parameters.JailParameters;
|
||||||
|
|
||||||
@CommandInfo(
|
@CommandInfo(
|
||||||
maxArgs = -1,
|
maxArgs = -1,
|
||||||
@ -13,39 +17,28 @@ import com.graywolf336.jail.command.CommandInfo;
|
|||||||
needsPlayer = false,
|
needsPlayer = false,
|
||||||
pattern = "jail|j",
|
pattern = "jail|j",
|
||||||
permission = "jail.command.jail",
|
permission = "jail.command.jail",
|
||||||
usage = "/jail [p:name] (t:time) (j:Jail name) (c:Cell name) (m:Muted) (r:Reason)"
|
usage = "/jail [-p name] (-t time) (-j JailName) (-c CellName) (-m Muted) (-r A reason for jailing)"
|
||||||
)
|
)
|
||||||
public class JailCommand implements Command {
|
public class JailCommand implements Command {
|
||||||
|
|
||||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||||
String player = "", jail = "", cell = "", reason = "";
|
JailParameters params = new JailParameters();
|
||||||
int time = 30;
|
|
||||||
boolean muted = false;
|
|
||||||
|
|
||||||
for(String s : args) {
|
try {
|
||||||
if(s.startsWith("p:")) {
|
new JCommander(params, args);
|
||||||
player = s.substring(2);
|
}catch(ParameterException e) {
|
||||||
}else if(s.startsWith("t:")) {
|
return false;
|
||||||
time = Integer.parseInt(s.substring(2));
|
|
||||||
}else if(s.startsWith("j:")) {
|
|
||||||
jail = s.substring(2);
|
|
||||||
}else if(s.startsWith("c:")) {
|
|
||||||
cell = s.substring(2);
|
|
||||||
}else if(s.startsWith("m:")) {
|
|
||||||
muted = Boolean.parseBoolean(s.substring(2));
|
|
||||||
}else if(s.startsWith("r:")) {
|
|
||||||
reason = s.substring(2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Player p = jm.getPlugin().getServer().getPlayer(player);
|
|
||||||
|
Player p = jm.getPlugin().getServer().getPlayer(params.player());
|
||||||
|
|
||||||
//Player is not online
|
//Player is not online
|
||||||
if(p == null) {
|
if(p == null) {
|
||||||
sender.sendMessage(player + " is offline and will be jailed for " + time + " minutes in the jail " + jail + " and will be muted: " + muted + ".");
|
sender.sendMessage(params.player() + " is offline and will be jailed for " + params.time() + " minutes in the jail " + params.jail() + "in the cell " + params.cell() + " and will be muted: " + params.muted() + ".");
|
||||||
}else {
|
}else {
|
||||||
//Player *is* online
|
//Player *is* online
|
||||||
sender.sendMessage(player + " is offline and will be jailed for " + time + " minutes in the jail " + jail + " and will be muted: " + muted + ".");
|
sender.sendMessage(params.player() + " is online and will be jailed for " + params.time() + " minutes in the jail " + params.jail() + "in the cell " + params.cell() + " and will be muted: " + params.muted() + ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -12,7 +12,7 @@ import com.graywolf336.jail.command.CommandInfo;
|
|||||||
maxArgs = 0,
|
maxArgs = 0,
|
||||||
minimumArgs = 0,
|
minimumArgs = 0,
|
||||||
needsPlayer = false,
|
needsPlayer = false,
|
||||||
pattern = "jaillist|jc",
|
pattern = "jaillist|jl",
|
||||||
permission = "jail.command.jaillist",
|
permission = "jail.command.jaillist",
|
||||||
usage = "/jaillist"
|
usage = "/jaillist"
|
||||||
)
|
)
|
||||||
|
@ -0,0 +1,81 @@
|
|||||||
|
package com.graywolf336.jail.command.parameters;
|
||||||
|
|
||||||
|
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.0
|
||||||
|
* @since 3.0.0
|
||||||
|
*/
|
||||||
|
public class JailParameters {
|
||||||
|
@Parameter
|
||||||
|
private List<String> parameters = new ArrayList<String>();
|
||||||
|
|
||||||
|
@Parameter(names = { "-p", "-player", "-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.")
|
||||||
|
private String time = "";
|
||||||
|
|
||||||
|
@Parameter(names = { "-j", "-jail", "-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.")
|
||||||
|
private String cell = "";
|
||||||
|
|
||||||
|
@Parameter(names = { "-m", "-muted" }, description = "Whether they can talk or not.", arity = 1)
|
||||||
|
private boolean muted = false;
|
||||||
|
|
||||||
|
@Parameter(names = { "-r", "-reason" }, 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the jail parameter. */
|
||||||
|
public String jail() {
|
||||||
|
return jail;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the cell parameter. */
|
||||||
|
public String cell() {
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the muted parameter. */
|
||||||
|
public boolean muted() {
|
||||||
|
return 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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package test.java.com.graywolf336.jail;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.beust.jcommander.JCommander;
|
||||||
|
import com.graywolf336.jail.command.parameters.JailParameters;
|
||||||
|
|
||||||
|
public class TestCommandParams {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestJailCommand() {
|
||||||
|
JailParameters jail = new JailParameters();
|
||||||
|
//"/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", "true", "-r", "He", "was", "a", "very", "bad", "boy." };
|
||||||
|
new JCommander(jail, params);
|
||||||
|
|
||||||
|
Assert.assertEquals("The player is not the one we provided.", jail.player(), "graywolf336");
|
||||||
|
Assert.assertEquals("The time doesn't match what we gave.", jail.time(), "30");
|
||||||
|
Assert.assertEquals("The jail is not the one we specified.", jail.jail(), "den");
|
||||||
|
Assert.assertEquals("The cell doesn't match up.", jail.cell(), "cell_01");
|
||||||
|
Assert.assertEquals("The muted is not true.", jail.muted(), true);
|
||||||
|
Assert.assertEquals("Jailed reason didn't match up.", jail.reason(), "He was a very bad boy.");
|
||||||
|
}
|
||||||
|
}
|
@ -9,7 +9,9 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.beust.jcommander.JCommander;
|
import com.beust.jcommander.JCommander;
|
||||||
import com.beust.jcommander.Parameter;
|
import com.beust.jcommander.Parameter;
|
||||||
|
import com.beust.jcommander.Parameters;
|
||||||
|
|
||||||
|
@Parameters(separators = ":")
|
||||||
public class TestJCommander {
|
public class TestJCommander {
|
||||||
@Parameter
|
@Parameter
|
||||||
public List<String> parameters = new ArrayList<String>();
|
public List<String> parameters = new ArrayList<String>();
|
||||||
@ -25,7 +27,7 @@ public class TestJCommander {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJCommander() {
|
public void testJCommander() {
|
||||||
String[] args = { "-log", "2", "-groups", "unit" };
|
String[] args = { "-log:2", "-groups:unit" };
|
||||||
new JCommander(this, args);
|
new JCommander(this, args);
|
||||||
|
|
||||||
Assert.assertEquals(this.verbose.intValue(), 2);
|
Assert.assertEquals(this.verbose.intValue(), 2);
|
||||||
|
Loading…
Reference in New Issue
Block a user