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

Fix potential stack overflow with chat rules #592

Merged
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
Expand Up @@ -11,6 +11,6 @@ public interface BeaconBlockEntityRendererInvoker {
@SuppressWarnings("unused")
@Invoker("renderBeam")
static void renderBeam(MatrixStack matrices, VertexConsumerProvider vertexConsumers, float tickDelta, long worldTime, int yOffset, int maxY, float[] color) {
throw new IllegalStateException("Mixin invoker failed to apply.");
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.hysky.skyblocker.mixin.accessor;

import net.minecraft.client.network.message.MessageHandler;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

import java.time.Instant;

@Mixin(MessageHandler.class)
public interface MessageHandlerAccessor {
@Invoker
void invokeAddToChatLog(Text message, Instant timestamp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

@Mixin(PlayerListHud.class)
public interface PlayerListHudAccessor {

@Accessor("ENTRY_ORDERING")
static Comparator<PlayerListEntry> getOrdering() {
throw new AssertionError();
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public interface RecipeBookWidgetAccessor {
@Accessor
String getSearchText();

@Accessor
TextFieldWidget getSearchField();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.google.gson.JsonParser;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;

import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.mixin.accessor.MessageHandlerAccessor;
import de.hysky.skyblocker.utils.Http;
import de.hysky.skyblocker.utils.Utils;
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
Expand All @@ -21,10 +21,10 @@

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -53,16 +53,16 @@ public static void init() {
private static void loadChatRules() {
try (BufferedReader reader = Files.newBufferedReader(CHAT_RULE_FILE)) {
Map<String, List<ChatRule>> chatRules = MAP_CODEC.parse(JsonOps.INSTANCE, JsonParser.parseReader(reader)).result().orElseThrow();
LOGGER.info("[Sky: " + chatRules.toString());
LOGGER.info("[Skyblocker Chat Rules]: {}", chatRules);

chatRuleList.addAll(chatRules.get("rules"));

LOGGER.info("[Skyblocker] Loaded chat rules");
LOGGER.info("[Skyblocker Chat Rules] Loaded chat rules");
} catch (NoSuchFileException e) {
registerDefaultChatRules();
LOGGER.warn("[Skyblocker] chat rule file not found, using default rules. This is normal when using for the first time.");
LOGGER.warn("[Skyblocker Chat Rules] chat rules file not found, using default rules. This is normal when using for the first time.");
} catch (Exception e) {
LOGGER.error("[Skyblocker] Failed to load shortcuts file", e);
LOGGER.error("[Skyblocker Chat Rules] Failed to load chat rules file", e);
}
}

Expand Down Expand Up @@ -92,7 +92,7 @@ private static void loadLocations() {
locations.put(entry.getValue().getAsString().replace(" ", "").toLowerCase(), entry.getKey());
}
} catch (Exception e) {
LOGGER.error("[Skyblocker] Failed to load locations!", e);
LOGGER.error("[Skyblocker Chat Rules] Failed to load locations!", e);
}
}

Expand All @@ -101,9 +101,9 @@ protected static void saveChatRules() {
chatRuleJson.add("rules", ChatRule.LIST_CODEC.encodeStart(JsonOps.INSTANCE, chatRuleList).result().orElseThrow());
try (BufferedWriter writer = Files.newBufferedWriter(CHAT_RULE_FILE)) {
SkyblockerMod.GSON.toJson(chatRuleJson, writer);
LOGGER.info("[Skyblocker] Saved chat rules file");
LOGGER.info("[Skyblocker Chat Rules] Saved chat rules file");
} catch (Exception e) {
LOGGER.error("[Skyblocker] Failed to save chat rules file", e);
LOGGER.error("[Skyblocker Chat Rules] Failed to save chat rules file", e);
}
}

Expand Down Expand Up @@ -136,9 +136,12 @@ private static boolean checkMessage(Text message, boolean overlay) {
CLIENT.player.sendMessage(newMessage, true);
}

//hide message
//show replacement message in chat
//bypass MessageHandler#onGameMessage to avoid activating chat rules again
if (!rule.getHideMessage() && CLIENT.player != null) {
CLIENT.player.sendMessage(newMessage, false);
CLIENT.inGameHud.getChatHud().addMessage(newMessage);
((MessageHandlerAccessor) CLIENT.getMessageHandler()).invokeAddToChatLog(newMessage, Instant.now());
CLIENT.getNarratorManager().narrateSystemMessage(newMessage);
}

//play sound
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/skyblocker.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"accessor.FrustumInvoker",
"accessor.HandledScreenAccessor",
"accessor.ItemStackAccessor",
"accessor.MessageHandlerAccessor",
"accessor.PlayerListHudAccessor",
"accessor.RecipeBookWidgetAccessor",
"accessor.ScreenAccessor",
Expand Down