Skip to content

Commit 9c8166e

Browse files
committed
Ported Scripting Changes from my fork of CU
+ SORT_LIST * fixed text fields created by scripts not being editable (literally just changed one value in a function lmao)
1 parent 997dbe0 commit 9c8166e

File tree

7 files changed

+74
-7
lines changed

7 files changed

+74
-7
lines changed

src/main/java/io/github/techstreet/dfscript/script/action/ScriptActionType.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@
2222
import io.github.techstreet.dfscript.script.menu.ScriptWidget;
2323
import io.github.techstreet.dfscript.script.util.ScriptValueItem;
2424
import io.github.techstreet.dfscript.script.util.ScriptValueJson;
25-
import io.github.techstreet.dfscript.script.values.ScriptDictionaryValue;
26-
import io.github.techstreet.dfscript.script.values.ScriptListValue;
27-
import io.github.techstreet.dfscript.script.values.ScriptNumberValue;
28-
import io.github.techstreet.dfscript.script.values.ScriptTextValue;
29-
import io.github.techstreet.dfscript.script.values.ScriptUnknownValue;
30-
import io.github.techstreet.dfscript.script.values.ScriptValue;
25+
import io.github.techstreet.dfscript.script.values.*;
3126
import io.github.techstreet.dfscript.util.ComponentUtil;
3227
import io.github.techstreet.dfscript.util.FileUtil;
3328
import io.github.techstreet.dfscript.util.ItemUtil;
@@ -1517,7 +1512,7 @@ public enum ScriptActionType {
15171512

15181513
if (io.github.techstreet.dfscript.DFScript.MC.currentScreen instanceof ScriptMenu menu) {
15191514
if (menu.ownedBy(ctx.script())) {
1520-
menu.widgets.add(new ScriptMenuTextField("",x,y,width,height,false,identifier));
1515+
menu.widgets.add(new ScriptMenuTextField("",x,y,width,height,true,identifier));
15211516
} else {
15221517
ChatUtil.error("Unable to add text field to menu! (Not owned by script)");
15231518
}
@@ -1631,6 +1626,29 @@ public enum ScriptActionType {
16311626
.hasChildren(true)
16321627
.action(ctx -> {
16331628
ctx.setLastIfResult(!ctx.lastIfResult());
1629+
})),
1630+
1631+
SORT_LIST(builder -> builder.name("Sort List")
1632+
.description("Sorts a list in ascending order.")
1633+
.icon(Items.REPEATING_COMMAND_BLOCK)
1634+
.arg("Result", ScriptActionArgumentType.VARIABLE)
1635+
.arg("List", ScriptActionArgumentType.LIST, b -> b.optional(true))
1636+
.category(ScriptActionCategory.LISTS)
1637+
.action(ctx -> {
1638+
List<ScriptValue> list = null;
1639+
1640+
if(ctx.argMap().containsKey("List"))
1641+
{
1642+
list = ctx.value("List").asList();
1643+
}
1644+
else
1645+
{
1646+
list = ctx.value("Result").asList();
1647+
}
1648+
1649+
list.sort(new ScriptValueComparator());
1650+
1651+
ctx.context().setVariable(ctx.variable("Result").name(), new ScriptListValue(list));
16341652
}));
16351653

16361654
private Consumer<ScriptActionContext> action = (ctx) -> {

src/main/java/io/github/techstreet/dfscript/script/values/ScriptDictionaryValue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,11 @@ public boolean valueEquals(ScriptValue other) {
4545
public String asText() {
4646
return value.toString();
4747
}
48+
49+
@Override
50+
public ScriptValue getCompareValue() {
51+
HashMap<String, ScriptValue> dict = asDictionary();
52+
53+
return dict.get(dict.keySet().toArray()[0]).getCompareValue();
54+
}
4855
}

src/main/java/io/github/techstreet/dfscript/script/values/ScriptListValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ public boolean valueEquals(ScriptValue other) {
4343
public String asText() {
4444
return value.toString();
4545
}
46+
47+
@Override
48+
public ScriptValue getCompareValue() {
49+
return asList().get(0).getCompareValue();
50+
}
4651
}

src/main/java/io/github/techstreet/dfscript/script/values/ScriptNumberValue.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,20 @@ public String asText() {
3434
}
3535
return String.valueOf(value);
3636
}
37+
38+
@Override
39+
public int compare(ScriptValue other) {
40+
if(other instanceof ScriptNumberValue) {
41+
if(asNumber() == other.asNumber()) {
42+
return 0;
43+
}
44+
45+
if(asNumber() > other.asNumber()) {
46+
return 1;
47+
}
48+
49+
return -1;
50+
}
51+
return asText().compareTo(other.asText());
52+
}
3753
}

src/main/java/io/github/techstreet/dfscript/script/values/ScriptUnknownValue.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ public List<ScriptValue> asList() {
3535
public double asNumber() {
3636
return 0;
3737
}
38+
39+
public int compare(ScriptValue other) {
40+
return 0;
41+
}
3842
}

src/main/java/io/github/techstreet/dfscript/script/values/ScriptValue.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ public String toString() {
2929
}
3030

3131
public abstract boolean valueEquals(ScriptValue other);
32+
33+
public ScriptValue getCompareValue() {
34+
return this;
35+
}
36+
37+
public int compare(ScriptValue other) {
38+
return asText().compareTo(other.asText());
39+
}
3240
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.github.techstreet.dfscript.script.values;
2+
3+
import java.util.Comparator;
4+
public class ScriptValueComparator implements Comparator<ScriptValue> {
5+
@Override
6+
public int compare(ScriptValue o1, ScriptValue o2) {
7+
return o1.getCompareValue().compare(o2.getCompareValue());
8+
}
9+
}

0 commit comments

Comments
 (0)