ForgePatch/src/main/java/net/minecraftforge/items/SlotItemHandler.java

151 lines
4.6 KiB
Java

/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* 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
*/
package net.minecraftforge.items;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.TextComponentString;
import javax.annotation.Nonnull;
public class SlotItemHandler extends Slot
{
private static IInventory emptyInventory = new InventoryBasic(new TextComponentString("[Null]"), 0);
private final IItemHandler itemHandler;
private final int index;
public SlotItemHandler(IItemHandler itemHandler, int index, int xPosition, int yPosition)
{
super(emptyInventory, index, xPosition, yPosition);
this.itemHandler = itemHandler;
this.index = index;
}
@Override
public boolean isItemValid(@Nonnull ItemStack stack)
{
if (stack.isEmpty() || !itemHandler.isItemValid(index, stack))
return false;
IItemHandler handler = this.getItemHandler();
ItemStack remainder;
if (handler instanceof IItemHandlerModifiable)
{
IItemHandlerModifiable handlerModifiable = (IItemHandlerModifiable) handler;
ItemStack currentStack = handlerModifiable.getStackInSlot(index);
handlerModifiable.setStackInSlot(index, ItemStack.EMPTY);
remainder = handlerModifiable.insertItem(index, stack, true);
handlerModifiable.setStackInSlot(index, currentStack);
}
else
{
remainder = handler.insertItem(index, stack, true);
}
return remainder.getCount() < stack.getCount();
}
@Override
@Nonnull
public ItemStack getStack()
{
return this.getItemHandler().getStackInSlot(index);
}
// Override if your IItemHandler does not implement IItemHandlerModifiable
@Override
public void putStack(@Nonnull ItemStack stack)
{
((IItemHandlerModifiable) this.getItemHandler()).setStackInSlot(index, stack);
this.onSlotChanged();
}
@Override
public void onSlotChange(@Nonnull ItemStack p_75220_1_, @Nonnull ItemStack p_75220_2_)
{
}
@Override
public int getSlotStackLimit()
{
return this.itemHandler.getSlotLimit(this.index);
}
@Override
public int getItemStackLimit(@Nonnull ItemStack stack)
{
ItemStack maxAdd = stack.copy();
int maxInput = stack.getMaxStackSize();
maxAdd.setCount(maxInput);
IItemHandler handler = this.getItemHandler();
ItemStack currentStack = handler.getStackInSlot(index);
if (handler instanceof IItemHandlerModifiable) {
IItemHandlerModifiable handlerModifiable = (IItemHandlerModifiable) handler;
handlerModifiable.setStackInSlot(index, ItemStack.EMPTY);
ItemStack remainder = handlerModifiable.insertItem(index, maxAdd, true);
handlerModifiable.setStackInSlot(index, currentStack);
return maxInput - remainder.getCount();
}
else
{
ItemStack remainder = handler.insertItem(index, maxAdd, true);
int current = currentStack.getCount();
int added = maxInput - remainder.getCount();
return current + added;
}
}
@Override
public boolean canTakeStack(EntityPlayer playerIn)
{
return !this.getItemHandler().extractItem(index, 1, true).isEmpty();
}
@Override
@Nonnull
public ItemStack decrStackSize(int amount)
{
return this.getItemHandler().extractItem(index, amount, false);
}
public IItemHandler getItemHandler()
{
return itemHandler;
}
/* TODO Slot patches
@Override
public boolean isSameInventory(Slot other)
{
return other instanceof SlotItemHandler && ((SlotItemHandler) other).getItemHandler() == this.itemHandler;
}*/
}