This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
forked from JuergenSchT/chathistory
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
7393783
commit 6183232
Showing
8 changed files
with
158 additions
and
30 deletions.
There are no files selected for viewing
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
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
23 changes: 0 additions & 23 deletions
23
src/main/java/me/thegiggitybyte/chathistory/mixin/MinecraftServerMixin.java
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
src/main/java/me/thegiggitybyte/chathistory/mixin/PlayerAdvancementTrackerMixin.java
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
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); | ||
} | ||
} |
35 changes: 33 additions & 2 deletions
35
src/main/java/me/thegiggitybyte/chathistory/mixin/PlayerManagerMixin.java
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
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()); | ||
} | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/me/thegiggitybyte/chathistory/mixin/ServerPlayNetworkHandlerMixin.java
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
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); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/me/thegiggitybyte/chathistory/mixin/ServerPlayerEntityMixin.java
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
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); | ||
} | ||
} |
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