MinecraftScripterAI
Discord ↗
Link Copied to Clipboard
×
"Show me how to make a tank block that will keep its contents when it is destroyed."
/* To make a tank block that keeps its contents when destroyed, you need to create a custom block entity for your block. Override getUpdateTag and read methods to save and load the contents of the tank. Also, override getDrops to add the contents of the tank to the drops when the block is destroyed. */ public class TankBlock extends Block implements EntityBlock { public TankBlock(AbstractBlock.Properties props) { super(props); } @Nullable @Override public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { return TileEntityInit.TANK.get().create(pos, state); } } public class TankBlockEntity extends BlockEntity { private ItemStackHandler itemHandler = new ItemStackHandler(1); public TankBlockEntity(BlockPos pos, BlockState state) { super(TileEntityInit.TANK.get(), pos, state); } @Override public CompoundNBT getUpdateTag() { CompoundNBT nbt = super.getUpdateTag(); nbt.put("items", itemHandler.serializeNBT()); return nbt; } @Override public void read(CompoundNBT nbt) { itemHandler.deserializeNBT(nbt.getCompound("items")); super.read(nbt); } public NonNullList<ItemStack> getDrops() { NonNullList<ItemStack> drops = NonNullList.create(); for (int i = 0; i < itemHandler.getSlots(); i++) { drops.add(itemHandler.getStackInSlot(i)); } return drops; } }