Files
MinecraftForge/src/main/java/net/minecraftforge/items/SlotItemHandler.java
T

130 lines
3.8 KiB
Java
Raw Normal View History

/*
* Minecraft Forge
2019-02-10 17:57:03 -05:00
* Copyright (c) 2016-2019.
*
* 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;
2019-05-23 16:02:15 -07:00
import net.minecraft.entity.player.PlayerEntity;
2016-01-17 16:41:34 +00:00
import net.minecraft.inventory.IInventory;
2019-05-23 16:02:15 -07:00
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Slot;
2016-01-17 16:41:34 +00:00
import net.minecraft.item.ItemStack;
2016-11-13 17:09:54 -05:00
import javax.annotation.Nonnull;
2016-01-17 16:41:34 +00:00
public class SlotItemHandler extends Slot
{
2019-05-23 16:02:15 -07:00
private static IInventory emptyInventory = new Inventory(0);
private final IItemHandler itemHandler;
2016-01-17 16:41:34 +00:00
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
2016-11-13 17:09:54 -05:00
public boolean isItemValid(@Nonnull ItemStack stack)
2016-01-17 16:41:34 +00:00
{
if (stack.isEmpty())
2016-01-17 16:41:34 +00:00
return false;
return itemHandler.isItemValid(index, stack);
2016-01-17 16:41:34 +00:00
}
@Override
2016-11-13 17:09:54 -05:00
@Nonnull
2016-01-17 16:41:34 +00:00
public ItemStack getStack()
{
return this.getItemHandler().getStackInSlot(index);
2016-01-17 16:41:34 +00:00
}
// Override if your IItemHandler does not implement IItemHandlerModifiable
@Override
2016-11-13 17:09:54 -05:00
public void putStack(@Nonnull ItemStack stack)
2016-01-17 16:41:34 +00:00
{
((IItemHandlerModifiable) this.getItemHandler()).setStackInSlot(index, stack);
2016-01-17 16:41:34 +00:00
this.onSlotChanged();
}
@Override
2020-05-15 12:17:25 -07:00
public void onSlotChange(@Nonnull ItemStack oldStackIn, @Nonnull ItemStack newStackIn)
2016-01-17 16:41:34 +00:00
{
}
@Override
public int getSlotStackLimit()
{
return this.itemHandler.getSlotLimit(this.index);
}
2016-01-17 16:41:34 +00:00
@Override
2016-11-13 17:09:54 -05:00
public int getItemStackLimit(@Nonnull ItemStack stack)
2016-01-17 16:41:34 +00:00
{
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);
2016-01-17 16:41:34 +00:00
int current = currentStack.getCount();
int added = maxInput - remainder.getCount();
return current + added;
}
2016-01-17 16:41:34 +00:00
}
@Override
2019-05-23 16:02:15 -07:00
public boolean canTakeStack(PlayerEntity playerIn)
2016-01-17 16:41:34 +00:00
{
return !this.getItemHandler().extractItem(index, 1, true).isEmpty();
2016-01-17 16:41:34 +00:00
}
@Override
2016-11-13 17:09:54 -05:00
@Nonnull
2016-01-17 16:41:34 +00:00
public ItemStack decrStackSize(int amount)
{
return this.getItemHandler().extractItem(index, amount, false);
}
public IItemHandler getItemHandler()
{
return itemHandler;
2016-01-17 16:41:34 +00:00
}
2018-09-14 12:30:56 -04:00
/* TODO Slot patches
2016-06-04 03:26:51 +03:00
@Override
public boolean isSameInventory(Slot other)
{
return other instanceof SlotItemHandler && ((SlotItemHandler) other).getItemHandler() == this.itemHandler;
2018-09-14 12:30:56 -04:00
}*/
}