Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sign calculator #686

Merged
merged 20 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/de/hysky/skyblocker/SkyblockerMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.debug.Debug;
import de.hysky.skyblocker.skyblock.*;
import de.hysky.skyblocker.skyblock.calculators.CalculatorCommand;
import de.hysky.skyblocker.skyblock.chat.ChatRuleAnnouncementScreen;
import de.hysky.skyblocker.skyblock.chat.ChatRulesHandler;
import de.hysky.skyblocker.skyblock.crimson.kuudra.Kuudra;
Expand Down Expand Up @@ -132,6 +133,7 @@ public void onInitializeClient() {
Shortcuts.init();
ChatRulesHandler.init();
ChatRuleAnnouncementScreen.init();
CalculatorCommand.init();
DiscordRPCManager.init();
LividColor.init();
FishingHelper.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,28 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.build())
.build())

//Input Calculator
.group(OptionGroup.createBuilder()
.name(Text.translatable("skyblocker.config.uiAndVisuals.inputCalculator"))
.collapsed(true)
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.uiAndVisuals.inputCalculator.enabled"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.uiAndVisuals.inputCalculator.enabled.@Tooltip")))
.binding(defaults.uiAndVisuals.inputCalculator.enabled,
() -> config.uiAndVisuals.inputCalculator.enabled,
newValue -> config.uiAndVisuals.inputCalculator.enabled = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.uiAndVisuals.inputCalculator.requiresEquals"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.uiAndVisuals.inputCalculator.requiresEquals.@Tooltip")))
.binding(defaults.uiAndVisuals.inputCalculator.requiresEquals,
() -> config.uiAndVisuals.inputCalculator.requiresEquals,
newValue -> config.uiAndVisuals.inputCalculator.requiresEquals = newValue)
.controller(ConfigUtils::createBooleanController)
.build())
.build())

