| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.NonNullList;
|
| 6 | import net.minecraft.core.Vec3i;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.world.Container;
|
| 12 | import net.minecraft.world.ContainerHelper;
|
| 13 | import net.minecraft.world.entity.ContainerUser;
|
| 14 | import net.minecraft.world.entity.player.Inventory;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 17 | import net.minecraft.world.inventory.ChestMenu;
|
| 18 | import net.minecraft.world.item.ItemStack;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.block.BarrelBlock;
|
| 21 | import net.minecraft.world.level.block.state.BlockState;
|
| 22 | import net.minecraft.world.level.storage.ValueInput;
|
| 23 | import net.minecraft.world.level.storage.ValueOutput;
|
| 24 |
|
| 25 | public class BarrelBlockEntity extends RandomizableContainerBlockEntity {
|
| 26 | private static final Component DEFAULT_NAME = Component.translatable("container.barrel");
|
| 27 | private NonNullList<ItemStack> items = NonNullList.withSize(27, ItemStack.EMPTY);
|
| 28 | private final ContainerOpenersCounter openersCounter = new ContainerOpenersCounter() {
|
| 29 | @Override
|
| 30 | protected void onOpen(final Level level, final BlockPos pos, final BlockState state) {
|
| 31 | BarrelBlockEntity.this.playSound(state, SoundEvents.BARREL_OPEN);
|
| 32 | BarrelBlockEntity.this.updateBlockState(state, true);
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | protected void onClose(final Level level, final BlockPos pos, final BlockState state) {
|
| 37 | BarrelBlockEntity.this.playSound(state, SoundEvents.BARREL_CLOSE);
|
| 38 | BarrelBlockEntity.this.updateBlockState(state, false);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected void openerCountChanged(final Level level, final BlockPos pos, final BlockState blockState, final int previous, final int current) {
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public boolean isOwnContainer(final Player player) {
|
| 47 | if (player.containerMenu instanceof ChestMenu) {
|
| 48 | Container container = ((ChestMenu)player.containerMenu).getContainer();
|
| 49 | return container == BarrelBlockEntity.this;
|
| 50 | } else {
|
| 51 | return false;
|
| 52 | }
|
| 53 | }
|
| 54 | };
|
| 55 |
|
| 56 | public BarrelBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 57 | super(BlockEntityTypes.BARREL, worldPosition, blockState);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected void saveAdditional(final ValueOutput output) {
|
| 62 | super.saveAdditional(output);
|
| 63 | if (!this.trySaveLootTable(output)) {
|
| 64 | ContainerHelper.saveAllItems(output, this.items);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | protected void loadAdditional(final ValueInput input) {
|
| 70 | super.loadAdditional(input);
|
| 71 | this.items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
|
| 72 | if (!this.tryLoadLootTable(input)) {
|
| 73 | ContainerHelper.loadAllItems(input, this.items);
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public int getContainerSize() {
|
| 79 | return 27;
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | protected NonNullList<ItemStack> getItems() {
|
| 84 | return this.items;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | protected void setItems(final NonNullList<ItemStack> items) {
|
| 89 | this.items = items;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | protected Component getDefaultName() {
|
| 94 | return DEFAULT_NAME;
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected AbstractContainerMenu createMenu(final int containerId, final Inventory inventory) {
|
| 99 | return ChestMenu.threeRows(containerId, inventory, this);
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public void startOpen(final ContainerUser containerUser) {
|
| 104 | if (!this.remove && !containerUser.getLivingEntity().isSpectator()) {
|
| 105 | this.openersCounter
|
| 106 | .incrementOpeners(containerUser.getLivingEntity(), this.getLevel(), this.getBlockPos(), this.getBlockState(), containerUser.getContainerInteractionRange());
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public void stopOpen(final ContainerUser containerUser) {
|
| 112 | if (!this.remove && !containerUser.getLivingEntity().isSpectator()) {
|
| 113 | this.openersCounter.decrementOpeners(containerUser.getLivingEntity(), this.getLevel(), this.getBlockPos(), this.getBlockState());
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public List<ContainerUser> getEntitiesWithContainerOpen() {
|
| 119 | return this.openersCounter.getEntitiesWithContainerOpen(this.getLevel(), this.getBlockPos());
|
| 120 | }
|
| 121 |
|
| 122 | public void recheckOpen() {
|
| 123 | if (!this.remove) {
|
| 124 | this.openersCounter.recheckOpeners(this.getLevel(), this.getBlockPos(), this.getBlockState());
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | private void updateBlockState(final BlockState state, final boolean isOpen) {
|
| 129 | this.level.setBlockAndUpdate(this.getBlockPos(), state.setValue(BarrelBlock.OPEN, isOpen));
|
| 130 | }
|
| 131 |
|
| 132 | private void playSound(final BlockState state, final SoundEvent event) {
|
| 133 | Vec3i direction = state.getValue(BarrelBlock.FACING).getUnitVec3i();
|
| 134 | double x = this.worldPosition.getX() + 0.5 + direction.getX() / 2.0;
|
| 135 | double y = this.worldPosition.getY() + 0.5 + direction.getY() / 2.0;
|
| 136 | double z = this.worldPosition.getZ() + 0.5 + direction.getZ() / 2.0;
|
| 137 | this.level.playSound(null, x, y, z, event, SoundSource.BLOCKS, 0.5F, this.level.getRandom().nextFloat() * 0.1F + 0.9F);
|
| 138 | }
|
| 139 | }
|
| 140 |
|