Skip to content

Commit

Permalink
Ensure max stack size is respected when a transporter with items in i…
Browse files Browse the repository at this point in the history
…t is destroyed (#7982)
  • Loading branch information
pupnewfster committed Feb 29, 2024
1 parent a3e0e1d commit 0ef8269
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 15 deletions.
Expand Up @@ -75,7 +75,7 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
return InteractionResult.FAIL;
}
if (player.isShiftKeyDown()) {
WorldUtils.dismantleBlock(state, world, pos);
WorldUtils.dismantleBlock(state, world, pos, player);
return InteractionResult.SUCCESS;
}
Direction change = tile.getDirection().getClockWise();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mekanism/common/block/prefab/BlockBase.java
Expand Up @@ -114,7 +114,7 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
@NotNull BlockHitResult hit) {
if (player.isShiftKeyDown() && MekanismUtils.canUseAsWrench(player.getItemInHand(hand))) {
if (!world.isClientSide) {
WorldUtils.dismantleBlock(state, world, pos);
WorldUtils.dismantleBlock(state, world, pos, player);
}
return InteractionResult.SUCCESS;
}
Expand Down
Expand Up @@ -76,7 +76,7 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
ItemStack stack = player.getItemInHand(hand);
if (MekanismUtils.canUseAsWrench(stack) && player.isShiftKeyDown()) {
if (!world.isClientSide) {
WorldUtils.dismantleBlock(state, world, pos);
WorldUtils.dismantleBlock(state, world, pos, player);
}
return InteractionResult.SUCCESS;
}
Expand Down
Expand Up @@ -501,7 +501,7 @@ public WrenchResult tryWrench(BlockState state, Player player, InteractionHand h
return WrenchResult.NO_SECURITY;
}
if (player.isShiftKeyDown()) {
WorldUtils.dismantleBlock(state, getLevel(), worldPosition, this);
WorldUtils.dismantleBlock(state, getLevel(), worldPosition, this, player);
return WrenchResult.DISMANTLED;
}
//Special ITileDirectional handling
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/mekanism/common/util/TransporterUtils.java
Expand Up @@ -70,13 +70,15 @@ public static EnumColor decrement(EnumColor color) {
}

public static void drop(LogisticalTransporterBase transporter, TransporterStack stack) {
BlockPos blockPos = transporter.getBlockPos();
BlockPos blockPos;
if (stack.hasPath()) {
float[] pos = TransporterUtils.getStackPosition(transporter, stack, 0);
blockPos = blockPos.offset(Mth.floor(pos[0]), Mth.floor(pos[1]), Mth.floor(pos[2]));
blockPos = transporter.getBlockPos().offset(Mth.floor(pos[0]), Mth.floor(pos[1]), Mth.floor(pos[2]));
} else {
blockPos = transporter.getBlockPos();
}
TransporterManager.remove(transporter.getLevel(), stack);
Block.popResource(transporter.getLevel(), blockPos, stack.itemStack);
InventoryUtils.dropStack(stack.itemStack, item -> Block.popResource(transporter.getLevel(), blockPos, item));
}

public static float[] getStackPosition(LogisticalTransporterBase transporter, TransporterStack stack, float partial) {
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/mekanism/common/util/WorldUtils.java
Expand Up @@ -9,10 +9,10 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.SectionPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
Expand Down Expand Up @@ -385,18 +385,15 @@ public static void markChunkDirty(Level world, BlockPos pos) {
/**
* Dismantles a block, dropping it and removing it from the world.
*/
public static void dismantleBlock(BlockState state, Level world, BlockPos pos) {
dismantleBlock(state, world, pos, getTileEntity(world, pos));
public static void dismantleBlock(BlockState state, Level world, BlockPos pos, @Nullable Entity entity) {
dismantleBlock(state, world, pos, getTileEntity(world, pos), entity);
}

/**
* Dismantles a block, dropping it and removing it from the world.
*/
public static void dismantleBlock(BlockState state, Level world, BlockPos pos, @Nullable BlockEntity tile) {
if (world instanceof ServerLevel level) {//Copy of Block#dropResources but skipping the xp dropping
Block.getDrops(state, level, pos, tile).forEach(stack -> Block.popResource(world, pos, stack));
state.spawnAfterBreak(level, pos, ItemStack.EMPTY, false);
}
public static void dismantleBlock(BlockState state, Level world, BlockPos pos, @Nullable BlockEntity tile, @Nullable Entity entity) {
Block.dropResources(state, world, pos, tile, entity, ItemStack.EMPTY, false);
world.removeBlock(pos, false);
}

Expand Down

0 comments on commit 0ef8269

Please sign in to comment.