Skip to content

Commit

Permalink
Fix 'nested ifs' resolving tags and definitions too early.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Jul 19, 2013
1 parent 5d3197f commit 8bbe362
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -822,7 +822,7 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
}

if (attribute.startsWith("equipment")) {
// Add later
return new dInventory(getLivingEntity()).getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("world")) {
Expand Down
Expand Up @@ -27,6 +27,7 @@ public class ScriptBuilder {
public static List<ScriptEntry> addObjectToEntries(List<ScriptEntry> scriptEntryList, String key, Object obj) {
for (ScriptEntry entry : scriptEntryList) {
entry.addObject(key, obj);
entry.trackObject(key);
}
return scriptEntryList;
}
Expand Down
Expand Up @@ -47,13 +47,17 @@ public void revolve(ScriptQueue scriptQueue) {
dB.echoError("Enable '/denizen stacktrace' for the nitty-gritty.");
else e.printStackTrace();
}
// Set as last entry exectured
// Set as last entry executed
scriptQueue.setLastEntryExecuted(scriptEntry);

if (scriptEntry.isInstant() || scriptQueue.ticks == 0) {
if (scriptEntry.isInstant() || scriptQueue.ticks == 0 && !scriptQueue.hasInjectedItems) {
// Remove from execution list
scriptEntry = scriptQueue.getNext();
}
else if (scriptQueue.hasInjectedItems) {
scriptQueue.hasInjectedItems = false;
break;
}
// If entry isn't instant, end the revolution and wait for another
else
break;
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/net/aufdemrand/denizen/scripts/ScriptQueue.java
Expand Up @@ -122,7 +122,23 @@ public static boolean _queueExists(String id) {

// ScriptQueues can have a bit of context, keyed by a String Id. All that can be accessed by either getContext()
// or a dScript replaceable tag <context.id>
public Map<String, String> context = new ConcurrentHashMap<String, String>(8, 0.9f, 1);
private ConcurrentHashMap<String, String> context = new ConcurrentHashMap<String, String>(8, 0.9f, 1);

public String getContext(String key) {
return context.get(key.toLowerCase());
}

public boolean hasContext(String key) {
return context.containsKey(key.toLowerCase());
}

public void addContext(String key, String value) {
context.put(key.toLowerCase(), value);
}

public ConcurrentHashMap<String, String> getAllContext() {
return context;
}

protected ScriptEntry lastEntryExecuted = null;

Expand Down Expand Up @@ -281,11 +297,13 @@ public ScriptQueue addEntries(List<ScriptEntry> entries) {
return this;
}

protected boolean hasInjectedItems = false;

public ScriptQueue injectEntries(List<ScriptEntry> entries, int position) {
if (position > scriptEntries.size() || position < 0) position = 1;
if (scriptEntries.size() == 0) position = 0;
scriptEntries.addAll(position, entries);
hasInjectedItems = true;
return this;
}

Expand All @@ -307,6 +325,7 @@ public ScriptQueue injectEntry(ScriptEntry entry, int position) {
if (position > scriptEntries.size() || position < 0) position = 1;
if (scriptEntries.size() == 0) position = 0;
scriptEntries.add(position, entry);
hasInjectedItems = true;
return this;
}

Expand Down
Expand Up @@ -37,10 +37,9 @@ 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()))
if (scriptEntry.getResidingQueue().hasContext(m.group(1).toLowerCase()))
m.appendReplacement(sb,
scriptEntry.getResidingQueue().context.get(m.group(1)));
scriptEntry.getResidingQueue().getContext(m.group(1).toLowerCase()));

else m.appendReplacement(sb, "null");
}
Expand Down Expand Up @@ -83,18 +82,24 @@ public boolean execute(ScriptEntry scriptEntry) {

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

if (!scriptEntry.getResidingQueue().context.isEmpty())
dB.echoDebug("Available Definitions: " + scriptEntry.getResidingQueue().context.toString());
int nested_depth = 0;

for (String arg : scriptEntry.getArguments()) {
if (arg.equals("{")) nested_depth++;
if (arg.equals("}")) nested_depth--;

if (nested_depth > 0) {
newArgs.add(arg);
continue;
}

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

else m.appendReplacement(sb, "null");
}
Expand Down
Expand Up @@ -59,9 +59,13 @@ else if (!scriptEntry.hasObject("value")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

scriptEntry.getResidingQueue().context
.put((String) scriptEntry.getObject("definition"),
(String) scriptEntry.getObject("value"));
dB.report(getName(), aH.debugObj("queue", scriptEntry.getResidingQueue().id)
+ aH.debugObj("definition", scriptEntry.getObject("definition").toString())
+ aH.debugObj("value", scriptEntry.getObject("value").toString()));

scriptEntry.getResidingQueue().addContext(
(String) scriptEntry.getObject("definition"),
(String) scriptEntry.getObject("value"));
}

}
Expand Up @@ -6,6 +6,7 @@
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.exceptions.ScriptEntryCreationException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.scripts.ScriptBuilder;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
Expand Down Expand Up @@ -285,12 +286,6 @@ private void doCommand(ScriptEntry scriptEntry, String mapName) {
.setNPC(scriptEntry.getNPC()).setInstant(true)
.addObject("reqId", scriptEntry.getObject("reqId"));

entry.setSendingQueue(scriptEntry.getResidingQueue());

// Put tracked objects into new script entries.
for (String tracked_object : scriptEntry.tracked_objects)
entry.addObject(tracked_object, scriptEntry.getObject(tracked_object));

entries.add(entry);

} catch (ScriptEntryCreationException e) {
Expand All @@ -303,6 +298,13 @@ private void doCommand(ScriptEntry scriptEntry, String mapName) {
}
}


