| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.stream.IntStream;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.NonNullList;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.world.ContainerHelper;
|
| 13 | import net.minecraft.world.WorldlyContainer;
|
| 14 | import net.minecraft.world.entity.ContainerUser;
|
| 15 | import net.minecraft.world.entity.Entity;
|
| 16 | import net.minecraft.world.entity.MoverType;
|
| 17 | import net.minecraft.world.entity.monster.Shulker;
|
| 18 | import net.minecraft.world.entity.player.Inventory;
|
| 19 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 20 | import net.minecraft.world.inventory.ShulkerBoxMenu;
|
| 21 | import net.minecraft.world.item.DyeColor;
|
| 22 | import net.minecraft.world.item.ItemStack;
|
| 23 | import net.minecraft.world.level.Level;
|
| 24 | import net.minecraft.world.level.block.Block;
|
| 25 | import net.minecraft.world.level.block.ShulkerBoxBlock;
|
| 26 | import net.minecraft.world.level.block.state.BlockState;
|
| 27 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 28 | import net.minecraft.world.level.material.PushReaction;
|
| 29 | import net.minecraft.world.level.storage.ValueInput;
|
| 30 | import net.minecraft.world.level.storage.ValueOutput;
|
| 31 | import net.minecraft.world.phys.AABB;
|
| 32 | import net.minecraft.world.phys.Vec3;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 |
|
| 35 | public class ShulkerBoxBlockEntity extends RandomizableContainerBlockEntity implements WorldlyContainer {
|
| 36 | public static final int COLUMNS = 9;
|
| 37 | public static final int ROWS = 3;
|
| 38 | public static final int CONTAINER_SIZE = 27;
|
| 39 | public static final int EVENT_SET_OPEN_COUNT = 1;
|
| 40 | public static final int OPENING_TICK_LENGTH = 10;
|
| 41 | public static final float MAX_LID_HEIGHT = 0.5F;
|
| 42 | public static final float MAX_LID_ROTATION = 270.0F;
|
| 43 | private static final int[] SLOTS = IntStream.range(0, 27).toArray();
|
| 44 | private static final Component DEFAULT_NAME = Component.translatable("container.shulkerBox");
|
| 45 | private NonNullList<ItemStack> itemStacks = NonNullList.withSize(27, ItemStack.EMPTY);
|
| 46 | private int openCount;
|
| 47 | private ShulkerBoxBlockEntity.AnimationStatus animationStatus = ShulkerBoxBlockEntity.AnimationStatus.CLOSED;
|
| 48 | private float progress;
|
| 49 | private float progressOld;
|
| 50 | private final @Nullable DyeColor color;
|
| 51 |
|
| 52 | public ShulkerBoxBlockEntity(final @Nullable DyeColor color, final BlockPos worldPosition, final BlockState blockState) {
|
| 53 | super(BlockEntityTypes.SHULKER_BOX, worldPosition, blockState);
|
| 54 | this.color = color;
|
| 55 | }
|
| 56 |
|
| 57 | public ShulkerBoxBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 58 | super(BlockEntityTypes.SHULKER_BOX, worldPosition, blockState);
|
| 59 | this.color = blockState.getBlock() instanceof ShulkerBoxBlock shulkerBoxBlock ? shulkerBoxBlock.getColor() : null;
|
| 60 | }
|
| 61 |
|
| 62 | public static void tick(final Level level, final BlockPos pos, final BlockState state, final ShulkerBoxBlockEntity entity) {
|
| 63 | entity.updateAnimation(level, pos, state);
|
| 64 | }
|
| 65 |
|
| 66 | private void updateAnimation(final Level level, final BlockPos pos, final BlockState blockState) {
|
| 67 | this.progressOld = this.progress;
|
| 68 | switch (this.animationStatus) {
|
| 69 | case CLOSED:
|
| 70 | this.progress = 0.0F;
|
| 71 | break;
|
| 72 | case OPENING:
|
| 73 | this.progress += 0.1F;
|
| 74 | if (this.progressOld == 0.0F) {
|
| 75 | doNeighborUpdates(level, pos, blockState);
|
| 76 | }
|
| 77 |
|
| 78 | if (this.progress >= 1.0F) {
|
| 79 | this.animationStatus = ShulkerBoxBlockEntity.AnimationStatus.OPENED;
|
| 80 | this.progress = 1.0F;
|
| 81 | doNeighborUpdates(level, pos, blockState);
|
| 82 | }
|
| 83 |
|
| 84 | this.moveCollidedEntities(level, pos, blockState);
|
| 85 | break;
|
| 86 | case OPENED:
|
| 87 | this.progress = 1.0F;
|
| 88 | break;
|
| 89 | case CLOSING:
|
| 90 | this.progress -= 0.1F;
|
| 91 | if (this.progressOld == 1.0F) {
|
| 92 | doNeighborUpdates(level, pos, blockState);
|
| 93 | }
|
| 94 |
|
| 95 | if (this.progress <= 0.0F) {
|
| 96 | this.animationStatus = ShulkerBoxBlockEntity.AnimationStatus.CLOSED;
|
| 97 | this.progress = 0.0F;
|
| 98 | doNeighborUpdates(level, pos, blockState);
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public ShulkerBoxBlockEntity.AnimationStatus getAnimationStatus() {
|
| 104 | return this.animationStatus;
|
| 105 | }
|
| 106 |
|
| 107 | public AABB getBoundingBox(final BlockState state) {
|
| 108 | Vec3 bottomCenter = new Vec3(0.5, 0.0, 0.5);
|
| 109 | return Shulker.getProgressAabb(1.0F, state.getValue(ShulkerBoxBlock.FACING), 0.5F * this.getProgress(1.0F), bottomCenter);
|
| 110 | }
|
| 111 |
|
| 112 | private void moveCollidedEntities(final Level level, final BlockPos pos, final BlockState state) {
|
| 113 | if (state.getBlock() instanceof ShulkerBoxBlock) {
|
| 114 | Direction direction = state.getValue(ShulkerBoxBlock.FACING);
|
| 115 | AABB aabb = Shulker.getProgressDeltaAabb(1.0F, direction, this.progressOld, this.progress, Vec3.atBottomCenterOf(pos));
|
| 116 | List<Entity> entities = level.getEntities(null, aabb);
|
| 117 | if (!entities.isEmpty()) {
|
| 118 | for (Entity entity : entities) {
|
| 119 | if (entity.getPistonPushReaction() != PushReaction.IGNORE_ENTITY) {
|
| 120 | entity.move(
|
| 121 | MoverType.SHULKER_BOX,
|
| 122 | new Vec3(
|
| 123 | (aabb.getXsize() + 0.01) * direction.getStepX(), (aabb.getYsize() + 0.01) * direction.getStepY(), (aabb.getZsize() + 0.01) * direction.getStepZ()
|
| 124 | )
|
| 125 | );
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public int getContainerSize() {
|
| 134 | return this.itemStacks.size();
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public boolean triggerEvent(final int b0, final int b1) {
|
| 139 | if (b0 == 1) {
|
| 140 | this.openCount = b1;
|
| 141 | if (b1 == 0) {
|
| 142 | this.animationStatus = ShulkerBoxBlockEntity.AnimationStatus.CLOSING;
|
| 143 | }
|
| 144 |
|
| 145 | if (b1 == 1) {
|
| 146 | this.animationStatus = ShulkerBoxBlockEntity.AnimationStatus.OPENING;
|
| 147 | }
|
| 148 |
|
| 149 | return true;
|
| 150 | } else {
|
| 151 | return super.triggerEvent(b0, b1);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | private static void doNeighborUpdates(final Level level, final BlockPos pos, final BlockState blockState) {
|
| 156 | blockState.updateNeighbourShapes(level, pos, 3);
|
| 157 | level.updateNeighborsAt(pos, blockState.getBlock());
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public void preRemoveSideEffects(final BlockPos pos, final BlockState state) {
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public void startOpen(final ContainerUser containerUser) {
|
| 166 | if (!this.remove && !containerUser.getLivingEntity().isSpectator()) {
|
| 167 | if (this.openCount < 0) {
|
| 168 | this.openCount = 0;
|
| 169 | }
|
| 170 |
|
| 171 | this.openCount++;
|
| 172 | this.level.blockEvent(this.worldPosition, this.getBlockState().getBlock(), 1, this.openCount);
|
| 173 | if (this.openCount == 1) {
|
| 174 | this.level.gameEvent(containerUser.getLivingEntity(), GameEvent.CONTAINER_OPEN, this.worldPosition);
|
| 175 | this.level.playSound(null, this.worldPosition, SoundEvents.SHULKER_BOX_OPEN, SoundSource.BLOCKS, 0.5F, this.level.getRandom().nextFloat() * 0.1F + 0.9F);
|
| 176 | }
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public void stopOpen(final ContainerUser containerUser) {
|
| 182 | if (!this.remove && !containerUser.getLivingEntity().isSpectator()) {
|
| 183 | this.openCount--;
|
| 184 | this.level.blockEvent(this.worldPosition, this.getBlockState().getBlock(), 1, this.openCount);
|
| 185 | if (this.openCount <= 0) {
|
| 186 | this.level.gameEvent(containerUser.getLivingEntity(), GameEvent.CONTAINER_CLOSE, this.worldPosition);
|
| 187 | this.level.playSound(null, this.worldPosition, SoundEvents.SHULKER_BOX_CLOSE, SoundSource.BLOCKS, 0.5F, this.level.getRandom().nextFloat() * 0.1F + 0.9F);
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | protected Component getDefaultName() {
|
| 194 | return DEFAULT_NAME;
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | protected void loadAdditional(final ValueInput input) {
|
| 199 | super.loadAdditional(input);
|
| 200 | this.loadFromTag(input);
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | protected void saveAdditional(final ValueOutput output) {
|
| 205 | super.saveAdditional(output);
|
| 206 | if (!this.trySaveLootTable(output)) {
|
| 207 | ContainerHelper.saveAllItems(output, this.itemStacks, false);
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | public void loadFromTag(final ValueInput input) {
|
| 212 | this.itemStacks = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
|
| 213 | if (!this.tryLoadLootTable(input)) {
|
| 214 | ContainerHelper.loadAllItems(input, this.itemStacks);
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | protected NonNullList<ItemStack> getItems() {
|
| 220 | return this.itemStacks;
|
| 221 | }
|
| 222 |
|
| 223 | @Override
|
| 224 | protected void setItems(final NonNullList<ItemStack> items) {
|
| 225 | this.itemStacks = items;
|
| 226 | }
|
| 227 |
|
| 228 | @Override
|
| 229 | public int[] getSlotsForFace(final Direction direction) {
|
| 230 | return SLOTS;
|
| 231 | }
|
| 232 |
|
| 233 | @Override
|
| 234 | public boolean canPlaceItemThroughFace(final int slot, final ItemStack itemStack, final @Nullable Direction direction) {
|
| 235 | return !(Block.byItem(itemStack.getItem()) instanceof ShulkerBoxBlock);
|
| 236 | }
|
| 237 |
|
| 238 | @Override
|
| 239 | public boolean canTakeItemThroughFace(final int slot, final ItemStack itemStack, final Direction direction) {
|
| 240 | return true;
|
| 241 | }
|
| 242 |
|
| 243 | public float getProgress(final float a) {
|
| 244 | return Mth.lerp(a, this.progressOld, this.progress);
|
| 245 | }
|
| 246 |
|
| 247 | public @Nullable DyeColor getColor() {
|
| 248 | return this.color;
|
| 249 | }
|
| 250 |
|
| 251 | @Override
|
| 252 | protected AbstractContainerMenu createMenu(final int containerId, final Inventory inventory) {
|
| 253 | return new ShulkerBoxMenu(containerId, inventory, this);
|
| 254 | }
|
| 255 |
|
| 256 | public boolean isClosed() {
|
| 257 | return this.animationStatus == ShulkerBoxBlockEntity.AnimationStatus.CLOSED;
|
| 258 | }
|
| 259 |
|
| 260 | public enum AnimationStatus {
|
| 261 | CLOSED,
|
| 262 | OPENING,
|
| 263 | OPENED,
|
| 264 | CLOSING;
|
| 265 | }
|
| 266 | }
|
| 267 |
|