Skip to content

Commit ccac5b8

Browse files
committed
Added GET_VALUE_INDEX action and BOOL script option
1 parent caab6b8 commit ccac5b8

File tree

7 files changed

+89
-2
lines changed

7 files changed

+89
-2
lines changed

src/main/java/io/github/techstreet/dfscript/screen/widget/CTexturedButton.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public class CTexturedButton extends CButton {
88

9-
private final String texture;
9+
private String texture;
1010
private final float ux;
1111
private final float uy;
1212
private final float uw;
@@ -25,6 +25,10 @@ public CTexturedButton(int x, int y, int width, int height, String texture, Runn
2525
this.hoverUy = hoverUy;
2626
}
2727

28+
public void setTexture(String newTexture) {
29+
texture = newTexture;
30+
}
31+
2832
@Override
2933
public void render(MatrixStack stack, int mouseX, int mouseY, float tickDelta) {
3034
stack.push();

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,26 @@ public enum ScriptActionType {
519519
}
520520
})),
521521

522+
GET_VALUE_INDEX(builder -> builder.name("Get List Index of Value")
523+
.description("Searches for a value in a list variable and gets the index if found.")
524+
.icon(Items.FLINT)
525+
.category(ScriptActionCategory.LISTS)
526+
.arg("Result", ScriptActionArgumentType.VARIABLE)
527+
.arg("List", ScriptActionArgumentType.VARIABLE)
528+
.arg("Value", ScriptActionArgumentType.ANY)
529+
.action(ctx -> {
530+
List<ScriptValue> list = ctx.value("List").asList();
531+
ScriptValue value = ctx.value("Value");
532+
int index = 0;
533+
for(int i = 0; i < list.size(); i++) {
534+
if (list.get(i).valueEquals(value)) {
535+
index = i + 1;
536+
break;
537+
}
538+
}
539+
ctx.context().setVariable(ctx.variable("Result").name(), new ScriptNumberValue(index));
540+
})),
541+
522542
SET_LIST_VALUE(builder -> builder.name("Set List Value")
523543
.description("Sets a value in a list.")
524544
.icon(Items.WRITABLE_BOOK)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.github.techstreet.dfscript.script.options;
2+
3+
import com.google.gson.JsonPrimitive;
4+
import io.github.techstreet.dfscript.DFScript;
5+
import io.github.techstreet.dfscript.screen.widget.CScrollPanel;
6+
import io.github.techstreet.dfscript.screen.widget.CTextField;
7+
import io.github.techstreet.dfscript.screen.widget.CTexturedButton;
8+
import io.github.techstreet.dfscript.script.argument.ScriptArgument;
9+
import io.github.techstreet.dfscript.script.argument.ScriptTextArgument;
10+
import net.minecraft.item.Item;
11+
import net.minecraft.item.Items;
12+
13+
public class ScriptBoolOption implements ScriptOption {
14+
15+
boolean value = false;
16+
17+
public ScriptBoolOption(boolean value) {
18+
this.value = value;
19+
}
20+
21+
public ScriptBoolOption() {}
22+
23+
@Override
24+
public ScriptArgument getValue() {
25+
return new ScriptTextArgument(value ? "true" : "false");
26+
}
27+
28+
@Override
29+
public String getName() { return "Boolean"; }
30+
31+
@Override
32+
public int create(CScrollPanel panel, int x, int y, int width) {
33+
CTexturedButton button = new CTexturedButton(x, y, 8, 8, getTexture(), null, 0, 0, 1, 0.5f, 0, 0.5f);
34+
button.setOnClick(() -> {
35+
value = !value;
36+
button.setTexture(getTexture());
37+
});
38+
panel.add(button);
39+
40+
return y + 10;
41+
}
42+
43+
private String getTexture() {
44+
return DFScript.MOD_ID + (value ? ":on_button.png" : ":off_button.png");
45+
}
46+
47+
@Override
48+
public Item getIcon() {
49+
return Items.LEVER;
50+
}
51+
52+
@Override
53+
public String getType() {
54+
return "BOOL";
55+
}
56+
57+
@Override
58+
public JsonPrimitive getJsonPrimitive() {
59+
return new JsonPrimitive(value);
60+
}
61+
}

src/main/java/io/github/techstreet/dfscript/script/options/ScriptNamedOption.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public ScriptNamedOption deserialize(JsonElement json, Type typeOfT, JsonDeseria
6666
case "INT" -> option = new ScriptIntOption(object.get("value").getAsInt());
6767
case "FLOAT" -> option = new ScriptFloatOption(object.get("value").getAsDouble());
6868
case "KEY" -> option = new ScriptKeyOption(InputUtil.fromTranslationKey(object.get("value").getAsString()));
69+
case "BOOL" -> option = new ScriptBoolOption(object.get("value").getAsBoolean());
6970
default -> throw new JsonParseException("Unknown option type: " + type);
7071
}
7172

src/main/java/io/github/techstreet/dfscript/script/options/ScriptOptionEnum.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public enum ScriptOptionEnum {
1717
TEXT("Text Option", "A single option, no checks.", Items.BOOK, ScriptTextOption.class),
1818
INT("Integer Option", "A single option, must be an int.", Items.SLIME_BALL, ScriptIntOption.class),
1919
FLOAT("Floating-Point Option", "A single option, must be an int or a float.", Items.SLIME_BLOCK, ScriptFloatOption.class),
20-
KEY("Key Option", "A single option, acts as a key bind.", Items.STONE_BUTTON, ScriptKeyOption.class);
20+
KEY("Key Option", "A single option, acts as a key bind.", Items.STONE_BUTTON, ScriptKeyOption.class),
21+
BOOL("Boolean Option", "A true/false option. Returns either \"true\" or \"false\".", Items.LEVER, ScriptBoolOption.class);
2122

2223
String name;
2324
String description;
11.7 KB
Loading
10.5 KB
Loading

0 commit comments

Comments
 (0)