package refinedstorage.api.network;

import io.netty.buffer.ByteBuf;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.oredict.OreDictionary;
import refinedstorage.api.RefinedStorageAPI;
import refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.autocrafting.task.ICraftingTask;
import refinedstorage.api.storage.CompareUtils;

import java.util.List;

/**
 * Utilities for network manipulation.
 */
public final class NetworkUtils {
    public static ItemStack extractItem(INetworkMaster network, ItemStack stack, int size) {
        return network.extractItem(stack, size, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
    }

    public static ItemStack extractItemOreDict(INetworkMaster network, ItemStack stack, int size) {
        int ids[] = OreDictionary.getOreIDs(stack);
        String names[] = new String[ids.length];

        for (int i = 0; i < ids.length; ++i) {
            names[i] = OreDictionary.getOreName(ids[i]);
        }

        for (String name : names) {
            List<ItemStack> possibilities = OreDictionary.getOres(name);

            for (ItemStack possibility : possibilities) {
                ItemStack result = network.extractItem(possibility, size, 0);

                if (result != null) {
                    return result;
                }
            }
        }

        return null;
    }

    public static FluidStack extractFluid(INetworkMaster network, FluidStack stack, int size) {
        return network.extractFluid(stack, size, CompareUtils.COMPARE_NBT);
    }

    public static ICraftingPattern getPattern(INetworkMaster network, ItemStack stack) {
        return network.getPattern(stack, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
    }

    public static ICraftingTask createCraftingTask(INetworkMaster network, ICraftingPattern pattern) {
        return RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(pattern.getId()).create(network.getNetworkWorld(), null, pattern);
    }

    public static boolean hasPattern(INetworkMaster network, ItemStack stack) {
        return getPattern(network, stack) != null;
    }

    public static void rebuildGraph(INetworkMaster network) {
        network.getNodeGraph().rebuild(network.getPosition(), true);
    }

    public static int getItemStackHashCode(ItemStack stack) {
        return stack.getItem().hashCode() * (stack.getItemDamage() + 1) * (stack.hasTagCompound() ? stack.getTagCompound().hashCode() : 1);
    }

    public static int getFluidStackHashCode(FluidStack stack) {
        return stack.getFluid().hashCode() * (stack.tag != null ? stack.tag.hashCode() : 1);
    }

    public static void scheduleCraftingTaskIfUnscheduled(INetworkMaster network, ItemStack stack, int toSchedule, int compare) {
        int alreadyScheduled = 0;

        for (ICraftingTask task : network.getCraftingTasks()) {
            for (ItemStack output : task.getPattern().getOutputs()) {
                if (CompareUtils.compareStack(output, stack, compare)) {
                    alreadyScheduled++;
                }
            }
        }

        for (int i = 0; i < toSchedule - alreadyScheduled; ++i) {
            ICraftingPattern pattern = network.getPattern(stack, compare);

            if (pattern != null) {
                network.addCraftingTask(createCraftingTask(network, pattern));
            }
        }
    }

    public static void writeItemStack(ByteBuf buf, INetworkMaster network, ItemStack stack) {
        buf.writeInt(Item.getIdFromItem(stack.getItem()));
        buf.writeInt(stack.stackSize);
        buf.writeInt(stack.getItemDamage());
        ByteBufUtils.writeTag(buf, stack.getTagCompound());
        buf.writeInt(getItemStackHashCode(stack));
        buf.writeBoolean(hasPattern(network, stack));
    }

    public static void writeFluidStack(ByteBuf buf, FluidStack stack) {
        buf.writeInt(getFluidStackHashCode(stack));
        ByteBufUtils.writeUTF8String(buf, FluidRegistry.getFluidName(stack.getFluid()));
        buf.writeInt(stack.amount);
        ByteBufUtils.writeTag(buf, stack.tag);
    }
}

@@ -287,57 +287,8 @@
 ls;%0A
-import net.minecraftforge.oredict.OreDictionary;%0A
 impo
@@ -328,16 +328,16 @@
 ageAPI;%0A
+
 import r
@@ -497,32 +497,8 @@
 s;%0A%0A
-import java.util.List;%0A%0A
 /**%0A
@@ -659,32 +659,32 @@
 ck, int size) %7B%0A
+
         return n
@@ -783,716 +783,8 @@
  %7D%0A%0A
-    public static ItemStack extractItemOreDict(INetworkMaster network, ItemStack stack, int size) %7B%0A        int ids%5B%5D = OreDictionary.getOreIDs(stack);%0A        String names%5B%5D = new String%5Bids.length%5D;%0A%0A        for (int i = 0; i %3C ids.length; ++i) %7B%0A            names%5Bi%5D = OreDictionary.getOreName(ids%5Bi%5D);%0A        %7D%0A%0A        for (String name : names) %7B%0A            List%3CItemStack%3E possibilities = OreDictionary.getOres(name);%0A%0A            for (ItemStack possibility : possibilities) %7B%0A                ItemStack result = network.extractItem(possibility, size, 0);%0A%0A                if (result != null) %7B%0A                    return result;%0A                %7D%0A            %7D%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A
     

Remove unneeded method