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

82 lines
3.1 KiB
Java
Raw Normal View History

/*
* Minecraft Forge
2020-07-02 19:49:11 +02:00
* Copyright (c) 2016-2020.
*
* 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;
2019-05-23 16:02:15 -07:00
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.Direction;
2016-01-17 16:41:34 +00:00
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
2019-05-23 16:02:15 -07:00
public INBT writeNBT(Capability<IItemHandler> capability, IItemHandler instance, Direction side)
2016-01-17 16:41:34 +00:00
{
2019-05-23 16:02:15 -07:00
ListNBT nbtTagList = new ListNBT();
2016-01-17 16:41:34 +00:00
int size = instance.getSlots();
for (int i = 0; i < size; i++)
{
ItemStack stack = instance.getStackInSlot(i);
if (!stack.isEmpty())
2016-01-17 16:41:34 +00:00
{
2019-05-23 16:02:15 -07:00
CompoundNBT itemTag = new CompoundNBT();
itemTag.putInt("Slot", i);
stack.save(itemTag);
2018-08-27 13:10:07 -04:00
nbtTagList.add(itemTag);
2016-01-17 16:41:34 +00:00
}
}
return nbtTagList;
}
@Override
2019-05-23 16:02:15 -07:00
public void readNBT(Capability<IItemHandler> capability, IItemHandler instance, Direction side, INBT base)
2016-01-17 16:41:34 +00:00
{
if (!(instance instanceof IItemHandlerModifiable))
throw new RuntimeException("IItemHandler instance does not implement IItemHandlerModifiable");
IItemHandlerModifiable itemHandlerModifiable = (IItemHandlerModifiable) instance;
2019-05-23 16:02:15 -07:00
ListNBT tagList = (ListNBT) base;
2018-08-27 13:10:07 -04:00
for (int i = 0; i < tagList.size(); i++)
2016-01-17 16:41:34 +00:00
{
2019-05-23 16:02:15 -07:00
CompoundNBT itemTags = tagList.getCompound(i);
2018-09-22 03:40:22 -07:00
int j = itemTags.getInt("Slot");
2016-01-17 16:41:34 +00:00
if (j >= 0 && j < instance.getSlots())
2016-01-17 16:41:34 +00:00
{
itemHandlerModifiable.setStackInSlot(j, ItemStack.of(itemTags));
2016-01-17 16:41:34 +00:00
}
}
}
2017-06-27 23:18:09 -07:00
}, ItemStackHandler::new);
2016-01-17 16:41:34 +00:00
}
}