mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-02-16 20:31:30 +01:00
Fixed up new command features + test
This commit is contained in:
parent
6c47757453
commit
0e3f29c6f8
@ -19,12 +19,11 @@
|
||||
package net.citizensnpcs.command;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class CommandContext {
|
||||
@ -39,29 +38,46 @@ public class CommandContext {
|
||||
public CommandContext(String[] args) {
|
||||
int i = 1;
|
||||
for (; i < args.length; i++) {
|
||||
// initial pass for quotes
|
||||
args[i] = args[i].trim();
|
||||
if (args[i].length() == 0) {
|
||||
// Ignore this
|
||||
continue;
|
||||
} else if (args[i].charAt(0) == '\'' || args[i].charAt(0) == '"') {
|
||||
char quote = args[i].charAt(0);
|
||||
String quoted = args[i].substring(1); // remove initial quote
|
||||
for (int inner = i + 1; inner < args.length; inner++) {
|
||||
if (args[inner].isEmpty())
|
||||
continue;
|
||||
String test = args[inner].trim();
|
||||
args[i] += " " + test;
|
||||
args[inner] = "";
|
||||
quoted += " " + test;
|
||||
if (test.charAt(test.length() - 1) == quote) {
|
||||
args[i] = quoted.substring(0, quoted.length() - 1);
|
||||
for (int j = i + 1; j != inner; ++j)
|
||||
args[j] = "";
|
||||
// remove ending quote
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (i + 1 < args.length && args[i].length() > 2 && args[i].matches("^--[a-zA-Z]+$")) {
|
||||
int inner = i;
|
||||
while (args[inner++].isEmpty()) {
|
||||
if (inner == args.length) {
|
||||
}
|
||||
}
|
||||
for (i = 1; i < args.length; ++i) {
|
||||
// second pass for flags
|
||||
if (args[i].length() == 0)
|
||||
continue;
|
||||
if (i + 1 < args.length && args[i].length() > 2 && args[i].matches("^--[a-zA-Z]+$")) {
|
||||
int inner = i + 1;
|
||||
while (args[inner].length() == 0) {
|
||||
// later args may have been quoted
|
||||
++inner;
|
||||
if (inner >= args.length) {
|
||||
inner = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (inner != -1) {
|
||||
System.out.println(args[inner]);
|
||||
valueFlags.put(args[i].replaceFirst("--", ""), args[inner]);
|
||||
args[i] = "";
|
||||
args[inner] = "";
|
||||
@ -72,8 +88,13 @@ public class CommandContext {
|
||||
args[i] = "";
|
||||
}
|
||||
}
|
||||
this.args = Iterables.toArray(Splitter.on(" ").omitEmptyStrings().split(Joiner.on(" ").skipNulls().join(args)),
|
||||
String.class);
|
||||
List<String> copied = Lists.newArrayList();
|
||||
for (String arg : args) {
|
||||
if (arg == null || arg.isEmpty())
|
||||
continue;
|
||||
copied.add(arg);
|
||||
}
|
||||
this.args = copied.toArray(new String[copied.size()]);
|
||||
}
|
||||
|
||||
public int argsLength() {
|
||||
@ -136,6 +157,10 @@ public class CommandContext {
|
||||
return flags.contains(ch);
|
||||
}
|
||||
|
||||
public boolean hasValueFlag(String ch) {
|
||||
return valueFlags.containsKey(ch);
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return args.length;
|
||||
}
|
||||
@ -143,4 +168,47 @@ public class CommandContext {
|
||||
public boolean matches(String command) {
|
||||
return args[0].equalsIgnoreCase(command);
|
||||
}
|
||||
|
||||
public Map<String, String> getValueFlags() {
|
||||
return valueFlags;
|
||||
}
|
||||
|
||||
public String getFlag(String ch) {
|
||||
return valueFlags.get(ch);
|
||||
}
|
||||
|
||||
public String getFlag(String ch, String def) {
|
||||
final String value = valueFlags.get(ch);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public int getFlagInteger(String ch) throws NumberFormatException {
|
||||
return Integer.parseInt(valueFlags.get(ch));
|
||||
}
|
||||
|
||||
public int getFlagInteger(String ch, int def) throws NumberFormatException {
|
||||
final String value = valueFlags.get(ch);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
|
||||
public double getFlagDouble(String ch) throws NumberFormatException {
|
||||
return Double.parseDouble(valueFlags.get(ch));
|
||||
}
|
||||
|
||||
public double getFlagDouble(String ch, double def) throws NumberFormatException {
|
||||
final String value = valueFlags.get(ch);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
return Double.parseDouble(value);
|
||||
}
|
||||
}
|
@ -8,7 +8,6 @@ import net.citizensnpcs.api.npc.trait.trait.Spawned;
|
||||
import net.citizensnpcs.npc.ai.CitizensAI;
|
||||
import net.citizensnpcs.trait.Inventory;
|
||||
import net.citizensnpcs.util.Messaging;
|
||||
|
||||
import net.minecraft.server.EntityLiving;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
@ -85,6 +84,7 @@ public abstract class CitizensNPC extends AbstractNPC {
|
||||
|
||||
mcEntity = createHandle(loc);
|
||||
mcEntity.world.addEntity(mcEntity);
|
||||
mcEntity.world.players.remove(mcEntity);
|
||||
|
||||
// Set the location
|
||||
addTrait(new SpawnLocation(loc));
|
||||
|
@ -26,10 +26,6 @@ public class CitizensHumanNPC extends CitizensNPC {
|
||||
super.update();
|
||||
if (mcEntity == null)
|
||||
return;
|
||||
if (mcEntity.noDamageTicks > 0)
|
||||
mcEntity.noDamageTicks--;
|
||||
if (mcEntity.attackTicks > 0)
|
||||
mcEntity.attackTicks--;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
58
src/net/citizensnpcs/test/CommandContextTest.java
Normal file
58
src/net/citizensnpcs/test/CommandContextTest.java
Normal file
@ -0,0 +1,58 @@
|
||||
package net.citizensnpcs.test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import net.citizensnpcs.command.CommandContext;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CommandContextTest {
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testIllegalInteger() {
|
||||
getContext("notInt").getInteger(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParsing() {
|
||||
assertTrue(0.0 == getContext("0").getDouble(0));
|
||||
assertTrue(0 == getContext("0").getInteger(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoining() {
|
||||
assertTrue(getContext("join strings").getJoinedStrings(0).equals("join strings"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValueFlags() {
|
||||
assertTrue(getContext("--test values").getFlag("test").equals("values"));
|
||||
assertTrue(getContext("--t 0").getFlagInteger("t") == 0);
|
||||
assertTrue(getContext("--test 'extended quotes' afterwards").getFlag("test").equals("extended quotes"));
|
||||
assertFalse(getContext("--t").hasFlag('t'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlags() {
|
||||
assertTrue(getContext("-f").getFlags().contains('f'));
|
||||
Set<Character> multi = getContext("-f -mm test -ghl").getFlags();
|
||||
List<Character> shouldContain = Arrays.asList('f', 'm', 'g', 'h', 'l');
|
||||
assertTrue(multi.containsAll(shouldContain));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuotes() {
|
||||
assertTrue(getContext("'this is a quote'").getString(0).equals("this is a quote"));
|
||||
assertTrue(getContext("'this is unclosed\"").getString(0).equals("'this"));
|
||||
assertTrue(getContext("\"test double quotes\"").getString(0).equals("test double quotes"));
|
||||
assertTrue(getContext("'this is a quote'").getString(0).equals("this is a quote"));
|
||||
}
|
||||
|
||||
private static CommandContext getContext(String cmd) {
|
||||
return new CommandContext("dummy " + cmd);
|
||||
}
|
||||
}
|
@ -9,19 +9,22 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Messaging {
|
||||
import com.google.common.base.Joiner;
|
||||
|
||||
public static void debug(Object msg) {
|
||||
public class Messaging {
|
||||
private static final Joiner SPACE = Joiner.on(" ");
|
||||
|
||||
public static void debug(Object... msg) {
|
||||
if (Setting.DEBUG_MODE.asBoolean())
|
||||
log(msg);
|
||||
}
|
||||
|
||||
public static void log(Level level, Object msg) {
|
||||
Bukkit.getLogger().log(level, "[Citizens] " + msg);
|
||||
public static void log(Level level, Object... msg) {
|
||||
Bukkit.getLogger().log(level, "[Citizens] " + SPACE.join(msg));
|
||||
}
|
||||
|
||||
public static void log(Object msg) {
|
||||
log(Level.INFO, msg);
|
||||
public static void log(Object... msg) {
|
||||
log(Level.INFO, SPACE.join(msg));
|
||||
}
|
||||
|
||||
public static void send(Player player, Object msg) {
|
||||
|
Loading…
Reference in New Issue
Block a user