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

226 lines
5.9 KiB
Java
Raw Normal View History

/*
* 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
*/
2016-01-17 16:41:34 +00:00
package net.minecraftforge.items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
2016-11-13 17:09:54 -05:00
import net.minecraft.util.NonNullList;
2016-01-17 16:41:34 +00:00
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.INBTSerializable;
2016-11-13 17:09:54 -05:00
import javax.annotation.Nonnull;
2016-01-17 16:41:34 +00:00
public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, INBTSerializable<NBTTagCompound>
{
2016-11-13 17:09:54 -05:00
protected NonNullList<ItemStack> stacks;
2016-01-17 16:41:34 +00:00
public ItemStackHandler()
{
this(1);
}
public ItemStackHandler(int size)
{
stacks = NonNullList.withSize(size, ItemStack.EMPTY);
2016-01-17 16:41:34 +00:00
}
2016-11-13 17:09:54 -05:00
public ItemStackHandler(NonNullList<ItemStack> stacks)
2016-03-21 09:24:36 -05:00
{
this.stacks = stacks;
}
2016-01-17 16:41:34 +00:00
public void setSize(int size)
{
stacks = NonNullList.withSize(size, ItemStack.EMPTY);
2016-01-17 16:41:34 +00:00
}
@Override
2016-11-13 17:09:54 -05:00
public void setStackInSlot(int slot, @Nonnull ItemStack stack)
2016-01-17 16:41:34 +00:00
{
validateSlotIndex(slot);
2016-11-13 17:09:54 -05:00
this.stacks.set(slot, stack);
2016-01-17 16:41:34 +00:00
onContentsChanged(slot);
}
@Override
public int getSlots()
{
2016-11-13 17:09:54 -05:00
return stacks.size();
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 getStackInSlot(int slot)
{
validateSlotIndex(slot);
2016-11-13 17:09:54 -05:00
return this.stacks.get(slot);
2016-01-17 16:41:34 +00:00
}
@Override
2016-11-13 17:09:54 -05:00
@Nonnull
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate)
2016-01-17 16:41:34 +00:00
{
if (stack.isEmpty())
return ItemStack.EMPTY;
2016-01-17 16:41:34 +00:00
validateSlotIndex(slot);
2016-11-13 17:09:54 -05:00
ItemStack existing = this.stacks.get(slot);
2016-01-17 16:41:34 +00:00
int limit = getStackLimit(slot, stack);
if (!existing.isEmpty())
2016-01-17 16:41:34 +00:00
{
if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
return stack;
limit -= existing.getCount();
2016-01-17 16:41:34 +00:00
}
if (limit <= 0)
return stack;
boolean reachedLimit = stack.getCount() > limit;
2016-01-17 16:41:34 +00:00
if (!simulate)
{
if (existing.isEmpty())
2016-01-17 16:41:34 +00:00
{
2016-11-13 17:09:54 -05:00
this.stacks.set(slot, reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
2016-01-17 16:41:34 +00:00
}
else
{
existing.grow(reachedLimit ? limit : stack.getCount());
2016-01-17 16:41:34 +00:00
}
onContentsChanged(slot);
}
return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount()- limit) : ItemStack.EMPTY;
2016-01-17 16:41:34 +00:00
}
2017-06-17 18:24:17 -07:00
@Override
2016-11-13 17:09:54 -05:00
@Nonnull
2016-01-17 16:41:34 +00:00
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
if (amount == 0)
return ItemStack.EMPTY;
2016-01-17 16:41:34 +00:00
validateSlotIndex(slot);
2016-11-13 17:09:54 -05:00
ItemStack existing = this.stacks.get(slot);
2016-01-17 16:41:34 +00:00
if (existing.isEmpty())
return ItemStack.EMPTY;
2016-01-17 16:41:34 +00:00
int toExtract = Math.min(amount, existing.getMaxStackSize());
if (existing.getCount() <= toExtract)
2016-01-17 16:41:34 +00:00
{
if (!simulate)
{
this.stacks.set(slot, ItemStack.EMPTY);
2016-01-17 16:41:34 +00:00
onContentsChanged(slot);
}
return existing;
}
else
{
if (!simulate)
{
this.stacks.set(slot, ItemHandlerHelper.copyStackWithSize(existing, existing.getCount() - toExtract));
2016-01-17 16:41:34 +00:00
onContentsChanged(slot);
}
return ItemHandlerHelper.copyStackWithSize(existing, toExtract);
}
}
@Override
public int getSlotLimit(int slot)
{
return 64;
}
protected int getStackLimit(int slot, @Nonnull ItemStack stack)
2016-01-17 16:41:34 +00:00
{
return Math.min(getSlotLimit(slot), stack.getMaxStackSize());
2016-01-17 16:41:34 +00:00
}
@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack)
{
return true;
}
2016-01-17 16:41:34 +00:00
@Override
public NBTTagCompound serializeNBT()
{
NBTTagList nbtTagList = new NBTTagList();
2016-11-13 17:09:54 -05:00
for (int i = 0; i < stacks.size(); i++)
2016-01-17 16:41:34 +00:00
{
if (!stacks.get(i).isEmpty())
2016-01-17 16:41:34 +00:00
{
NBTTagCompound itemTag = new NBTTagCompound();
itemTag.setInteger("Slot", i);
2016-11-13 17:09:54 -05:00
stacks.get(i).writeToNBT(itemTag);
2016-01-17 16:41:34 +00:00
nbtTagList.appendTag(itemTag);
}
}
NBTTagCompound nbt = new NBTTagCompound();
nbt.setTag("Items", nbtTagList);
2016-11-13 17:09:54 -05:00
nbt.setInteger("Size", stacks.size());
2016-01-17 16:41:34 +00:00
return nbt;
}
@Override
public void deserializeNBT(NBTTagCompound nbt)
{
2016-11-13 17:09:54 -05:00
setSize(nbt.hasKey("Size", Constants.NBT.TAG_INT) ? nbt.getInteger("Size") : stacks.size());
2016-01-17 16:41:34 +00:00
NBTTagList tagList = nbt.getTagList("Items", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < tagList.tagCount(); i++)
{
NBTTagCompound itemTags = tagList.getCompoundTagAt(i);
int slot = itemTags.getInteger("Slot");
2016-11-13 17:09:54 -05:00
if (slot >= 0 && slot < stacks.size())
2016-01-17 16:41:34 +00:00
{
2016-11-13 17:09:54 -05:00
stacks.set(slot, new ItemStack(itemTags));
2016-01-17 16:41:34 +00:00
}
}
onLoad();
}
protected void validateSlotIndex(int slot)
{
2016-11-13 17:09:54 -05:00
if (slot < 0 || slot >= stacks.size())
throw new RuntimeException("Slot " + slot + " not in valid range - [0," + stacks.size() + ")");
2016-01-17 16:41:34 +00:00
}
protected void onLoad()
{
}
protected void onContentsChanged(int slot)
{
}
}