|
| 1 | +package io.papermc.paper.datacomponent.item; |
| 2 | + |
| 3 | +import com.google.common.base.Preconditions; |
| 4 | +import io.papermc.paper.adventure.PaperAdventure; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import net.kyori.adventure.text.Component; |
| 8 | +import net.kyori.adventure.text.ComponentLike; |
| 9 | +import org.bukkit.DyeColor; |
| 10 | +import org.bukkit.craftbukkit.util.Handleable; |
| 11 | + |
| 12 | +public record PaperSignText( |
| 13 | + net.minecraft.world.level.block.entity.SignText impl |
| 14 | +) implements SignText, Handleable<net.minecraft.world.level.block.entity.SignText> { |
| 15 | + |
| 16 | + @Override |
| 17 | + public net.minecraft.world.level.block.entity.SignText getHandle() { |
| 18 | + return this.impl; |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + public List<Component> messages() { |
| 23 | + return io.papermc.paper.adventure.PaperAdventure.asAdventure(this.impl.getMessages(false)); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public List<Component> filteredMessages() { |
| 28 | + return io.papermc.paper.adventure.PaperAdventure.asAdventure(this.impl.getMessages(true)); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public DyeColor color() { |
| 33 | + return DyeColor.getByWoolData((byte) this.impl.getColor().getId()); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public boolean hasGlowingText() { |
| 38 | + return this.impl.hasGlowingText(); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public Builder toBuilder() { |
| 43 | + return new BuilderImpl(this.impl.getMessages(false)) |
| 44 | + .color(DyeColor.getByWoolData((byte) this.impl.getColor().getId())) |
| 45 | + .hasGlowingText(this.impl.hasGlowingText()); |
| 46 | + } |
| 47 | + |
| 48 | + static final class BuilderImpl implements Builder { |
| 49 | + |
| 50 | + private List<net.minecraft.network.chat.Component> messages = net.minecraft.world.level.block.entity.SignText.EMPTY.getMessages(false); |
| 51 | + private List<net.minecraft.network.chat.Component> filteredMessages = net.minecraft.world.level.block.entity.SignText.EMPTY.getMessages(true); |
| 52 | + private net.minecraft.world.item.DyeColor color = net.minecraft.world.level.block.entity.SignText.EMPTY.getColor(); |
| 53 | + private boolean hasGlowingText = net.minecraft.world.level.block.entity.SignText.EMPTY.hasGlowingText(); |
| 54 | + |
| 55 | + BuilderImpl() { |
| 56 | + } |
| 57 | + |
| 58 | + BuilderImpl(List<net.minecraft.network.chat.Component> messages) { |
| 59 | + this.messages = messages; |
| 60 | + this.filteredMessages = messages; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Builder messages(final List<? extends ComponentLike> messages) { |
| 65 | + validateLineCount(0, messages.size()); |
| 66 | + this.messages = PaperAdventure.asVanilla(new ArrayList<>(ComponentLike.asComponents(messages))); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Builder addMessage(final ComponentLike message) { |
| 72 | + validateLineCount(this.messages.size(), 1); |
| 73 | + this.messages.add(PaperAdventure.asVanilla(message.asComponent())); |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Builder color(final DyeColor color) { |
| 79 | + this.color = net.minecraft.world.item.DyeColor.byId(color.getWoolData()); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Builder hasGlowingText(final boolean hasGlowingText) { |
| 85 | + this.hasGlowingText = hasGlowingText; |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public SignText build() { |
| 91 | + if (this.messages.isEmpty()) { |
| 92 | + return new PaperSignText(net.minecraft.world.level.block.entity.SignText.EMPTY); |
| 93 | + } |
| 94 | + |
| 95 | + return new PaperSignText(new net.minecraft.world.level.block.entity.SignText( |
| 96 | + this.messages, |
| 97 | + (this.filteredMessages.isEmpty()) ? this.messages : this.filteredMessages, |
| 98 | + this.color, |
| 99 | + this.hasGlowingText |
| 100 | + )); |
| 101 | + } |
| 102 | + |
| 103 | + private static void validateLineCount(final int current, final int add) { |
| 104 | + final int newSize = current + add; |
| 105 | + Preconditions.checkArgument( |
| 106 | + newSize <= net.minecraft.world.level.block.entity.SignText.LINES, |
| 107 | + "Cannot have more than %s lines, had %s", |
| 108 | + net.minecraft.world.level.block.entity.SignText.LINES, |
| 109 | + newSize |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments