Skip to content

Commit 6f9333c

Browse files
authored
Fix a tag stocking input bus crash with invalid syntax in one of the fields (#25)
* TagMatcher: fix a server crash on syntax errors * Tag Stocking: show invalid syntax visually * spotlessApply
1 parent 7cc85c3 commit 6f9333c

6 files changed

Lines changed: 148 additions & 36 deletions

File tree

src/main/java/net/neganote/gtutilities/common/gui/widgets/MultilineTextField.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class MultilineTextField extends WidgetGroup {
3232
private final Supplier<String> textSupplier;
3333
private final Consumer<String> textConsumer;
3434
private final Component placeholder;
35+
private final Supplier<Integer> borderColorSupplier;
3536

3637
private int maxLength = DEFAULT_MAX_LENGTH;
3738

@@ -50,18 +51,28 @@ public MultilineTextField(
5051
int x, int y, int width, int height,
5152
Supplier<String> textSupplier,
5253
Consumer<String> textConsumer) {
53-
this(x, y, width, height, textSupplier, textConsumer, Component.empty());
54+
this(x, y, width, height, textSupplier, textConsumer, Component.empty(), null);
5455
}
5556

5657
public MultilineTextField(
5758
int x, int y, int width, int height,
5859
Supplier<String> textSupplier,
5960
Consumer<String> textConsumer,
6061
Component placeholder) {
62+
this(x, y, width, height, textSupplier, textConsumer, placeholder, null);
63+
}
64+
65+
public MultilineTextField(
66+
int x, int y, int width, int height,
67+
Supplier<String> textSupplier,
68+
Consumer<String> textConsumer,
69+
Component placeholder,
70+
Supplier<Integer> borderColorSupplier) {
6171
super(new Position(x, y), new Size(width, height));
6272
this.textSupplier = textSupplier;
6373
this.textConsumer = textConsumer;
6474
this.placeholder = placeholder == null ? Component.empty() : placeholder;
75+
this.borderColorSupplier = borderColorSupplier;
6576

6677
String init = safe(textSupplier.get());
6778
this.lastSent = init;
@@ -467,7 +478,8 @@ public void drawInBackground(net.minecraft.client.gui.GuiGraphics graphics, int
467478
int h = getSize().height;
468479

469480
int bg = 0xFF202020;
470-
int border = hasFocus ? 0xFFFFFFFF : 0xFF808080;
481+
Integer suppliedBorder = borderColorSupplier != null ? borderColorSupplier.get() : null;
482+
int border = suppliedBorder != null ? suppliedBorder : hasFocus ? 0xFFFFFFFF : 0xFF808080;
471483

472484
graphics.fill(x0 - 1, y0 - 1, x0 + w + 1, y0 + h + 1, 0xAA000000);
473485
graphics.fill(x0, y0, x0 + w, y0 + h, bg);

src/main/java/net/neganote/gtutilities/integration/ae2/machine/MEEnlargedTagStockingInputBusPartMachine.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@
5353
import it.unimi.dsi.fastutil.objects.Object2LongMap;
5454
import org.jetbrains.annotations.Nullable;
5555

56-
import java.util.*;
56+
import java.util.ArrayList;
57+
import java.util.Comparator;
58+
import java.util.List;
59+
import java.util.Objects;
60+
import java.util.PriorityQueue;
5761
import java.util.concurrent.atomic.AtomicInteger;
5862
import java.util.function.Consumer;
5963
import java.util.function.Predicate;
@@ -75,6 +79,12 @@ public class MEEnlargedTagStockingInputBusPartMachine extends MEStockingBusPartM
7579
@DescSynced
7680
protected String blacklistExpr = "";
7781

82+
@DescSynced
83+
protected boolean whitelistBadSyntax = false;
84+
85+
@DescSynced
86+
protected boolean blacklistBadSyntax = false;
87+
7888
@DescSynced
7989
protected String Wltmp = "";
8090

@@ -168,11 +178,13 @@ private void ensureCompiledUpToDate() {
168178
if (!Objects.equals(wl, wlLast)) {
169179
wlLast = wl;
170180
wlCompiled = TagMatcher.compile(wl);
181+
whitelistBadSyntax = !wlCompiled.isValid();
171182
decisionCache.clear();
172183
}
173184
if (!Objects.equals(bl, blLast)) {
174185
blLast = bl;
175186
blCompiled = TagMatcher.compile(bl);
187+
blacklistBadSyntax = !blCompiled.isValid();
176188
decisionCache.clear();
177189
}
178190

@@ -183,6 +195,7 @@ private void ensureCompiledUpToDate() {
183195

184196
protected boolean isAllowed(AEItemKey key) {
185197
ensureCompiledUpToDate();
198+
if (whitelistBadSyntax || blacklistBadSyntax) return false;
186199

187200
if ((wlLast == null || wlLast.isEmpty()) && (blLast == null || blLast.isEmpty())) return false;
188201

@@ -380,15 +393,17 @@ public boolean mouseWheelMove(double mouseX, double mouseY, double wheelDelta) {
380393
7, y, 160, 25,
381394
() -> Wltmp,
382395
v -> { Wltmp = v; },
383-
Component.literal("Whitelist tags..."));
396+
Component.literal("Whitelist tags..."),
397+
() -> whitelistBadSyntax ? 0xFFFF0000 : null);
384398
group.addWidget(WLField);
385399

386400
y += 29;
387401
var BLField = new MultilineTextField(
388402
7, y, 160, 25,
389403
() -> Bltmp,
390404
v -> { Bltmp = v; },
391-
Component.literal("Blacklist tags..."));
405+
Component.literal("Blacklist tags..."),
406+
() -> blacklistBadSyntax ? 0xFFFF0000 : null);
392407
group.addWidget(BLField);
393408

394409
WLField.setDirectly(whitelistExpr);

src/main/java/net/neganote/gtutilities/integration/ae2/machine/MEEnlargedTagStockingInputHatchPartMachine.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import com.gregtechceu.gtceu.config.ConfigHolder;
1313
import com.gregtechceu.gtceu.integration.ae2.gui.widget.AEFluidConfigWidget;
1414
import com.gregtechceu.gtceu.integration.ae2.machine.MEStockingHatchPartMachine;
15-
import com.gregtechceu.gtceu.integration.ae2.slot.*;
15+
import com.gregtechceu.gtceu.integration.ae2.slot.ExportOnlyAEFluidList;
16+
import com.gregtechceu.gtceu.integration.ae2.slot.ExportOnlyAEFluidSlot;
17+
import com.gregtechceu.gtceu.integration.ae2.slot.ExportOnlyAESlot;
1618
import com.gregtechceu.gtceu.integration.ae2.utils.AEUtil;
1719

1820
import com.lowdragmc.lowdraglib.gui.texture.GuiTextureGroup;
@@ -50,7 +52,11 @@
5052
import it.unimi.dsi.fastutil.objects.Object2LongMap;
5153
import org.jetbrains.annotations.Nullable;
5254

53-
import java.util.*;
55+
import java.util.ArrayList;
56+
import java.util.Comparator;
57+
import java.util.List;
58+
import java.util.Objects;
59+
import java.util.PriorityQueue;
5460
import java.util.concurrent.atomic.AtomicInteger;
5561
import java.util.function.Consumer;
5662
import java.util.function.Predicate;
@@ -75,6 +81,12 @@ public class MEEnlargedTagStockingInputHatchPartMachine extends MEStockingHatchP
7581
@DescSynced
7682
protected String Bltmp = "";
7783

84+
@DescSynced
85+
protected boolean whitelistBadSyntax = false;
86+
87+
@DescSynced
88+
protected boolean blacklistBadSyntax = false;
89+
7890
private Predicate<GenericStack> tagAutoPullTest = ($) -> true;
7991

8092
private transient String wlLast = null;
@@ -161,11 +173,13 @@ private void ensureCompiledUpToDate() {
161173
if (!Objects.equals(wl, wlLast)) {
162174
wlLast = wl;
163175
wlCompiled = TagMatcher.compile(wl);
176+
whitelistBadSyntax = !wlCompiled.isValid();
164177
decisionCache.clear();
165178
}
166179
if (!Objects.equals(bl, blLast)) {
167180
blLast = bl;
168181
blCompiled = TagMatcher.compile(bl);
182+
blacklistBadSyntax = !blCompiled.isValid();
169183
decisionCache.clear();
170184
}
171185

@@ -176,6 +190,7 @@ private void ensureCompiledUpToDate() {
176190

177191
protected boolean isAllowed(AEFluidKey key) {
178192
ensureCompiledUpToDate();
193+
if (whitelistBadSyntax || blacklistBadSyntax) return false;
179194

180195
if ((wlLast == null || wlLast.isEmpty()) && (blLast == null || blLast.isEmpty())) return false;
181196

@@ -367,15 +382,17 @@ public boolean mouseWheelMove(double mouseX, double mouseY, double wheelDelta) {
367382
7, y, 160, 25,
368383
() -> Wltmp,
369384
v -> { Wltmp = v; },
370-
Component.literal("Whitelist tags..."));
385+
Component.literal("Whitelist tags..."),
386+
() -> whitelistBadSyntax ? 0xFFFF0000 : null);
371387
group.addWidget(WLField);
372388

373389
y += 29;
374390
var BLField = new MultilineTextField(
375391
7, y, 160, 25,
376392
() -> Bltmp,
377393
v -> { Bltmp = v; },
378-
Component.literal("Blacklist tags..."));
394+
Component.literal("Blacklist tags..."),
395+
() -> blacklistBadSyntax ? 0xFFFF0000 : null);
379396
group.addWidget(BLField);
380397

381398
WLField.setDirectly(whitelistExpr);

src/main/java/net/neganote/gtutilities/integration/ae2/machine/METagStockingInputBusPartMachine.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public class METagStockingInputBusPartMachine extends MEStockingBusPartMachine {
7777
@DescSynced
7878
protected String Bltmp = "";
7979

80+
@DescSynced
81+
protected boolean whitelistBadSyntax = false;
82+
83+
@DescSynced
84+
protected boolean blacklistBadSyntax = false;
85+
8086
private Predicate<GenericStack> tagAutoPullTest = ($) -> true;
8187

8288
private transient String wlLast = null;
@@ -159,11 +165,13 @@ private void ensureCompiledUpToDate() {
159165
if (!Objects.equals(wl, wlLast)) {
160166
wlLast = wl;
161167
wlCompiled = TagMatcher.compile(wl);
168+
whitelistBadSyntax = !wlCompiled.isValid();
162169
decisionCache.clear();
163170
}
164171
if (!Objects.equals(bl, blLast)) {
165172
blLast = bl;
166173
blCompiled = TagMatcher.compile(bl);
174+
blacklistBadSyntax = !blCompiled.isValid();
167175
decisionCache.clear();
168176
}
169177

@@ -174,6 +182,7 @@ private void ensureCompiledUpToDate() {
174182

175183
protected boolean isAllowed(AEItemKey key) {
176184
ensureCompiledUpToDate();
185+
if (whitelistBadSyntax || blacklistBadSyntax) return false;
177186

178187
if ((wlLast == null || wlLast.isEmpty()) && (blLast == null || blLast.isEmpty())) return false;
179188

@@ -283,7 +292,8 @@ public Widget createUIWidget() {
283292
v -> {
284293
Wltmp = v;
285294
},
286-
Component.literal("Whitelist tags..."));
295+
Component.literal("Whitelist tags..."),
296+
() -> whitelistBadSyntax ? 0xFFFF0000 : null);
287297
group.addWidget(WLField);
288298

289299
y += 29;
@@ -293,7 +303,8 @@ public Widget createUIWidget() {
293303
v -> {
294304
Bltmp = v;
295305
},
296-
Component.literal("Blacklist tags..."));
306+
Component.literal("Blacklist tags..."),
307+
() -> blacklistBadSyntax ? 0xFFFF0000 : null);
297308
group.addWidget(BLField);
298309

299310
WLField.setDirectly(whitelistExpr);

src/main/java/net/neganote/gtutilities/integration/ae2/machine/METagStockingInputHatchPartMachine.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import com.gregtechceu.gtceu.integration.ae2.gui.widget.AEFluidConfigWidget;
1414
import com.gregtechceu.gtceu.integration.ae2.machine.MEHatchPartMachine;
1515
import com.gregtechceu.gtceu.integration.ae2.machine.MEStockingHatchPartMachine;
16-
import com.gregtechceu.gtceu.integration.ae2.slot.*;
16+
import com.gregtechceu.gtceu.integration.ae2.slot.ExportOnlyAEFluidList;
17+
import com.gregtechceu.gtceu.integration.ae2.slot.ExportOnlyAEFluidSlot;
18+
import com.gregtechceu.gtceu.integration.ae2.slot.ExportOnlyAESlot;
1719
import com.gregtechceu.gtceu.integration.ae2.utils.AEUtil;
1820

1921
import com.lowdragmc.lowdraglib.gui.texture.GuiTextureGroup;
@@ -75,6 +77,12 @@ public class METagStockingInputHatchPartMachine extends MEStockingHatchPartMachi
7577
@DescSynced
7678
protected String Bltmp = "";
7779

80+
@DescSynced
81+
protected boolean whitelistBadSyntax = false;
82+
83+
@DescSynced
84+
protected boolean blacklistBadSyntax = false;
85+
7886
private Predicate<GenericStack> tagAutoPullTest = ($) -> true;
7987

8088
private transient String wlLast = null;
@@ -157,11 +165,13 @@ private void ensureCompiledUpToDate() {
157165
if (!Objects.equals(wl, wlLast)) {
158166
wlLast = wl;
159167
wlCompiled = TagMatcher.compile(wl);
168+
whitelistBadSyntax = !wlCompiled.isValid();
160169
decisionCache.clear();
161170
}
162171
if (!Objects.equals(bl, blLast)) {
163172
blLast = bl;
164173
blCompiled = TagMatcher.compile(bl);
174+
blacklistBadSyntax = !wlCompiled.isValid();
165175
decisionCache.clear();
166176
}
167177

@@ -172,6 +182,7 @@ private void ensureCompiledUpToDate() {
172182

173183
protected boolean isAllowed(AEFluidKey key) {
174184
ensureCompiledUpToDate();
185+
if (whitelistBadSyntax || blacklistBadSyntax) return false;
175186

176187
if ((wlLast == null || wlLast.isEmpty()) && (blLast == null || blLast.isEmpty())) return false;
177188

@@ -282,7 +293,8 @@ public Widget createUIWidget() {
282293
v -> {
283294
Wltmp = v;
284295
},
285-
Component.literal("Whitelist tags..."));
296+
Component.literal("Whitelist tags..."),
297+
() -> whitelistBadSyntax ? 0xFFFF0000 : null);
286298
group.addWidget(WLField);
287299

288300
y += 29;
@@ -292,7 +304,8 @@ public Widget createUIWidget() {
292304
v -> {
293305
Bltmp = v;
294306
},
295-
Component.literal("Blacklist tags..."));
307+
Component.literal("Blacklist tags..."),
308+
() -> blacklistBadSyntax ? 0xFFFF0000 : null);
296309
group.addWidget(BLField);
297310

298311
WLField.setDirectly(whitelistExpr);

0 commit comments

Comments
 (0)