diff options
| author | Roman / Nea <roman.graef@gmail.com> | 2022-07-07 18:07:08 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-07 18:07:08 +0200 |
| commit | a2ad09ee13cee2ef70268336b9df41368574f45a (patch) | |
| tree | e813f2efc671941b8fe57c9b880aa5a2abb49c47 /src/main/java/io/github/moulberry/notenoughupdates/miscgui/SignCalculator.java | |
| parent | 1ed053c9a8bbbbbe9b34c61fcdcc1a43d78118fd (diff) | |
| download | notenoughupdates-a2ad09ee13cee2ef70268336b9df41368574f45a.tar.gz notenoughupdates-a2ad09ee13cee2ef70268336b9df41368574f45a.tar.bz2 notenoughupdates-a2ad09ee13cee2ef70268336b9df41368574f45a.zip | |
Added a calculator (#180)
* Added a calculator
* Remove stuff
* also add t
* Update Misc.java
* On by default
* add x for multiplication, fixed - not working and quick math solver
Co-authored-by: nopo <noahogno@gmail.com>
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/miscgui/SignCalculator.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/miscgui/SignCalculator.java | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/SignCalculator.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/SignCalculator.java new file mode 100644 index 00000000..115458c9 --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/SignCalculator.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2022 NotEnoughUpdates contributors + * + * This file is part of NotEnoughUpdates. + * + * NotEnoughUpdates is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * NotEnoughUpdates is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>. + */ + +package io.github.moulberry.notenoughupdates.miscgui; + +import io.github.moulberry.notenoughupdates.NotEnoughUpdates; +import io.github.moulberry.notenoughupdates.events.SignSubmitEvent; +import io.github.moulberry.notenoughupdates.mixins.AccessorGuiEditSign; +import io.github.moulberry.notenoughupdates.util.Calculator; +import io.github.moulberry.notenoughupdates.util.Utils; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.inventory.GuiEditSign; +import net.minecraft.tileentity.TileEntitySign; +import net.minecraft.util.EnumChatFormatting; +import net.minecraftforge.client.event.GuiScreenEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +import java.math.BigDecimal; +import java.util.Objects; + +public class SignCalculator { + + String lastSource = null; + BigDecimal lastResult = null; + Calculator.CalculatorException lastException = null; + + private boolean isEnabled() { + return NotEnoughUpdates.INSTANCE.config.misc.calculationMode != 0; + } + + @SubscribeEvent + public void onSignDrawn(GuiScreenEvent.DrawScreenEvent.Post event) { + if (!(event.gui instanceof GuiEditSign)) + return; + if (!isEnabled()) return; + GuiEditSign guiEditSign = (GuiEditSign) event.gui; + TileEntitySign tileSign = ((AccessorGuiEditSign) guiEditSign).getTileSign(); + if (!tileSign.signText[1].getUnformattedText().equals("^^^^^^^^^^^^^^^")) return; + refresh(tileSign.signText[0].getUnformattedText()); + Utils.drawStringCentered( + getRenderedString(), + Minecraft.getMinecraft().fontRendererObj, + guiEditSign.width / 2F, + guiEditSign.height / 4F - 120, + false, + 0x808080FF + ); + } + + @SubscribeEvent + public void onSignSubmitted(SignSubmitEvent event) { + if (!isEnabled()) return; + if (Objects.equals(event.lines[1], "^^^^^^^^^^^^^^^")) { + refresh(event.lines[0]); + if (lastResult != null) { + event.lines[0] = lastResult.toPlainString(); + } + } + } + + public String getRenderedString() { + if (lastResult != null) { + String lr = lastResult.toPlainString(); + if (Minecraft.getMinecraft().fontRendererObj.getStringWidth(lr) > 90) { + return EnumChatFormatting.WHITE + lastSource + " = " + EnumChatFormatting.RED + "Result too long"; + } + return EnumChatFormatting.WHITE + lastSource + " = " + EnumChatFormatting.GREEN + lr; + } else if (lastException != null) { + return EnumChatFormatting.RED + lastException.getMessage(); + } + return EnumChatFormatting.RED + "No calculation has been done."; + } + + private void refresh(String source) { + if (Objects.equals(source, lastSource)) return; + lastSource = source; + int calculationMode = NotEnoughUpdates.INSTANCE.config.misc.calculationMode; + if (source.isEmpty() || calculationMode == 0 || (calculationMode == 1 && !source.startsWith("!"))) { + lastResult = null; + lastException = null; + return; + } + try { + lastResult = Calculator.calculate(calculationMode == 1 ? source.substring(1) : source); + lastException = null; + } catch (Calculator.CalculatorException ex) { + lastException = ex; + lastResult = null; + } + } +} |
