-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #686 from olim88/sign-calculator
Sign calculator
- Loading branch information
Showing
11 changed files
with
460 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/de/hysky/skyblocker/mixins/SignEditScreenMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/de/hysky/skyblocker/skyblock/calculators/CalculatorCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/de/hysky/skyblocker/skyblock/calculators/SignCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.