From 03eba1564e8f072a1a56762f36cb3219849a7ef3 Mon Sep 17 00:00:00 2001 From: Michael Hillcox Date: Sun, 17 Jan 2021 14:04:42 +0000 Subject: [PATCH] Added inventory dropping on tile break --- .../render/tiles/BatteryBoxTileRender.java | 4 --- .../common/blocks/FunctionalBlock.java | 27 ++++++++++++++----- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/BatteryBoxTileRender.java b/src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/BatteryBoxTileRender.java index 03b67446d..780952457 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/BatteryBoxTileRender.java +++ b/src/main/java/de/ellpeck/actuallyadditions/client/render/tiles/BatteryBoxTileRender.java @@ -12,16 +12,12 @@ import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.model.ItemCameraTransforms; import net.minecraft.client.renderer.tileentity.TileEntityRenderer; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; -import net.minecraft.client.resources.I18n; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemStack; import net.minecraft.util.Util; -import net.minecraft.util.math.vector.Quaternion; import net.minecraft.util.math.vector.Vector3f; import net.minecraftforge.energy.CapabilityEnergy; -import java.text.NumberFormat; - public class BatteryBoxTileRender extends TileEntityRenderer { public BatteryBoxTileRender(TileEntityRendererDispatcher rendererDispatcherIn) { super(rendererDispatcherIn); diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/blocks/FunctionalBlock.java b/src/main/java/de/ellpeck/actuallyadditions/common/blocks/FunctionalBlock.java index 94553c8a4..724652984 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/blocks/FunctionalBlock.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/blocks/FunctionalBlock.java @@ -15,8 +15,11 @@ import net.minecraftforge.energy.CapabilityEnergy; import net.minecraftforge.energy.IEnergyStorage; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.wrapper.InvWrapper; import javax.annotation.Nullable; +import java.io.Console; +import java.util.Optional; import java.util.function.Supplier; /** @@ -74,13 +77,25 @@ public abstract class FunctionalBlock extends ActuallyBlock { } @Override - public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) { - super.onReplaced(state, worldIn, pos, newState, isMoving); - System.out.println("Hello"); - - if (newState != state && this.dropsInventory(worldIn, pos)) { - System.out.println("Should have dropped"); + public void onReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean isMoving) { + if (newState.getBlock() != this && this.dropsInventory(world, pos)) { + this.getTile(world, pos).ifPresent(tile -> tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(inv -> { + for (int i = 0; i < inv.getSlots(); i++) { + net.minecraft.inventory.InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), inv.getStackInSlot(i)); + } + })); } + + super.onReplaced(state, world, pos, newState, isMoving); + } + + public Optional getTile(World world, BlockPos pos) { + TileEntity entity = world.getTileEntity(pos); + if (entity == null) { + return Optional.empty(); + } + + return Optional.of(entity); } public static class ActivatedContext {