1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-18 22:25:12 +00:00

Prevent stack overflows when using turtle.place() with a full inventory

This commit is contained in:
apemanzilla
2018-08-22 19:09:52 -04:00
committed by SquidDev
parent d2a9e7e458
commit 89c7183a1d
5 changed files with 53 additions and 43 deletions

View File

@@ -30,7 +30,6 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
@@ -38,6 +37,7 @@ import net.minecraftforge.fml.common.eventhandler.Event;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import java.util.List;
public class TurtlePlaceCommand implements ITurtleCommand
{
@@ -241,14 +241,10 @@ public class TurtlePlaceCommand implements ITurtleCommand
// Start claiming entity drops
Entity hitEntity = hit.getKey();
Vec3d hitPos = hit.getValue();
ComputerCraft.setDropConsumer( hitEntity, ( drop ) ->
{
ItemStack remainder = InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() );
if( !remainder.isEmpty() )
{
WorldUtil.dropItemStack( remainder, world, position, turtle.getDirection().getOpposite() );
}
} );
ComputerCraft.setDropConsumer(
hitEntity,
drop -> InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() )
);
// Place on the entity
boolean placed = false;
@@ -285,7 +281,11 @@ public class TurtlePlaceCommand implements ITurtleCommand
}
// Stop claiming drops
ComputerCraft.clearDropConsumer();
List<ItemStack> remainingDrops = ComputerCraft.clearDropConsumer();
for( ItemStack remaining : remainingDrops )
{
WorldUtil.dropItemStack( remaining, world, position, turtle.getDirection().getOpposite() );
}
// Put everything we collected into the turtles inventory, then return
ItemStack remainder = turtlePlayer.unloadInventory( turtle );

View File

@@ -42,9 +42,8 @@ import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.vecmath.Matrix4f;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
public class TurtleTool implements ITurtleUpgrade
{
@@ -187,8 +186,7 @@ public class TurtleTool implements ITurtleUpgrade
}
// Start claiming entity drops
List<ItemStack> extra = new ArrayList<>();
ComputerCraft.setDropConsumer( hitEntity, turtleDropConsumer( turtle, extra ) );
ComputerCraft.setDropConsumer( hitEntity, turtleDropConsumer( turtle ) );
// Attack the entity
boolean attacked = false;
@@ -220,7 +218,7 @@ public class TurtleTool implements ITurtleUpgrade
}
// Stop claiming drops
stopConsuming( turtle, extra );
stopConsuming( turtle );
// Put everything we collected into the turtles inventory, then return
if( attacked )
@@ -277,8 +275,7 @@ public class TurtleTool implements ITurtleUpgrade
}
// Consume the items the block drops
List<ItemStack> extra = new ArrayList<>();
ComputerCraft.setDropConsumer( world, blockPosition, turtleDropConsumer( turtle, extra ) );
ComputerCraft.setDropConsumer( world, blockPosition, turtleDropConsumer( turtle ) );
TileEntity tile = world.getTileEntity( blockPosition );
@@ -297,7 +294,7 @@ public class TurtleTool implements ITurtleUpgrade
state.getBlock().harvestBlock( world, turtlePlayer, blockPosition, state, tile, turtlePlayer.getHeldItemMainhand() );
}
stopConsuming( turtle, extra );
stopConsuming( turtle );
// Remember the previous block
if( turtle instanceof TurtleBrain )
@@ -312,18 +309,14 @@ public class TurtleTool implements ITurtleUpgrade
return TurtleCommandResult.failure( "Nothing to dig here" );
}
private Consumer<ItemStack> turtleDropConsumer( ITurtleAccess turtle, List<ItemStack> extra )
private Function<ItemStack, ItemStack> turtleDropConsumer( ITurtleAccess turtle )
{
return ( drop ) ->
{
ItemStack remainder = InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() );
if( !remainder.isEmpty() ) extra.add( remainder );
};
return drop -> InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() );
}
private void stopConsuming( ITurtleAccess turtle, List<ItemStack> extra )
private void stopConsuming( ITurtleAccess turtle )
{
ComputerCraft.clearDropConsumer();
List<ItemStack> extra = ComputerCraft.clearDropConsumer();
for( ItemStack remainder : extra )
{
WorldUtil.dropItemStack( remainder, turtle.getWorld(), turtle.getPosition(), turtle.getDirection().getOpposite() );