| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.NonNullList;
|
| 6 | import net.minecraft.core.component.DataComponentGetter;
|
| 7 | import net.minecraft.core.component.DataComponentMap;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.network.chat.ComponentSerialization;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.sounds.SoundEvents;
|
| 13 | import net.minecraft.sounds.SoundSource;
|
| 14 | import net.minecraft.world.Container;
|
| 15 | import net.minecraft.world.ContainerHelper;
|
| 16 | import net.minecraft.world.LockCode;
|
| 17 | import net.minecraft.world.MenuProvider;
|
| 18 | import net.minecraft.world.Nameable;
|
| 19 | import net.minecraft.world.entity.player.Inventory;
|
| 20 | import net.minecraft.world.entity.player.Player;
|
| 21 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 22 | import net.minecraft.world.item.ItemStack;
|
| 23 | import net.minecraft.world.item.component.ItemContainerContents;
|
| 24 | import net.minecraft.world.level.Level;
|
| 25 | import net.minecraft.world.level.block.state.BlockState;
|
| 26 | import net.minecraft.world.level.storage.ValueInput;
|
| 27 | import net.minecraft.world.level.storage.ValueOutput;
|
| 28 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 29 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 30 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 31 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 32 | import net.minecraft.world.phys.Vec3;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 |
|
| 35 | public abstract class BaseContainerBlockEntity extends BlockEntity implements Container, MenuProvider, Nameable {
|
| 36 | private LockCode lockKey = LockCode.NO_LOCK;
|
| 37 | private @Nullable Component name;
|
| 38 |
|
| 39 | protected BaseContainerBlockEntity(final BlockEntityType<?> type, final BlockPos worldPosition, final BlockState blockState) {
|
| 40 | super(type, worldPosition, blockState);
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void loadAdditional(final ValueInput input) {
|
| 45 | super.loadAdditional(input);
|
| 46 | this.lockKey = LockCode.fromTag(input);
|
| 47 | this.name = parseCustomNameSafe(input, "CustomName");
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected void saveAdditional(final ValueOutput output) {
|
| 52 | super.saveAdditional(output);
|
| 53 | this.lockKey.addToTag(output);
|
| 54 | output.storeNullable("CustomName", ComponentSerialization.CODEC, this.name);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public Component getName() {
|
| 59 | return this.name != null ? this.name : this.getDefaultName();
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public Component getDisplayName() {
|
| 64 | return this.getName();
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public @Nullable Component getCustomName() {
|
| 69 | return this.name;
|
| 70 | }
|
| 71 |
|
| 72 | protected abstract Component getDefaultName();
|
| 73 |
|
| 74 | public boolean canOpen(final Player player) {
|
| 75 | return this.lockKey.canUnlock(player);
|
| 76 | }
|
| 77 |
|
| 78 | public static void sendChestLockedNotifications(final Vec3 pos, final Player player, final Component displayName) {
|
| 79 | Level level = player.level();
|
| 80 | player.sendOverlayMessage(Component.translatable("container.isLocked", displayName));
|
| 81 | if (!level.isClientSide()) {
|
| 82 | level.playSound(null, pos.x(), pos.y(), pos.z(), SoundEvents.CHEST_LOCKED, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public boolean isLocked() {
|
| 87 | return !this.lockKey.equals(LockCode.NO_LOCK);
|
| 88 | }
|
| 89 |
|
| 90 | protected abstract NonNullList<ItemStack> getItems();
|
| 91 |
|
| 92 | protected abstract void setItems(NonNullList<ItemStack> items);
|
| 93 |
|
| 94 | @Override
|
| 95 | public boolean isEmpty() {
|
| 96 | for (ItemStack itemStack : this.getItems()) {
|
| 97 | if (!itemStack.isEmpty()) {
|
| 98 | return false;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | return true;
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public ItemStack getItem(final int slot) {
|
| 107 | return this.getItems().get(slot);
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public ItemStack removeItem(final int slot, final int count) {
|
| 112 | ItemStack result = ContainerHelper.removeItem(this.getItems(), slot, count);
|
| 113 | if (!result.isEmpty()) {
|
| 114 | this.setChanged();
|
| 115 | }
|
| 116 |
|
| 117 | return result;
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public ItemStack removeItemNoUpdate(final int slot) {
|
| 122 | return ContainerHelper.takeItem(this.getItems(), slot);
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public void setItem(final int slot, final ItemStack itemStack) {
|
| 127 | this.getItems().set(slot, itemStack);
|
| 128 | itemStack.limitSize(this.getMaxStackSize(itemStack));
|
| 129 | this.setChanged();
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public boolean stillValid(final Player player) {
|
| 134 | return Container.stillValidBlockEntity(this, player);
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public void clearContent() {
|
| 139 | this.getItems().clear();
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | public @Nullable AbstractContainerMenu createMenu(final int containerId, final Inventory inventory, final Player player) {
|
| 144 | if (this.canOpen(player)) {
|
| 145 | return this.createMenu(containerId, inventory);
|
| 146 | }
|
| 147 |
|
| 148 | sendChestLockedNotifications(Vec3.atCenterOf(this.getBlockPos()), player, this.getDisplayName());
|
| 149 | return null;
|
| 150 | }
|
| 151 |
|
| 152 | protected abstract AbstractContainerMenu createMenu(final int containerId, final Inventory inventory);
|
| 153 |
|
| 154 | @Override
|
| 155 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 156 | super.applyImplicitComponents(components);
|
| 157 | this.name = components.get(DataComponents.CUSTOM_NAME);
|
| 158 | this.lockKey = components.getOrDefault(DataComponents.LOCK, LockCode.NO_LOCK);
|
| 159 | components.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY).copyInto(this.getItems());
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 164 | super.collectImplicitComponents(components);
|
| 165 | components.set(DataComponents.CUSTOM_NAME, this.name);
|
| 166 | if (this.isLocked()) {
|
| 167 | components.set(DataComponents.LOCK, this.lockKey);
|
| 168 | }
|
| 169 |
|
| 170 | components.set(DataComponents.CONTAINER, ItemContainerContents.fromItems(this.getItems()));
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 175 | output.discard("CustomName");
|
| 176 | output.discard("lock");
|
| 177 | output.discard("Items");
|
| 178 | }
|
| 179 |
|
| 180 | protected LootContext getLootContext(final ServerLevel level) {
|
| 181 | return new LootContext.Builder(
|
| 182 | new LootParams.Builder(level)
|
| 183 | .withParameter(LootContextParams.BLOCK_STATE, this.getBlockState())
|
| 184 | .withParameter(LootContextParams.BLOCK_ENTITY, this)
|
| 185 | .withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(this.getBlockPos()))
|
| 186 | .withParameter(LootContextParams.CONTAINER, this)
|
| 187 | .create(LootContextParamSets.CONTAINER_PROCESS)
|
| 188 | )
|
| 189 | .create(Optional.empty());
|
| 190 | }
|
| 191 | }
|
| 192 |
|