| 1 | package net.minecraft.network.chat;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.commands.CommandSourceStack;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.Registry;
|
| 9 | import net.minecraft.core.RegistryAccess;
|
| 10 | import net.minecraft.core.registries.Registries;
|
| 11 | import net.minecraft.data.worldgen.BootstrapContext;
|
| 12 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 13 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 14 | import net.minecraft.network.codec.StreamCodec;
|
| 15 | import net.minecraft.resources.Identifier;
|
| 16 | import net.minecraft.resources.ResourceKey;
|
| 17 | import net.minecraft.world.entity.Entity;
|
| 18 |
|
| 19 | public record ChatType(ChatTypeDecoration chat, ChatTypeDecoration narration) {
|
| 20 | public static final Codec<ChatType> DIRECT_CODEC = RecordCodecBuilder.create(
|
| 21 | i -> i.group(ChatTypeDecoration.CODEC.fieldOf("chat").forGetter(ChatType::chat), ChatTypeDecoration.CODEC.fieldOf("narration").forGetter(ChatType::narration))
|
| 22 | .apply(i, ChatType::new)
|
| 23 | );
|
| 24 | public static final StreamCodec<RegistryFriendlyByteBuf, ChatType> DIRECT_STREAM_CODEC = StreamCodec.composite(
|
| 25 | ChatTypeDecoration.STREAM_CODEC, ChatType::chat, ChatTypeDecoration.STREAM_CODEC, ChatType::narration, ChatType::new
|
| 26 | );
|
| 27 | public static final StreamCodec<RegistryFriendlyByteBuf, Holder<ChatType>> STREAM_CODEC = ByteBufCodecs.holder(Registries.CHAT_TYPE, DIRECT_STREAM_CODEC);
|
| 28 | public static final ChatTypeDecoration DEFAULT_CHAT_DECORATION = ChatTypeDecoration.withSender("chat.type.text");
|
| 29 | public static final ResourceKey<ChatType> CHAT = create("chat");
|
| 30 | public static final ResourceKey<ChatType> SAY_COMMAND = create("say_command");
|
| 31 | public static final ResourceKey<ChatType> MSG_COMMAND_INCOMING = create("msg_command_incoming");
|
| 32 | public static final ResourceKey<ChatType> MSG_COMMAND_OUTGOING = create("msg_command_outgoing");
|
| 33 | public static final ResourceKey<ChatType> TEAM_MSG_COMMAND_INCOMING = create("team_msg_command_incoming");
|
| 34 | public static final ResourceKey<ChatType> TEAM_MSG_COMMAND_OUTGOING = create("team_msg_command_outgoing");
|
| 35 | public static final ResourceKey<ChatType> EMOTE_COMMAND = create("emote_command");
|
| 36 |
|
| 37 | private static ResourceKey<ChatType> create(final String name) {
|
| 38 | return ResourceKey.create(Registries.CHAT_TYPE, Identifier.withDefaultNamespace(name));
|
| 39 | }
|
| 40 |
|
| 41 | public static void bootstrap(final BootstrapContext<ChatType> context) {
|
| 42 | context.register(CHAT, new ChatType(DEFAULT_CHAT_DECORATION, ChatTypeDecoration.withSender("chat.type.text.narrate")));
|
| 43 | context.register(SAY_COMMAND, new ChatType(ChatTypeDecoration.withSender("chat.type.announcement"), ChatTypeDecoration.withSender("chat.type.text.narrate")));
|
| 44 | context.register(
|
| 45 | MSG_COMMAND_INCOMING,
|
| 46 | new ChatType(ChatTypeDecoration.incomingDirectMessage("commands.message.display.incoming"), ChatTypeDecoration.withSender("chat.type.text.narrate"))
|
| 47 | );
|
| 48 | context.register(
|
| 49 | MSG_COMMAND_OUTGOING,
|
| 50 | new ChatType(ChatTypeDecoration.outgoingDirectMessage("commands.message.display.outgoing"), ChatTypeDecoration.withSender("chat.type.text.narrate"))
|
| 51 | );
|
| 52 | context.register(
|
| 53 | TEAM_MSG_COMMAND_INCOMING, new ChatType(ChatTypeDecoration.teamMessage("chat.type.team.text"), ChatTypeDecoration.withSender("chat.type.text.narrate"))
|
| 54 | );
|
| 55 | context.register(
|
| 56 | TEAM_MSG_COMMAND_OUTGOING, new ChatType(ChatTypeDecoration.teamMessage("chat.type.team.sent"), ChatTypeDecoration.withSender("chat.type.text.narrate"))
|
| 57 | );
|
| 58 | context.register(EMOTE_COMMAND, new ChatType(ChatTypeDecoration.withSender("chat.type.emote"), ChatTypeDecoration.withSender("chat.type.emote")));
|
| 59 | }
|
| 60 |
|
| 61 | public static ChatType.Bound bind(final ResourceKey<ChatType> chatType, final Entity entity) {
|
| 62 | return bind(chatType, entity.level().registryAccess(), entity.getDisplayName());
|
| 63 | }
|
| 64 |
|
| 65 | public static ChatType.Bound bind(final ResourceKey<ChatType> chatType, final CommandSourceStack source) {
|
| 66 | return bind(chatType, source.registryAccess(), source.getDisplayName());
|
| 67 | }
|
| 68 |
|
| 69 | public static ChatType.Bound bind(final ResourceKey<ChatType> chatType, final RegistryAccess registryAccess, final Component name) {
|
| 70 | Registry<ChatType> registry = registryAccess.lookupOrThrow(Registries.CHAT_TYPE);
|
| 71 | return new ChatType.Bound(registry.getOrThrow(chatType), name);
|
| 72 | }
|
| 73 |
|
| 74 | public record Bound(Holder<ChatType> chatType, Component name, Optional<Component> targetName) {
|
| 75 | public static final StreamCodec<RegistryFriendlyByteBuf, ChatType.Bound> STREAM_CODEC = StreamCodec.composite(
|
| 76 | ChatType.STREAM_CODEC,
|
| 77 | ChatType.Bound::chatType,
|
| 78 | ComponentSerialization.TRUSTED_STREAM_CODEC,
|
| 79 | ChatType.Bound::name,
|
| 80 | ComponentSerialization.TRUSTED_OPTIONAL_STREAM_CODEC,
|
| 81 | ChatType.Bound::targetName,
|
| 82 | ChatType.Bound::new
|
| 83 | );
|
| 84 |
|
| 85 | private Bound(final Holder<ChatType> chatType, final Component name) {
|
| 86 | this(chatType, name, Optional.empty());
|
| 87 | }
|
| 88 |
|
| 89 | public Component decorate(final Component content) {
|
| 90 | return this.chatType.value().chat().decorate(content, this);
|
| 91 | }
|
| 92 |
|
| 93 | public Component decorateNarration(final Component content) {
|
| 94 | return this.chatType.value().narration().decorate(content, this);
|
| 95 | }
|
| 96 |
|
| 97 | public ChatType.Bound withTargetName(final Component targetName) {
|
| 98 | return new ChatType.Bound(this.chatType, this.name, Optional.of(targetName));
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 |
|