Skip to content

Instantly share code, notes, and snippets.

@John-Paul-R
Last active January 9, 2022 02:04
Show Gist options
  • Save John-Paul-R/06a746e4611c79538722fc930dcb41e4 to your computer and use it in GitHub Desktop.
Save John-Paul-R/06a746e4611c79538722fc930dcb41e4 to your computer and use it in GitHub Desktop.
Client Chat Listener Mixin
package com.packagename.access;
import net.minecraft.client.gui.ClientChatListener;
import net.minecraft.network.MessageType;
public interface InGameHudAccess {
void registerChatListener(MessageType messageType, ClientChatListener listener);
}
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);
}
}
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()));
});
});
}
}
{
"required": true,
"package": "com.packagename.mixins",
"compatibilityLevel": "JAVA_17",
"mixins": [
"InGameHudMixin"
],
"injectors": {
"defaultRequire": 1
}
}
Copy link

ghost commented Jan 9, 2022

Good, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment