From 9a9b3f8a68a3ba01ac4f9ac23981c959ff395aa5 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Fri, 1 Nov 2024 17:13:42 +0300 Subject: [PATCH] Change from regex to sibling checking and try catch everything --- .../events/greatspook/MathTeacherHelper.java | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/events/greatspook/MathTeacherHelper.java b/src/main/java/de/hysky/skyblocker/skyblock/events/greatspook/MathTeacherHelper.java index dad444ec50..b25267fccc 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/events/greatspook/MathTeacherHelper.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/events/greatspook/MathTeacherHelper.java @@ -7,20 +7,17 @@ import net.minecraft.screen.ScreenTexts; import net.minecraft.text.ClickEvent; import net.minecraft.text.HoverEvent; -import net.minecraft.text.MutableText; +import net.minecraft.text.PlainTextContent; import net.minecraft.text.Text; import net.minecraft.util.Formatting; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.List; /** *

A helper class for the Math Teachers that can spawn after killing Primal Fears in the Great Spook event.

*

It only shows the result and allows for easily copying rather than sending the result to chat to not breach any hypixel rules.

*/ public final class MathTeacherHelper { - private static final Pattern MATH_TEACHER_PATTERN = Pattern.compile("^QUICK MATHS! Solve: (.*)"); - @Init public static void init() { ClientReceiveMessageEvents.MODIFY_GAME.register(MathTeacherHelper::onMessage); @@ -31,19 +28,27 @@ public static void init() { */ public static Text onMessage(Text message, boolean overlay) { if (overlay) return message; - Matcher matcher = MATH_TEACHER_PATTERN.matcher(message.getString()); - if (!matcher.matches()) return message; - String expression = matcher.group(1).replace("x", "*"); // Hypixel uses x for multiplication while our calculator uses * - String result = "%.0f".formatted(Calculator.calculate(expression)); - return ((MutableText) message).append(" = ") - .append(Text.literal(result) - .formatted(Formatting.AQUA)) - .append(ScreenTexts.SPACE) - .append(Text.translatable("text.skyblocker.clickToCopy") - .formatted(Formatting.GREEN) - .styled(style -> - style.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, result)) - .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Constants.PREFIX.get().append(Text.translatable("text.skyblocker.clickToCopy.@Tooltip")))) - )); + List siblings = message.getSiblings(); + if (message.getContent() != PlainTextContent.EMPTY || siblings.size() != 3) return message; + if (!siblings.getFirst().getString().equals("QUICK MATHS! ")) return message; + + String expression = siblings.get(2).getString().replace('x', '*'); // Hypixel uses x for multiplication while our calculator uses * + try { + String result = "%.0f".formatted(Calculator.calculate(expression)); + + return message.copy() + .append(" = ") + .append(Text.literal(result) + .formatted(Formatting.AQUA)) + .append(ScreenTexts.SPACE) + .append(Text.translatable("text.skyblocker.clickToCopy") + .formatted(Formatting.GREEN) + .styled(style -> + style.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, result)) + .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Constants.PREFIX.get().append(Text.translatable("text.skyblocker.clickToCopy.@Tooltip")))) + )); + } catch (Exception e) { + return message; + } } }