Add universal bucket

This commit is contained in:
Bernhard Bonigl committed 2016-02-16 00:49:01 +01:00
1 parent 06130912c5
commit 1055206ba8
16 files changed
+1560 -28

No files matched your search

@@ -34,12 +34,14 @@ public interface IItemHandler
/**
* Inserts an ItemStack into the given slot and return the remainder.
* The ItemStack should not be modified in this function!
* Note: This behaviour is subtly different from IFluidHandlers.fill()
*
* @param slot Slot to insert into.
* @param stack ItemStack to insert
* @param stack ItemStack to insert.
* @param simulate If true, the insertion is only simulated
* @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return null)
* @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return null).
* May be the same as the input ItemStack if unchanged, otherwise a new ItemStack.
**/
ItemStack insertItem(int slot, ItemStack stack, boolean simulate);
@@ -1,7 +1,11 @@
package net.minecraftforge.items;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
public class ItemHandlerHelper
{
@@ -32,12 +36,138 @@ public class ItemHandlerHelper
return (aTag != null || bTag == null) && (aTag == null || aTag.equals(bTag));
}
/**
* A relaxed version of canItemStacksStack that stacks itemstacks with different metadata if they don't have subtypes.
* This usually only applies when players pick up items.
*/
public static boolean canItemStacksStackRelaxed(ItemStack a, ItemStack b)
{
if (a == null || b == null || a.getItem() != b.getItem())
return false;
if (!a.isStackable())
return false;
// Metadata value only matters when the item has subtypes
// Vanilla stacks non-subtype items with different metadata together
// e.g. a stick with metadata 0 and a stick with metadata 1 stack
if (a.getHasSubtypes() && a.getMetadata() != b.getMetadata())
return false;
final NBTTagCompound aTag = a.getTagCompound();
final NBTTagCompound bTag = b.getTagCompound();
return (aTag != null || bTag == null) && (aTag == null || aTag.equals(bTag));
}
public static ItemStack copyStackWithSize(ItemStack itemStack, int size)
{
if (size == 0)
return null;
ItemStack copy = ItemStack.copyItemStack(itemStack);
copy.stackSize = size;
if (copy != null)
copy.stackSize = size;
return copy;
}
/**
* Inserts the ItemStack into the inventory, filling up already present stacks first.
* This is equivalent to the behaviour of a player picking up an item.
* Note: This function stacks items without subtypes with different metadata together.
*/
public static ItemStack insertItemStacked(IItemHandler inventory, ItemStack stack, boolean simulate)
{
if (inventory == null || stack == null)
return stack;
// not stackable -> just insert into a new slot
if (!stack.isStackable())
{
return insertItem(inventory, stack, simulate);
}
int sizeInventory = inventory.getSlots();
// go through the inventory and try to fill up already existing items
for (int i = 0; i < sizeInventory; i++)
{
ItemStack slot = inventory.getStackInSlot(i);
if (canItemStacksStackRelaxed(slot, stack))
{
stack = inventory.insertItem(i, stack, simulate);
if (stack == null)
{
break;
}
}
}
// insert remainder into empty slots
if (stack != null)
{
// find empty slot
for (int i = 0; i < sizeInventory; i++)
{
if (inventory.getStackInSlot(i) == null)
{
stack = inventory.insertItem(i, stack, simulate);
if (stack == null)
{
break;
}
}
}
}
return stack;
}
/** giveItemToPlayer without preferred slot */
public static void giveItemToPlayer(EntityPlayer player, ItemStack stack) {
giveItemToPlayer(player, stack, -1);
}
/**
* Inserts the given itemstack into the players inventory.
* If the inventory can't hold it, the item will be dropped in the world at the players position.
*
* @param player The player to give the item to
* @param stack The itemstack to insert
*/
public static void giveItemToPlayer(EntityPlayer player, ItemStack stack, int preferredSlot)
{
IItemHandler inventory = new PlayerMainInvWrapper(player.inventory);
World world = player.worldObj;
// try adding it into the inventory
ItemStack remainder = null;
// insert into preferred slot first
if(preferredSlot >= 0)
{
remainder = inventory.insertItem(preferredSlot, stack, false);
}
// then into the inventory in general
if(remainder != null)
{
remainder = insertItemStacked(inventory, stack, false);
}
// play sound if something got picked up
if (remainder == null || remainder.stackSize != stack.stackSize)
{
world.playSoundEffect(player.posX, player.posY, player.posZ,
"random.pop", 0.2F, ((world.rand.nextFloat() - world.rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
}
// drop remaining itemstack into the world
if (remainder != null && !world.isRemote)
{
EntityItem entityitem = new EntityItem(world, player.posX, player.posY + 0.5, player.posZ, stack);
entityitem.setPickupDelay(40);
entityitem.motionX = 0;
entityitem.motionZ = 0;
world.spawnEntityInWorld(entityitem);
}
}
}
@@ -0,0 +1,102 @@
package net.minecraftforge.items.wrapper;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;
// combines multiple IItemHandlerModifiable into one interface
public class CombinedInvWrapper implements IItemHandlerModifiable
{
protected final IItemHandlerModifiable[] itemHandler; // the handlers
protected final int[] baseIndex; // index-offsets of the different handlers
protected final int slotCount; // numeber of total slots
public CombinedInvWrapper(IItemHandlerModifiable... itemHandler)
{
this.itemHandler = itemHandler;
this.baseIndex = new int[itemHandler.length];
int index = 0;
for (int i = 0; i < itemHandler.length; i++)
{
index += itemHandler[i].getSlots();
baseIndex[i] = index;
}
this.slotCount = index;
}
// returns the handler index for the slot
protected int getIndexForSlot(int slot)
{
if (slot < 0)
return -1;
for (int i = 0; i < baseIndex.length; i++)
{
if (slot - baseIndex[i] < 0)
{
return i;
}
}
return -1;
}
protected IItemHandlerModifiable getHandlerFromIndex(int index)
{
if (index < 0 || index >= itemHandler.length)
{
return (IItemHandlerModifiable)EmptyHandler.INSTANCE;
}
return itemHandler[index];
}
protected int getSlotFromIndex(int index)
{
if (index <= 0 || index >= baseIndex.length)
{
return 0;
}
return baseIndex[index - 1];
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
int index = getIndexForSlot(slot);
IItemHandlerModifiable handler = getHandlerFromIndex(index);
slot = slot - getSlotFromIndex(index);
handler.setStackInSlot(slot, stack);
}
@Override
public int getSlots()
{
return slotCount;
}
@Override
public ItemStack getStackInSlot(int slot)
{
int index = getIndexForSlot(slot);
IItemHandlerModifiable handler = getHandlerFromIndex(index);
slot = getSlotFromIndex(index);
return handler.getStackInSlot(slot);
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
int index = getIndexForSlot(slot);
IItemHandlerModifiable handler = getHandlerFromIndex(index);
slot = getSlotFromIndex(index);
return handler.insertItem(slot, stack, simulate);
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
int index = getIndexForSlot(slot);
IItemHandlerModifiable handler = getHandlerFromIndex(index);
slot = getSlotFromIndex(index);
return handler.extractItem(slot, amount, simulate);
}
}
@@ -2,8 +2,9 @@ package net.minecraftforge.items.wrapper;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
public class EmptyHandler implements IItemHandler
public class EmptyHandler implements IItemHandlerModifiable
{
public static IItemHandler INSTANCE = new EmptyHandler();
@@ -30,4 +31,10 @@ public class EmptyHandler implements IItemHandler
{
return null;
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
// nothing to do here
}
}
@@ -79,6 +79,8 @@ public class InvWrapper implements IItemHandlerModifiable
}
else
{
// copy the stack to not modify the original one
stack = stack.copy();
if (!simulate)
{
ItemStack copy = stack.splitStack(m);
@@ -99,6 +101,8 @@ public class InvWrapper implements IItemHandlerModifiable
m = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit());
if (m < stack.stackSize)
{
// copy the stack to not modify the original one
stack = stack.copy();
if (!simulate)
{
inv.setInventorySlotContents(slot, stack.splitStack(m));
@@ -0,0 +1,53 @@
package net.minecraftforge.items.wrapper;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
public class PlayerArmorInvWrapper extends InvWrapper
{
public final InventoryPlayer inventoryPlayer;
public final int offset;
public PlayerArmorInvWrapper(InventoryPlayer inv)
{
super(inv);
inventoryPlayer = inv;
offset = inventoryPlayer.mainInventory.length;
}
@Override
public int getSlots()
{
return inventoryPlayer.armorInventory.length;
}
@Override
public ItemStack getStackInSlot(int slot)
{
return super.getStackInSlot(slot + offset);
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
// check if it's valid for the armor slot
if (slot < 4 && stack != null && stack.getItem().isValidArmor(stack, 3 - slot, inventoryPlayer.player))
{
return super.insertItem(slot + offset, stack, simulate);
}
return stack;
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
super.setStackInSlot(slot + offset, stack);
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
return super.extractItem(slot + offset, amount, simulate);
}
}
@@ -0,0 +1,11 @@
package net.minecraftforge.items.wrapper;
import net.minecraft.entity.player.InventoryPlayer;
public class PlayerInvWrapper extends CombinedInvWrapper
{
public PlayerInvWrapper(InventoryPlayer inv)
{
super(new PlayerMainInvWrapper(inv), new PlayerArmorInvWrapper(inv));
}
}
@@ -0,0 +1,89 @@
package net.minecraftforge.items.wrapper;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
/**
* Exposes the player inventory WITHOUT the armor inventory as IItemHandler.
* Also takes core of inserting/extracting having the same logic as picking up items.
*/
public class PlayerMainInvWrapper extends InvWrapper
{
public final InventoryPlayer inventoryPlayer;
public PlayerMainInvWrapper(InventoryPlayer inv)
{
super(inv);
inventoryPlayer = inv;
}
@Override
public int getSlots()
{
return inventoryPlayer.mainInventory.length;
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
// prevent setting of armor inventory
if (slot > getSlots())
{
return;
}
super.setStackInSlot(slot, stack);
}
@Override
public ItemStack getStackInSlot(int slot)
{
// prevent getting of armor inventory
if (slot > getSlots())
{
return null;
}
return super.getStackInSlot(slot);
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
// prevent inserting into armor inventory
if (slot > getSlots())
{
return stack;
}
ItemStack rest = super.insertItem(slot, stack, simulate);
if (rest == null || rest.stackSize != stack.stackSize)
{
// the stack in the slot changed, animate it
ItemStack inSlot = getStackInSlot(slot);
if(inSlot != null)
{
if (inventoryPlayer.player.worldObj.isRemote)
{
inSlot.animationsToGo = 5;
}
else if(inventoryPlayer.player instanceof EntityPlayerMP) {
inventoryPlayer.player.openContainer.detectAndSendChanges();
}
}
}
return rest;
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
// prevent extraction from armor inventory
if (slot > getSlots())
{
return null;
}
return super.extractItem(slot, amount, simulate);
}
}
@@ -97,6 +97,8 @@ public class SidedInvWrapper implements IItemHandlerModifiable
}
else
{
// copy the stack to not modify the original one
stack = stack.copy();
if (!simulate)
{
ItemStack copy = stack.splitStack(m);
@@ -116,6 +118,8 @@ public class SidedInvWrapper implements IItemHandlerModifiable
m = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit());
if (m < stack.stackSize)
{
// copy the stack to not modify the original one
stack = stack.copy();
if (!simulate)
{
inv.setInventorySlotContents(slot1, stack.splitStack(m));