Skip to content

Commit

Permalink
Make wires drop when coils break
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiDragon committed May 20, 2023
1 parent 214edb3 commit 0033d7f
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit 0033d7f

Please sign in to comment.