Skip to content

Commit

Permalink
Clean up ExecuteCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Mar 7, 2013
1 parent 25ba0dd commit f1af64f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 110 deletions.
Expand Up @@ -14,41 +14,30 @@

public class ExecuteCommand extends AbstractCommand {

enum ExecuteType { AS_SERVER, AS_NPC, AS_PLAYER, AS_OP }
enum Type { AS_SERVER, AS_NPC, AS_PLAYER, AS_OP }

String command = null;
ExecuteType executeType = null;
LivingEntity target = null;

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

// Parse arguments

String command = null;
Type executeType = null;

// Parse arguments
for (String arg : scriptEntry.getArguments()) {

if (aH.matchesArg("ASPLAYER, AS_PLAYER", arg)) {
executeType = ExecuteType.AS_PLAYER;
target = scriptEntry.getPlayer();
dB.echoDebug(Messages.DEBUG_SET_TYPE, arg);

} else if (aH.matchesArg("ASOPPLAYER, ASOP, AS_OP, AS_OP_PLAYER", arg)) {
executeType = ExecuteType.AS_OP;
target = scriptEntry.getPlayer();
dB.echoDebug(Messages.DEBUG_SET_TYPE, arg);

} else if (aH.matchesArg("ASNPC, AS_NPC", arg)) {
executeType = ExecuteType.AS_NPC;
target = scriptEntry.getNPC().getEntity();
dB.echoDebug(Messages.DEBUG_SET_TYPE, arg);

} else if (aH.matchesArg("ASSERVER, AS_SERVER", arg)) {
executeType = ExecuteType.AS_SERVER;
dB.echoDebug(Messages.DEBUG_SET_TYPE, arg);

} else {
command = arg;
dB.echoDebug(Messages.DEBUG_SET_COMMAND, arg);
}
if (aH.matchesArg("ASPLAYER, AS_PLAYER", arg))
executeType = Type.AS_PLAYER;

else if (aH.matchesArg("ASOPPLAYER, ASOP, AS_OP, AS_OP_PLAYER", arg))
executeType = Type.AS_OP;

else if (aH.matchesArg("ASNPC, AS_NPC", arg))
executeType = Type.AS_NPC;

else if (aH.matchesArg("ASSERVER, AS_SERVER", arg))
executeType = Type.AS_SERVER;

else command = arg;
}

if (executeType == null)
Expand All @@ -57,31 +46,42 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
if (command == null)
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "COMMAND_TEXT");

scriptEntry.addObject("command", command)
.addObject("type", executeType);

}

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

switch (executeType) {
String command = (String) scriptEntry.getObject("command");
Type type = (Type) scriptEntry.getObject("type");

// Report to dB
dB.report(getName(),
aH.debugObj("Type", type.toString())
+ aH.debugObj("Command", command));

switch (type) {

case AS_PLAYER:
((Player) target).performCommand(command);
scriptEntry.getPlayer().performCommand(command);
return;

case AS_OP:
boolean isOp = false;
if (((Player) target).isOp()) isOp = true;
if (!isOp) ((Player) target).setOp(true);
((Player) target).performCommand(command);
if (!isOp) ((Player) target).setOp(false);
if (scriptEntry.getPlayer().isOp()) isOp = true;
if (!isOp) scriptEntry.getPlayer().setOp(true);
scriptEntry.getPlayer().performCommand(command);
if (!isOp) scriptEntry.getPlayer().setOp(false);
return;

case AS_NPC:
if (target.getType() != EntityType.PLAYER)
if (scriptEntry.getNPC().getEntity().getType() != EntityType.PLAYER)
throw new CommandExecutionException("Cannot EXECUTE AS_NPC unless the NPC is Player-Type.");
((Player) target).setOp(true);
((Player) target).performCommand(command);
((Player) target).setOp(false);
((Player) scriptEntry.getNPC().getEntity()).setOp(true);
((Player) scriptEntry.getNPC().getEntity()).performCommand(command);
((Player) scriptEntry.getNPC().getEntity()).setOp(false);
return;

case AS_SERVER:
Expand Down
Expand Up @@ -207,7 +207,8 @@ else return new Element(seconds + "s")
.getAttribute(attribute.fulfill(1));
}

return new Element(as_dScriptArg()).getAttribute(attribute);
return new Element(dScriptArgValue())
.getAttribute(attribute);
}


Expand Down
Expand Up @@ -88,8 +88,8 @@ public String getAttribute(Attribute attribute) {
}

if (attribute.startsWith(".asboolean"))
return Boolean.valueOf(element).toString();

