| 1 | package net.minecraft.client.multiplayer.chat;
|
| 2 |
|
| 3 | import com.mojang.authlib.GameProfile;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.time.Instant;
|
| 7 | import java.time.ZoneId;
|
| 8 | import java.time.ZonedDateTime;
|
| 9 | import java.time.format.DateTimeFormatter;
|
| 10 | import java.time.format.FormatStyle;
|
| 11 | import java.util.UUID;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.ChatFormatting;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.chat.ComponentSerialization;
|
| 17 | import net.minecraft.network.chat.PlayerChatMessage;
|
| 18 | import net.minecraft.util.ExtraCodecs;
|
| 19 | import net.minecraft.util.Util;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public interface LoggedChatMessage extends LoggedChatEvent {
|
| 23 | static LoggedChatMessage.Player player(final GameProfile profile, final PlayerChatMessage message, final ChatTrustLevel trustLevel) {
|
| 24 | return new LoggedChatMessage.Player(profile, message, trustLevel);
|
| 25 | }
|
| 26 |
|
| 27 | static LoggedChatMessage.System system(final Component message, final Instant timeStamp) {
|
| 28 | return new LoggedChatMessage.System(message, timeStamp);
|
| 29 | }
|
| 30 |
|
| 31 | Component toContentComponent();
|
| 32 |
|
| 33 | default Component toNarrationComponent() {
|
| 34 | return this.toContentComponent();
|
| 35 | }
|
| 36 |
|
| 37 | boolean canReport(UUID reportedPlayerId);
|
| 38 |
|
| 39 | @Environment(EnvType.CLIENT)
|
| 40 | record Player(GameProfile profile, PlayerChatMessage message, ChatTrustLevel trustLevel) implements LoggedChatMessage {
|
| 41 | public static final MapCodec<LoggedChatMessage.Player> CODEC = RecordCodecBuilder.mapCodec(
|
| 42 | i -> i.group(
|
| 43 | ExtraCodecs.AUTHLIB_GAME_PROFILE.fieldOf("profile").forGetter(LoggedChatMessage.Player::profile),
|
| 44 | PlayerChatMessage.MAP_CODEC.forGetter(LoggedChatMessage.Player::message),
|
| 45 | ChatTrustLevel.CODEC.optionalFieldOf("trust_level", ChatTrustLevel.SECURE).forGetter(LoggedChatMessage.Player::trustLevel)
|
| 46 | )
|
| 47 | .apply(i, LoggedChatMessage.Player::new)
|
| 48 | );
|
| 49 | private static final DateTimeFormatter TIME_FORMATTER = Util.localizedDateFormatter(FormatStyle.SHORT);
|
| 50 |
|
| 51 | @Override
|
| 52 | public Component toContentComponent() {
|
| 53 | if (!this.message.filterMask().isEmpty()) {
|
| 54 | Component filtered = this.message.filterMask().applyWithFormatting(this.message.signedContent());
|
| 55 | return filtered != null ? filtered : Component.empty();
|
| 56 | } else {
|
| 57 | return this.message.decoratedContent();
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public Component toNarrationComponent() {
|
| 63 | Component content = this.toContentComponent();
|
| 64 | Component time = this.getTimeComponent();
|
| 65 | return Component.translatable("gui.chatSelection.message.narrate", this.profile.name(), content, time);
|
| 66 | }
|
| 67 |
|
| 68 | public Component toHeadingComponent() {
|
| 69 | Component time = this.getTimeComponent();
|
| 70 | return Component.translatable("gui.chatSelection.heading", this.profile.name(), time);
|
| 71 | }
|
| 72 |
|
| 73 | private Component getTimeComponent() {
|
| 74 | ZonedDateTime dateTime = ZonedDateTime.ofInstant(this.message.timeStamp(), ZoneId.systemDefault());
|
| 75 | return Component.literal(dateTime.format(TIME_FORMATTER)).withStyle(ChatFormatting.ITALIC, ChatFormatting.GRAY);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public boolean canReport(final UUID reportedPlayerId) {
|
| 80 | return this.message.hasSignatureFrom(reportedPlayerId);
|
| 81 | }
|
| 82 |
|
| 83 | public UUID profileId() {
|
| 84 | return this.profile.id();
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public LoggedChatEvent.Type type() {
|
| 89 | return LoggedChatEvent.Type.PLAYER;
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Environment(EnvType.CLIENT)
|
| 94 | record System(Component message, Instant timeStamp) implements LoggedChatMessage {
|
| 95 | public static final MapCodec<LoggedChatMessage.System> CODEC = RecordCodecBuilder.mapCodec(
|
| 96 | i -> i.group(
|
| 97 | ComponentSerialization.CODEC.fieldOf("message").forGetter(LoggedChatMessage.System::message),
|
| 98 | ExtraCodecs.INSTANT_ISO8601.fieldOf("time_stamp").forGetter(LoggedChatMessage.System::timeStamp)
|
| 99 | )
|
| 100 | .apply(i, LoggedChatMessage.System::new)
|
| 101 | );
|
| 102 |
|
| 103 | @Override
|
| 104 | public Component toContentComponent() {
|
| 105 | return this.message;
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public boolean canReport(final UUID reportedPlayerId) {
|
| 110 | return false;
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public LoggedChatEvent.Type type() {
|
| 115 | return LoggedChatEvent.Type.SYSTEM;
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 |
|