| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import net.minecraft.commands.CommandSource;
|
| 4 | import net.minecraft.commands.CommandSourceStack;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.network.chat.ResolutionContext;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.server.permissions.LevelBasedPermissionSet;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.world.Clearable;
|
| 14 | import net.minecraft.world.Container;
|
| 15 | import net.minecraft.world.MenuProvider;
|
| 16 | import net.minecraft.world.entity.item.ItemEntity;
|
| 17 | import net.minecraft.world.entity.player.Inventory;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 20 | import net.minecraft.world.inventory.ContainerData;
|
| 21 | import net.minecraft.world.inventory.LecternMenu;
|
| 22 | import net.minecraft.world.item.ItemStack;
|
| 23 | import net.minecraft.world.item.component.WritableBookContent;
|
| 24 | import net.minecraft.world.item.component.WrittenBookContent;
|
| 25 | import net.minecraft.world.level.block.LecternBlock;
|
| 26 | import net.minecraft.world.level.block.state.BlockState;
|
| 27 | import net.minecraft.world.level.storage.ValueInput;
|
| 28 | import net.minecraft.world.level.storage.ValueOutput;
|
| 29 | import net.minecraft.world.phys.Vec2;
|
| 30 | import net.minecraft.world.phys.Vec3;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | public class LecternBlockEntity extends BlockEntity implements Clearable, MenuProvider {
|
| 34 | public static final int DATA_PAGE = 0;
|
| 35 | public static final int NUM_DATA = 1;
|
| 36 | public static final int SLOT_BOOK = 0;
|
| 37 | public static final int NUM_SLOTS = 1;
|
| 38 | private final Container bookAccess = new Container() {
|
| 39 | @Override
|
| 40 | public int getContainerSize() {
|
| 41 | return 1;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public boolean isEmpty() {
|
| 46 | return LecternBlockEntity.this.book.isEmpty();
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public ItemStack getItem(final int slot) {
|
| 51 | return slot == 0 ? LecternBlockEntity.this.book : ItemStack.EMPTY;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public ItemStack removeItem(final int slot, final int count) {
|
| 56 | if (slot == 0) {
|
| 57 | ItemStack result = LecternBlockEntity.this.book.split(count);
|
| 58 | if (LecternBlockEntity.this.book.isEmpty()) {
|
| 59 | LecternBlockEntity.this.onBookItemRemove();
|
| 60 | }
|
| 61 |
|
| 62 | return result;
|
| 63 | } else {
|
| 64 | return ItemStack.EMPTY;
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public ItemStack removeItemNoUpdate(final int slot) {
|
| 70 | if (slot == 0) {
|
| 71 | ItemStack prev = LecternBlockEntity.this.book;
|
| 72 | LecternBlockEntity.this.book = ItemStack.EMPTY;
|
| 73 | LecternBlockEntity.this.onBookItemRemove();
|
| 74 | return prev;
|
| 75 | } else {
|
| 76 | return ItemStack.EMPTY;
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public void setItem(final int slot, final ItemStack itemStack) {
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public int getMaxStackSize() {
|
| 86 | return 1;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public void setChanged() {
|
| 91 | LecternBlockEntity.this.setChanged();
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public boolean stillValid(final Player player) {
|
| 96 | return Container.stillValidBlockEntity(LecternBlockEntity.this, player) && LecternBlockEntity.this.hasBook();
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public boolean canPlaceItem(final int slot, final ItemStack itemStack) {
|
| 101 | return false;
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public void clearContent() {
|
| 106 | }
|
| 107 | };
|
| 108 | private final ContainerData dataAccess = new ContainerData() {
|
| 109 | @Override
|
| 110 | public int get(final int dataId) {
|
| 111 | return dataId == 0 ? LecternBlockEntity.this.page : 0;
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public void set(final int dataId, final int value) {
|
| 116 | if (dataId == 0) {
|
| 117 | LecternBlockEntity.this.setPage(value);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public int getCount() {
|
| 123 | return 1;
|
| 124 | }
|
| 125 | };
|
| 126 | private ItemStack book = ItemStack.EMPTY;
|
| 127 | private int page;
|
| 128 | private int pageCount;
|
| 129 |
|
| 130 | public LecternBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 131 | super(BlockEntityTypes.LECTERN, worldPosition, blockState);
|
| 132 | }
|
| 133 |
|
| 134 | public ItemStack getBook() {
|
| 135 | return this.book;
|
| 136 | }
|
| 137 |
|
| 138 | public boolean hasBook() {
|
| 139 | return this.book.has(DataComponents.WRITABLE_BOOK_CONTENT) || this.book.has(DataComponents.WRITTEN_BOOK_CONTENT);
|
| 140 | }
|
| 141 |
|
| 142 | public void setBook(final ItemStack book) {
|
| 143 | this.setBook(book, null);
|
| 144 | }
|
| 145 |
|
| 146 | private void onBookItemRemove() {
|
| 147 | this.page = 0;
|
| 148 | this.pageCount = 0;
|
| 149 | LecternBlock.resetBookState(null, this.getLevel(), this.getBlockPos(), this.getBlockState(), false);
|
| 150 | }
|
| 151 |
|
| 152 | public void setBook(final ItemStack book, final @Nullable Player resolutionContext) {
|
| 153 | this.book = this.resolveBook(book, resolutionContext);
|
| 154 | this.page = 0;
|
| 155 | this.pageCount = getPageCount(this.book);
|
| 156 | this.setChanged();
|
| 157 | }
|
| 158 |
|
| 159 | private void setPage(final int page) {
|
| 160 | int newPage = Mth.clamp(page, 0, this.pageCount - 1);
|
| 161 | if (newPage != this.page) {
|
| 162 | this.page = newPage;
|
| 163 | this.setChanged();
|
| 164 | LecternBlock.signalPageChange(this.getLevel(), this.getBlockPos(), this.getBlockState());
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | public int getPage() {
|
| 169 | return this.page;
|
| 170 | }
|
| 171 |
|
| 172 | public int getRedstoneSignal() {
|
| 173 | float pageProgress = this.pageCount > 1 ? this.getPage() / (this.pageCount - 1.0F) : 1.0F;
|
| 174 | return Mth.floor(pageProgress * 14.0F) + (this.hasBook() ? 1 : 0);
|
| 175 | }
|
| 176 |
|
| 177 | private ItemStack resolveBook(final ItemStack book, final @Nullable Player player) {
|
| 178 | if (this.level instanceof ServerLevel serverLevel) {
|
| 179 | ResolutionContext context = ResolutionContext.create(this.createCommandSourceStack(player, serverLevel));
|
| 180 | WrittenBookContent.resolveForItem(book, context, this.level.registryAccess());
|
| 181 | }
|
| 182 |
|
| 183 | return book;
|
| 184 | }
|
| 185 |
|
| 186 | private CommandSourceStack createCommandSourceStack(final @Nullable Player player, final ServerLevel level) {
|
| 187 | Vec3 pos = Vec3.atCenterOf(this.worldPosition);
|
| 188 | return player != null
|
| 189 | ? new CommandSourceStack(CommandSource.NULL, pos, Vec2.ZERO, level, LevelBasedPermissionSet.GAMEMASTER, level.getServer(), player)
|
| 190 | : new CommandSourceStack(CommandSource.NULL, pos, Vec2.ZERO, level, LevelBasedPermissionSet.GAMEMASTER, Component.literal("Lectern"), level.getServer());
|
| 191 | }
|
| 192 |
|
| 193 | @Override
|
| 194 | protected void loadAdditional(final ValueInput input) {
|
| 195 | super.loadAdditional(input);
|
| 196 | this.book = input.read("Book", ItemStack.CODEC).map(book -> this.resolveBook(book, null)).orElse(ItemStack.EMPTY);
|
| 197 | this.pageCount = getPageCount(this.book);
|
| 198 | this.page = Mth.clamp(input.getIntOr("Page", 0), 0, this.pageCount - 1);
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | protected void saveAdditional(final ValueOutput output) {
|
| 203 | super.saveAdditional(output);
|
| 204 | if (!this.getBook().isEmpty()) {
|
| 205 | output.store("Book", ItemStack.CODEC, this.getBook());
|
| 206 | output.putInt("Page", this.page);
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public void clearContent() {
|
| 212 | this.setBook(ItemStack.EMPTY);
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | public void preRemoveSideEffects(final BlockPos pos, final BlockState state) {
|
| 217 | if (state.getValue(LecternBlock.HAS_BOOK) && this.level != null) {
|
| 218 | Direction direction = state.getValue(LecternBlock.FACING);
|
| 219 | ItemStack book = this.getBook().copy();
|
| 220 | float xo = 0.25F * direction.getStepX();
|
| 221 | float zo = 0.25F * direction.getStepZ();
|
| 222 | ItemEntity entity = new ItemEntity(this.level, pos.getX() + 0.5 + xo, pos.getY() + 1, pos.getZ() + 0.5 + zo, book);
|
| 223 | entity.setDefaultPickUpDelay();
|
| 224 | this.level.addFreshEntity(entity);
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | @Override
|
| 229 | public AbstractContainerMenu createMenu(final int containerId, final Inventory inventory, final Player player) {
|
| 230 | return new LecternMenu(containerId, this.bookAccess, this.dataAccess);
|
| 231 | }
|
| 232 |
|
| 233 | @Override
|
| 234 | public Component getDisplayName() {
|
| 235 | return Component.translatable("container.lectern");
|
| 236 | }
|
| 237 |
|
| 238 | private static int getPageCount(final ItemStack book) {
|
| 239 | WrittenBookContent writtenContent = book.get(DataComponents.WRITTEN_BOOK_CONTENT);
|
| 240 | if (writtenContent != null) {
|
| 241 | return writtenContent.pages().size();
|
| 242 | }
|
| 243 |
|
| 244 | WritableBookContent writableContent = book.get(DataComponents.WRITABLE_BOOK_CONTENT);
|
| 245 | return writableContent != null ? writableContent.pages().size() : 0;
|
| 246 | }
|
| 247 | }
|
| 248 |
|