diff options
| author | olim <bobq4582@gmail.com> | 2024-04-26 18:01:09 +0100 |
|---|---|---|
| committer | olim <bobq4582@gmail.com> | 2024-05-09 22:42:35 +0100 |
| commit | feee94e2c2ce01262f3eb47d7b356ca9821bdb79 (patch) | |
| tree | f8ad3cec616891250d445b2cf02f4dbc5504466b /src/main/java/de/hysky/skyblocker/skyblock/calculators | |
| parent | a442eefc1d47fdc66d361a6dc35a9412521626f8 (diff) | |
| download | Skyblocker-feee94e2c2ce01262f3eb47d7b356ca9821bdb79.tar.gz Skyblocker-feee94e2c2ce01262f3eb47d7b356ca9821bdb79.tar.bz2 Skyblocker-feee94e2c2ce01262f3eb47d7b356ca9821bdb79.zip | |
add command
add command to be able to use the calculator and fix crash with emty equations
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/skyblock/calculators')
| -rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/calculators/CalculatorCommand.java | 57 | ||||
| -rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/calculators/SignCalculator.java | 62 |
2 files changed, 119 insertions, 0 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/calculators/CalculatorCommand.java b/src/main/java/de/hysky/skyblocker/skyblock/calculators/CalculatorCommand.java new file mode 100644 index 00000000..f9b8b4cf --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/calculators/CalculatorCommand.java @@ -0,0 +1,57 @@ +package de.hysky.skyblocker.skyblock.calculators; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import de.hysky.skyblocker.SkyblockerMod; +import de.hysky.skyblocker.utils.Calculator; +import de.hysky.skyblocker.utils.Constants; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; + +import java.text.DecimalFormat; + +import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +public class CalculatorCommand { + private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); + + private static final DecimalFormat FORMATTER = new DecimalFormat("#,###.##"); + + public static void init() { + ClientCommandRegistrationCallback.EVENT.register(CalculatorCommand::calculate); + } + + private static void calculate(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { + dispatcher.register(literal(SkyblockerMod.NAMESPACE) + .then(literal("calculate") + .then(argument("equation", StringArgumentType.greedyString()) + .executes(context -> doCalculation(getString(context, "equation")) ) + ) + ) + ); + } + + private static int doCalculation(String calculation) { + MutableText text = Constants.PREFIX.get(); + try { + text.append(Text.literal(FORMATTER.format(Calculator.calculate(calculation))).formatted(Formatting.GREEN)); + } catch (UnsupportedOperationException e) { + text.append(Text.literal("text.autoconfig.skyblocker.option.general.enableSignCalculator.invalidEquation").formatted(Formatting.RED)); + } + + if (CLIENT == null || CLIENT.player == null) { + return 0; + } + + CLIENT.player.sendMessage(text, false); + return Command.SINGLE_SUCCESS; + } +} diff --git a/src/main/java/de/hysky/skyblocker/skyblock/calculators/SignCalculator.java b/src/main/java/de/hysky/skyblocker/skyblock/calculators/SignCalculator.java new file mode 100644 index 00000000..7dbeabd9 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/calculators/SignCalculator.java @@ -0,0 +1,62 @@ +package de.hysky.skyblocker.skyblock.calculators; + +import de.hysky.skyblocker.utils.Calculator; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; + +import java.text.DecimalFormat; + +public class SignCalculator { + + private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); + + private static final DecimalFormat FORMATTER = new DecimalFormat("#,###.##"); + + private static String lastInput; + private static String input; + private static Double output; + + public static void renderSign(DrawContext context, String[] messages){ + input = messages[0]; + + + //only update output if new input + if (!input.equals(lastInput)) { // + try { + output = Calculator.calculate(input); + } catch (Exception e){ + output = null; + } + } + + render(context); + + lastInput = input; + } + + public static String getNewValue(Boolean isPrice) { + if (output == null) { + return ""; + } + //price can except decimals and exponents + if (isPrice) { + return output.toString(); + } + //amounts want an integer number so round + return Long.toString(Math.round(output)); + } + + private static void render(DrawContext context) { + Text text; + if (output == null) { + text = Text.translatable("text.autoconfig.skyblocker.option.general.enableSignCalculator.invalidEquation").formatted(Formatting.RED); + } + else { + text = Text.literal(input +" = " + FORMATTER.format(output)).formatted(Formatting.GREEN); + } + + context.drawCenteredTextWithShadow(CLIENT.textRenderer, text,context.getScaledWindowWidth() /2 , 55,0xFFFFFFFF); + } +} |
