From 87e7c92526ac894525079305932de80f20372679 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Fri, 19 Aug 2022 19:23:39 +0200 Subject: integrate with forge chat event (#106) --- .../oneconfig/internal/mixin/EventBusMixin.java | 58 ++++++++++++++++++++++ .../internal/mixin/NetHandlerPlayClientMixin.java | 51 ------------------- .../oneconfig/internal/mixin/EventBusMixin.java | 57 +++++++++++++++++++++ .../internal/mixin/NetHandlerPlayClientMixin.java | 51 ------------------- .../oneconfig/internal/mixin/EventBusMixin.java | 56 +++++++++++++++++++++ .../internal/mixin/NetHandlerPlayClientMixin.java | 55 -------------------- .../internal/plugin/OneConfigMixinPlugin.java | 10 ++-- versions/src/main/resources/mixins.oneconfig.json | 1 - 8 files changed, 178 insertions(+), 161 deletions(-) create mode 100644 versions/1.12.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java delete mode 100644 versions/1.12.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java create mode 100644 versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java delete mode 100644 versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java create mode 100644 versions/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java delete mode 100644 versions/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java diff --git a/versions/1.12.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java b/versions/1.12.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java new file mode 100644 index 0000000..64ca336 --- /dev/null +++ b/versions/1.12.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java @@ -0,0 +1,58 @@ +/* + * This file is part of OneConfig. + * OneConfig - Next Generation Config Library for Minecraft: Java Edition + * Copyright (C) 2021, 2022 Polyfrost. + * + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * OneConfig is licensed under the terms of version 3 of the GNU Lesser + * General Public License as published by the Free Software Foundation, AND + * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, + * either version 1.0 of the Additional Terms, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License. If not, see . You should + * have also received a copy of the Additional Terms Applicable + * to OneConfig, as published by Polyfrost. If not, see + * + */ + +//#if FORGE==1 +package cc.polyfrost.oneconfig.internal.mixin; + +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.ChatReceiveEvent; +import net.minecraft.util.text.ChatType; +import net.minecraftforge.client.event.ClientChatReceivedEvent; +import net.minecraftforge.fml.common.eventhandler.Event; +import net.minecraftforge.fml.common.eventhandler.EventBus; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(EventBus.class) +public class EventBusMixin { + + @Inject(method = "post", at = @At(value = "HEAD"), remap = false) + private void post(Event e, CallbackInfoReturnable cir) { + if(!(e instanceof ClientChatReceivedEvent)) return; + ClientChatReceivedEvent event = (ClientChatReceivedEvent) e; + if (event.getType() == ChatType.CHAT) { + ChatReceiveEvent customEvent = new ChatReceiveEvent(event.getMessage()); + EventManager.INSTANCE.post(customEvent); + if (customEvent.isCancelled) { + e.setCanceled(true); + } + } + } +} + +//#endif \ No newline at end of file diff --git a/versions/1.12.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java b/versions/1.12.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java deleted file mode 100644 index dd801c3..0000000 --- a/versions/1.12.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * This file is part of OneConfig. - * OneConfig - Next Generation Config Library for Minecraft: Java Edition - * Copyright (C) 2021, 2022 Polyfrost. - * - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * OneConfig is licensed under the terms of version 3 of the GNU Lesser - * General Public License as published by the Free Software Foundation, AND - * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, - * either version 1.0 of the Additional Terms, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License. If not, see . You should - * have also received a copy of the Additional Terms Applicable - * to OneConfig, as published by Polyfrost. If not, see - * - */ - -package cc.polyfrost.oneconfig.internal.mixin; - -import cc.polyfrost.oneconfig.events.EventManager; -import cc.polyfrost.oneconfig.events.event.ChatReceiveEvent; -import net.minecraft.client.network.NetHandlerPlayClient; -import net.minecraft.network.play.server.SPacketChat; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(value = NetHandlerPlayClient.class, priority = Integer.MAX_VALUE) -public class NetHandlerPlayClientMixin { - - @Inject(method = "handleChat", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/event/ForgeEventFactory;onClientChat(Lnet/minecraft/util/text/ChatType;Lnet/minecraft/util/text/ITextComponent;)Lnet/minecraft/util/text/ITextComponent;", remap = false), cancellable = true, remap = true) - private void onClientChat(SPacketChat packetIn, CallbackInfo ci) { - if (packetIn.getType().getId() == 0) { - ChatReceiveEvent event = new ChatReceiveEvent(packetIn.getChatComponent()); - EventManager.INSTANCE.post(event); - if (event.isCancelled) { - ci.cancel(); - } - } - } -} \ No newline at end of file diff --git a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java new file mode 100644 index 0000000..047a8dd --- /dev/null +++ b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java @@ -0,0 +1,57 @@ +/* + * This file is part of OneConfig. + * OneConfig - Next Generation Config Library for Minecraft: Java Edition + * Copyright (C) 2021, 2022 Polyfrost. + * + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * OneConfig is licensed under the terms of version 3 of the GNU Lesser + * General Public License as published by the Free Software Foundation, AND + * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, + * either version 1.0 of the Additional Terms, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License. If not, see . You should + * have also received a copy of the Additional Terms Applicable + * to OneConfig, as published by Polyfrost. If not, see + * + */ + +//#if FORGE==1 +package cc.polyfrost.oneconfig.internal.mixin; + +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.ChatReceiveEvent; +import net.minecraft.util.text.ChatType; +import net.minecraftforge.client.event.ClientChatReceivedEvent; +import net.minecraftforge.eventbus.EventBus; +import net.minecraftforge.eventbus.api.Event; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(EventBus.class) +public class EventBusMixin { + + @Inject(method = "post", at = @At(value = "HEAD"), remap = false) + private void post(Event e, CallbackInfoReturnable cir) { + if(!(e instanceof ClientChatReceivedEvent)) return; + ClientChatReceivedEvent event = (ClientChatReceivedEvent) e; + if (event.getType() == ChatType.CHAT) { + ChatReceiveEvent customEvent = new ChatReceiveEvent(event.getMessage()); + EventManager.INSTANCE.post(customEvent); + if (customEvent.isCancelled) { + e.setCanceled(true); + } + } + } +} +//#endif \ No newline at end of file diff --git a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java b/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java deleted file mode 100644 index dc00be9..0000000 --- a/versions/1.16.2-forge/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * This file is part of OneConfig. - * OneConfig - Next Generation Config Library for Minecraft: Java Edition - * Copyright (C) 2021, 2022 Polyfrost. - * - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * OneConfig is licensed under the terms of version 3 of the GNU Lesser - * General Public License as published by the Free Software Foundation, AND - * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, - * either version 1.0 of the Additional Terms, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License. If not, see . You should - * have also received a copy of the Additional Terms Applicable - * to OneConfig, as published by Polyfrost. If not, see - * - */ - -package cc.polyfrost.oneconfig.internal.mixin; - -import cc.polyfrost.oneconfig.events.EventManager; -import cc.polyfrost.oneconfig.events.event.ChatReceiveEvent; -import net.minecraft.client.network.play.ClientPlayNetHandler; -import net.minecraft.network.play.server.SChatPacket; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(value = ClientPlayNetHandler.class, priority = Integer.MAX_VALUE) -public class NetHandlerPlayClientMixin { - - @Inject(method = "handleChat", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/event/ForgeEventFactory;onClientChat(Lnet/minecraft/util/text/ChatType;Lnet/minecraft/util/text/ITextComponent;Ljava/util/UUID;)Lnet/minecraft/util/text/ITextComponent;", remap = false), cancellable = true, remap = true) - private void onClientChat(SChatPacket packetIn, CallbackInfo ci) { - if (packetIn.getType().getId() == 0) { - ChatReceiveEvent event = new ChatReceiveEvent(packetIn.getChatComponent()); - EventManager.INSTANCE.post(event); - if (event.isCancelled) { - ci.cancel(); - } - } - } -} \ No newline at end of file diff --git a/versions/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java b/versions/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java new file mode 100644 index 0000000..231a861 --- /dev/null +++ b/versions/src/main/java/cc/polyfrost/oneconfig/internal/mixin/EventBusMixin.java @@ -0,0 +1,56 @@ +/* + * This file is part of OneConfig. + * OneConfig - Next Generation Config Library for Minecraft: Java Edition + * Copyright (C) 2021, 2022 Polyfrost. + * + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * OneConfig is licensed under the terms of version 3 of the GNU Lesser + * General Public License as published by the Free Software Foundation, AND + * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, + * either version 1.0 of the Additional Terms, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License. If not, see . You should + * have also received a copy of the Additional Terms Applicable + * to OneConfig, as published by Polyfrost. If not, see + * + */ + +//#if MC==10809 +package cc.polyfrost.oneconfig.internal.mixin; + +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.ChatReceiveEvent; +import net.minecraftforge.client.event.ClientChatReceivedEvent; +import net.minecraftforge.fml.common.eventhandler.Event; +import net.minecraftforge.fml.common.eventhandler.EventBus; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(EventBus.class) +public class EventBusMixin { + + @Inject(method = "post", at = @At(value = "HEAD"), remap = false) + private void post(Event e, CallbackInfoReturnable cir) { + if(!(e instanceof ClientChatReceivedEvent)) return; + ClientChatReceivedEvent event = (ClientChatReceivedEvent) e; + if (event.type == 0) { + ChatReceiveEvent customEvent = new ChatReceiveEvent(event.message); + EventManager.INSTANCE.post(customEvent); + if (customEvent.isCancelled) { + e.setCanceled(true); + } + } + } +} +//#endif \ No newline at end of file diff --git a/versions/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java b/versions/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java deleted file mode 100644 index d8a6691..0000000 --- a/versions/src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * This file is part of OneConfig. - * OneConfig - Next Generation Config Library for Minecraft: Java Edition - * Copyright (C) 2021, 2022 Polyfrost. - * - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * OneConfig is licensed under the terms of version 3 of the GNU Lesser - * General Public License as published by the Free Software Foundation, AND - * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, - * either version 1.0 of the Additional Terms, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License. If not, see . You should - * have also received a copy of the Additional Terms Applicable - * to OneConfig, as published by Polyfrost. If not, see - * - */ - -//#if MC==10809 -package cc.polyfrost.oneconfig.internal.mixin; - -import cc.polyfrost.oneconfig.events.EventManager; -import cc.polyfrost.oneconfig.events.event.ChatReceiveEvent; -import cc.polyfrost.oneconfig.events.event.SendPacketEvent; -import net.minecraft.client.network.NetHandlerPlayClient; -import net.minecraft.network.Packet; -import net.minecraft.network.play.server.S02PacketChat; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(value = NetHandlerPlayClient.class, priority = Integer.MAX_VALUE) -public class NetHandlerPlayClientMixin { - - @Inject(method = "handleChat", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/event/ForgeEventFactory;onClientChat(BLnet/minecraft/util/IChatComponent;)Lnet/minecraft/util/IChatComponent;", remap = false), cancellable = true, remap = true) - private void onClientChat(S02PacketChat packetIn, CallbackInfo ci) { - if (packetIn.getType() == 0) { - ChatReceiveEvent event = new ChatReceiveEvent(packetIn.getChatComponent()); - EventManager.INSTANCE.post(event); - if (event.isCancelled) { - ci.cancel(); - } - } - } -} -//#endif \ No newline at end of file diff --git a/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/OneConfigMixinPlugin.java b/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/OneConfigMixinPlugin.java index e272932..e242e2f 100644 --- a/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/OneConfigMixinPlugin.java +++ b/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/OneConfigMixinPlugin.java @@ -67,8 +67,13 @@ public class OneConfigMixinPlugin implements IMixinConfigPlugin { @Override public List getMixins() { + ArrayList mixins = new ArrayList<>(); + if (Platform.getInstance().getLoader().equals(Platform.Loader.FORGE)) { + mixins.add("EventBusMixin"); + } else if (Platform.getInstance().getLoader().equals(Platform.Loader.FABRIC)) { + mixins.add("NetHandlerPlayClientMixin"); + } if (Platform.getInstance().getMinecraftVersion() >= 11600) { - ArrayList mixins = new ArrayList<>(); if (Platform.getInstance().getLoader() == Platform.Loader.FORGE) { mixins.add("ClientModLoaderMixin"); } else { @@ -80,9 +85,8 @@ public class OneConfigMixinPlugin implements IMixinConfigPlugin { mixins.add("MouseMixin"); mixins.add("TickTimeTrackerMixin"); return mixins; - } else { - return null; } + return mixins.isEmpty() ? null : mixins; } @Override diff --git a/versions/src/main/resources/mixins.oneconfig.json b/versions/src/main/resources/mixins.oneconfig.json index 4a03fd0..3269468 100644 --- a/versions/src/main/resources/mixins.oneconfig.json +++ b/versions/src/main/resources/mixins.oneconfig.json @@ -11,7 +11,6 @@ "client": [ "GuiIngameForgeMixin", "MinecraftMixin", - "NetHandlerPlayClientMixin", "NetworkManagerMixin", "OptifineConfigMixin", "ShaderGroupAccessor", -- cgit