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

Add math teacher helper #1041

Merged
merged 5 commits into from
Nov 1, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package de.hysky.skyblocker.skyblock.events.greatspook;

import de.hysky.skyblocker.annotations.Init;
import de.hysky.skyblocker.utils.Calculator;
import de.hysky.skyblocker.utils.Constants;
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.PlainTextContent;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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>
*/
public final class MathTeacherHelper {
private static final Logger LOGGER = LoggerFactory.getLogger(MathTeacherHelper.class);

@Init
public static void init() {
ClientReceiveMessageEvents.MODIFY_GAME.register(MathTeacherHelper::onMessage);
}

/**
* Appends the result of the math expression to the message and a send in chat text that, well, sends the result in chat.
*/
public static Text onMessage(Text message, boolean overlay) {
if (overlay) return message;
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.clickToSend")
.formatted(Formatting.GREEN)
.styled(style ->
style.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/ac " + result))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Constants.PREFIX.get().append(Text.translatable("text.skyblocker.clickToSend.@Tooltip"))))
));
} catch (Exception e) {
LOGGER.error("[Skyblocker Math Teacher Helper] Failed to calculate math expression: {}", expression, e);
return message;
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/assets/skyblocker/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"text.skyblocker.translate": "Translate",
"text.skyblocker.modrinth": "Modrinth",
"text.skyblocker.discord": "Discord",
"text.skyblocker.clickToSend": "[Click To Send In Chat]",
"text.skyblocker.clickToSend.@Tooltip": "Click to send this in chat.",

"skyblocker.skyblockerScreen": "Skyblocker Main Screen",

Expand Down