Skip to content

Commit

Permalink
Only parse tags if necessary. Only debug tag replacement if necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Apr 8, 2013
1 parent ec1bb0d commit 9347a1e
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 120 deletions.
14 changes: 13 additions & 1 deletion src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java
Expand Up @@ -58,7 +58,7 @@ public ScriptEntry(String command, String[] arguments, ScriptContainer script) t
this.runTime = creationTime;
this.holdTime = creationTime;

// Check if this is an 'instant' command.
// Check if this is an 'instant' or 'waitfor' command.
if (command.startsWith("^")) {
instant = true;
this.command = command.substring(1);
Expand All @@ -70,8 +70,20 @@ public ScriptEntry(String command, String[] arguments, ScriptContainer script) t
this.args = new ArrayList<String>();
if (arguments != null)
this.args = Arrays.asList(arguments);

// Check for replaceable tags.
for (String arg : args) {
if (arg.indexOf("<") >= 0
&& arg.indexOf(">") >= 0) {
has_tags = true;
break;
}
}

}

public boolean has_tags = false;

public ScriptEntry addObject(String key, Object object) {
if (object == null) return this;
objects.put(key.toUpperCase(), object);
Expand Down
Expand Up @@ -19,39 +19,41 @@

public class CommandExecuter {

private Denizen plugin;
private Denizen plugin;

public CommandExecuter(Denizen denizen) {
plugin = denizen;
}
public CommandExecuter(Denizen denizen) {
plugin = denizen;
}

/*
* Executes a command defined in scriptEntry
*/

public boolean execute(ScriptEntry scriptEntry) {
if (plugin.getCommandRegistry().get(scriptEntry.getCommandName()) == null) {
dB.echoDebug(DebugElement.Header, "Executing command: " + scriptEntry.getCommandName());
dB.echoError(scriptEntry.getCommandName() + " is an invalid dScript command! Are you sure the command loaded?");
dB.echoDebug(DebugElement.Footer);
return false;
}
public boolean execute(ScriptEntry scriptEntry) {
if (plugin.getCommandRegistry().get(scriptEntry.getCommandName()) == null) {
dB.echoDebug(DebugElement.Header, "Executing command: " + scriptEntry.getCommandName());
dB.echoError(scriptEntry.getCommandName() + " is an invalid dScript command! Are you sure the command loaded?");
dB.echoDebug(DebugElement.Footer);
return false;
}

// Get the command instance ready for the execution of the scriptEntry
AbstractCommand command = plugin.getCommandRegistry().get(scriptEntry.getCommandName());
AbstractCommand command = plugin.getCommandRegistry().get(scriptEntry.getCommandName());

// Debugger information
if (scriptEntry.getPlayer() != null)
// Debugger information
if (scriptEntry.getPlayer() != null)
dB.echoDebug(DebugElement.Header, "Executing command: " + scriptEntry.getCommandName() + "/" + scriptEntry.getPlayer().getName());
else dB.echoDebug(DebugElement.Header, "Executing command: " + scriptEntry.getCommandName() + (scriptEntry.getNPC() != null ? "/" + scriptEntry.getNPC().getName() : ""));
else dB.echoDebug(DebugElement.Header, "Executing command: " + scriptEntry.getCommandName() + (scriptEntry.getNPC() != null ? "/" + scriptEntry.getNPC().getName() : ""));

// Don't execute() if problems arise in parseArgs()
boolean keepGoing = true;

try {
// Don't execute() if problems arise in parseArgs()
boolean keepGoing = true;

// Throw exception if arguments are required for this command, but not supplied.
if (command.getOptions().REQUIRED_ARGS > scriptEntry.getArguments().size()) throw new InvalidArgumentsException("");
try {

// Throw exception if arguments are required for this command, but not supplied.
if (command.getOptions().REQUIRED_ARGS > scriptEntry.getArguments().size()) throw new InvalidArgumentsException("");

if (scriptEntry.has_tags) {

/* If using NPCID:# or PLAYER:Name arguments, these need to be changed out immediately because...
* 1) Denizen/Player flags need the desired NPC/PLAYER before parseArgs's getFilledArguments() so that
Expand All @@ -61,105 +63,105 @@ public boolean execute(ScriptEntry scriptEntry) {
* here, instead of requiring each command to take care of the argument.
*/

scriptEntry.setArguments(plugin.tagManager().fillArguments(scriptEntry.getArguments(), scriptEntry, true)); // Replace tags

List<String> newArgs = new ArrayList<String>();

for (String arg : scriptEntry.getArguments()) {
String[] split = arg.split(":");

// Fill player/off-line player
if (aH.matchesValueArg("PLAYER", arg, aH.ArgumentType.String)) {
boolean foundNewPlayer = false;
dB.echoDebug("...replacing the linked Player.");
for (Player playa : Bukkit.getServer().getOnlinePlayers())
if (playa.getName().equalsIgnoreCase(split[1])) {
foundNewPlayer = true;
scriptEntry.setPlayer(playa);
}
if (foundNewPlayer) dB.echoDebug("...player set to '%s'.", split[1]);
else {
dB.echoDebug("This player is not online! Searching for offline player...");
for (OfflinePlayer playa : Bukkit.getServer().getOfflinePlayers())
if (playa.getName().equalsIgnoreCase(split[1])) {
scriptEntry.setPlayer(null);
scriptEntry.setOfflinePlayer(playa);
foundNewPlayer = true;
}
}
if (foundNewPlayer) {
dB.echoDebug("Found an offline player.. linking.");
}
else { dB.echoError("Could not find a valid player!"); scriptEntry.setPlayer(null); }
scriptEntry.setArguments(plugin.tagManager().fillArguments(scriptEntry.getArguments(), scriptEntry, true)); // Replace tags

List<String> newArgs = new ArrayList<String>();

for (String arg : scriptEntry.getArguments()) {
String[] split = arg.split(":");

// Fill player/off-line player
if (aH.matchesValueArg("PLAYER", arg, aH.ArgumentType.String)) {
boolean foundNewPlayer = false;
dB.echoDebug("...replacing the linked Player.");
for (Player playa : Bukkit.getServer().getOnlinePlayers())
if (playa.getName().equalsIgnoreCase(split[1])) {
foundNewPlayer = true;
scriptEntry.setPlayer(playa);
}
if (foundNewPlayer) dB.echoDebug("...player set to '%s'.", split[1]);
else {
dB.echoDebug("This player is not online! Searching for offline player...");
for (OfflinePlayer playa : Bukkit.getServer().getOfflinePlayers())
if (playa.getName().equalsIgnoreCase(split[1])) {
scriptEntry.setPlayer(null);
scriptEntry.setOfflinePlayer(playa);
foundNewPlayer = true;
}
}
if (foundNewPlayer) {
dB.echoDebug("Found an offline player.. linking.");
}
else { dB.echoError("Could not find a valid player!"); scriptEntry.setPlayer(null); }
}

// Fill Denizen with NPCID
else if (aH.matchesValueArg("NPCID", arg, aH.ArgumentType.String)) {
dB.echoDebug("...replacing the linked NPCID.");
try {
if (CitizensAPI.getNPCRegistry().getById(Integer.valueOf(split[1])) != null)
scriptEntry.setNPC(plugin.getNPCRegistry().getDenizen(CitizensAPI.getNPCRegistry().getById(Integer.valueOf(split[1]))));
dB.echoDebug("...NPC set to '%s'.", split[1]);
} catch (Exception e) {
dB.echoError("NPCID specified could not be matched to an NPC!");
scriptEntry.setNPC(null);
}
}
else {
newArgs.add(arg);
}
}
// Add the arguments back to the scriptEntry.
scriptEntry.setArguments(newArgs);
// Now process non-instant tags.
scriptEntry.setArguments(plugin.tagManager().fillArguments(scriptEntry.getArguments(), scriptEntry, false));

// dBug the filled arguments
dB.echoDebug(ChatColor.AQUA + "+> " + ChatColor.DARK_GRAY + "Filled tags: " + scriptEntry.getArguments().toString());
}

// Parse the rest of the arguments for execution.
command.parseArgs(scriptEntry);

} catch (InvalidArgumentsException e) {
keepGoing = false;
// Give usage hint if InvalidArgumentsException was called.
dB.echoError("Woah! Invalid arguments were specified!");
dB.echoDebug(ChatColor.YELLOW + "+> MESSAGE follows: " + ChatColor.WHITE + "'" + e.getMessage() + "'");
dB.echoDebug("Usage: " + command.getUsageHint());
dB.echoDebug(DebugElement.Footer);

} catch (Exception e) {
keepGoing = false;
dB.echoError("Woah! An exception has been called with this command!");
if (!dB.showStackTraces)
dB.echoError("Enable '/denizen stacktrace' for the nitty-gritty.");
else e.printStackTrace();
dB.echoDebug(DebugElement.Footer);

} finally {
if (keepGoing)
try {
// Fire event for last minute cancellation/alterations
ScriptEntryExecuteEvent event = new ScriptEntryExecuteEvent(scriptEntry);
Bukkit.getServer().getPluginManager().callEvent(event);

// If event is altered, update the scriptEntry.
if (event.isAltered()) scriptEntry = event.getScriptEntry();

// Run the execute method in the command
if (!event.isCancelled()) command.execute(scriptEntry);

else dB.echoDebug("ScriptEntry has been cancelled.");
} catch (Exception e) {
dB.echoError("Woah!! An exception has been called with this command!");
if (!dB.showStackTraces)
dB.echoError("Enable '/denizen stacktrace' for the nitty-gritty.");
else e.printStackTrace();
}
}

// Fill Denizen with NPCID
else if (aH.matchesValueArg("NPCID", arg, aH.ArgumentType.String)) {
dB.echoDebug("...replacing the linked NPCID.");
try {
if (CitizensAPI.getNPCRegistry().getById(Integer.valueOf(split[1])) != null)
scriptEntry.setNPC(plugin.getNPCRegistry().getDenizen(CitizensAPI.getNPCRegistry().getById(Integer.valueOf(split[1]))));
dB.echoDebug("...NPC set to '%s'.", split[1]);
} catch (Exception e) {
dB.echoError("NPCID specified could not be matched to an NPC!");
scriptEntry.setNPC(null);
}
}
else {
newArgs.add(arg);
}
}

// Add the arguments back to the scriptEntry.
scriptEntry.setArguments(newArgs);

scriptEntry.setArguments(plugin.tagManager().fillArguments(scriptEntry.getArguments(), scriptEntry, false));

// dBug the filled arguments
dB.echoDebug(ChatColor.AQUA + "+> " + ChatColor.DARK_GRAY + "Filled tags: " + scriptEntry.getArguments().toString());

// Parse the rest of the arguments for execution.
command.parseArgs(scriptEntry);
} catch (InvalidArgumentsException e) {
keepGoing = false;
// Give usage hint if InvalidArgumentsException was called.
dB.echoError("Woah! Invalid arguments were specified!");
dB.echoDebug(ChatColor.YELLOW + "+> MESSAGE follows: " + ChatColor.WHITE + "'" + e.getMessage() + "'");
dB.echoDebug("Usage: " + command.getUsageHint());
dB.echoDebug(DebugElement.Footer);
} catch (Exception e) {
keepGoing = false;
dB.echoError("Woah! An exception has been called with this command!");
if (!dB.showStackTraces)
dB.echoError("Enable '/denizen stacktrace' for the nitty-gritty.");
else e.printStackTrace();
dB.echoDebug(DebugElement.Footer);

} finally {

if (keepGoing)
try {
// Fire event for last minute cancellation/alterations
ScriptEntryExecuteEvent event = new ScriptEntryExecuteEvent(scriptEntry);
Bukkit.getServer().getPluginManager().callEvent(event);

// If event is altered, update the scriptEntry.
if (event.isAltered()) scriptEntry = event.getScriptEntry();

// Run the execute method in the command
if (!event.isCancelled()) command.execute(scriptEntry);

else dB.echoDebug("ScriptEntry has been cancelled.");
} catch (Exception e) {
dB.echoError("Woah!! An exception has been called with this command!");
if (!dB.showStackTraces)
dB.echoError("Enable '/denizen stacktrace' for the nitty-gritty.");
else e.printStackTrace();
}
}

return true;

}
return true;
}

}

0 comments on commit 9347a1e

Please sign in to comment.