| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Collections;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.function.Function;
|
| 10 | import java.util.function.UnaryOperator;
|
| 11 | import net.minecraft.ChatFormatting;
|
| 12 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 13 | import net.minecraft.network.chat.ClickEvent;
|
| 14 | import net.minecraft.network.chat.CommonComponents;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.chat.ComponentSerialization;
|
| 17 | import net.minecraft.network.chat.Style;
|
| 18 | import net.minecraft.network.chat.contents.PlainTextContents;
|
| 19 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 20 | import net.minecraft.network.codec.StreamCodec;
|
| 21 | import net.minecraft.util.ExtraCodecs;
|
| 22 | import net.minecraft.util.FormattedCharSequence;
|
| 23 | import net.minecraft.world.item.DyeColor;
|
| 24 | import net.minecraft.world.item.component.TooltipProvider;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class SignText {
|
| 28 | public static final int LINES = 4;
|
| 29 | public static final Codec<SignText> CODEC = RecordCodecBuilder.<SignText>create(
|
| 30 | i -> i.group(
|
| 31 | ComponentSerialization.CODEC.listOf(4, 4).fieldOf("messages").forGetter(o -> o.messages),
|
| 32 | ComponentSerialization.CODEC.listOf(4, 4).lenientOptionalFieldOf("filtered_messages").forGetter(SignText::filteredMessagesForSerialization),
|
| 33 | ExtraCodecs.optionalAlwaysPresentFieldOf(DyeColor.CODEC, "color", DyeColor.BLACK).forGetter(o -> o.color),
|
| 34 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.BOOL, "has_glowing_text", false).forGetter(o -> o.hasGlowingText)
|
| 35 | )
|
| 36 | .apply(i, SignText::load)
|
| 37 | )
|
| 38 | .validate(SignText::validateLineCounts);
|
| 39 | public static final StreamCodec<RegistryFriendlyByteBuf, SignText> STREAM_CODEC = StreamCodec.composite(
|
| 40 | ComponentSerialization.STREAM_CODEC.apply(ByteBufCodecs.fixedSizeList(4)),
|
| 41 | o -> o.messages,
|
| 42 | ComponentSerialization.STREAM_CODEC.apply(ByteBufCodecs.fixedSizeList(4)).apply(ByteBufCodecs::optional),
|
| 43 | SignText::filteredMessagesForSerialization,
|
| 44 | DyeColor.STREAM_CODEC,
|
| 45 | o -> o.color,
|
| 46 | ByteBufCodecs.BOOL,
|
| 47 | o -> o.hasGlowingText,
|
| 48 | SignText::load
|
| 49 | );
|
| 50 | public static final TooltipProvider.Getter<SignText> FRONT_TEXT = createTooltip(Component.translatable("sign.front_text").withStyle(ChatFormatting.GRAY));
|
| 51 | public static final TooltipProvider.Getter<SignText> BACK_TEXT = createTooltip(Component.translatable("sign.back_text").withStyle(ChatFormatting.GRAY));
|
| 52 | private static final List<Component> EMPTY_MESSAGES = Collections.nCopies(4, CommonComponents.EMPTY);
|
| 53 | public static final SignText EMPTY = new SignText(EMPTY_MESSAGES, EMPTY_MESSAGES, DyeColor.BLACK, false);
|
| 54 | private final List<Component> messages;
|
| 55 | private final List<Component> filteredMessages;
|
| 56 | private final DyeColor color;
|
| 57 | private final boolean hasGlowingText;
|
| 58 | private FormattedCharSequence @Nullable [] renderMessages;
|
| 59 | private boolean renderMessagedFiltered;
|
| 60 |
|
| 61 | public SignText(final List<Component> messages, final List<Component> filteredMessages, final DyeColor color, final boolean hasGlowingText) {
|
| 62 | this.messages = messages;
|
| 63 | this.filteredMessages = filteredMessages;
|
| 64 | this.color = color;
|
| 65 | this.hasGlowingText = hasGlowingText;
|
| 66 | }
|
| 67 |
|
| 68 | private static SignText load(
|
| 69 | final List<Component> messages, final Optional<List<Component>> filteredMessages, final DyeColor color, final boolean hasGlowingText
|
| 70 | ) {
|
| 71 | return new SignText(messages, filteredMessages.orElse(messages), color, hasGlowingText);
|
| 72 | }
|
| 73 |
|
| 74 | private static DataResult<SignText> validateLineCounts(final SignText signText) {
|
| 75 | return signText.filteredMessages.size() != signText.messages.size()
|
| 76 | ? DataResult.error(() -> "Filtered and raw line counts are not equal")
|
| 77 | : DataResult.success(signText);
|
| 78 | }
|
| 79 |
|
| 80 | public boolean hasGlowingText() {
|
| 81 | return this.hasGlowingText;
|
| 82 | }
|
| 83 |
|
| 84 | public SignText withGlowingText(final boolean hasGlowingText) {
|
| 85 | return hasGlowingText == this.hasGlowingText ? this : new SignText(this.messages, this.filteredMessages, this.color, hasGlowingText);
|
| 86 | }
|
| 87 |
|
| 88 | public DyeColor getColor() {
|
| 89 | return this.color;
|
| 90 | }
|
| 91 |
|
| 92 | public SignText withColor(final DyeColor color) {
|
| 93 | return color == this.getColor() ? this : new SignText(this.messages, this.filteredMessages, color, this.hasGlowingText);
|
| 94 | }
|
| 95 |
|
| 96 | public boolean hasMessage(final boolean shouldFilter) {
|
| 97 | return this.getMessages(shouldFilter).stream().anyMatch(component -> !component.getString().isEmpty());
|
| 98 | }
|
| 99 |
|
| 100 | public List<Component> getMessages(final boolean shouldFilter) {
|
| 101 | return shouldFilter ? this.filteredMessages : this.messages;
|
| 102 | }
|
| 103 |
|
| 104 | public FormattedCharSequence[] getRenderMessages(final boolean shouldFilter, final Function<Component, FormattedCharSequence> prepare) {
|
| 105 | if (this.renderMessages == null || this.renderMessagedFiltered != shouldFilter) {
|
| 106 | List<Component> messages = this.getMessages(shouldFilter);
|
| 107 | this.renderMessagedFiltered = shouldFilter;
|
| 108 | this.renderMessages = new FormattedCharSequence[messages.size()];
|
| 109 |
|
| 110 | for (int i = 0; i < messages.size(); i++) {
|
| 111 | this.renderMessages[i] = prepare.apply(messages.get(i));
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | return this.renderMessages;
|
| 116 | }
|
| 117 |
|
| 118 | private Optional<List<Component>> filteredMessagesForSerialization() {
|
| 119 | return this.filteredMessages.equals(this.messages) ? Optional.empty() : Optional.of(this.filteredMessages);
|
| 120 | }
|
| 121 |
|
| 122 | public boolean hasAnyClickCommands(final boolean shouldFilter) {
|
| 123 | for (Component message : this.getMessages(shouldFilter)) {
|
| 124 | Style style = message.getStyle();
|
| 125 | ClickEvent event = style.getClickEvent();
|
| 126 | if (event != null && event.action() == ClickEvent.Action.RUN_COMMAND) {
|
| 127 | return true;
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | return false;
|
| 132 | }
|
| 133 |
|
| 134 | public boolean hasEditableText(final boolean shouldFilter) {
|
| 135 | return this.getMessages(shouldFilter)
|
| 136 | .stream()
|
| 137 | .allMatch(message -> message.equals(CommonComponents.EMPTY) || message.getContents() instanceof PlainTextContents);
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public boolean equals(final Object object) {
|
| 142 | return !(object instanceof SignText signText)
|
| 143 | ? false
|
| 144 | : this.hasGlowingText == signText.hasGlowingText
|
| 145 | && this.messages.equals(signText.messages)
|
| 146 | && this.filteredMessages.equals(signText.filteredMessages)
|
| 147 | && this.color == signText.color;
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public int hashCode() {
|
| 152 | int result = 1;
|
| 153 | result = 31 * result + this.messages.hashCode();
|
| 154 | result = 31 * result + this.filteredMessages.hashCode();
|
| 155 | result = 31 * result + this.color.hashCode();
|
| 156 | return 31 * result + Boolean.hashCode(this.hasGlowingText);
|
| 157 | }
|
| 158 |
|
| 159 | public SignText.Mutable asMutable() {
|
| 160 | return new SignText.Mutable(this);
|
| 161 | }
|
| 162 |
|
| 163 | public static TooltipProvider.Getter<SignText> createTooltip(final Component title) {
|
| 164 | return component -> component.equals(EMPTY) ? (var0x, var1x, var2, var3) -> {} : (var2, consumer, var4, var5) -> {
|
| 165 | consumer.accept(title);
|
| 166 |
|
| 167 | for (Component message : component.messages) {
|
| 168 | consumer.accept(Component.literal(" ").append(message));
|
| 169 | }
|
| 170 | };
|
| 171 | }
|
| 172 |
|
| 173 | public static class Mutable {
|
| 174 | private final Component[] messages;
|
| 175 | private final Component[] filteredMessages;
|
| 176 | private DyeColor color;
|
| 177 | private boolean isTextGlowing;
|
| 178 |
|
| 179 | private Mutable(final SignText original) {
|
| 180 | this.messages = original.messages.toArray(Component[]::new);
|
| 181 | this.filteredMessages = original.messages.toArray(Component[]::new);
|
| 182 | this.color = original.color;
|
| 183 | this.isTextGlowing = original.hasGlowingText;
|
| 184 | }
|
| 185 |
|
| 186 | public SignText asImmutable() {
|
| 187 | return new SignText(List.of(this.messages), List.of(this.filteredMessages), this.color, this.isTextGlowing);
|
| 188 | }
|
| 189 |
|
| 190 | public SignText.Mutable setLine(final int index, final Component raw, final Component filtered) {
|
| 191 | this.messages[index] = raw;
|
| 192 | this.filteredMessages[index] = filtered;
|
| 193 | return this;
|
| 194 | }
|
| 195 |
|
| 196 | public SignText.Mutable setLine(final int index, final Component text) {
|
| 197 | return this.setLine(index, text, text);
|
| 198 | }
|
| 199 |
|
| 200 | public SignText.Mutable modifyLine(final int index, final UnaryOperator<Component> modifier) {
|
| 201 | this.messages[index] = modifier.apply(this.messages[index]);
|
| 202 | this.filteredMessages[index] = modifier.apply(this.filteredMessages[index]);
|
| 203 | return this;
|
| 204 | }
|
| 205 |
|
| 206 | public SignText.Mutable modifyLines(final UnaryOperator<Component> modifier) {
|
| 207 | for (int i = 0; i < this.messages.length; i++) {
|
| 208 | this.modifyLine(i, modifier);
|
| 209 | }
|
| 210 |
|
| 211 | return this;
|
| 212 | }
|
| 213 |
|
| 214 | public SignText.Mutable setColor(final DyeColor color) {
|
| 215 | this.color = color;
|
| 216 | return this;
|
| 217 | }
|
| 218 |
|
| 219 | public SignText.Mutable setTextGlowing(final boolean flag) {
|
| 220 | this.isTextGlowing = flag;
|
| 221 | return this;
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 |
|