//Flame Overlay
.group(OptionGroup.createBuilder()
.name(Text.translatable("skyblocker.config.uiAndVisuals.flameOverlay"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class UIAndVisualsConfig {
@SerialEntry
public SearchOverlay searchOverlay = new SearchOverlay();

@SerialEntry
public InputCalculator inputCalculator = new InputCalculator();

@SerialEntry
public FlameOverlay flameOverlay = new FlameOverlay();

Expand Down Expand Up @@ -239,6 +242,14 @@ public static class SearchOverlay {
public List<String> auctionHistory = new ArrayList<>();
}

public static class InputCalculator {
@SerialEntry
public boolean enabled = true;

@SerialEntry
public boolean requiresEquals = false;
}

public static class FlameOverlay {
@SerialEntry
public int flameHeight = 100;
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/de/hysky/skyblocker/mixins/SignEditScreenMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package de.hysky.skyblocker.mixins;


import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.skyblock.calculators.SignCalculator;
import de.hysky.skyblocker.utils.Utils;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.AbstractSignEditScreen;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Objects;

@Mixin(AbstractSignEditScreen.class)
public abstract class SignEditScreenMixin {
@Shadow
@Final
private String[] messages;

@Inject(method = "render", at = @At("HEAD"))
private void skyblocker$render(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
//if the sign is being used to enter number send it to the sign calculator
if (Utils.isOnSkyblock() && SkyblockerConfigManager.get().uiAndVisuals.inputCalculator.enabled && Objects.equals(messages[1], "^^^^^^^^^^^^^^^")) {
SignCalculator.renderCalculator(context, messages[0], context.getScaledWindowWidth() / 2, 55);
}
}

@Inject(method = "finishEditing", at = @At("HEAD"))
private void skyblocker$finishEditing(CallbackInfo ci) {
//if the sign is being used to enter number get number from calculator for if maths has been done
if (Utils.isOnSkyblock() && SkyblockerConfigManager.get().uiAndVisuals.inputCalculator.enabled && Objects.equals(messages[1], "^^^^^^^^^^^^^^^")) {
boolean isPrice = messages[2].contains("price");
String value = SignCalculator.getNewValue(isPrice);
if (value.length() >= 15) {
value = value.substring(0, 15);
}
messages[0] = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.hysky.skyblocker.skyblock.auction;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.skyblock.calculators.SignCalculator;
import de.hysky.skyblocker.utils.render.gui.AbstractPopupScreen;
import net.minecraft.block.entity.SignBlockEntity;
import net.minecraft.client.MinecraftClient;
Expand All @@ -9,7 +11,6 @@
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;

public class EditBidPopup extends AbstractPopupScreen {
private DirectionalLayoutWidget layout = DirectionalLayoutWidget.vertical();
Expand Down Expand Up @@ -55,6 +56,9 @@ protected void init() {
public void renderBackground(DrawContext context, int mouseX, int mouseY, float delta) {
super.renderBackground(context, mouseX, mouseY, delta);
drawPopupBackground(context, layout.getX(), layout.getY(), layout.getWidth(), layout.getHeight());
if (SkyblockerConfigManager.get().uiAndVisuals.inputCalculator.enabled) {
SignCalculator.renderCalculator(context, textFieldWidget.getText(), context.getScaledWindowWidth() / 2, textFieldWidget.getY() - 8);
}
}

private boolean isStringGood(String s) {
Expand All @@ -69,8 +73,13 @@ private void buttonMinimumBid(ButtonWidget widget) {
}

private void done(ButtonWidget widget) {
if (!isStringGood(textFieldWidget.getText().trim())) return;
sendPacket(textFieldWidget.getText().trim());
if (SkyblockerConfigManager.get().uiAndVisuals.inputCalculator.enabled) {
if (!isStringGood(SignCalculator.getNewValue(false))) return;
sendPacket(SignCalculator.getNewValue(false));
} else {
if (!isStringGood(textFieldWidget.getText().trim())) return;
sendPacket(textFieldWidget.getText().trim());
}
this.close();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.NumberFormat;

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 NumberFormat FORMATTER = NumberFormat.getInstance();

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.translatable("skyblocker.config.uiAndVisuals.inputCalculator.invalidEquation").formatted(Formatting.RED));
}

if (CLIENT == null || CLIENT.player == null) {
return 0;
}

CLIENT.player.sendMessage(text, false);
return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package de.hysky.skyblocker.skyblock.calculators;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
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.NumberFormat;

public class SignCalculator {
private static final MinecraftClient CLIENT = MinecraftClient.getInstance();
private static final NumberFormat FORMATTER = NumberFormat.getInstance();

private static String lastInput;
private static double output;

public static void renderCalculator(DrawContext context, String message, int renderX, int renderY) {
if (SkyblockerConfigManager.get().uiAndVisuals.inputCalculator.requiresEquals && !message.startsWith("=")) {
output = -1;
lastInput = message;
return;
}
if (message.startsWith("=")) {
message = message.substring(1);
}
//only update output if new input
if (!message.equals(lastInput)) { //
try {
output = Calculator.calculate(message);
} catch (Exception e) {
output = -1;
}
}

render(context, message, renderX, renderY);

lastInput = message;
}

public static String getNewValue(Boolean isPrice) {
if (output == -1) {
//if mode is not activated or just invalid equation return what the user typed in
return lastInput;
}

//price can except decimals and exponents
if (isPrice) {
return FORMATTER.format(output);
}
//amounts want an integer number so round
return Long.toString(Math.round(output));
}

private static void render(DrawContext context, String input, int renderX, int renderY) {
Text text;
if (output == -1) {
text = Text.translatable("skyblocker.config.uiAndVisuals.inputCalculator.invalidEquation").formatted(Formatting.RED);
} else {
text = Text.literal(input + " = " + FORMATTER.format(output)).formatted(Formatting.GREEN);
}

context.drawCenteredTextWithShadow(CLIENT.textRenderer, text, renderX, renderY, 0xFFFFFFFF);
}
}
Loading