Skip to content

Commit

Permalink
Clean the Command Executor
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 12, 2015
1 parent e433ac7 commit 8047da3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package net.aufdemrand.denizencore;

import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.ScriptEntryData;
import net.aufdemrand.denizencore.scripts.queues.ScriptQueue;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.YamlConfiguration;
import net.aufdemrand.denizencore.utilities.debugging.Debuggable;
import net.aufdemrand.denizencore.utilities.debugging.dB.DebugElement;
Expand Down Expand Up @@ -46,6 +48,11 @@ public interface DenizenImplementation {
*/
public abstract void debugError(ScriptQueue queue, String error);

/**
* Output an error to console, specific to a script queue.
*/
public abstract void debugError(ScriptQueue queue, Throwable error);

/**
* Output a command information report.
*/
Expand Down Expand Up @@ -109,4 +116,63 @@ public interface DenizenImplementation {
* Temporary.
*/
public abstract List<YamlConfiguration> getOutsideScripts();

/**
if ((scriptEntry.entryData).hasNPC() && (scriptEntry.entryData).getNPC().getCitizen() == null)
(scriptEntry.entryData).setNPC(null);
*/
public abstract void handleCommandSpecialCases(ScriptEntry entry);

/**
if (scriptEntry.getOriginalArguments() == null ||
scriptEntry.getOriginalArguments().size() == 0 ||
!scriptEntry.getOriginalArguments().get(0).equals("\0CALLBACK")) {
if ((scriptEntry.entryData).getPlayer() != null)
dB.echoDebug(scriptEntry, DebugElement.Header,
"Executing dCommand: " + scriptEntry.getCommandName() + "/p@" +
(scriptEntry.entryData).getPlayer().getName());
else
dB.echoDebug(scriptEntry, DebugElement.Header, "Executing dCommand: " +
scriptEntry.getCommandName() + ((scriptEntry.entryData).getNPC() != null ?
"/n@" + (scriptEntry.entryData).getNPC().getName() : ""));
}
*/
public abstract void debugCommandHeader(ScriptEntry entry);

/**
* new BukkitTagContext(scriptEntry != null ? ((BukkitScriptEntryData)scriptEntry.entryData).getPlayer(): null,
scriptEntry != null ? ((BukkitScriptEntryData)scriptEntry.entryData).getNPC(): null,
true, scriptEntry, scriptEntry != null ? scriptEntry.shouldDebug(): true,
scriptEntry != null ? scriptEntry.getScript(): null
)
*/
public abstract TagContext getTagContextFor(ScriptEntry entry, boolean instant);

/**
*
// Fill player/off-line player
if (arg.matchesPrefix("player") && !if_ignore) {
dB.echoDebug(scriptEntry, "...replacing the linked player with " + arg.getValue());
String value = TagManager.tag(arg.getValue(), new BukkitTagContext(scriptEntry, false));
dPlayer player = dPlayer.valueOf(value);
if (player == null || !player.isValid()) {
dB.echoError(scriptEntry.getResidingQueue(), value + " is an invalid player!");
return false;
}
((BukkitScriptEntryData)scriptEntry.entryData).setPlayer(player);
}
// Fill NPCID/NPC argument
else if (arg.matchesPrefix("npc, npcid") && !if_ignore) {
dB.echoDebug(scriptEntry, "...replacing the linked NPC with " + arg.getValue());
String value = TagManager.tag(arg.getValue(), new BukkitTagContext(scriptEntry, false));
dNPC npc = dNPC.valueOf(value);
if (npc == null || !npc.isValid()) {
dB.echoError(scriptEntry.getResidingQueue(), value + " is an invalid NPC!");
return false;
}
((BukkitScriptEntryData)scriptEntry.entryData).setNPC(npc);
}
*/
public abstract void handleCustomArgs(ScriptEntry entry, aH.Argument arg);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.tags.TagManager;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.debugging.dB.DebugElement;

import net.aufdemrand.denizencore.scripts.commands.BaseAbstractCommand;

public class CommandExecuter {

private static final Pattern definition_pattern = Pattern.compile("%(.+?)%");
Expand Down Expand Up @@ -71,22 +70,10 @@ public boolean execute(ScriptEntry scriptEntry) {
return false;
}

if ((criptEntry.entryData).hasNPC() && (scriptEntry.entryData).getNPC().getCitizen() == null)
(scriptEntry.entryData).setNPC(null);
DenizenCore.getImplementation().handleCommandSpecialCases(scriptEntry);

// Debugger information
if (scriptEntry.getOriginalArguments() == null ||
scriptEntry.getOriginalArguments().size() == 0 ||
!scriptEntry.getOriginalArguments().get(0).equals("\0CALLBACK")) {
if ((scriptEntry.entryData).getPlayer() != null)
dB.echoDebug(scriptEntry, DebugElement.Header,
"Executing dCommand: " + scriptEntry.getCommandName() + "/p@" +
(scriptEntry.entryData).getPlayer().getName());
else
dB.echoDebug(scriptEntry, DebugElement.Header, "Executing dCommand: " +
scriptEntry.getCommandName() + ((scriptEntry.entryData).getNPC() != null ?
"/n@" + (scriptEntry.entryData).getNPC().getName() : ""));
}
DenizenCore.getImplementation().debugCommandHeader(scriptEntry);

// Don't execute() if problems arise in parseArgs()
boolean keepGoing = true;
Expand All @@ -98,11 +85,7 @@ public boolean execute(ScriptEntry scriptEntry) {

if (scriptEntry.has_tags) {
scriptEntry.setArguments(TagManager.fillArguments(scriptEntry.getArguments(),
new BukkitTagContext(scriptEntry != null ? ((BukkitScriptEntryData)scriptEntry.entryData).getPlayer(): null,
scriptEntry != null ? ((BukkitScriptEntryData)scriptEntry.entryData).getNPC(): null,
true, scriptEntry, scriptEntry != null ? scriptEntry.shouldDebug(): true,
scriptEntry != null ? scriptEntry.getScript(): null
))); // Replace tags
DenizenCore.getImplementation().getTagContextFor(scriptEntry, true))); // Replace tags
}

/* If using NPC:# or PLAYER:Name arguments, these need to be changed out immediately because...
Expand Down Expand Up @@ -162,36 +145,14 @@ public boolean execute(ScriptEntry scriptEntry) {
// If using IF, check if we've reached the command + args
// so that we don't fill player: or npc: prematurely
if (command.getName().equalsIgnoreCase("if")
&& DenizenAPI.getCurrentInstance().getCommandRegistry().get(arg.getValue()) != null)
&& DenizenCore.getCommandRegistry().get(arg.getValue()) != null)
if_ignore = true;

// Fill player/off-line player
if (arg.matchesPrefix("player") && !if_ignore) {
dB.echoDebug(scriptEntry, "...replacing the linked player with " + arg.getValue());
String value = TagManager.tag(arg.getValue(), new BukkitTagContext(scriptEntry, false));
dPlayer player = dPlayer.valueOf(value);
if (player == null || !player.isValid()) {
dB.echoError(scriptEntry.getResidingQueue(), value + " is an invalid player!");
return false;
}
((BukkitScriptEntryData)scriptEntry.entryData).setPlayer(player);
}

// Fill NPCID/NPC argument
else if (arg.matchesPrefix("npc, npcid") && !if_ignore) {
dB.echoDebug(scriptEntry, "...replacing the linked NPC with " + arg.getValue());
String value = TagManager.tag(arg.getValue(), new BukkitTagContext(scriptEntry, false));
dNPC npc = dNPC.valueOf(value);
if (npc == null || !npc.isValid()) {
dB.echoError(scriptEntry.getResidingQueue(), value + " is an invalid NPC!");
return false;
}
((BukkitScriptEntryData)scriptEntry.entryData).setNPC(npc);
}
DenizenCore.getImplementation().handleCustomArgs(scriptEntry, arg);

// Save the scriptentry if needed later for fetching scriptentry context
else if (arg.matchesPrefix("save") && !if_ignore) {
String saveName = TagManager.tag(arg.getValue(), new BukkitTagContext(scriptEntry, false));
if (arg.matchesPrefix("save") && !if_ignore) {
String saveName = TagManager.tag(arg.getValue(), new TagContext(false, scriptEntry.shouldDebug(), scriptEntry));
dB.echoDebug(scriptEntry, "...remembering this script entry as '" + saveName + "'!");
scriptEntry.getResidingQueue().holdScriptEntry(saveName, scriptEntry);
}
Expand All @@ -204,11 +165,7 @@ else if (arg.matchesPrefix("save") && !if_ignore) {

// Now process non-instant tags.
scriptEntry.setArguments(TagManager.fillArguments(scriptEntry.getArguments(),
new BukkitTagContext(scriptEntry != null ? ((BukkitScriptEntryData)scriptEntry.entryData).getPlayer(): null,
scriptEntry != null ? ((BukkitScriptEntryData)scriptEntry.entryData).getNPC(): null,
false, scriptEntry, scriptEntry != null ? scriptEntry.shouldDebug(): true,
scriptEntry != null ? scriptEntry.getScript(): null
))); // Replace tags
DenizenCore.getImplementation().getTagContextFor(scriptEntry, false))); // Replace tags

// Parse the rest of the arguments for execution.
((AbstractCommand)command).parseArgs(scriptEntry);
Expand All @@ -219,7 +176,7 @@ else if (arg.matchesPrefix("save") && !if_ignore) {
// Give usage hint if InvalidArgumentsException was called.
dB.echoError(scriptEntry.getResidingQueue(), "Woah! Invalid arguments were specified!");
if (e.getMessage() != null && e.getMessage().length() > 0)
dB.log(ChatColor.YELLOW + "+> MESSAGE follows: " + ChatColor.WHITE + "'" + e.getMessage() + "'");
dB.log("+> MESSAGE follows: " + "'" + e.getMessage() + "'");
dB.log("Usage: " + command.getUsageHint());
dB.echoDebug(scriptEntry, DebugElement.Footer);
scriptEntry.setFinished(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public static void echoError(ScriptQueue queue, String error) {
DenizenCore.getImplementation().debugError(queue, error);
}

public static void echoError(ScriptQueue queue, Throwable error) {
DenizenCore.getImplementation().debugError(queue, error);
}

public static void echoError(Throwable ex) {
DenizenCore.getImplementation().debugException(ex);
}
Expand Down

0 comments on commit 8047da3

Please sign in to comment.