Skip to content

Commit

Permalink
Use MathExpressionParser from GTNHLib. (#202)
Browse files Browse the repository at this point in the history
* Use MathExpressionParser from GTNHLib.

* Defer to AE2 implementation + cache GTNHLib loaded.

* Reuse pattern.

* spotless
  • Loading branch information
AbdielKavash authored Mar 24, 2024
1 parent 958393a commit f175d70
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {

implementation("com.github.GTNewHorizons:WirelessCraftingTerminal:1.11.1:dev")

compileOnly("com.github.GTNewHorizons:GTNHLib:0.2.10:dev") { transitive = false }
compileOnly('com.github.GTNewHorizons:Baubles:1.0.4:dev')
compileOnly('com.github.GTNewHorizons:ExtraCells2:2.5.34:dev') { transitive = false }
compileOnly('com.github.GTNewHorizons:ForestryMC:4.8.7:dev')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
Expand Down Expand Up @@ -40,6 +41,7 @@
import com.glodblock.github.util.FCGuiColors;
import com.glodblock.github.util.NameConst;
import com.glodblock.github.util.Util;
import com.gtnewhorizon.gtnhlib.util.parsing.MathExpressionParser;

import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.DimensionalCoord;
Expand All @@ -56,6 +58,7 @@
import codechicken.nei.api.INEIGuiHandler;
import codechicken.nei.api.TaggedInventoryArea;
import cofh.core.render.CoFHFontRenderer;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.Optional;

@Optional.Interface(modid = "NotEnoughItems", iface = "codechicken.nei.api.INEIGuiHandler")
Expand All @@ -75,6 +78,10 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
protected Util.DimensionalCoordSide originalBlockPos;
protected GuiTabButton originalGuiBtn;

protected static final boolean isGTNHLibLoaded = Loader.isModLoaded("gtnhlib");
protected static final Pattern numberPattern = isGTNHLibLoaded ? MathExpressionParser.EXPRESSION_PATTERN
: Pattern.compile("^[0-9]+");

public GuiLevelMaintainer(InventoryPlayer ipl, TileLevelMaintainer tile) {
super(new ContainerLevelMaintainer(ipl, tile));
this.cont = (ContainerLevelMaintainer) inventorySlots;
Expand Down Expand Up @@ -278,7 +285,7 @@ protected void keyTyped(final char character, final int key) {
this.input.textboxKeyTyped(character, key);
}
super.keyTyped(character, key);
if (!this.input.getText().matches("^[0-9]+")) {
if (!numberPattern.matcher(this.input.getText()).matches()) {
this.input.setTextColor(0xFF0000);
} else {
this.input.setTextColor(0xFFFFFF);
Expand Down Expand Up @@ -411,7 +418,12 @@ public void setEnable(boolean enable) {

private boolean send(Widget widget) {
if (((SlotFluidConvertingFake) cont.inventorySlots.get(widget.idx)).getHasStack()) {
if (!widget.textField.getText().isEmpty() && widget.textField.getText().matches("^[0-9]+")) {
if (isGTNHLibLoaded) {
long value = (long) MathExpressionParser.parse(widget.textField.getText());
widget.textField.setText(String.valueOf(value));
}
if (!widget.textField.getText().isEmpty()
&& numberPattern.matcher(widget.textField.getText()).matches()) {
String str = widget.textField.getText().replaceAll("^(0+)", "");
widget.textField.setText(str.isEmpty() ? "0" : str);
FluidCraft.proxy.netHandler.sendToServer(
Expand Down

0 comments on commit f175d70

Please sign in to comment.