Skip to content

Commit

Permalink
New arguments system is (mostly) working.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Jun 2, 2013
1 parent e0d71e3 commit 73216cf
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 69 deletions.
18 changes: 14 additions & 4 deletions src/main/java/net/aufdemrand/denizen/objects/aH.java
Expand Up @@ -41,7 +41,7 @@ public enum PrimitiveType { Float, Double, Integer, Boolean, String, Word}
final static Pattern wordPrimitive = Pattern.compile("\\w+");

public static class Argument {
String raw_value;
public String raw_value;
String prefix;
String value;

Expand All @@ -53,10 +53,16 @@ public Argument(String string) {
int first_colon = string.indexOf(":");
int first_space = string.indexOf(" ");

if (first_space < first_colon) value = string; else {
dB.log("Constructing Argument: " + raw_value + " " + first_colon + "," + first_space);

if ((first_space > -1 && first_space < first_colon) || first_colon == -1) value = string;
else {
prefix = string.split(":")[0];
value = string.split(":")[1];
}

dB.log("Constructed Argument: " + prefix + ":" + value);

}

public boolean matchesEnum(Enum[] values) {
Expand Down Expand Up @@ -103,8 +109,10 @@ public boolean matchesPrimitive(PrimitiveType argumentType) {

public boolean matchesArgumentType(Class<? extends dScriptArgument> clazz) {

dB.log("Calling matches: " + prefix + ":" + value + " " + clazz.toString());

try {
return (Boolean) clazz.getMethod("matches", Boolean.class).invoke(null, value);
return (Boolean) clazz.getMethod("matches", String.class).invoke(null, value);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -118,9 +126,11 @@ public Element asElement() {

public <T extends dScriptArgument> T asType(Class<? extends dScriptArgument> clazz) {

dB.log("Calling asType: " + prefix + ":" + value + " " + clazz.toString());

dScriptArgument arg = null;
try {
arg = (dScriptArgument) clazz.getMethod("valueOf", clazz)
arg = (dScriptArgument) clazz.getMethod("valueOf", String.class)
.invoke(null, value);

return (T) clazz.cast(arg).setPrefix(prefix);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -176,7 +176,7 @@ public static boolean matches(String arg) {

arg = arg.replace("e@", "");

if (ScriptRegistry.containsScript(m.group(1), EntityScriptContainer.class))
if (ScriptRegistry.containsScript(arg, EntityScriptContainer.class))
return true;

for (EntityType type : EntityType.values())
Expand Down
20 changes: 4 additions & 16 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -162,23 +162,11 @@ public static dItem valueOf(String string) {


public static boolean matches(String arg) {

final Pattern entity_by_id =
Pattern.compile("((n@|e@|p@)(.+))",
Pattern.CASE_INSENSITIVE);
Matcher m;
m = entity_by_id.matcher(arg);
if (m.matches()) return true;

arg = arg.replace("e@", "");

if (ScriptRegistry.containsScript(m.group(1), EntityScriptContainer.class))
return true;

for (EntityType type : EntityType.values())
if (type.name().equalsIgnoreCase(arg)) return true;

// TODO: Make this better. Probably creating some unnecessary
// objects by doing this :(
if (valueOf(arg) != null) return true;
return false;

}


Expand Down
Expand Up @@ -150,7 +150,7 @@ else if (split.length == 6)
}


public boolean matches(String string) {
public static boolean matches(String string) {
final Pattern location_by_saved = Pattern.compile("(l@)(.+)");
Matcher m = location_by_saved.matcher(string);
if (m.matches())
Expand Down
Expand Up @@ -39,8 +39,10 @@ public String getHelp() {
"- anchor walknear id:front_porch range:5";
}


private enum Action { ADD, REMOVE, ASSUME, WALKTO, WALKNEAR }


@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

Expand Down Expand Up @@ -72,7 +74,7 @@ else if (!scriptEntry.hasObject("location")
scriptEntry.addObject("location", arg.asType(dLocation.class).setPrefix("location"));


else throw new InvalidArgumentsException(dB.Messages.ERROR_UNKNOWN_ARGUMENT);
else dB.echoError("Unhandled argument: '" + arg.raw_value + "'");
}


Expand All @@ -81,6 +83,7 @@ else if (!scriptEntry.hasObject("location")

}


@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

Expand Down
Expand Up @@ -2,11 +2,9 @@

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.entity.EntityType;
Expand All @@ -20,71 +18,102 @@

public class DropCommand extends AbstractCommand {

public String getHelp() {
return "Drops things into the world. Must specify something to drop," +
"such as 'experience', an item, or an entity. Must also" +
"specify a location, and if more than 1, a quantity. \n" +
" \n" +
"Use to drop items, even custom item_scripts. \n" +
"- drop iron_helmet location:<npc.location> \n" +
"- drop i@butter qty:5 location:<notable.location[churn]> \n" +
"Use to reward the player with some experience. \n" +
"- drop experience qty:1000 location:<player.location> \n" +
"Use to drop entities, such as boats or minecarts. \n" +
"- drop e@boat location:<player.flag[dock_location]>";
}

enum Action { DROP_ITEM, DROP_EXP, DROP_ENTITY }

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

// Initialize necessary fields
dItem item = null;
Integer qty = null;
dLocation location = null;
Boolean exp = false;
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("action")
&& arg.matchesArgumentType(dItem.class)) {
// dItem arg
scriptEntry.addObject("action", new Element(Action.DROP_ITEM.toString()).setPrefix("action"));
scriptEntry.addObject("item", arg.asType(dItem.class).setPrefix("item")); }


else if (!scriptEntry.hasObject("action")
&& arg.matchesPrefix("experience, exp, xp"))
// Experience arg
scriptEntry.addObject("action", new Element(Action.DROP_EXP.toString()).setPrefix("action"));


// Set some defaults
if (scriptEntry.getPlayer() != null)
location = new dLocation(scriptEntry.getPlayer().getPlayerEntity().getLocation());
if (location == null && scriptEntry.getNPC() != null)
location = new dLocation(scriptEntry.getNPC().getLocation());
else if (!scriptEntry.hasObject("action")
&& arg.matchesArgumentType(dEntity.class)) {
// Entity arg
scriptEntry.addObject("action", new Element(Action.DROP_ITEM.toString()).setPrefix("action"));
scriptEntry.addObject("entity", arg.asType(dEntity.class).setPrefix("entity")); }

for (String arg : scriptEntry.getArguments()) {
if (aH.matchesItem(arg)) {
item = aH.getItemFrom(arg);

} else if (aH.matchesArg("XP, EXP", arg)) {
exp = true;
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class))
// dLocation arg
scriptEntry.addObject("location", arg.asType(dLocation.class).setPrefix("location"));

} else if (aH.matchesQuantity(arg)) {
qty = aH.getIntegerFrom(arg);

} else if (aH.matchesLocation(arg)) {
location = aH.getLocationFrom(arg);
else if (!scriptEntry.hasObject("qty")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer))
// Quantity arg
scriptEntry.addObject("qty", arg.asElement().setPrefix("qty"));

} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);

else dB.echoError("Unhandled argument: '" + arg.raw_value + "'");
}

if (item == null && !exp) throw new InvalidArgumentsException(Messages.ERROR_INVALID_ITEM);
if (location == null) throw new InvalidArgumentsException(Messages.ERROR_MISSING_LOCATION);
if (!scriptEntry.hasObject("action"))
throw new InvalidArgumentsException("Must specify something to drop!");

if (!scriptEntry.hasObject("qty")) scriptEntry.addObject("qty", Element.valueOf("1"));

// Stash objects
scriptEntry.addObject("location", location);
scriptEntry.addObject("item", item);
scriptEntry.addObject("exp", exp);
scriptEntry.addObject("qty", qty);
}


@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

// Get objects
dLocation location = (dLocation) scriptEntry.getObject("location");
Integer qty = (Integer) scriptEntry.getObject("qty");
Boolean exp = (Boolean) scriptEntry.getObject("exp");
Element qty = (Element) scriptEntry.getObject("qty");
Element action = (Element) scriptEntry.getObject("action");
dItem item = (dItem) scriptEntry.getObject("item");

// Set quantity if not specified
if (qty != null && item != null)
item.getItemStack().setAmount(qty);
else qty = 1;
dEntity entity = (dEntity) scriptEntry.getObject("entity");

// Report to dB
dB.report(getName(),
location.debug()
+ (item != null ? item.debug()
: aH.debugObj("Exp", String.valueOf(qty))));

if (exp)
((ExperienceOrb) location.getWorld().spawnEntity(location, EntityType.EXPERIENCE_ORB))
.setExperience(qty);
else
location.getWorld().dropItemNaturally(location, item.getItemStack());
action.debug() + location.debug() + qty.debug()
+ (Action.valueOf(action.asString()) == Action.DROP_ITEM ? item.debug() : "")
+ (Action.valueOf(action.asString()) == Action.DROP_ENTITY ? entity.debug() : ""));

switch (Action.valueOf(action.asString())) {
case DROP_EXP:
((ExperienceOrb) location.getWorld()
.spawnEntity(location, EntityType.EXPERIENCE_ORB))
.setExperience(qty.asInt());
break;

case DROP_ITEM:
location.getWorld()
.dropItemNaturally(location, item.getItemStack());
break;

case DROP_ENTITY:
entity.spawnAt(location);
break;
}

}

Expand Down

0 comments on commit 73216cf

Please sign in to comment.