Skip to content

Commit

Permalink
Instant->Date fix and rename util index
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbuilder1961 committed Dec 22, 2023
1 parent d46a785 commit 3e856f0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.MessageIndicator;
import net.minecraft.client.network.message.MessageHandler;
import net.minecraft.network.message.MessageSignatureData;
import net.minecraft.network.message.MessageType;
import net.minecraft.network.message.SignedMessage;
import net.minecraft.text.Text;
Expand All @@ -23,13 +21,13 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.time.Instant;
import java.util.Date;
import java.util.UUID;

/**
* A mixin used to cache the metadata of the most recent message
* received by the client. This is used in
* {@link ChatHudMixin#modifyMessage(Text, Text, MessageSignatureData, int, MessageIndicator, boolean)}
* {@link ChatHudMixin#modifyMessage(Text, boolean)}
* to provide more accurate timestamp data, the correct player
* name, and the player's UUID.
*/
Expand All @@ -48,7 +46,7 @@ public abstract class MessageHandlerMixin {
private void cacheChatData(SignedMessage message, GameProfile sender, MessageType.Parameters params, CallbackInfo ci) {
// only logs the metadata if it was a player-sent message (otherwise tries to format some commands like /msg and /me)
if( params.type().chat().translationKey().equals("chat.type.text") )
SharedVariables.lastMsg = new ChatUtils.MessageData(sender, message.getTimestamp(), true);
SharedVariables.lastMsg = new ChatUtils.MessageData(sender, Date.from(message.getTimestamp()), true);
else
SharedVariables.lastMsg = ChatUtils.NIL_MSG_DATA;
}
Expand All @@ -64,7 +62,7 @@ private void cacheGameData(Text message, boolean overlay, CallbackInfo ci) {
UUID uuid = name == null ? Util.NIL_UUID : client.getSocialInteractionsManager().getUuid(name);

SharedVariables.lastMsg = !uuid.equals(Util.NIL_UUID)
? new ChatUtils.MessageData(new GameProfile(uuid, name), Instant.now(), true)
? new ChatUtils.MessageData(new GameProfile(uuid, name), new Date(), true)
: ChatUtils.NIL_MSG_DATA;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.List;

import static obro1961.chatpatches.ChatPatches.config;
import static obro1961.chatpatches.util.ChatUtils.OG_MSG_INDEX;
import static obro1961.chatpatches.util.ChatUtils.MSG_INDEX;
import static obro1961.chatpatches.util.SharedVariables.lastMsg;

/**
Expand Down Expand Up @@ -149,7 +149,7 @@ private Text modifyMessage(Text m, @Local(argsOnly = true) boolean refreshing) {
final Style style = m.getStyle();
boolean lastEmpty = lastMsg.equals(ChatUtils.NIL_MSG_DATA);
boolean boundary = Flags.BOUNDARY_LINE.isRaised() && config.boundary && !config.vanillaClearing;
Date now = lastEmpty ? new Date() : Date.from(lastMsg.timestamp());
Date now = lastEmpty ? new Date() : lastMsg.timestamp();
String nowTime = String.valueOf( now.getTime() ); // for copy menu and storing timestamp data! only affects the timestamp


Expand Down Expand Up @@ -300,7 +300,7 @@ private Text addCounter(Text incoming, boolean refreshing) {
// exclude the first message, already checked above
messages.subList(1, attemptDistance)
.stream()
.filter( hudLine -> hudLine.content().getSiblings().get(OG_MSG_INDEX).getString().equalsIgnoreCase( incoming.getSiblings().get(OG_MSG_INDEX).getString() ) )
.filter( hudLine -> hudLine.content().getSiblings().get(MSG_INDEX).getString().equalsIgnoreCase( incoming.getSiblings().get(MSG_INDEX).getString() ) )
.findFirst()
.ifPresent( hudLine -> ChatUtils.getCondensedMessage(incoming, messages.indexOf(hudLine)) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ protected void initSearchStuff(CallbackInfo ci) {
return "";
}));
hoverButtons.put(COPY_NAME, of(1, COPY_NAME, () -> {
Text message = selectedLine.content().getSiblings().get(ChatUtils.OG_MSG_INDEX);
Text message = selectedLine.content().getSiblings().get(ChatUtils.MSG_INDEX);
Text text = message.getSiblings().size() > ChatUtils.MSG_NAME_INDEX ? message.getSiblings().get(ChatUtils.MSG_NAME_INDEX) : Text.empty();
HoverEvent.EntityContent player = text.getStyle().getHoverEvent() != null ? text.getStyle().getHoverEvent().getValue(SHOW_ENTITY) : null;
return player != null && player.name.isPresent() ? player.name.get().getString() : text.getString();
}));
hoverButtons.put(COPY_UUID, of(1, COPY_UUID, () -> {
Text message = selectedLine.content().getSiblings().get(ChatUtils.OG_MSG_INDEX);
Text message = selectedLine.content().getSiblings().get(ChatUtils.MSG_INDEX);
Text text = message.getSiblings().size() > ChatUtils.MSG_NAME_INDEX ? message.getSiblings().get(ChatUtils.MSG_NAME_INDEX) : Text.empty();
HoverEvent.EntityContent player = text.getStyle().getHoverEvent() != null ? text.getStyle().getHoverEvent().getValue(SHOW_ENTITY) : null;
return player != null ? player.uuid.toString() : text.getString();
Expand Down Expand Up @@ -619,7 +619,7 @@ private boolean loadCopyMenu(double mX, double mY) {
mainButtons.get(COPY_UNIX).readyToRender(true);

// add player data and reply buttons
Text originalMessage = selectedLine.content().getSiblings().size() > ChatUtils.OG_MSG_INDEX ? selectedLine.content().getSiblings().get(ChatUtils.OG_MSG_INDEX) : Text.empty();
Text originalMessage = selectedLine.content().getSiblings().size() > ChatUtils.MSG_INDEX ? selectedLine.content().getSiblings().get(ChatUtils.MSG_INDEX) : Text.empty();
Style style = originalMessage.getSiblings().size() > 0 ? originalMessage.getSiblings().get(ChatUtils.MSG_NAME_INDEX).getStyle() : Style.EMPTY;
if( !style.equals(Style.EMPTY) && style.getHoverEvent() != null && style.getHoverEvent().getAction() == HoverEvent.Action.SHOW_ENTITY ) {
PlayerListEntry player = client.getNetworkHandler().getPlayerListEntry( UUID.fromString(hoverButtons.get(COPY_UUID).copySupplier.get()) );
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/obro1961/chatpatches/util/ChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import obro1961.chatpatches.mixin.gui.ChatHudMixin;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
Expand All @@ -24,9 +25,9 @@
*/
public class ChatUtils {
public static final UUID NIL_UUID = new UUID(0, 0);
public static final MessageData NIL_MSG_DATA = new MessageData(new GameProfile(ChatUtils.NIL_UUID, ""), Instant.EPOCH, false);
public static final int TIMESTAMP_INDEX = 0, OG_MSG_INDEX = 1, DUPE_COUNTER_INDEX = 2; // indices of all main (modified message) components
public static final int MSG_NAME_INDEX = 0, MSG_MSG_INDEX = 1, MSG_FORMATTED_TEXT_INDEX = 2; // indices of all OG_MSG_INDEX components
public static final MessageData NIL_MSG_DATA = new MessageData(new GameProfile(ChatUtils.NIL_UUID, ""), Date.from(Instant.EPOCH), false);
public static final int TIMESTAMP_INDEX = 0, MSG_INDEX = 1, DUPE_COUNTER_INDEX = 2; // indices of all main (modified message) components
public static final int MSG_NAME_INDEX = 0, MSG_MSG_INDEX = 1, MSG_FORMATTED_TEXT_INDEX = 2; // indices of all MSG_INDEX components
/**
* Matches a vanilla message, with captures for the playername and message.
* Considers a message invalid if {@link net.minecraft.SharedConstants#isValidChar(char)}
Expand Down Expand Up @@ -74,7 +75,7 @@ public static Text getCondensedMessage(Text incoming, int index) {


// IF the last and incoming message bodies are equal, continue
if( incomingParts.get(OG_MSG_INDEX).getString().equalsIgnoreCase(comparingParts.get(OG_MSG_INDEX).getString()) ) {
if( incomingParts.get(MSG_INDEX).getString().equalsIgnoreCase(comparingParts.get(MSG_INDEX).getString()) ) {

// info: according to some limited testing, incoming messages (incomingParts) will never contain a dupe counter, so it's been omitted from this check
int dupes = (
Expand Down Expand Up @@ -113,5 +114,5 @@ public static Text getCondensedMessage(Text incoming, int index) {


/** Represents the metadata of a chat message. */
public record MessageData(GameProfile sender, Instant timestamp, boolean vanilla) {}
public record MessageData(GameProfile sender, Date timestamp, boolean vanilla) {}
}

0 comments on commit 3e856f0

Please sign in to comment.