From 100289a077277f8860e19125cede8414757fbb61 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Sun, 7 Aug 2022 21:58:18 +0200 Subject: Calculations (#204) * add support for calculations in neu searchbar * moved calculation in search bar into own class * saving one line out of 3k lines * brackets change everything --- .../moulberry/notenoughupdates/NEUOverlay.java | 3 +- .../commands/misc/CalculatorCommand.java | 16 +++---- .../notenoughupdates/listener/ChatListener.java | 10 ++-- .../miscgui/NeuSearchCalculator.java | 55 ++++++++++++++++++++++ .../notenoughupdates/miscgui/SignCalculator.java | 18 +++++-- .../options/seperateSections/Misc.java | 4 +- 6 files changed, 86 insertions(+), 20 deletions(-) create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/miscgui/NeuSearchCalculator.java diff --git a/src/main/java/io/github/moulberry/notenoughupdates/NEUOverlay.java b/src/main/java/io/github/moulberry/notenoughupdates/NEUOverlay.java index b54dfee9..c04e0389 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/NEUOverlay.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/NEUOverlay.java @@ -40,6 +40,7 @@ import io.github.moulberry.notenoughupdates.miscfeatures.EnchantingSolvers; import io.github.moulberry.notenoughupdates.miscfeatures.PetInfoOverlay; import io.github.moulberry.notenoughupdates.miscfeatures.SunTzu; import io.github.moulberry.notenoughupdates.miscgui.GuiPriceGraph; +import io.github.moulberry.notenoughupdates.miscgui.NeuSearchCalculator; import io.github.moulberry.notenoughupdates.options.NEUConfigEditor; import io.github.moulberry.notenoughupdates.util.Constants; import io.github.moulberry.notenoughupdates.util.GuiTextures; @@ -378,7 +379,7 @@ public class NEUOverlay extends Gui { } //Search bar text - fr.drawString(textField.getText(), (int) x + 5, + fr.drawString(NeuSearchCalculator.format(textField.getText()), (int) x + 5, (int) y - 4 + getHeight() / 2, Color.WHITE.getRGB() ); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/commands/misc/CalculatorCommand.java b/src/main/java/io/github/moulberry/notenoughupdates/commands/misc/CalculatorCommand.java index e6472d04..916ce072 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/commands/misc/CalculatorCommand.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/commands/misc/CalculatorCommand.java @@ -47,20 +47,20 @@ public class CalculatorCommand extends ClientCommandBase { if ((args.length == 1 && Objects.equals(args[0], "help")) || args.length == 0) { sender.addChatMessage(new ChatComponentText( "\n§e[NEU] §5It's a calculator.\n" + - "§eFor Example §b/neucalc 3m*7k§e.\n" + - "§eYou can also use suffixes (k, m, b, t, s)§e.\n" + - "§eThe \"s\" suffix acts as 64.\n" + - "§eTurn on Sign Calculator in /neu misc to also support this in sign popups.\n")); + "§eFor Example §b/neucalc 3m*7k§e.\n" + + "§eYou can also use suffixes (k, m, b, t, s)§e.\n" + + "§eThe \"s\" suffix acts as 64.\n" + + "§eTurn on Sign Calculator in /neu misc to also support this in sign popups.\n")); return; } String source = String.join(" ", args); try { BigDecimal calculate = Calculator.calculate(source); - DecimalFormat formatter = new DecimalFormat("#,###.00"); - String lr = formatter.format(calculate); + DecimalFormat formatter = new DecimalFormat("#,##0.##"); + String format = formatter.format(calculate); sender.addChatMessage(new ChatComponentText( - EnumChatFormatting.YELLOW + "[NEU] " + EnumChatFormatting.WHITE + source + " = " + EnumChatFormatting.GREEN + - lr + EnumChatFormatting.YELLOW + "[NEU] " + EnumChatFormatting.WHITE + source + " " + EnumChatFormatting.YELLOW + + "= " + EnumChatFormatting.GREEN + format )); } catch (Calculator.CalculatorException e) { sender.addChatMessage(new ChatComponentText( diff --git a/src/main/java/io/github/moulberry/notenoughupdates/listener/ChatListener.java b/src/main/java/io/github/moulberry/notenoughupdates/listener/ChatListener.java index 457042ee..5bd47f3a 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/listener/ChatListener.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/listener/ChatListener.java @@ -216,10 +216,12 @@ public class ChatListener { CookieWarning.resetNotification(); } else if (unformatted.startsWith("QUICK MATHS! Solve:")) { if (Math.random() < 0.2) { - ClientCommandHandler.instance.executeCommand( - Minecraft.getMinecraft().thePlayer, - "/neucalc " + unformatted.substring("QUICK MATHS! Solve: ".length()) - ); + if (NotEnoughUpdates.INSTANCE.config.misc.calculationMode == 2) { + ClientCommandHandler.instance.executeCommand( + Minecraft.getMinecraft().thePlayer, + "/neucalc " + unformatted.substring("QUICK MATHS! Solve: ".length()) + ); + } } } if (e.message.getFormattedText().contains( diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/NeuSearchCalculator.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/NeuSearchCalculator.java new file mode 100644 index 00000000..f7306f2a --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/NeuSearchCalculator.java @@ -0,0 +1,55 @@ +/* + * 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 . + */ + +package io.github.moulberry.notenoughupdates.miscgui; + +import io.github.moulberry.notenoughupdates.NotEnoughUpdates; +import io.github.moulberry.notenoughupdates.util.Calculator; + +import java.math.BigDecimal; +import java.text.DecimalFormat; + +public class NeuSearchCalculator { + + static String lastInput = ""; + static String lastResult = null; + + public static String format(String text) { + String calculate = calculateInSearchBar(text); + return text + (calculate != null ? " §e= §a" + calculate : ""); + } + + private static String calculateInSearchBar(String input) { + int calculationMode = NotEnoughUpdates.INSTANCE.config.misc.calculationMode; + if (input.isEmpty() || calculationMode == 0 || (calculationMode == 1 && !input.startsWith("!"))) return null; + input = calculationMode == 1 ? input.substring(1) : input; + + if (!lastInput.equals(input)) { + lastInput = input; + try { + BigDecimal calculate = Calculator.calculate(input); + lastResult = new DecimalFormat("#,##0.##").format(calculate); + } catch (Calculator.CalculatorException ignored) { + lastResult = null; + } + } + + return lastResult; + } +} diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/SignCalculator.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/SignCalculator.java index 51e1d15a..ac676a98 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/SignCalculator.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/SignCalculator.java @@ -52,8 +52,14 @@ public class SignCalculator { if (!isEnabled()) return; GuiEditSign guiEditSign = (GuiEditSign) event.gui; TileEntitySign tileSign = ((AccessorGuiEditSign) guiEditSign).getTileSign(); - if (!tileSign.signText[1].getUnformattedText().equals("^^^^^^^^^^^^^^^") && !tileSign.signText[1].getUnformattedText().equals("^^^^^^")) return; - refresh(tileSign.signText[0].getUnformattedText()); + if (!tileSign.signText[1].getUnformattedText().equals("^^^^^^^^^^^^^^^") && + !tileSign.signText[1].getUnformattedText().equals("^^^^^^")) return; + String source = tileSign.signText[0].getUnformattedText(); + refresh(source); + + int calculationMode = NotEnoughUpdates.INSTANCE.config.misc.calculationMode; + if ((calculationMode == 1 && !source.startsWith("!"))) return; + Utils.drawStringCentered( getRenderedString(), Minecraft.getMinecraft().fontRendererObj, @@ -77,12 +83,14 @@ public class SignCalculator { public String getRenderedString() { if (lastResult != null) { - DecimalFormat formatter = new DecimalFormat("#,###.00"); + DecimalFormat formatter = new DecimalFormat("#,##0.##"); String lr = formatter.format(lastResult); if (Minecraft.getMinecraft().fontRendererObj.getStringWidth(lr) > 90) { - return EnumChatFormatting.WHITE + lastSource + " = " + EnumChatFormatting.RED + "Result too long"; + return EnumChatFormatting.WHITE + lastSource + " " + EnumChatFormatting.YELLOW + "= " + EnumChatFormatting.RED + + "Result too long"; } - return EnumChatFormatting.WHITE + lastSource + " = " + EnumChatFormatting.GREEN + lr; + return EnumChatFormatting.WHITE + lastSource + " " + EnumChatFormatting.YELLOW + "= " + EnumChatFormatting.GREEN + + lr; } else if (lastException != null) { return EnumChatFormatting.RED + lastException.getMessage(); } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/Misc.java b/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/Misc.java index c0033cde..d87082b4 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/Misc.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/Misc.java @@ -217,8 +217,8 @@ public class Misc { @Expose @ConfigOption( - name = "Sign Calculator", - desc = "§7Replace calculations like §9\"1+2\"§7 with the calculation result in sign popups (AH/BZ)" + name = "Calculator", + desc = "Replace calculations like §9\"1+2\"§7 with the calculation result in sign popups (AH/BZ) and in the neu search bar" ) @ConfigEditorDropdown(values = {"Off", "Enabled with ! Prefix", "Always enabled"}) public int calculationMode = 2; -- cgit