108 lines
2.9 KiB
Java
108 lines
2.9 KiB
Java
package net.minecraft.world.level.block.entity;
|
|
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.NonNullList;
|
|
import net.minecraft.core.HolderLookup.Provider;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.world.ContainerHelper;
|
|
import net.minecraft.world.entity.player.Inventory;
|
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
|
import net.minecraft.world.inventory.DispenserMenu;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
public class DispenserBlockEntity extends RandomizableContainerBlockEntity {
|
|
public static final int CONTAINER_SIZE = 9;
|
|
private NonNullList<ItemStack> items = NonNullList.withSize(9, ItemStack.EMPTY);
|
|
|
|
protected DispenserBlockEntity(BlockEntityType<?> blockEntityType, BlockPos blockPos, BlockState blockState) {
|
|
super(blockEntityType, blockPos, blockState);
|
|
}
|
|
|
|
public DispenserBlockEntity(BlockPos pos, BlockState blockState) {
|
|
this(BlockEntityType.DISPENSER, pos, blockState);
|
|
}
|
|
|
|
@Override
|
|
public int getContainerSize() {
|
|
return 9;
|
|
}
|
|
|
|
public int getRandomSlot(RandomSource random) {
|
|
this.unpackLootTable(null);
|
|
int i = -1;
|
|
int j = 1;
|
|
|
|
for (int k = 0; k < this.items.size(); k++) {
|
|
if (!this.items.get(k).isEmpty() && random.nextInt(j++) == 0) {
|
|
i = k;
|
|
}
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
public ItemStack insertItem(ItemStack stack) {
|
|
int i = this.getMaxStackSize(stack);
|
|
|
|
for (int j = 0; j < this.items.size(); j++) {
|
|
ItemStack itemStack = this.items.get(j);
|
|
if (itemStack.isEmpty() || ItemStack.isSameItemSameComponents(stack, itemStack)) {
|
|
int k = Math.min(stack.getCount(), i - itemStack.getCount());
|
|
if (k > 0) {
|
|
if (itemStack.isEmpty()) {
|
|
this.setItem(j, stack.split(k));
|
|
} else {
|
|
stack.shrink(k);
|
|
itemStack.grow(k);
|
|
}
|
|
}
|
|
|
|
if (stack.isEmpty()) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return stack;
|
|
}
|
|
|
|
@Override
|
|
protected Component getDefaultName() {
|
|
return Component.translatable("container.dispenser");
|
|
}
|
|
|
|
@Override
|
|
protected void loadAdditional(CompoundTag tag, Provider registries) {
|
|
super.loadAdditional(tag, registries);
|
|
this.items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
|
|
if (!this.tryLoadLootTable(tag)) {
|
|
ContainerHelper.loadAllItems(tag, this.items, registries);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void saveAdditional(CompoundTag tag, Provider registries) {
|
|
super.saveAdditional(tag, registries);
|
|
if (!this.trySaveLootTable(tag)) {
|
|
ContainerHelper.saveAllItems(tag, this.items, registries);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected NonNullList<ItemStack> getItems() {
|
|
return this.items;
|
|
}
|
|
|
|
@Override
|
|
protected void setItems(NonNullList<ItemStack> items) {
|
|
this.items = items;
|
|
}
|
|
|
|
@Override
|
|
protected AbstractContainerMenu createMenu(int containerId, Inventory inventory) {
|
|
return new DispenserMenu(containerId, inventory, this);
|
|
}
|
|
}
|