Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
Improved message caching.
Browse files Browse the repository at this point in the history
Caching messages is handled by a mixin which saves messages of every kind; this leads to undesirable output such as console commands to be cached and displayed to the user on login. This commit replaces that single mixin with several precise mixins which cache messages from specific events.
  • Loading branch information
Giggitybyte committed Sep 30, 2021
1 parent 7393783 commit 6183232
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ minecraft_version=1.17.1
yarn_mappings=1.17.1+build.61
loader_version=0.11.7

mod_version=1.0.0
mod_version=1.1.0
maven_group=me.thegiggitybyte
archives_base_name=chathistory
9 changes: 8 additions & 1 deletion src/main/java/me/thegiggitybyte/chathistory/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package me.thegiggitybyte.chathistory;

import net.minecraft.network.MessageType;
import net.minecraft.text.Text;

import java.util.UUID;

public class ChatMessage {
private final Text content;
private final MessageType type;
private final UUID senderUuid;

public ChatMessage(Text content, UUID sender) {
public ChatMessage(Text content, MessageType type, UUID sender) {
this.content = content;
this.type = type;
this.senderUuid = sender;
}

public Text getContent() {
return content;
}

public MessageType getType() {
return type;
}

public UUID getSender() {
return senderUuid;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package me.thegiggitybyte.chathistory.mixin;

import me.thegiggitybyte.chathistory.ChatHistory;
import me.thegiggitybyte.chathistory.ChatMessage;
import net.minecraft.advancement.Advancement;
import net.minecraft.advancement.PlayerAdvancementTracker;
import net.minecraft.network.MessageType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Util;
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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PlayerAdvancementTracker.class)
public class PlayerAdvancementTrackerMixin {

@Shadow
private ServerPlayerEntity owner;

@Inject(
method = "grantCriterion(Lnet/minecraft/advancement/Advancement;Ljava/lang/String;)Z",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"
)
)
public void cacheAdvancementMessage(Advancement advancement, String criterionName, CallbackInfoReturnable<Boolean> cir) {
var translationKey = "chat.type.advancement." + advancement.getDisplay().getFrame().getId();
var content = new TranslatableText(translationKey, this.owner.getDisplayName(), advancement.toHoverableText());
var message = new ChatMessage(content, MessageType.SYSTEM, Util.NIL_UUID);

ChatHistory.MESSAGE_CACHE.add(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
package me.thegiggitybyte.chathistory.mixin;

import com.mojang.authlib.GameProfile;
import me.thegiggitybyte.chathistory.ChatHistory;
import me.thegiggitybyte.chathistory.ChatMessage;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.MessageType;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.MutableText;
import net.minecraft.util.UserCache;
import net.minecraft.util.Util;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.WorldProperties;
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;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.Optional;

@Environment(EnvType.SERVER)
@Mixin(PlayerManager.class)
public abstract class PlayerManagerMixin {

@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;onSpawn()V"), method = "onPlayerConnect")
@Inject(
method = "onPlayerConnect",
locals = LocalCapture.CAPTURE_FAILHARD,
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"
)
)
public void cacheJoinMessage(ClientConnection connection, ServerPlayerEntity player, CallbackInfo ci, GameProfile gameProfile, UserCache userCache, Optional optional, String string, NbtCompound nbtCompound, RegistryKey registryKey, ServerWorld serverWorld, ServerWorld serverWorld3, String string2, WorldProperties worldProperties, ServerPlayNetworkHandler serverPlayNetworkHandler, MutableText content) { // :(
var message = new ChatMessage(content, MessageType.SYSTEM, Util.NIL_UUID);
ChatHistory.MESSAGE_CACHE.add(message);
}

@Inject(
method = "onPlayerConnect",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/network/ServerPlayerEntity;onSpawn()V"
)
)
public void sendCachedMessages(ClientConnection connection, ServerPlayerEntity player, CallbackInfo ci) {
for (var message : ChatHistory.MESSAGE_CACHE) {
player.sendMessage(message.getContent(), MessageType.CHAT, message.getSender());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.thegiggitybyte.chathistory.mixin;

import me.thegiggitybyte.chathistory.ChatHistory;
import me.thegiggitybyte.chathistory.ChatMessage;
import net.minecraft.network.MessageType;
import net.minecraft.server.filter.TextStream;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.TranslatableText;
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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerPlayNetworkHandler.class)
public class ServerPlayNetworkHandlerMixin {

@Shadow
public ServerPlayerEntity player;

@Inject(
method = "handleMessage",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Ljava/util/function/Function;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"
)
)
public void cachePlayerMessage(TextStream.Message messageStream, CallbackInfo ci) {
var content = new TranslatableText("chat.type.text", this.player.getDisplayName(), messageStream.getRaw());
var message = new ChatMessage(content, MessageType.CHAT, this.player.getUuid());

ChatHistory.MESSAGE_CACHE.add(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.thegiggitybyte.chathistory.mixin;

import me.thegiggitybyte.chathistory.ChatHistory;
import me.thegiggitybyte.chathistory.ChatMessage;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.network.MessageType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.Util;
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(ServerPlayerEntity.class)
public class ServerPlayerEntityMixin {

@Inject(at = @At("HEAD"), method = "onDisconnect()V")
public void cacheLeaveMessage(CallbackInfo info) {
var player = (ServerPlayerEntity) (Object) this;
var content = new TranslatableText("multiplayer.player.left", player.getDisplayName()).formatted(Formatting.YELLOW);
var message = new ChatMessage(content, MessageType.SYSTEM, Util.NIL_UUID);

ChatHistory.MESSAGE_CACHE.add(message);
}

@Inject(
method = "onDeath(Lnet/minecraft/entity/damage/DamageSource;)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"
)
)
public void cacheDeathMessage(DamageSource source, CallbackInfo ci) {
var player = (ServerPlayerEntity) (Object) this;
var message = new ChatMessage(source.getDeathMessage(player), MessageType.SYSTEM, Util.NIL_UUID);

ChatHistory.MESSAGE_CACHE.add(message);
}
}
6 changes: 3 additions & 3 deletions src/main/resources/chathistory.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"minVersion": "0.8",
"package": "me.thegiggitybyte.chathistory.mixin",
"compatibilityLevel": "JAVA_16",
"mixins": [
],
"server": [
"PlayerManagerMixin",
"MinecraftServerMixin"
"ServerPlayerEntityMixin",
"ServerPlayNetworkHandlerMixin",
"PlayerAdvancementTrackerMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 6183232

Please sign in to comment.