| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.component.DataComponentGetter;
|
| 5 | import net.minecraft.core.component.DataComponentMap;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.resources.ResourceKey;
|
| 8 | import net.minecraft.world.RandomizableContainer;
|
| 9 | import net.minecraft.world.entity.player.Inventory;
|
| 10 | import net.minecraft.world.entity.player.Player;
|
| 11 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.item.component.SeededContainerLoot;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.storage.ValueOutput;
|
| 16 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 17 | import net.minecraft.world.phys.Vec3;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | public abstract class RandomizableContainerBlockEntity extends BaseContainerBlockEntity implements RandomizableContainer {
|
| 21 | protected @Nullable ResourceKey<LootTable> lootTable;
|
| 22 | protected long lootTableSeed = 0L;
|
| 23 |
|
| 24 | protected RandomizableContainerBlockEntity(final BlockEntityType<?> type, final BlockPos worldPosition, final BlockState blockState) {
|
| 25 | super(type, worldPosition, blockState);
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public @Nullable ResourceKey<LootTable> getLootTable() {
|
| 30 | return this.lootTable;
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public void setLootTable(final @Nullable ResourceKey<LootTable> lootTable) {
|
| 35 | this.lootTable = lootTable;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public long getLootTableSeed() {
|
| 40 | return this.lootTableSeed;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void setLootTableSeed(final long lootTableSeed) {
|
| 45 | this.lootTableSeed = lootTableSeed;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public boolean isEmpty() {
|
| 50 | this.unpackLootTable(null);
|
| 51 | return super.isEmpty();
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public ItemStack getItem(final int slot) {
|
| 56 | this.unpackLootTable(null);
|
| 57 | return super.getItem(slot);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public ItemStack removeItem(final int slot, final int count) {
|
| 62 | this.unpackLootTable(null);
|
| 63 | return super.removeItem(slot, count);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public ItemStack removeItemNoUpdate(final int slot) {
|
| 68 | this.unpackLootTable(null);
|
| 69 | return super.removeItemNoUpdate(slot);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public void setItem(final int slot, final ItemStack itemStack) {
|
| 74 | this.unpackLootTable(null);
|
| 75 | super.setItem(slot, itemStack);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public boolean canOpen(final Player player) {
|
| 80 | return (this.lootTable == null || !player.isSpectator()) && super.canOpen(player);
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public @Nullable AbstractContainerMenu createMenu(final int containerId, final Inventory inventory, final Player player) {
|
| 85 | if (this.canOpen(player)) {
|
| 86 | this.unpackLootTable(inventory.player);
|
| 87 | return this.createMenu(containerId, inventory);
|
| 88 | }
|
| 89 |
|
| 90 | if (!player.isSpectator()) {
|
| 91 | BaseContainerBlockEntity.sendChestLockedNotifications(Vec3.atCenterOf(this.getBlockPos()), player, this.getDisplayName());
|
| 92 | }
|
| 93 |
|
| 94 | return null;
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 99 | super.applyImplicitComponents(components);
|
| 100 | SeededContainerLoot loot = components.get(DataComponents.CONTAINER_LOOT);
|
| 101 | if (loot != null) {
|
| 102 | this.lootTable = loot.lootTable();
|
| 103 | this.lootTableSeed = loot.seed();
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 109 | super.collectImplicitComponents(components);
|
| 110 | if (this.lootTable != null) {
|
| 111 | components.set(DataComponents.CONTAINER_LOOT, new SeededContainerLoot(this.lootTable, this.lootTableSeed));
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 117 | super.removeComponentsFromTag(output);
|
| 118 | output.discard("LootTable");
|
| 119 | output.discard("LootTableSeed");
|
| 120 | }
|
| 121 | }
|
| 122 |
|