Skip to content

Commit

Permalink
Allow definitions to be used as command name. ie: - %command_name% %c…
Browse files Browse the repository at this point in the history
…ommand_arg%
  • Loading branch information
aufdemrand committed Jul 11, 2013
1 parent 85754f0 commit 85dc5dd
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 83 deletions.
173 changes: 94 additions & 79 deletions src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java
Expand Up @@ -11,14 +11,14 @@

/**
* ScriptEntry contain information about a single entry from a dScript.
*
*
* @author Jeremy Schroeder
*
*/
public class ScriptEntry {

// The name of the command that will be executed
private String command;
private String command;

// The queuetime and allowed-run-time can dictate whether it's okay
// for this command to run in the queue.
Expand All @@ -27,19 +27,19 @@ public class ScriptEntry {
private long runTime;
private long holdTime;

private boolean instant = false;
private boolean instant = false;
private boolean waitfor = false;
private boolean done = false;

private dPlayer player = null;
private dNPC npc = null;
private dNPC npc = null;

private dScript script = null;

private ScriptQueue queue = null;
private List<String> args = null;
private ScriptQueue queue = null;
private List<String> args = null;

private Map<String, Object> objects = new HashMap<String, Object>();
private Map<String, Object> objects = new HashMap<String, Object>();

public ScriptEntry(String command, String[] arguments, ScriptContainer script) throws ScriptEntryCreationException {

Expand All @@ -51,22 +51,22 @@ public ScriptEntry(String command, String[] arguments, ScriptContainer script) t
this.script = script.getAsScriptArg();

// Internal, never null. runTime/holdTime can be adjusted mid-execution
this.creationTime = System.currentTimeMillis();
this.creationTime = System.currentTimeMillis();
this.queueTime = creationTime;
this.runTime = creationTime;
this.runTime = creationTime;
this.holdTime = creationTime;

// Check if this is an 'instant' or 'waitfor' command.
if (command.startsWith("^")) {
instant = true;
this.command = command.substring(1);
} else if (command.startsWith("~")) {
if (command.startsWith("^")) {
instant = true;
this.command = command.substring(1);
} else if (command.startsWith("~")) {
waitfor = true;
this.command = command.substring(1);
}

this.args = new ArrayList<String>();
if (arguments != null)
this.args = new ArrayList<String>();
if (arguments != null)
this.args = Arrays.asList(arguments);

// Check for replaceable tags.
Expand All @@ -77,37 +77,37 @@ public ScriptEntry(String command, String[] arguments, ScriptContainer script) t
}
}

}
}

public boolean has_tags = false;

public ScriptEntry addObject(String key, Object object) {
public ScriptEntry addObject(String key, Object object) {
if (object == null) return this;
if (object instanceof dObject)
if (object instanceof dObject)
((dObject) object).setPrefix(key);
objects.put(key.toUpperCase(), object);
return this;
}
return this;
}

public long getRunTime() {
return runTime;
}
public long getRunTime() {
return runTime;
}

public long getHoldTime() {
return holdTime;
}

public List<String> getArguments() {
return args;
}
public List<String> getArguments() {
return args;
}

public String getCommandName() {
return command;
}
public String getCommandName() {
return command;
}

public dNPC getNPC() {
return npc;
}
public dNPC getNPC() {
return npc;
}

public void setFinished(boolean finished) {
done = finished;
Expand All @@ -118,37 +118,49 @@ public dPlayer getPlayer() {
}

public Map<String, Object> getObjects() {
return objects;
}

public Object getObject(String key) {
try {
return objects.get(key.toUpperCase());
} catch (Exception e) { return null; }
}
return objects;
}

public Object getObject(String key) {
try {
return objects.get(key.toUpperCase());
} catch (Exception e) { return null; }
}

public dObject getdObject(String key) {
try {
return (dObject) objects.get(key.toUpperCase());
} catch (Exception e) { return null; }
}

public Element getElement(String key) {
try {
return (Element) objects.get(key.toUpperCase());
} catch (Exception e) { return null; }
}

