mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-22 13:09:59 +00:00
Add universal bucket
This commit is contained in:
1 parent
06130912c5
commit
1055206ba8
16 files changed
+1560
-28
No files matched your search
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user