From 500c2abd51f8660bc046a03a65c710c0f0a852c9 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Wed, 18 Dec 2013 17:26:39 -0600 Subject: [PATCH] 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. --- .../jail/command/commands/JailCommand.java | 35 ++++---- .../command/commands/JailListCommand.java | 2 +- .../command/parameters/JailParameters.java | 81 +++++++++++++++++++ .../graywolf336/jail/TestCommandParams.java | 26 ++++++ .../com/graywolf336/jail/TestJCommander.java | 4 +- 5 files changed, 125 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/graywolf336/jail/command/parameters/JailParameters.java create mode 100644 src/test/java/test/java/com/graywolf336/jail/TestCommandParams.java diff --git a/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java b/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java index 1259d3d..fb9fb38 100644 --- a/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java +++ b/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java @@ -3,9 +3,13 @@ package com.graywolf336.jail.command.commands; 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.command.Command; import com.graywolf336.jail.command.CommandInfo; +import com.graywolf336.jail.command.parameters.JailParameters; @CommandInfo( maxArgs = -1, @@ -13,39 +17,28 @@ import com.graywolf336.jail.command.CommandInfo; needsPlayer = false, pattern = "jail|j", 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 boolean execute(JailManager jm, CommandSender sender, String... args) { - String player = "", jail = "", cell = "", reason = ""; - int time = 30; - boolean muted = false; + JailParameters params = new JailParameters(); - for(String s : args) { - if(s.startsWith("p:")) { - player = s.substring(2); - }else if(s.startsWith("t:")) { - 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); - } + try { + new JCommander(params, args); + }catch(ParameterException e) { + return false; } - Player p = jm.getPlugin().getServer().getPlayer(player); + + Player p = jm.getPlugin().getServer().getPlayer(params.player()); //Player is not online 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 { //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; diff --git a/src/main/java/com/graywolf336/jail/command/commands/JailListCommand.java b/src/main/java/com/graywolf336/jail/command/commands/JailListCommand.java index c2275ec..d4cd32d 100644 --- a/src/main/java/com/graywolf336/jail/command/commands/JailListCommand.java +++ b/src/main/java/com/graywolf336/jail/command/commands/JailListCommand.java @@ -12,7 +12,7 @@ import com.graywolf336.jail.command.CommandInfo; maxArgs = 0, minimumArgs = 0, needsPlayer = false, - pattern = "jaillist|jc", + pattern = "jaillist|jl", permission = "jail.command.jaillist", usage = "/jaillist" ) diff --git a/src/main/java/com/graywolf336/jail/command/parameters/JailParameters.java b/src/main/java/com/graywolf336/jail/command/parameters/JailParameters.java new file mode 100644 index 0000000..cce6c40 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/command/parameters/JailParameters.java @@ -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 parameters = new ArrayList(); + + @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 reason = new ArrayList(); + + /** Returns the parameters. */ + public List 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; + } +} diff --git a/src/test/java/test/java/com/graywolf336/jail/TestCommandParams.java b/src/test/java/test/java/com/graywolf336/jail/TestCommandParams.java new file mode 100644 index 0000000..a110795 --- /dev/null +++ b/src/test/java/test/java/com/graywolf336/jail/TestCommandParams.java @@ -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."); + } +} diff --git a/src/test/java/test/java/com/graywolf336/jail/TestJCommander.java b/src/test/java/test/java/com/graywolf336/jail/TestJCommander.java index e36614f..50a7a1e 100644 --- a/src/test/java/test/java/com/graywolf336/jail/TestJCommander.java +++ b/src/test/java/test/java/com/graywolf336/jail/TestJCommander.java @@ -9,7 +9,9 @@ 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 parameters = new ArrayList(); @@ -25,7 +27,7 @@ public class TestJCommander { @Test public void testJCommander() { - String[] args = { "-log", "2", "-groups", "unit" }; + String[] args = { "-log:2", "-groups:unit" }; new JCommander(this, args); Assert.assertEquals(this.verbose.intValue(), 2);