Skip to content

Commit

Permalink
improve list.sort
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 17, 2016
1 parent 9b144b9 commit f920d8d
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/main/java/net/aufdemrand/denizencore/objects/dList.java
Original file line number Diff line number Diff line change
Expand Up @@ -1136,23 +1136,21 @@ public int compare(String o1, String o2) {
// @returns Element
// @description
// returns a list sorted according to the return values of a procedure.
// The <procedure> should link a procedure script that takes two definitions
// each of which will be an item in the list, and returns -1, 0, or 1 based on
// whether the second item should be added. EG, if a procedure with definitions
// "one" and "two" returned 1, it would place "two" after "one". Note that this
// uses some complex internal sorting code that could potentially throw errors
// if the procedure does not return consistently - EG, if "one" and "two" returned
// 1, but "two" and "one" returned 1 as well - obviously, "two" can not be both
// before AND after "one"!
// Note that the script should ALWAYS return -1, 0, or 1, or glitches will happen!
// The <procedure> should link a procedure script that takes two definitions each of which will be an item
// in the list, and returns -1, 0, or 1 based on whether the second item should be added. EG, if a procedure
// with definitions "one" and "two" returned -1, it would place "two" after "one". Note that this
// uses some complex internal sorting code that could potentially throw errors if the procedure does not return
// consistently - EG, if "one" and "two" returned 1, but "two" and "one" returned 1 as well - obviously,
// "two" can not be both before AND after "one"!
// Note that the script should ALWAYS return -1, 0, or 1, or glitches could happen!
// Note that if two inputs are exactly equal, the procedure should always return 0.
// -->

registerTag("sort", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
dList obj = new dList((dList) object);
final ProcedureScriptContainer script = ScriptRegistry.getScriptContainer(attribute.getContext(1));
final ProcedureScriptContainer script = (ProcedureScriptContainer) dScript.valueOf(attribute.getContext(1)).getContainer();
if (script == null) {
dB.echoError("'" + attribute.getContext(1) + "' is not a valid procedure script!");
return obj.getAttribute(attribute.fulfill(1));
Expand All @@ -1176,7 +1174,8 @@ public String run(Attribute attribute, dObject object) {
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
List<ScriptEntry> entries = script.getBaseEntries(entry.entryData.clone());
List<ScriptEntry> entries = script.getBaseEntries(entry == null ?
DenizenCore.getImplementation().getEmptyScriptEntryData() : entry.entryData.clone());
if (entries.isEmpty()) {
return 0;
}
Expand All @@ -1199,7 +1198,7 @@ public int compare(String o1, String o2) {
String name = definition_names != null && definition_names.length >= x ?
definition_names[x - 1].trim() : String.valueOf(x);
queue.addDefinition(name, definition);
dB.echoDebug(entry, "Adding definition %" + name + "% as " + definition);
dB.echoDebug(entries.get(0), "Adding definition %" + name + "% as " + definition);
x++;
}
queue.start();
Expand All @@ -1220,7 +1219,7 @@ else if (res > 0) {
});
}
catch (Exception e) {
dB.echoError("list.sort[...] tag failed - procedure returned unreasonable valid - internal error: " + e.getMessage());
dB.echoError("list.sort[...] tag failed - procedure returned unreasonable response - internal error: " + e.getMessage());
}
return new dList(list).getAttribute(attribute);
}
Expand Down

0 comments on commit f920d8d

Please sign in to comment.