Skip to content

Commit

Permalink
Fix compact drawer item duping on break and item loss if slot invento…
Browse files Browse the repository at this point in the history
…ry > 64.
  • Loading branch information
jaquadro committed Jun 22, 2015
1 parent 76b9d28 commit 9a13d2f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityDrawersStandard;
import com.jaquadro.minecraft.storagedrawers.core.ModCreativeTabs;
import com.jaquadro.minecraft.storagedrawers.core.ModItems;
import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStatus;
import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStorage;
import com.jaquadro.minecraft.storagedrawers.inventory.DrawerInventoryHelper;
import com.jaquadro.minecraft.storagedrawers.core.handlers.GuiHandler;
import com.jaquadro.minecraft.storagedrawers.network.BlockClickMessage;

Expand All @@ -28,7 +27,6 @@
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -423,7 +421,7 @@ public void breakBlock (World world, BlockPos pos, IBlockState state) {
spawnAsEntity(world, pos, stack);
}

InventoryHelper.dropInventoryItems(world, pos, tile);
DrawerInventoryHelper.dropInventoryItems(world, pos, tile);
}

super.breakBlock(world, pos, state);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.jaquadro.minecraft.storagedrawers.inventory;

import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer;
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;

import java.util.Random;

public class DrawerInventoryHelper
{
private static final Random RANDOM = new Random();

public static void dropInventoryItems (World world, BlockPos pos, IDrawerGroup group) {
for (int i = 0; i < group.getDrawerCount(); i++) {
if (!group.isDrawerEnabled(i))
continue;

IDrawer drawer = group.getDrawer(i);
while (drawer.getStoredItemCount() > 0) {
ItemStack stack = drawer.getStoredItemCopy();
if (stack == null || stack.stackSize == 0)
break;

spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), stack);
drawer.setStoredItemCount(0);
}
}
}

private static void spawnItemStack (World world, double x, double y, double z, ItemStack stack)
{
float xOff = RANDOM.nextFloat() * 0.8F + 0.1F;
float yOff = RANDOM.nextFloat() * 0.8F + 0.1F;
float zOff = RANDOM.nextFloat() * 0.8F + 0.1F;

while (stack.stackSize > 0)
{
int dropAmt = RANDOM.nextInt(21) + 10;

if (dropAmt > stack.stackSize)
{
dropAmt = stack.stackSize;
}

stack.stackSize -= dropAmt;
EntityItem entityitem = new EntityItem(world, x + (double)xOff, y + (double)yOff, z + (double)zOff, new ItemStack(stack.getItem(), dropAmt, stack.getMetadata()));

if (stack.hasTagCompound())
{
entityitem.getEntityItem().setTagCompound((NBTTagCompound)stack.getTagCompound().copy());
}

float velocity = 0.05F;
entityitem.motionX = RANDOM.nextGaussian() * (double)velocity;
entityitem.motionY = RANDOM.nextGaussian() * (double)velocity + 0.20000000298023224D;
entityitem.motionZ = RANDOM.nextGaussian() * (double)velocity;

world.spawnEntityInWorld(entityitem);
}
}
}

0 comments on commit 9a13d2f

Please sign in to comment.