diff options
| author | Aizistral | 2022-06-20 15:14:50 +0200 |
|---|---|---|
| committer | GitHub | 2022-06-20 15:14:50 +0200 |
| commit | 713ccbd67017e889139d79f1bfff2028f2585e71 (patch) | |
| tree | 53daf5f5ca7e8df36a5c8fb3b8056fc6fa9b00eb /src/main/java/com | |
| parent | a1401d6e6170ad52f2c72f1aada2bbdd38eef84f (diff) | |
| parent | c70592edc99fcf6294df0323eda51d5886115e15 (diff) | |
Merge pull request #1 from JFronny/1.19.X-Fabric
Implement serverside bypass for signature checking by using a system chat packet
Diffstat (limited to 'src/main/java/com')
3 files changed, 57 insertions, 18 deletions
diff --git a/src/main/java/com/aizistral/nochatreports/handlers/NoReportsConfig.java b/src/main/java/com/aizistral/nochatreports/handlers/NoReportsConfig.java index 6612ac4..0a9734e 100644 --- a/src/main/java/com/aizistral/nochatreports/handlers/NoReportsConfig.java +++ b/src/main/java/com/aizistral/nochatreports/handlers/NoReportsConfig.java @@ -1,10 +1,10 @@ package com.aizistral.nochatreports.handlers; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; +import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.IOException; -import java.util.Optional; +import java.nio.file.Files; +import java.nio.file.Path; import javax.annotation.Nullable; @@ -12,13 +12,12 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.client.Minecraft; public class NoReportsConfig { - private static final File CONFIG_FOLDER = FabricLoader.getInstance().getConfigDir().toFile(); - private static final File CONFIG_FILE = new File(CONFIG_FOLDER, "NoChatReports.json"); + private static final Path CONFIG_FILE = FabricLoader.getInstance().getConfigDir().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 +32,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 +58,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..0c53d51 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..68749f6 --- /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, resolveChatTypeId(ChatType.SYSTEM)); + } else { + NoChatReports.LOGGER.warn("Unexpected packet type in sendChatMessage, skipping"); + } + } + listener.send(packet); + } + +} |