public boolean hasObject(String key) {
if (objects.containsKey(key.toUpperCase())
&& objects.get(key.toUpperCase()) != null)
return true;
return true;
else return false;
}

public dScript getScript() {
public dScript getScript() {
return script;
}
}

public ScriptQueue getResidingQueue() {
return queue;
}
public ScriptQueue getResidingQueue() {
return queue;
}

public Long getQueueTime() {
return queueTime;
}
public Long getQueueTime() {
return queueTime;
}

public boolean isInstant() {
return instant;
}
public boolean isInstant() {
return instant;
}

public boolean shouldWaitFor() {
return waitfor;
Expand All @@ -158,39 +170,39 @@ public boolean isDone() {
return done;
}

public ScriptEntry setRunTime(Long newTime) {
runTime = newTime;
return this;
}

public ScriptEntry setArguments(List<String> arguments) {
args = arguments;
return this;
}
public ScriptEntry setRunTime(Long newTime) {
runTime = newTime;
return this;
}

public ScriptEntry setInstant(boolean instant) {
this.instant = instant;
return this;
}
public ScriptEntry setArguments(List<String> arguments) {
args = arguments;
return this;
}

public ScriptEntry setInstant(boolean instant) {
this.instant = instant;
return this;
}

public ScriptEntry setPlayer(dPlayer player) {
this.player = player;
return this;
}
public ScriptEntry setNPC(dNPC dNPC) {
this.npc = dNPC;
return this;
}
public ScriptEntry setScript(String scriptName) {
this.script = dScript.valueOf(scriptName);
return this;
}

public void setSendingQueue(ScriptQueue scriptQueue) {
queue = scriptQueue;
}

public ScriptEntry setNPC(dNPC dNPC) {
this.npc = dNPC;
return this;
}

public ScriptEntry setScript(String scriptName) {
this.script = dScript.valueOf(scriptName);
return this;
}

public void setSendingQueue(ScriptQueue scriptQueue) {
queue = scriptQueue;
}

// Keep track of objects which were added by mass
// so that IF can inject them into new entries.
Expand All @@ -202,4 +214,7 @@ public ScriptEntry trackObject(String key) {
return this;
}

public void setCommandName(String commandName) {
this.command = commandName;
}
}
Expand Up @@ -33,6 +33,20 @@ public CommandExecuter(Denizen denizen) {
*/

public boolean execute(ScriptEntry scriptEntry) {

Matcher m = definition_pattern.matcher(scriptEntry.getCommandName());
StringBuffer sb = new StringBuffer();
while (m.find()) {
if (scriptEntry.getResidingQueue().context
.containsKey(m.group(1).toLowerCase()))
m.appendReplacement(sb,
scriptEntry.getResidingQueue().context.get(m.group(1)));

else m.appendReplacement(sb, "null");
}
m.appendTail(sb);
scriptEntry.setCommandName(sb.toString());

if (plugin.getCommandRegistry().get(scriptEntry.getCommandName()) == null) {
dB.echoDebug(DebugElement.Header, "Executing command: " + scriptEntry.getCommandName());
dB.echoError(scriptEntry.getCommandName() + " is an invalid dCommand! Are you sure it loaded?");
Expand Down Expand Up @@ -74,8 +88,8 @@ public boolean execute(ScriptEntry scriptEntry) {

for (String arg : scriptEntry.getArguments()) {

Matcher m = definition_pattern.matcher(arg);
StringBuffer sb = new StringBuffer();
m = definition_pattern.matcher(arg);
sb = new StringBuffer();
while (m.find()) {
if (scriptEntry.getResidingQueue().context
.containsKey(m.group(1).toLowerCase()))
Expand All @@ -87,8 +101,7 @@ public boolean execute(ScriptEntry scriptEntry) {
m.appendTail(sb);
arg = sb.toString();




// Fill player/off-line player
if (aH.matchesValueArg("player", arg, aH.ArgumentType.String)) {
arg = TagManager.tag(scriptEntry.getPlayer(), scriptEntry.getNPC(), arg, false);
Expand Down

0 comments on commit 85dc5dd

Please sign in to comment.