Skip to content

Commit

Permalink
Allow multiple determinations
Browse files Browse the repository at this point in the history
Because restricting to just one determination is annoying
  • Loading branch information
mcmonkey4eva committed Nov 25, 2014
1 parent 40ffd47 commit b344316
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 20 deletions.
26 changes: 22 additions & 4 deletions src/main/java/net/aufdemrand/denizen/events/EventManager.java
Expand Up @@ -229,6 +229,16 @@ public static String doEvents(List<String> eventNames, dNPC npc, dPlayer player,
npc, player, context);
}

public static List<String> doEvents1(List<String> eventNames, dNPC npc, dPlayer player, Map<String, dObject> context,
boolean usesIdentifiers) {

// If a list of events uses identifiers, also add those events to the list
// with their identifiers stripped
return doEvents1(usesIdentifiers ? addAlternates(eventNames)
: eventNames,
npc, player, context);
}


public static List<String> addAlternates(List<String> events) {

Expand Down Expand Up @@ -289,9 +299,17 @@ public static String doEvents(List<String> eventNames, dNPC npc, dPlayer player,
}

public static String doEvents(List<String> eventNames, dNPC npc, dPlayer player, Map<String, dObject> context) {
List<String> strs = doEvents1(eventNames, npc, player, context);
if (strs.isEmpty())
return DetermineCommand.DETERMINE_NONE;
else
return strs.get(0);
}

public static List<String> doEvents1(List<String> eventNames, dNPC npc, dPlayer player, Map<String, dObject> context) {

try {
String determination = "none";
List<String> determinations = new ArrayList<String>();

// Trim again to catch events that don't trim internally.
eventNames = trimEvents(eventNames);
Expand Down Expand Up @@ -339,15 +357,15 @@ public static String doEvents(List<String> eventNames, dNPC npc, dPlayer player,

// Check the determination
if (DetermineCommand.hasOutcome(id))
determination = DetermineCommand.getOutcome(id);
determinations = DetermineCommand.getOutcome(id);
}
}

return determination;
return determinations;
}
catch (Exception e) {
dB.echoError(e);
return "none";
return new ArrayList<String>();
}
}

