use of net.minecraftforge.items.IItemHandlerModifiable in project minecolonies by Minecolonies.
the class AbstractBuilding method getCapability.
@Nullable
@Override
public <T> T getCapability(@Nonnull final Capability<T> capability, @Nullable final EnumFacing facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY && facing == null) {
final Set<ICapabilityProvider> providers = new HashSet<>();
// Add myself
providers.add(getTileEntity());
// Add additional containers
providers.addAll(getAdditionalCountainers().stream().map(getTileEntity().getWorld()::getTileEntity).filter(entity -> (entity instanceof TileEntityChest) || (entity instanceof TileEntityRack)).collect(Collectors.toSet()));
providers.removeIf(Objects::isNull);
// Map all providers to IItemHandlers.
final Set<IItemHandlerModifiable> modifiables = providers.stream().flatMap(provider -> InventoryUtils.getItemHandlersFromProvider(provider).stream()).filter(handler -> handler instanceof IItemHandlerModifiable).map(handler -> (IItemHandlerModifiable) handler).collect(Collectors.toSet());
return (T) new CombinedItemHandler(getSchematicName(), modifiables.toArray(new IItemHandlerModifiable[modifiables.size()]));
}
return null;
}
use of net.minecraftforge.items.IItemHandlerModifiable in project minecolonies by Minecolonies.
the class CombinedItemHandler method deserializeNBT.
@SuppressWarnings({ RAWTYPES, UNCHECKED })
@Override
public void deserializeNBT(final NBTTagCompound nbt) {
final NBTTagList handlerList = nbt.getTagList(NBT_KEY_NAME, Constants.NBT.TAG_COMPOUND);
final NBTTagList indexList = nbt.getTagList(NBT_KEY_HANDLERS_INDEXLIST, Constants.NBT.TAG_INT);
if (handlerList.tagCount() == handlers.length) {
for (int i = 0; i < handlerList.tagCount(); i++) {
final NBTTagCompound handlerCompound = handlerList.getCompoundTagAt(i);
final IItemHandlerModifiable modifiable = handlers[indexList.getIntAt(i)];
if (modifiable instanceof INBTSerializable) {
final INBTSerializable serializable = (INBTSerializable) modifiable;
serializable.deserializeNBT(handlerCompound);
}
}
}
setName(nbt.hasKey(NBT_KEY_NAME) ? nbt.getString(NBT_KEY_NAME) : null);
}
use of net.minecraftforge.items.IItemHandlerModifiable in project minecolonies by Minecolonies.
the class CombinedItemHandler method serializeNBT.
@SuppressWarnings(RAWTYPES)
@Override
public NBTTagCompound serializeNBT() {
final NBTTagCompound compound = new NBTTagCompound();
int index = 0;
final NBTTagList handlerList = new NBTTagList();
final NBTTagList indexList = new NBTTagList();
for (final IItemHandlerModifiable handlerModifiable : handlers) {
if (handlerModifiable instanceof INBTSerializable) {
final INBTSerializable serializable = (INBTSerializable) handlerModifiable;
handlerList.appendTag(serializable.serializeNBT());
indexList.appendTag(new NBTTagInt(index));
}
index++;
}
compound.setTag(NBT_KEY_HANDLERS, handlerList);
if (hasCustomName()) {
compound.setString(NBT_KEY_NAME, customName);
}
return compound;
}
use of net.minecraftforge.items.IItemHandlerModifiable in project RecurrentComplex by Ivorforce.
the class ItemLootGenerationTag method applyGeneratorToInventory.
public static boolean applyGeneratorToInventory(WorldServer world, BlockPos pos, GeneratingItem generatingItem, ItemStack stack) {
TileEntity rightClicked = world.getTileEntity(pos);
if (rightClicked != null && ItemHandlers.hasModifiable(rightClicked)) {
if (!world.isRemote) {
IItemHandlerModifiable itemHandler = ItemHandlers.getModifiable(rightClicked);
generatingItem.generateInInventory(world, itemHandler, world.rand, stack, world.rand.nextInt(itemHandler.getSlots()));
LootGenerationHandler.generateAllTags(world, itemHandler, RecurrentComplex.specialRegistry.itemHidingMode(), world.rand);
}
return true;
}
return false;
}
Aggregations