diff options
Diffstat (limited to 'src/main/java/com')
3 files changed, 57 insertions, 13 deletions
diff --git a/src/main/java/com/aizistral/nochatreports/handlers/NoReportsConfig.java b/src/main/java/com/aizistral/nochatreports/handlers/NoReportsConfig.java index 9a7ac35..055fcbd 100644 --- a/src/main/java/com/aizistral/nochatreports/handlers/NoReportsConfig.java +++ b/src/main/java/com/aizistral/nochatreports/handlers/NoReportsConfig.java @@ -1,9 +1,13 @@ package com.aizistral.nochatreports.handlers; +import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Optional; import javax.annotation.Nullable; @@ -15,10 +19,10 @@ import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.fml.loading.FMLPaths; public class NoReportsConfig { - private static final File CONFIG_FOLDER = FMLPaths.CONFIGDIR.get().toFile(); - private static final File CONFIG_FILE = new File(CONFIG_FOLDER, "NoChatReports.json"); + private static final Path CONFIG_FILE = FMLPaths.CONFIGDIR.get().resolve("NoChatReports.json"); + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); private static NoReportsConfig INSTANCE; - private boolean demandOnClient, demandOnServer; + private boolean demandOnClient, demandOnServer, convertToGameMessage; public static void loadConfig() { INSTANCE = readFile(); @@ -33,21 +37,19 @@ public class NoReportsConfig { @Nullable private static NoReportsConfig readFile() { - if (!CONFIG_FILE.exists() || !CONFIG_FILE.isFile()) + if (!Files.isRegularFile(CONFIG_FILE)) return null; - try (FileReader reader = new FileReader(CONFIG_FILE)) { - NoReportsConfig transience = new Gson().fromJson(reader, NoReportsConfig.class); - return transience; + try (BufferedReader reader = Files.newBufferedReader(CONFIG_FILE)) { + return GSON.fromJson(reader, NoReportsConfig.class); } catch (IOException ex) { throw new RuntimeException(ex); } } private static void writeFile(NoReportsConfig instance) { - try (FileWriter writer = new FileWriter(CONFIG_FILE)) { - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - gson.toJson(instance, writer); + try (BufferedWriter writer = Files.newBufferedWriter(CONFIG_FILE)) { + GSON.toJson(instance, writer); } catch (Exception ex) { throw new RuntimeException(ex); } @@ -61,4 +63,8 @@ public class NoReportsConfig { return INSTANCE.demandOnServer; } + public static boolean convertsToGameMessage() { + return INSTANCE.convertToGameMessage; + } + } diff --git a/src/main/java/com/aizistral/nochatreports/mixins/MixinOptions.java b/src/main/java/com/aizistral/nochatreports/mixins/MixinOptions.java index 78c0343..271bbe9 100644 --- a/src/main/java/com/aizistral/nochatreports/mixins/MixinOptions.java +++ b/src/main/java/com/aizistral/nochatreports/mixins/MixinOptions.java @@ -25,9 +25,8 @@ public class MixinOptions { if (this.alternativeOption == null) { this.alternativeOption = new OptionInstance<>("options.onlyShowSecureChat", OptionInstance.cachedConstantTooltip(Component.translatable("gui.nochatreport.secureChat")), - (component, value) -> { - return value ? CommonComponents.OPTION_ON : CommonComponents.OPTION_OFF; - }, new OptionInstance.Enum<>(ImmutableList.of(Boolean.FALSE), Codec.BOOL), false, (value) -> {}); + (component, value) -> value ? CommonComponents.OPTION_ON : CommonComponents.OPTION_OFF, + new OptionInstance.Enum<>(ImmutableList.of(Boolean.FALSE), Codec.BOOL), false, (value) -> {}); } return this.alternativeOption; diff --git a/src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java b/src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java new file mode 100644 index 0000000..03b885b --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java @@ -0,0 +1,39 @@ +package com.aizistral.nochatreports.mixins; + +import com.aizistral.nochatreports.NoChatReports; +import com.aizistral.nochatreports.handlers.NoReportsConfig; +import net.minecraft.network.chat.ChatDecoration; +import net.minecraft.network.chat.ChatType; +import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket; +import net.minecraft.network.protocol.game.ClientboundSystemChatPacket; +import net.minecraft.resources.ResourceKey; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.network.ServerGamePacketListenerImpl; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(ServerPlayer.class) +public abstract class MixinServerPlayer { + + @Shadow + protected abstract int resolveChatTypeId(ResourceKey<ChatType> resourceKey); + + @Redirect(method = "sendChatMessage(Lnet/minecraft/network/chat/PlayerChatMessage;Lnet/minecraft/network/chat/ChatSender;Lnet/minecraft/resources/ResourceKey;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;send(Lnet/minecraft/network/protocol/Packet;)V")) + void extractChatMessage(ServerGamePacketListenerImpl listener, Packet<?> packet) { + if (NoReportsConfig.convertsToGameMessage()) { + if (packet instanceof ClientboundPlayerChatPacket chat) { + Component component = chat.unsignedContent().orElse(chat.signedContent()); + component = ChatDecoration.withSender("chat.type.text").decorate(component, chat.sender()); + packet = new ClientboundSystemChatPacket(component, this.resolveChatTypeId(ChatType.SYSTEM)); + } else { + NoChatReports.LOGGER.warn("Unexpected packet type in sendChatMessage, skipping"); + } + } + listener.send(packet); + } + +}
\ No newline at end of file |
