2016-06-23 03:49:47 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
|
|
|
* Copyright (c) 2016.
|
|
|
|
*
|
|
|
|
* 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 22:09:54 +00: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 22:09:54 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2016-01-17 16:41:34 +00:00
|
|
|
public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, INBTSerializable<NBTTagCompound>
|
|
|
|
{
|
2016-11-13 22:09:54 +00:00
|
|
|
protected NonNullList<ItemStack> stacks;
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
public ItemStackHandler()
|
|
|
|
{
|
|
|
|
this(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStackHandler(int size)
|
|
|
|
{
|
2016-11-17 00:06:52 +00:00
|
|
|
stacks = NonNullList.func_191197_a(size, ItemStack.field_190927_a);
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 22:09:54 +00:00
|
|
|
public ItemStackHandler(NonNullList<ItemStack> stacks)
|
2016-03-21 14:24:36 +00:00
|
|
|
{
|
|
|
|
this.stacks = stacks;
|
|
|
|
}
|
|
|
|
|
2016-01-17 16:41:34 +00:00
|
|
|
public void setSize(int size)
|
|
|
|
{
|
2016-11-13 22:09:54 +00:00
|
|
|
stacks = NonNullList.func_191197_a(size, ItemStack.field_190927_a);
|
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
|
|
|
{
|
|
|
|
validateSlotIndex(slot);
|
2016-11-13 22:09:54 +00:00
|
|
|
if (ItemStack.areItemStacksEqual(this.stacks.get(slot), stack))
|
2016-01-17 16:41:34 +00:00
|
|
|
return;
|
2016-11-13 22:09:54 +00:00
|
|
|
this.stacks.set(slot, stack);
|
2016-01-17 16:41:34 +00:00
|
|
|
onContentsChanged(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSlots()
|
|
|
|
{
|
2016-11-13 22:09:54 +00:00
|
|
|
return stacks.size();
|
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 getStackInSlot(int slot)
|
|
|
|
{
|
|
|
|
validateSlotIndex(slot);
|
2016-11-13 22:09:54 +00:00
|
|
|
return this.stacks.get(slot);
|
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-11-13 22:09:54 +00:00
|
|
|
if (stack.func_190926_b())
|
|
|
|
return ItemStack.field_190927_a;
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
validateSlotIndex(slot);
|
|
|
|
|
2016-11-13 22:09:54 +00:00
|
|
|
ItemStack existing = this.stacks.get(slot);
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
int limit = getStackLimit(slot, stack);
|
|
|
|
|
2016-11-13 22:09:54 +00:00
|
|
|
if (!existing.func_190926_b())
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
|
|
|
if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
|
|
|
|
return stack;
|
|
|
|
|
2016-11-13 22:09:54 +00:00
|
|
|
limit -= existing.func_190916_E();
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (limit <= 0)
|
|
|
|
return stack;
|
|
|
|
|
2016-11-13 22:09:54 +00:00
|
|
|
boolean reachedLimit = stack.func_190916_E() > limit;
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
if (!simulate)
|
|
|
|
{
|
2016-11-13 22:09:54 +00:00
|
|
|
if (existing.func_190926_b())
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2016-11-13 22:09:54 +00:00
|
|
|
this.stacks.set(slot, reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-13 22:09:54 +00:00
|
|
|
existing.func_190917_f(reachedLimit ? limit : stack.func_190916_E());
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
onContentsChanged(slot);
|
|
|
|
}
|
|
|
|
|
2016-11-13 22:09:54 +00:00
|
|
|
return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.func_190916_E()- limit) : ItemStack.field_190927_a;
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
|
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-11-13 22:09:54 +00:00
|
|
|
return ItemStack.field_190927_a;
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
validateSlotIndex(slot);
|
|
|
|
|
2016-11-13 22:09:54 +00:00
|
|
|
ItemStack existing = this.stacks.get(slot);
|
2016-01-17 16:41:34 +00:00
|
|
|
|
2016-11-13 22:09:54 +00:00
|
|
|
if (existing.func_190926_b())
|
|
|
|
return ItemStack.field_190927_a;
|
2016-01-17 16:41:34 +00:00
|
|
|
|
|
|
|
int toExtract = Math.min(amount, existing.getMaxStackSize());
|
|
|
|
|
2016-11-13 22:09:54 +00:00
|
|
|
if (existing.func_190916_E() <= toExtract)
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
|
|
|
if (!simulate)
|
|
|
|
{
|
2016-11-13 22:09:54 +00:00
|
|
|
this.stacks.set(slot, ItemStack.field_190927_a);
|
2016-01-17 16:41:34 +00:00
|
|
|
onContentsChanged(slot);
|
|
|
|
}
|
|
|
|
return existing;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!simulate)
|
|
|
|
{
|
2016-11-13 22:09:54 +00:00
|
|
|
this.stacks.set(slot, ItemHandlerHelper.copyStackWithSize(existing, existing.func_190916_E() - toExtract));
|
2016-01-17 16:41:34 +00:00
|
|
|
onContentsChanged(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ItemHandlerHelper.copyStackWithSize(existing, toExtract);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int getStackLimit(int slot, ItemStack stack)
|
|
|
|
{
|
|
|
|
return stack.getMaxStackSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NBTTagCompound serializeNBT()
|
|
|
|
{
|
|
|
|
NBTTagList nbtTagList = new NBTTagList();
|
2016-11-13 22:09:54 +00:00
|
|
|
for (int i = 0; i < stacks.size(); i++)
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2016-11-13 22:09:54 +00:00
|
|
|
if (!stacks.get(i).func_190926_b())
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
|
|
|
NBTTagCompound itemTag = new NBTTagCompound();
|
|
|
|
itemTag.setInteger("Slot", i);
|
2016-11-13 22:09:54 +00: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 22:09:54 +00: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 22:09:54 +00: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 22:09:54 +00:00
|
|
|
if (slot >= 0 && slot < stacks.size())
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2016-11-13 22:09:54 +00:00
|
|
|
stacks.set(slot, new ItemStack(itemTags));
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
onLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void validateSlotIndex(int slot)
|
|
|
|
{
|
2016-11-13 22:09:54 +00: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)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|