Expand Down
Expand Up @@ -75,8 +75,8 @@ public String doAction(String actionName, dNPC npc, dPlayer player, AssignmentSc

// Check the determination by asking the DetermineCommand
if (DetermineCommand.hasOutcome(id))
determination = DetermineCommand.getOutcome(id);

determination = DetermineCommand.getOutcome(id).get(0);
// TODO: Multiple determination system
return determination;
}
}
8 changes: 4 additions & 4 deletions src/main/java/net/aufdemrand/denizen/objects/dList.java
Expand Up @@ -807,9 +807,9 @@ else if (get(n).startsWith("e@") || get(n).startsWith("n@")) {
// @attribute <li@list.find_all_partial[<element>]>
// @returns dList(Element(Number))
// @description
// returns all the numbered locations of elements within a list,
// returns all the numbered locations of elements that contain the text within a list,
// or an empty list if the list does not contain that item.
// EG, .find[two] on a list of "one|two|three|two" will return "2|4".
// EG, .find_all_partial[tw] on a list of "one|two|three|two" will return "2|4".
// TODO: Take multiple inputs? Or a regex?
// -->
if (attribute.startsWith("find_all_partial") &&
Expand All @@ -826,7 +826,7 @@ else if (get(n).startsWith("e@") || get(n).startsWith("n@")) {
// @attribute <li@list.find_all[<element>]>
// @returns dList(Element(Number))
// @description
// returns all the numbered locations of elements within a list,
// returns all the numbered locations of elements that match the text within a list,
// or an empty list if the list does not contain that item.
// EG, .find[two] on a list of "one|two|three|two" will return "2|4".
// TODO: Take multiple inputs? Or a regex?
Expand Down Expand Up @@ -1043,7 +1043,7 @@ public int compare(String o1, String o2) {
queue.start();
int res = 0;
if (DetermineCommand.hasOutcome(id))
res = new Element(DetermineCommand.getOutcome(id)).asInt();
res = new Element(DetermineCommand.getOutcome(id).get(0)).asInt();
if (res < 0)
return -1;
else if (res > 0)
Expand Down
@@ -1,5 +1,7 @@
package net.aufdemrand.denizen.scripts.commands.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -28,7 +30,7 @@ public class DetermineCommand extends AbstractCommand {

// Map for keeping track of cache
// Key: ID, Value: outcome
private static Map<Long, String> cache = new ConcurrentHashMap<Long, String>(8, 0.9f, 1);
private static Map<Long, List<String>> cache = new ConcurrentHashMap<Long, List<String>>(8, 0.9f, 1);

// Start at 0
public static long uniqueId = 0;
Expand Down Expand Up @@ -56,7 +58,7 @@ public static long getNewId() {
* @return if the cache has the outcome
*/
public static boolean hasOutcome(long id) {
return cache.containsKey(id);
return cache.containsKey(id) && !cache.get(id).isEmpty();
}


Expand All @@ -66,8 +68,8 @@ public static boolean hasOutcome(long id) {
* @param id the outcome id to check
* @return the outcome
*/
public static String getOutcome(long id) {
String outcome = cache.get(id);
public static List<String> getOutcome(long id) {
List<String> outcome = cache.get(id);
cache.remove(id);
return outcome;
}
Expand All @@ -81,7 +83,7 @@ public static String getOutcome(long id) {
* @return the current value of the outcome
*/
public static String readOutcome(long id) {
return cache.get(id);
return cache.get(id).isEmpty() ? DETERMINE_NONE: cache.get(id).get(0);
}


Expand Down Expand Up @@ -136,7 +138,15 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
}

// Store the outcome in the cache
cache.put(uniqueId, outcome);
List<String> strs;
if (cache.containsKey(uniqueId)) {
strs = cache.get(uniqueId);
}
else {
strs = new ArrayList<String>();
cache.put(uniqueId, strs);
}
strs.add(outcome);

if (!passively)
// Stop the queue by clearing the remainder of it.
Expand Down
Expand Up @@ -161,7 +161,7 @@ public boolean runAllowedHelpProcedure(dPlayer player, dNPC npc, Map<String, dOb
}
}
queue.start();
return DetermineCommand.getOutcome(id).equalsIgnoreCase("true");
return DetermineCommand.hasOutcome(id) && DetermineCommand.getOutcome(id).get(0).equalsIgnoreCase("true");
}

public List<String> runTabCompleteProcedure(dPlayer player, dNPC npc, Map<String, dObject> context) {
Expand All @@ -178,7 +178,7 @@ public List<String> runTabCompleteProcedure(dPlayer player, dNPC npc, Map<String
}
queue.start();
if (DetermineCommand.hasOutcome(id))
return dList.valueOf(DetermineCommand.getOutcome(id));
return dList.valueOf(DetermineCommand.getOutcome(id).get(0));
else
return new ArrayList<String>();
}
Expand Down
Expand Up @@ -106,7 +106,7 @@ public ProcedureScriptTag(Denizen denizen) {
public void procedureTag(ReplaceableTagEvent event) {

// <--[tag]
// @attribute <proc[ProcedureScript].Context[<element>|...]>
// @attribute <proc[ProcedureScript].context[<element>|...]>
// @returns dObject
// @description
// Returns the 'determine' result of a procedure script with the given context.
Expand Down Expand Up @@ -191,7 +191,7 @@ public void procedureTag(ReplaceableTagEvent event) {
queue.start();

if (DetermineCommand.hasOutcome(id)) {
event.setReplaced(ObjectFetcher.pickObjectFor(DetermineCommand.getOutcome(id))
event.setReplaced(ObjectFetcher.pickObjectFor(DetermineCommand.getOutcome(id).get(0))
.getAttribute(attr.fulfill(attribs)));
}
}
Expand Down

0 comments on commit b344316

Please sign in to comment.