Fix ItemHandlerHelper.giveItemToPlayer creating item entities with incorrect contents (#5007)

This commit is contained in:
Ben Staddon 2018-07-03 06:41:18 +01:00 committed by mezz
parent 0cf5ef221c
commit 58d570a6f1

View file

@ -162,18 +162,20 @@ public class ItemHandlerHelper
*/
public static void giveItemToPlayer(EntityPlayer player, @Nonnull ItemStack stack, int preferredSlot)
{
if (stack.isEmpty()) return;
IItemHandler inventory = new PlayerMainInvWrapper(player.inventory);
World world = player.world;
// try adding it into the inventory
ItemStack remainder = stack;
// insert into preferred slot first
if(preferredSlot >= 0)
if (preferredSlot >= 0 && preferredSlot < inventory.getSlots())
{
remainder = inventory.insertItem(preferredSlot, stack, false);
}
// then into the inventory in general
if(!remainder.isEmpty())
if (!remainder.isEmpty())
{
remainder = insertItemStacked(inventory, remainder, false);
}
@ -188,7 +190,7 @@ public class ItemHandlerHelper
// drop remaining itemstack into the world
if (!remainder.isEmpty() && !world.isRemote)
{
EntityItem entityitem = new EntityItem(world, player.posX, player.posY + 0.5, player.posZ, stack);
EntityItem entityitem = new EntityItem(world, player.posX, player.posY + 0.5, player.posZ, remainder);
entityitem.setPickupDelay(40);
entityitem.motionX = 0;
entityitem.motionZ = 0;