Last active
January 9, 2022 02:04
-
-
Save John-Paul-R/06a746e4611c79538722fc930dcb41e4 to your computer and use it in GitHub Desktop.
Client Chat Listener Mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.packagename.access; | |
import net.minecraft.client.gui.ClientChatListener; | |
import net.minecraft.network.MessageType; | |
public interface InGameHudAccess { | |
void registerChatListener(MessageType messageType, ClientChatListener listener); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.packagename.mixin; | |
import com.packagename.access.InGameHudAccess; | |
import net.minecraft.client.gui.ClientChatListener; | |
import net.minecraft.client.gui.hud.InGameHud; | |
import net.minecraft.network.MessageType; | |
import org.spongepowered.asm.mixin.Final; | |
import org.spongepowered.asm.mixin.Mixin; | |
import org.spongepowered.asm.mixin.Shadow; | |
import java.util.List; | |
import java.util.Map; | |
@Mixin(InGameHud.class) | |
public class InGameHudMixin implements InGameHudAccess { | |
@Shadow | |
@Final | |
private Map<MessageType, List<ClientChatListener>> listeners; | |
public void registerChatListener(MessageType messageType, ClientChatListener listener) { | |
this.listeners.get(messageType).add(listener); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.packagename; | |
public final class MyModEntrypoint implements ModInitializer { | |
@Override | |
public void onInitialize() { | |
Logger LOGGER = LogManager.getLogger("TestMod"); | |
// Wait for CLIENT_STARTED, otherwise `inGameHud` will be null. | |
ClientLifecycleEvents.CLIENT_STARTED.register(client -> { | |
((InGameHudAccess)client.inGameHud).registerChatListener( | |
MessageType.CHAT, | |
(type, message, sender) -> { | |
LOGGER.info(String.format("%s:: %s", sender.toString(), message.toString())); | |
}); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"required": true, | |
"package": "com.packagename.mixins", | |
"compatibilityLevel": "JAVA_17", | |
"mixins": [ | |
"InGameHudMixin" | |
], | |
"injectors": { | |
"defaultRequire": 1 | |
} | |
} |
Because this hooks into the chat HUD, I think it should work for any type of chat message that the client displays, provided that you specify the correct MessageType
when calling registerChatListener
. (though I have not tested this)
Good, thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this work for client-sent messages as well as server-received messages?