API tutorials: creating a custom storage disk

Implementation

Getting the Refined Storage API

We'll need some helper methods from the Refined Storage API first.

import com.raoulvdberge.refinedstorage.api.IRSAPI;
import com.raoulvdberge.refinedstorage.api.RSAPIInject;

public class RSHelper {
    @RSAPIInject
    public static IRSAPI API;
}

Implementing the item

For educational purposes, we'll be implementing a "huge" storage disk that can store a million items.

Note that this item doesn't set the registry name in Forge, as it's irrelevant for this tutorial.

import com.raoulvdberge.refinedstorage.api.storage.IStorageDisk;
import com.raoulvdberge.refinedstorage.api.storage.IStorageDiskProvider;
import com.raoulvdberge.refinedstorage.api.storage.StorageDiskType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;

import javax.annotation.Nonnull;
import java.util.List;

public class ItemHugeStorageDisk extends Item implements IStorageDiskProvider<ItemStack> {
    @Override
    public void onUpdate(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
        super.onUpdate(stack, world, entity, slot, selected);

        if (!stack.hasTagCompound()) {
            RSHelper.API.getDefaultStorageDiskBehavior().initDisk(StorageDiskType.ITEMS, stack);
        }
    }

    @Override
    public void addInformation(ItemStack disk, EntityPlayer player, List<String> tooltip, boolean advanced) {
        IStorageDisk storage = create(disk);

        if (storage.isValid(disk)) {
            tooltip.add("Stored: " + RSHelper.API.getQuantityFormatter().format(storage.getStored()) + " / " + RSHelper.API.getQuantityFormatter().format(storage.getCapacity()));
        }
    }

    @Override
    public void onCreated(ItemStack stack, World world, EntityPlayer player) {
        super.onCreated(stack, world, player);

        RSHelper.API.getDefaultStorageDiskBehavior().initDisk(StorageDiskType.ITEMS, stack);
    }

    @Override
    public NBTTagCompound getNBTShareTag(ItemStack stack) {
        return RSHelper.API.getDefaultStorageDiskBehavior().getShareTag(StorageDiskType.ITEMS, stack);
    }

    @Nonnull
    @Override
    public IStorageDisk<ItemStack> create(ItemStack disk) {
        return RSHelper.API.getDefaultStorageDiskBehavior().createItemStorage(disk.getTagCompound(), 1_000_000);
    }
}

The magic happens in IStorageDiskProvider#create, where we return our storage.

As you can see, we're using IStorageDiskBehavior throughout the place with IRSAPI#getDefaultStorageDiskBehavior.

While we can technically return our own IStorageDisk<ItemStack> implementation from IStorageDiskProvider#create, I chose to use the default storage disk item storage for this tutorial.

If you want more advanced, complicated or non-standard behaviors I recommend you to implement your own IStorageDisk.

Fluid storage disk

Obviously, you can also create a fluid storage disk.

Just use StorageDiskType.FLUIDS instead of StorageDiskType.ITEMS and use IStorageDiskBehavior#createFluidStorage.