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:
graywolf336
2013-12-18 17:26:39 -06:00
parent 6158aae5b5
commit 500c2abd51
5 changed files with 125 additions and 23 deletions

View File

@ -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;

View File

@ -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"
)