Skip to content

Commit

Permalink
Fix potential stack overflow with chat rules (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 authored Mar 18, 2024
1 parent 02a7ecc commit 969fa4f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
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

0 comments on commit 969fa4f

Please sign in to comment.