2016-06-23 03:49:47 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2020-07-02 17:49:11 +00:00
|
|
|
* Copyright (c) 2016-2020.
|
2016-06-23 03:49:47 +00:00
|
|
|
*
|
|
|
|
* 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 23:02:15 +00: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 23:02:15 +00:00
|
|
|
public INBT writeNBT(Capability<IItemHandler> capability, IItemHandler instance, Direction side)
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2019-05-23 23:02:15 +00: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);
|
2017-01-11 23:16:07 +00:00
|
|
|
if (!stack.isEmpty())
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2019-05-23 23:02:15 +00:00
|
|
|
CompoundNBT itemTag = new CompoundNBT();
|
|
|
|
itemTag.putInt("Slot", i);
|
2018-09-22 10:40:22 +00:00
|
|
|
stack.write(itemTag);
|
2018-08-27 17:10:07 +00:00
|
|
|
nbtTagList.add(itemTag);
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nbtTagList;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-23 23:02:15 +00: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 23:02:15 +00:00
|
|
|
ListNBT tagList = (ListNBT) base;
|
2018-08-27 17:10:07 +00:00
|
|
|
for (int i = 0; i < tagList.size(); i++)
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2019-05-23 23:02:15 +00:00
|
|
|
CompoundNBT itemTags = tagList.getCompound(i);
|
2018-09-22 10:40:22 +00:00
|
|
|
int j = itemTags.getInt("Slot");
|
2016-01-17 16:41:34 +00:00
|
|
|
|
2016-04-05 18:44:20 +00:00
|
|
|
if (j >= 0 && j < instance.getSlots())
|
2016-01-17 16:41:34 +00:00
|
|
|
{
|
2018-09-22 10:40:22 +00:00
|
|
|
itemHandlerModifiable.setStackInSlot(j, ItemStack.read(itemTags));
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 06:18:09 +00:00
|
|
|
}, ItemStackHandler::new);
|
2016-01-17 16:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|