Skip to content

Commit

Permalink
Change from regex to sibling checking and try catch everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Emirlol committed Nov 1, 2024
1 parent 5b38c7e commit 9a9b3f8
Showing 1 changed file with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <p>A helper class for the Math Teachers that can spawn after killing Primal Fears in the Great Spook event.</p>
* <p>It only shows the result and allows for easily copying rather than sending the result to chat to not breach any hypixel rules.</p>
*/
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);
Expand All @@ -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<Text> 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;
}
}
}

0 comments on commit 9a9b3f8

Please sign in to comment.