2016-06-23 03:49:47 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2018-07-01 21:17:28 +00:00
|
|
|
* Copyright (c) 2016-2018.
|
2016-06-23 03:49:47 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2016-01-17 16:41:34 +00:00
|
|
|
package net.minecraftforge.items.wrapper;
|
|
|
|
|
|
|
|
import net.minecraft.inventory.ISidedInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraftforge.items.IItemHandlerModifiable;
|
|
|
|
import net.minecraftforge.items.ItemHandlerHelper;
|
|
|
|
|
2016-11-13 22:09:54 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2016-01-17 16:41:34 +00:00
|
|
|
public class SidedInvWrapper implements IItemHandlerModifiable
|
|
|
|
{
|
|
|
|
protected final ISidedInventory inv;
|
|
|
|
protected final EnumFacing side;
|
|
|
|
|
|
|
|
public SidedInvWrapper(ISidedInventory inv, EnumFacing side)
|
|
|
|
{
|
|
|
|
this.inv = inv;
|
|
|
|
this.side = side;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getSlot(ISidedInventory inv, int slot, EnumFacing side)
|
|
|
|
{
|
|
|
|
int[] slots = inv.getSlotsForFace(side);
|
|
|
|
if (slot < slots.length)
|
|
|
|
return slots[slot];
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o)
|
|
|
|
{
|
|
|
|
if (this == o)
|
|
|
|
return true;
|
|
|
|
if (o == null || getClass() != o.getClass())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SidedInvWrapper that = (SidedInvWrapper) o;
|
|
|
|
|
|
|
|
return inv.equals(that.inv) && side == that.side;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode()
|
|
|
|
{
|
|
|
|
int result = inv.hashCode();
|
|
|
|
result = 31 * result + side.hashCode();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSlots()
|
|
|
|
{
|
|
|
|
return inv.getSlotsForFace(side).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-11-13 22:09:54 +00:00
|
|
|
@Nonnull
|
2016-01-17 16:41:34 +00:00
|
|
|
public ItemStack getStackInSlot(int slot)
|
|
|
|
{
|
|
|
|
int i = getSlot(inv, slot, side);
|
2016-12-21 23:52:30 +00:00
|
|
|
return i == -1 ? ItemStack.EMPTY : inv.getStackInSlot(i);
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-11-13 22:09:54 +00:00
|
|
|
@Nonnull
|
|
|
|
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate)
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2016-12-21 23:52:30 +00:00
|
|
|
if (stack.isEmpty())
|
|
|
|
return ItemStack.EMPTY;
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
int slot1 = getSlot(inv, slot, side);
|
|
|
|
|
|
|
|
if (slot1 == -1)
|
|
|
|
return stack;
|
|
|
|
|
|
|
|
ItemStack stackInSlot = inv.getStackInSlot(slot1);
|
|
|
|
|
|
|
|
int m;
|
2016-12-21 23:52:30 +00:00
|
|
|
if (!stackInSlot.isEmpty())
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2017-10-31 17:41:33 +00:00
|
|
|
if (stackInSlot.getCount() >= Math.min(stackInSlot.getMaxStackSize(), getSlotLimit(slot)))
|
|
|
|
return stack;
|
|
|
|
|
2016-01-17 16:41:34 +00:00
|
|
|
if (!ItemHandlerHelper.canItemStacksStack(stack, stackInSlot))
|
|
|
|
return stack;
|
|
|
|
|
2017-10-31 17:41:33 +00:00
|
|
|
if (!inv.canInsertItem(slot1, stack, side) || !inv.isItemValidForSlot(slot1, stack))
|
2017-01-12 00:29:49 +00:00
|
|
|
return stack;
|
|
|
|
|
2016-12-21 23:52:30 +00:00
|
|
|
m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot)) - stackInSlot.getCount();
|
2016-01-17 16:41:34 +00:00
|
|
|
|
2016-12-21 23:52:30 +00:00
|
|
|
if (stack.getCount() <= m)
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
|
|
|
if (!simulate)
|
|
|
|
{
|
|
|
|
ItemStack copy = stack.copy();
|
2016-12-21 23:52:30 +00:00
|
|
|
copy.grow(stackInSlot.getCount());
|
2017-04-25 18:55:31 +00:00
|
|
|
setInventorySlotContents(slot1, copy);
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 23:52:30 +00:00
|
|
|
return ItemStack.EMPTY;
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-02 13:38:18 +00:00
|
|
|
// copy the stack to not modify the original one
|
|
|
|
stack = stack.copy();
|
2016-01-17 16:41:34 +00:00
|
|
|
if (!simulate)
|
|
|
|
{
|
|
|
|
ItemStack copy = stack.splitStack(m);
|
2016-12-21 23:52:30 +00:00
|
|
|
copy.grow(stackInSlot.getCount());
|
2017-04-25 18:55:31 +00:00
|
|
|
setInventorySlotContents(slot1, copy);
|
2016-01-17 16:41:34 +00:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-21 23:52:30 +00:00
|
|
|
stack.shrink(m);
|
2016-01-17 16:41:34 +00:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-31 17:41:33 +00:00
|
|
|
if (!inv.canInsertItem(slot1, stack, side) || !inv.isItemValidForSlot(slot1, stack))
|
2017-01-12 00:29:49 +00:00
|
|
|
return stack;
|
|
|
|
|
2016-11-29 00:03:47 +00:00
|
|
|
m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot));
|
2016-12-21 23:52:30 +00:00
|
|
|
if (m < stack.getCount())
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2016-01-02 13:38:18 +00:00
|
|
|
// copy the stack to not modify the original one
|
|
|
|
stack = stack.copy();
|
2016-01-17 16:41:34 +00:00
|
|
|
if (!simulate)
|
|
|
|
{
|
2017-04-25 18:55:31 +00:00
|
|
|
setInventorySlotContents(slot1, stack.splitStack(m));
|
2016-01-17 16:41:34 +00:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-21 23:52:30 +00:00
|
|
|
stack.shrink(m);
|
2016-01-17 16:41:34 +00:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!simulate)
|
2017-04-25 18:55:31 +00:00
|
|
|
setInventorySlotContents(slot1, stack);
|
2016-12-21 23:52:30 +00:00
|
|
|
return ItemStack.EMPTY;
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-11-13 22:09:54 +00:00
|
|
|
public void setStackInSlot(int slot, @Nonnull ItemStack stack)
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2016-05-02 19:19:54 +00:00
|
|
|
int slot1 = getSlot(inv, slot, side);
|
|
|
|
|
|
|
|
if (slot1 != -1)
|
2017-04-25 18:55:31 +00:00
|
|
|
setInventorySlotContents(slot1, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setInventorySlotContents(int slot, ItemStack stack) {
|
|
|
|
inv.markDirty(); //Notify vanilla of updates, We change the handler to be responsible for this instead of the caller. So mimic vanilla behavior
|
|
|
|
inv.setInventorySlotContents(slot, stack);
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-11-13 22:09:54 +00:00
|
|
|
@Nonnull
|
2016-01-17 16:41:34 +00:00
|
|
|
public ItemStack extractItem(int slot, int amount, boolean simulate)
|
|
|
|
{
|
|
|
|
if (amount == 0)
|
2016-12-21 23:52:30 +00:00
|
|
|
return ItemStack.EMPTY;
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
int slot1 = getSlot(inv, slot, side);
|
|
|
|
|
|
|
|
if (slot1 == -1)
|
2016-12-21 23:52:30 +00:00
|
|
|
return ItemStack.EMPTY;
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
ItemStack stackInSlot = inv.getStackInSlot(slot1);
|
|
|
|
|
2016-12-21 23:52:30 +00:00
|
|
|
if (stackInSlot.isEmpty())
|
|
|
|
return ItemStack.EMPTY;
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
if (!inv.canExtractItem(slot1, stackInSlot, side))
|
2016-12-21 23:52:30 +00:00
|
|
|
return ItemStack.EMPTY;
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
if (simulate)
|
|
|
|
{
|
2016-12-21 23:52:30 +00:00
|
|
|
if (stackInSlot.getCount() < amount)
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
|
|
|
return stackInSlot.copy();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ItemStack copy = stackInSlot.copy();
|
2016-12-21 23:52:30 +00:00
|
|
|
copy.setCount(amount);
|
2016-01-17 16:41:34 +00:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-21 23:52:30 +00:00
|
|
|
int m = Math.min(stackInSlot.getCount(), amount);
|
2017-04-25 18:55:31 +00:00
|
|
|
ItemStack ret = inv.decrStackSize(slot1, m);
|
|
|
|
inv.markDirty();
|
|
|
|
return ret;
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 00:03:47 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSlotLimit(int slot)
|
|
|
|
{
|
|
|
|
return inv.getInventoryStackLimit();
|
|
|
|
}
|
2018-07-29 00:38:18 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValid(int slot, @Nonnull ItemStack stack)
|
|
|
|
{
|
|
|
|
return inv.isItemValidForSlot(slot, stack);
|
|
|
|
}
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|