return new Element(Boolean.valueOf(element).toString())
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith(".substring")) {
int beginning_index = Integer.valueOf(attribute.getContext(1).split("\\|")[0]) - 1;
Expand Down
100 changes: 37 additions & 63 deletions src/main/java/net/aufdemrand/denizen/utilities/arguments/Item.java
Expand Up @@ -22,56 +22,15 @@ public class Item extends ItemStack implements dScriptArgument {
/////////////////


/**
* Gets a saved location based on an Id.
*
* @param id the Id key of the location
* @return the Location associated
*/
public static Item getSavedItem(String id) {
// TODO: Test to see if this is even possible. Keeping track of ACTUAL item instances may
// be more difficult than entities.
return null;
}

/**
* Checks if there is a saved item with this Id.
*
* @param id the Id to check
* @return true if it exists, false if not
*/
public static boolean isSavedItem(String id) {
// TODO: Test to see if this is even possible. Keeping track of ACTUAL item instances may
// be more difficult than entities.
return false;
}

/**
* Called on server startup or /denizen reload locations. Should probably not be called manually.
*/
public static void _recallItems() {
// TODO: Test to see if this is even possible. Keeping track of ACTUAL item instances may
// be more difficult than entities.
}

/**
* Called by Denizen internally on a server shutdown or /denizen save. Should probably
* not be called manually.
*/
public static void _saveItems() {
// TODO: Test to see if this is even possible. Keeping track of ACTUAL item instances may
// be more difficult than entities.
}

// Patterns used in valueOf...
// TODO: Make prettier.. maybe an array of Patterns isn't even necessary anymore.
// Maybe seperate them out instead?
// Seperate them out instead?
final static Pattern[] getItemPtrn = {
Pattern.compile("(?:(?:.+?:)|)(\\d+):(\\d+)"),
Pattern.compile("(?:(?:.+?:)|)(\\d+)"),
Pattern.compile("(?:(?:.+?:)|)([a-zA-Z\\x5F]+?):(\\d+)"),
Pattern.compile("(?:(?:.+?:)|)([a-zA-Z\\x5F]+)"),
Pattern.compile("(?:(?:.+?:)|)itemstack\\.(.+)", Pattern.CASE_INSENSITIVE),
Pattern.compile("(?:(?:.+?:)|)item\\.(.+)", Pattern.CASE_INSENSITIVE),
Pattern.compile("(?:(?:.+?:)|)(.+)"),
};

Expand Down Expand Up @@ -264,59 +223,74 @@ public String getAttribute(Attribute attribute) {
String id = this.id.toLowerCase();

if (attribute.startsWith(".qty"))
return String.valueOf(getAmount());
return new Element(String.valueOf(getAmount()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith(".id"))
return id;
return new Element(id)
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith(".typeid"))
return String.valueOf(getTypeId());
return new Element(String.valueOf(getTypeId()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith(".max_stack"))
return String.valueOf(getMaxStackSize());
return new Element(String.valueOf(getMaxStackSize()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith(".data"))
return String.valueOf(getData().getData());
return new Element(String.valueOf(getData().getData()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith(".durability"))
return String.valueOf(getDurability());
return new Element(String.valueOf(getDurability()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith(".material.formatted")) {

if (id.equals("air"))
return "nothing";
return new Element("nothing")
.getAttribute(attribute.fulfill(2));

if (id.equals("ice") || id.equals("dirt"))
return id;
return new Element(id)
.getAttribute(attribute.fulfill(2));

if (getAmount() > 1) {
if (id.equals("cactus"))
return "cactuses";
return new Element("cactuses")
.getAttribute(attribute.fulfill(2));
if (id.endsWith("y"))
return id.substring(0, id.length() - 1) + "ies"; // ex: lily -> lilies
return new Element(id.substring(0, id.length() - 1) + "ies")
.getAttribute(attribute.fulfill(2)); // ex: lily -> lilies
if (id.endsWith("s"))
return id; // ex: shears -> shears
return new Element(id)
.getAttribute(attribute.fulfill(2)); // ex: shears -> shears
// else
return id + "s"; // iron sword -> iron swords
return new Element(id + "s")
.getAttribute(attribute.fulfill(2)); // iron sword -> iron swords

} else {
if (id.equals("cactus")) return "a cactus";
if (id.endsWith("s")) return id;
if (id.equals("cactus")) return new Element("a cactus").getAttribute(attribute.fulfill(2));
if (id.endsWith("s")) return new Element(id).getAttribute(attribute.fulfill(2));
if (id.startsWith("a") || id.startsWith("e") || id.startsWith("i")
|| id.startsWith("o") || id.startsWith("u"))
return "an " + id; // ex: emerald -> an emerald
return new Element("an " + id)
.getAttribute(attribute.fulfill(2));// ex: emerald -> an emerald
// else
return "a " + id; // ex: diamond -> a diamond
return new Element("a " + id)
.getAttribute(attribute.fulfill(2));// ex: diamond -> a diamond
}

}

if (attribute.startsWith(".material"))
return getType().toString();
return new Element(getType().toString())
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith(".display"))
if (hasItemMeta() && getItemMeta().hasDisplayName())
return getItemMeta().getDisplayName();
return new Element(getItemMeta().getDisplayName())
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith(".enchantments")) {

Expand All @@ -328,7 +302,7 @@ public String getAttribute(Attribute attribute) {
else return new List("Empty List", "").getAttribute(attribute.fulfill(1));
}

return null;
return new Element(dScriptArgValue()).getAttribute(attribute.fulfill(1));
}

}
Expand Up @@ -101,7 +101,7 @@ public String getAttribute(Attribute attribute) {

if (attribute.startsWith(".get")) {
int index = attribute.getIntContext(1);
if (index > size()) return null;
if (index > size() || index == 0) return null;
String item = get(index - 1);
return new Element(item).getAttribute(attribute.fulfill(1));
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import net.aufdemrand.denizen.interfaces.dScriptArgument;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;

import java.util.regex.Matcher;
Expand Down Expand Up @@ -111,12 +112,10 @@ public dScriptArgument setPrefix(String prefix) {
}

@Override
public String getAttribute(String attribute) {
public String getAttribute(Attribute attribute) {

if (attribute == null) return as_dScriptArg();

// Desensitize the attribute for comparison
attribute = attribute.toLowerCase();

return null;
}
Expand Down

0 comments on commit f1af64f

Please sign in to comment.