83 lines
3.2 KiB
Java
83 lines
3.2 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.item.ItemStack;
|
|
import net.minecraft.nbt.INBTBase;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.nbt.NBTTagList;
|
|
import net.minecraft.util.EnumFacing;
|
|
import net.minecraftforge.common.capabilities.Capability;
|
|
import net.minecraftforge.common.capabilities.CapabilityInject;
|
|
import net.minecraftforge.common.capabilities.CapabilityManager;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
public class CapabilityItemHandler
|
|
{
|
|
@CapabilityInject(IItemHandler.class)
|
|
public static Capability<IItemHandler> ITEM_HANDLER_CAPABILITY = null;
|
|
|
|
public static void register()
|
|
{
|
|
CapabilityManager.INSTANCE.register(IItemHandler.class, new Capability.IStorage<IItemHandler>()
|
|
{
|
|
@Override
|
|
public INBTBase writeNBT(Capability<IItemHandler> capability, IItemHandler instance, EnumFacing side)
|
|
{
|
|
NBTTagList nbtTagList = new NBTTagList();
|
|
int size = instance.getSlots();
|
|
for (int i = 0; i < size; i++)
|
|
{
|
|
ItemStack stack = instance.getStackInSlot(i);
|
|
if (!stack.isEmpty())
|
|
{
|
|
NBTTagCompound itemTag = new NBTTagCompound();
|
|
itemTag.setInt("Slot", i);
|
|
stack.write(itemTag);
|
|
nbtTagList.add(itemTag);
|
|
}
|
|
}
|
|
return nbtTagList;
|
|
}
|
|
|
|
@Override
|
|
public void readNBT(Capability<IItemHandler> capability, IItemHandler instance, EnumFacing side, INBTBase base)
|
|
{
|
|
if (!(instance instanceof IItemHandlerModifiable))
|
|
throw new RuntimeException("IItemHandler instance does not implement IItemHandlerModifiable");
|
|
IItemHandlerModifiable itemHandlerModifiable = (IItemHandlerModifiable) instance;
|
|
NBTTagList tagList = (NBTTagList) base;
|
|
for (int i = 0; i < tagList.size(); i++)
|
|
{
|
|
NBTTagCompound itemTags = tagList.getCompound(i);
|
|
int j = itemTags.getInt("Slot");
|
|
|
|
if (j >= 0 && j < instance.getSlots())
|
|
{
|
|
itemHandlerModifiable.setStackInSlot(j, ItemStack.read(itemTags));
|
|
}
|
|
}
|
|
}
|
|
}, ItemStackHandler::new);
|
|
}
|
|
|
|
}
|