1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-24 06:03:28 +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

@ -98,7 +98,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@ -1125,18 +1125,18 @@ public static void addAllUpgradedTurtles( NonNullList<ItemStack> list )
turtleProxy.addAllUpgradedTurtles( list );
}
public static void setDropConsumer( Entity entity, Consumer<ItemStack> consumer )
public static void setDropConsumer( Entity entity, Function<ItemStack, ItemStack> consumer )
{
turtleProxy.setDropConsumer( entity, consumer );
}
public static void setDropConsumer( World world, BlockPos pos, Consumer<ItemStack> consumer )
public static void setDropConsumer( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer )
{
turtleProxy.setDropConsumer( world, pos, consumer );
}
public static void clearDropConsumer( )
public static List<ItemStack> clearDropConsumer( )
{
turtleProxy.clearDropConsumer();
return turtleProxy.clearDropConsumer();
}
}

View File

@ -48,17 +48,19 @@
import javax.annotation.Nonnull;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
{
private Map<Integer, ITurtleUpgrade> m_legacyTurtleUpgrades;
private Map<String, ITurtleUpgrade> m_turtleUpgrades;
private Consumer<ItemStack> dropConsumer;
private Function<ItemStack, ItemStack> dropConsumer;
private List<ItemStack> remainingDrops;
private WeakReference<World> dropWorld;
private BlockPos dropPos;
private AxisAlignedBB dropBounds;
@ -200,9 +202,10 @@ public void addAllUpgradedTurtles( NonNullList<ItemStack> list )
}
@Override
public void setDropConsumer( Entity entity, Consumer<ItemStack> consumer )
public void setDropConsumer( Entity entity, Function<ItemStack, ItemStack> consumer )
{
dropConsumer = consumer;
remainingDrops = new ArrayList<>();
dropEntity = new WeakReference<>( entity );
dropWorld = new WeakReference<>( entity.world );
dropPos = null;
@ -212,9 +215,10 @@ public void setDropConsumer( Entity entity, Consumer<ItemStack> consumer )
}
@Override
public void setDropConsumer( World world, BlockPos pos, Consumer<ItemStack> consumer )
public void setDropConsumer( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer )
{
dropConsumer = consumer;
remainingDrops = new ArrayList<>();
dropEntity = null;
dropWorld = new WeakReference<>( world );
dropPos = pos;
@ -222,7 +226,7 @@ public void setDropConsumer( World world, BlockPos pos, Consumer<ItemStack> cons
}
@Override
public void clearDropConsumer()
public List<ItemStack> clearDropConsumer()
{
if( dropEntity != null )
{
@ -232,17 +236,22 @@ public void clearDropConsumer()
entity.captureDrops = false;
if( entity.capturedDrops != null )
{
for( EntityItem entityItem : entity.capturedDrops ) dropConsumer.accept( entityItem.getItem() );
for( EntityItem entityItem : entity.capturedDrops ) handleDrops( entityItem.getItem() );
entity.capturedDrops.clear();
}
}
}
List<ItemStack> remainingStacks = remainingDrops;
dropConsumer = null;
remainingDrops = null;
dropEntity = null;
dropWorld = null;
dropPos = null;
dropBounds = null;
return remainingStacks;
}
private void registerTurtleUpgradeInternal( ITurtleUpgrade upgrade )
@ -471,6 +480,12 @@ private void registerForgeHandlers()
MinecraftForge.EVENT_BUS.register( handlers );
}
private void handleDrops(ItemStack stack)
{
ItemStack remaining = dropConsumer.apply(stack);
if (!remaining.isEmpty()) remainingDrops.add(remaining);
}
private class ForgeHandlers
{
@SubscribeEvent(priority = EventPriority.LOWEST)
@ -480,7 +495,7 @@ public void onEntityLivingDrops( LivingDropsEvent event )
if( dropEntity != null && event.getEntity() == dropEntity.get() )
{
List<EntityItem> drops = event.getDrops();
for( EntityItem entityItem : drops ) dropConsumer.accept( entityItem.getItem() );
for( EntityItem entityItem : drops ) handleDrops( entityItem.getItem() );
drops.clear();
}
}
@ -494,7 +509,7 @@ public void onHarvestDrops( BlockEvent.HarvestDropsEvent event )
{
for( ItemStack item : event.getDrops() )
{
if( event.getWorld().rand.nextFloat() < event.getDropChance() ) dropConsumer.accept( item );
if( event.getWorld().rand.nextFloat() < event.getDropChance() ) handleDrops( item );
}
event.getDrops().clear();
}
@ -507,7 +522,7 @@ public void onEntitySpawn( EntityJoinWorldEvent event )
if( dropWorld != null && dropWorld.get() == event.getWorld() && event.getEntity() instanceof EntityItem
&& dropBounds.contains( event.getEntity().getPositionVector() ) )
{
dropConsumer.accept( ((EntityItem) event.getEntity()).getItem() );
handleDrops( ((EntityItem) event.getEntity()).getItem() );
event.setCanceled( true );
}
}

View File

@ -14,7 +14,9 @@
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
public interface ICCTurtleProxy
{
@ -27,7 +29,7 @@ public interface ICCTurtleProxy
ITurtleUpgrade getTurtleUpgrade( @Nonnull ItemStack item );
void addAllUpgradedTurtles( NonNullList<ItemStack> list );
void setDropConsumer( Entity entity, Consumer<ItemStack> consumer );
void setDropConsumer( World world, BlockPos pos, Consumer<ItemStack> consumer );
void clearDropConsumer();
void setDropConsumer( Entity entity, Function<ItemStack, ItemStack> consumer );
void setDropConsumer( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer );
List<ItemStack> clearDropConsumer();
}

View File

@ -30,7 +30,6 @@
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 org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import java.util.List;
public class TurtlePlaceCommand implements ITurtleCommand
{
@ -241,14 +241,10 @@ private static ItemStack deployOnEntity( @Nonnull ItemStack stack, final ITurtle
// 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 @@ else if( hitEntity instanceof EntityLivingBase )
}
// 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 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 @@ private TurtleCommandResult attack( final ITurtleAccess turtle, EnumFacing direc
}
// 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 @@ private TurtleCommandResult attack( final ITurtleAccess turtle, EnumFacing direc
}
// Stop claiming drops
stopConsuming( turtle, extra );
stopConsuming( turtle );
// Put everything we collected into the turtles inventory, then return
if( attacked )
@ -277,8 +275,7 @@ private TurtleCommandResult dig( ITurtleAccess turtle, EnumFacing direction, Tur
}
// 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 @@ private TurtleCommandResult dig( ITurtleAccess turtle, EnumFacing direction, Tur
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 @@ private TurtleCommandResult dig( ITurtleAccess turtle, EnumFacing direction, Tur
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() );