mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 16:27:47 +00:00
License has not changed, this is just more compact and doesn't include years. Bump version for RB.
117 lines
3.2 KiB
Java
117 lines
3.2 KiB
Java
/*
|
|
* Minecraft Forge - Forge Development LLC
|
|
* SPDX-License-Identifier: LGPL-2.1-only
|
|
*/
|
|
|
|
package net.minecraftforge.items;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.Container;
|
|
import net.minecraft.world.SimpleContainer;
|
|
import net.minecraft.world.inventory.Slot;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import javax.annotation.Nonnull;
|
|
|
|
public class SlotItemHandler extends Slot
|
|
{
|
|
private static Container emptyInventory = new SimpleContainer(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 mayPlace(@Nonnull ItemStack stack)
|
|
{
|
|
if (stack.isEmpty())
|
|
return false;
|
|
return itemHandler.isItemValid(index, stack);
|
|
}
|
|
|
|
@Override
|
|
@Nonnull
|
|
public ItemStack getItem()
|
|
{
|
|
return this.getItemHandler().getStackInSlot(index);
|
|
}
|
|
|
|
// Override if your IItemHandler does not implement IItemHandlerModifiable
|
|
@Override
|
|
public void set(@Nonnull ItemStack stack)
|
|
{
|
|
((IItemHandlerModifiable) this.getItemHandler()).setStackInSlot(index, stack);
|
|
this.setChanged();
|
|
}
|
|
|
|
@Override
|
|
public void onQuickCraft(@Nonnull ItemStack oldStackIn, @Nonnull ItemStack newStackIn)
|
|
{
|
|
|
|
}
|
|
|
|
@Override
|
|
public int getMaxStackSize()
|
|
{
|
|
return this.itemHandler.getSlotLimit(this.index);
|
|
}
|
|
|
|
@Override
|
|
public int getMaxStackSize(@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 mayPickup(Player playerIn)
|
|
{
|
|
return !this.getItemHandler().extractItem(index, 1, true).isEmpty();
|
|
}
|
|
|
|
@Override
|
|
@Nonnull
|
|
public ItemStack remove(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;
|
|
}*/
|
|
}
|