Skip to content

Confirmation Prompt Helper 2.0 #1245

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -1,5 +1,6 @@
package de.hysky.skyblocker.skyblock.chat;

import java.util.List;
import java.util.Optional;

import de.hysky.skyblocker.annotations.Init;
Expand All @@ -16,19 +17,45 @@
import net.minecraft.text.ClickEvent;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfirmationPromptHelper {
public static final Logger LOGGER = LoggerFactory.getLogger(ConfirmationPromptHelper.class);
private static final List<String> CONFIRMATION_PHRASES = List.of(
"[Aye sure do!]", // [NPC] Carnival Pirateman
"[You guessed it!]", // [NPC] Carnival Fisherman
"[Sure thing, partner!]", // [NPC] Carnival Cowboy
"YES",
"Yes");

// Put here full lines with formatting codes, excluding '\n' and spaces (those are trimmed)
// It can be extracted by logging asString or from JSON of chat message. Logs also contain it
private static final List<String> CONFIRMATION_PHRASES_FORMATTING = List.of(
"§e ➜ §a[Aye sure do!]", // [NPC] Carnival Pirateman
"§e ➜ §a[You guessed it!]", // [NPC] Carnival Fisherman
"§e ➜ §a[Sure thing, partner!]", // [NPC] Carnival Cowboy
"§a§l[YES]",
"§a[Yes]");

private static String command;
private static long commandFoundAt;

@Init
public static void init() {
ClientReceiveMessageEvents.GAME.register(ConfirmationPromptHelper::onMessage);
ScreenEvents.AFTER_INIT.register((_client, screen, _scaledWidth, _scaledHeight) -> {
//Don't check for the command being present in case the user opens the chat before the prompt is sent
if (Utils.isOnSkyblock() && screen instanceof ChatScreen && SkyblockerConfigManager.get().chat.confirmationPromptHelper) {
ScreenMouseEvents.beforeMouseClick(screen).register((_screen1, _mouseX, _mouseY, _button) -> {
ScreenMouseEvents.beforeMouseClick(screen).register((_screen1, mouseX, mouseY, button) -> {
if (hasCommand()) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.currentScreen instanceof ChatScreen) { // Ignore clicks on other interactive elements
Style style = client.inGameHud.getChatHud().getTextStyleAt(mouseX, mouseY);
if (style != null && style.getClickEvent() != null) { // clicking on some prompts invalidates first prompt but not in all cases, so I decided not to nullify command
return;
}
}

MessageScheduler.INSTANCE.sendMessageAfterCooldown(command, true);
command = null;
commandFoundAt = 0;
Expand All @@ -46,13 +73,23 @@ private static boolean hasCommand() {
return command != null && commandFoundAt + 60_000 > System.currentTimeMillis();
}

private static boolean containsConfirmationPhrase(Text message) {
String messageStr = message.getString();
for (String phrase : CONFIRMATION_PHRASES) {
if (messageStr.contains(phrase)) {
return true;
}
}
return false;
}

private static void onMessage(Text message, boolean overlay) {
if (Utils.isOnSkyblock() && !overlay && SkyblockerConfigManager.get().chat.confirmationPromptHelper && message.getString().contains("[YES]")) {
if (Utils.isOnSkyblock() && !overlay && SkyblockerConfigManager.get().chat.confirmationPromptHelper && containsConfirmationPhrase(message)) {
Optional<String> confirmationCommand = message.visit((style, asString) -> {
ClickEvent event = style.getClickEvent();
asString = asString.replaceAll("\\s+", " ").trim(); // clear newline '\n' and trim spaces

//Check to see if its a yes and has the proper command
if (asString.equals("§a§l[YES]") && event != null && event.getAction() == ClickEvent.Action.RUN_COMMAND && event.getValue().startsWith("/chatprompt")) {
if (CONFIRMATION_PHRASES_FORMATTING.contains(asString) && event != null && event.getAction() == ClickEvent.Action.RUN_COMMAND && (event.getValue().startsWith("/chatprompt") || event.getValue().startsWith("/selectnpcoption"))) {
return Optional.of(event.getValue());
}

Expand Down