// Put tracked objects into new script entries.
for (String tracked_object : scriptEntry.tracked_objects) {
ScriptBuilder.addObjectToEntries(entries, tracked_object, scriptEntry.getObject(tracked_object));
dB.log(tracked_object);
}

scriptEntry.getResidingQueue().injectEntries(entries, 0);
}

Expand Down
Expand Up @@ -141,7 +141,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
try { definition_names = script.getContainer().getString("definitions").split("\\|"); }
catch (Exception e) { }
for (String definition : definitions) {
queue.context.put(definition_names != null && definition_names.length >= x ?
queue.addContext(definition_names != null && definition_names.length >= x ?
definition_names[x - 1].trim() : String.valueOf(x), definition);
x++;
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/net/aufdemrand/denizen/tags/TagManager.java
Expand Up @@ -129,7 +129,7 @@ public static String tag(dPlayer player, dNPC npc, String arg, boolean instant,
// Call Event
Bukkit.getServer().getPluginManager().callEvent(event);
if ((!event.replaced() && event.getAlternative() != null)
|| (event.getReplaced() == "null" && event.getAlternative() != null))
|| (event.getReplaced() == "null" && event.getAlternative() != null))
event.setReplaced(event.getAlternative());
arg = arg.substring(0, positions[0]) + event.getReplaced() + arg.substring(positions[1] + 1, arg.length());
}
Expand Down Expand Up @@ -161,9 +161,16 @@ public static List<String> fillArguments(List<String> args, ScriptEntry scriptEn

public static List<String> fillArguments(List<String> args, ScriptEntry scriptEntry, boolean instant) {
List<String> filledArgs = new ArrayList<String>();

int nested_level = 0;
if (args != null) {
for (String argument : args) {
filledArgs.add(tag(scriptEntry.getPlayer(), scriptEntry.getNPC(), argument, instant, scriptEntry));
if (argument.equals("{")) nested_level++;
if (argument.equals("}")) nested_level--;
if (nested_level < 1) {
filledArgs.add(tag(scriptEntry.getPlayer(), scriptEntry.getNPC(), argument, instant, scriptEntry));
}
else filledArgs.add(argument);
}
}
return filledArgs;
Expand Down
32 changes: 31 additions & 1 deletion src/main/java/net/aufdemrand/denizen/tags/core/UtilTags.java
Expand Up @@ -10,6 +10,7 @@
import net.aufdemrand.denizen.events.ReplaceableTagEvent;
import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptQueue;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
Expand Down Expand Up @@ -39,10 +40,39 @@ public void mathTags(ReplaceableTagEvent event) {
event.setReplaced(String.valueOf(evaluation));
}

@EventHandler
public void queueTags(ReplaceableTagEvent event) {
if (!event.matches("queue, q")) return;
Attribute attribute =
new Attribute(event.raw_tag, event.getScriptEntry()).fulfill(1);

if (attribute.startsWith("id"))
event.setReplaced(new Element(event.getScriptEntry().getResidingQueue().id)
.getAttribute(attribute.fulfill(1)));

if (attribute.startsWith("stats"))
event.setReplaced(new Element(ScriptQueue._getStats())
.getAttribute(attribute.fulfill(1)));

if (attribute.startsWith("size"))
event.setReplaced(new Element(event.getScriptEntry().getResidingQueue().getQueueSize())
.getAttribute(attribute.fulfill(1)));

if (attribute.startsWith("speed"))
event.setReplaced(event.getScriptEntry().getResidingQueue().getSpeed()
.getAttribute(attribute.fulfill(1)));

if (attribute.startsWith("definitions"))
event.setReplaced(new Element(event.getScriptEntry().getResidingQueue().getAllContext().toString())
.getAttribute(attribute.fulfill(1)));

}

@EventHandler
public void serverTags(ReplaceableTagEvent event) {
if (!event.matches("server, svr, global")) return;
Attribute attribute = new Attribute(event.raw_tag, event.getScriptEntry()).fulfill(1);
Attribute attribute =
new Attribute(event.raw_tag, event.getScriptEntry()).fulfill(1);

// server.flag[...]
// server.flag[...].is_expired
Expand Down

0 comments on commit 8bbe362

Please sign in to comment.