diff --git a/src/main/java/io/github/mattidragon/powernetworks/block/CoilBlock.java b/src/main/java/io/github/mattidragon/powernetworks/block/CoilBlock.java index 77c93b7..d9db8aa 100644 --- a/src/main/java/io/github/mattidragon/powernetworks/block/CoilBlock.java +++ b/src/main/java/io/github/mattidragon/powernetworks/block/CoilBlock.java @@ -5,6 +5,7 @@ import eu.pb4.polymer.core.api.utils.PolymerClientDecoded; import eu.pb4.polymer.core.api.utils.PolymerKeepModel; import eu.pb4.polymer.networking.api.PolymerServerNetworking; +import io.github.mattidragon.powernetworks.item.ModItems; import io.github.mattidragon.powernetworks.misc.CoilTier; import io.github.mattidragon.powernetworks.networking.PowerNetworksNetworking; import net.minecraft.block.*; @@ -13,6 +14,7 @@ import net.minecraft.block.entity.BlockEntityType; import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.SimpleInventory; import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; import net.minecraft.server.network.ServerPlayerEntity; @@ -24,6 +26,7 @@ import net.minecraft.util.ActionResult; import net.minecraft.util.Formatting; import net.minecraft.util.Hand; +import net.minecraft.util.ItemScatterer; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.shape.VoxelShape; @@ -118,8 +121,10 @@ public void prepare(BlockState state, WorldAccess world, BlockPos pos, int flags public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { var coil = getBlockEntity(world, pos); if (coil != null) { - if (!newState.isOf(this)) + if (!newState.isOf(this)) { + dropWires(world, pos, coil); coil.disconnectAllConnections(); + } if (coil.display != null) { coil.display.clear(); @@ -128,6 +133,18 @@ public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockSt super.onStateReplaced(state, world, pos, newState, moved); } + private void dropWires(World world, BlockPos pos, CoilBlockEntity coil) { + var itemsPerStack = ModItems.WIRE.getMaxCount(); + var connections = coil.getConnections().size(); + var inventory = new SimpleInventory((connections / itemsPerStack) + 1); + for (int slot = 0; slot < inventory.size(); slot++) { + var itemsToDrop = Math.min(connections, itemsPerStack); + connections -= itemsToDrop; + inventory.setStack(slot, new ItemStack(ModItems.WIRE, itemsToDrop)); + } + ItemScatterer.spawn(world, pos, inventory); + } + @SuppressWarnings("unchecked") @Nullable @Override