package net.mcft.copy.backpacks.misc.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import net.mcft.copy.backpacks.WearableBackpacks;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.Loader;

public final class IntermodUtils {
	
	private IntermodUtils() {  }
	
	private static boolean getRuneColorCached = false;
	private static Method setTargetStackMethod;
	private static Method getColorMethod;
	/** Returns the Quark rune color for this item or the default enchant glint
	 *  color if Quark isn't present or the item doesn't have a custom rune color. */
	public static int getRuneColor(ItemStack stack) {
		if (!getRuneColorCached) {
			if (Loader.isModLoaded("quark")) {
				try {
					Class<?> clazz = Class.forName("vazkii.quark.misc.feature.ColorRunes");
					setTargetStackMethod = clazz.getMethod("setTargetStack", ItemStack.class);
					getColorMethod = clazz.getMethod("getColor");
				} catch (ClassNotFoundException |
				         NoSuchMethodException ex) {
					WearableBackpacks.LOG.error("Error while fetching Quark ColorRunes methods", ex);
				}
			} else {
				setTargetStackMethod = null;
				getColorMethod = null;
			}
			getRuneColorCached = true;
		}
		if (setTargetStackMethod == null)
			return 0xFF8040CC;
		try {
			setTargetStackMethod.invoke(null, stack);
			return (int)getColorMethod.invoke(null);
		} catch (IllegalAccessException |
		         InvocationTargetException ex) {
			WearableBackpacks.LOG.error("Error while invoking Quark ColorRunes methods", ex);
			setTargetStackMethod = null;
			getColorMethod = null;
			return 0xFF8040CC;
		}
	}
	
}

@@ -66,119 +66,42 @@
 ect.
-InvocationTargetException;%0Aimport java.lang.reflect.Method;%0A%0Aimport net.mcft.copy.backpacks.WearableBackpack
+Method;%0Aimport java.util.Array
 s;%0A
+%0A
 impo
@@ -179,16 +179,67 @@
 oader;%0A%0A
+import net.mcft.copy.backpacks.WearableBackpacks;%0A%0A
 public f
@@ -299,16 +299,82 @@
  %7B  %7D%0A%09%0A
+%09private static final int DEFAULT_ENCHANTMENT_COLOR = 0xFF8040CC;%0A
 %09private
@@ -408,16 +408,17 @@
 rCached 
+ 
 = false;
@@ -461,16 +461,23 @@
 ckMethod
+ = null
 ;%0A%09priva
@@ -507,16 +507,29 @@
 orMethod
+       = null
 ;%0A%09/** R
@@ -981,32 +981,46 @@
 etColorMethod = 
+Arrays.stream(
 clazz.getMethod(
@@ -1022,19 +1022,92 @@
 thod
-(%22getColor%22
+s())%0A%09%09%09%09%09%09.filter(m -%3E %22getColor%22.equals(m.getName()))%0A%09%09%09%09%09%09.findAny().orElse(null
 );%0A%09
@@ -1281,80 +1281,8 @@
 %09%09%7D%0A
-%09%09%09%7D else %7B%0A%09%09%09%09setTargetStackMethod = null;%0A%09%09%09%09getColorMethod = null;%0A
 %09%09%09%7D
@@ -1321,16 +1321,17 @@
 %09%7D%0A%09%09if 
+(
 (setTarg
@@ -1352,37 +1352,81 @@
 == null)
-%0A%09%09%09return 0xFF8040CC
+ %7C%7C (getColorMethod == null))%0A%09%09%09return DEFAULT_ENCHANTMENT_COLOR
 ;%0A%09%09try 
@@ -1481,16 +1481,64 @@
 %09%09return
+ (getColorMethod.getParameterCount() == 0)%0A%09%09%09%09?
  (int)ge
@@ -1566,73 +1566,87 @@
 ull)
-;
 %0A%09%09
-%7D catch (IllegalAccessException %7C%0A%09%09         InvocationTarget
+%09%09: (int)getColorMethod.invoke(null, DEFAULT_ENCHANTMENT_COLOR);%0A%09%09%7D catch (
 Exce
@@ -1788,24 +1788,30 @@
 ColorMethod 
+      
 = null;%0A%09%09%09r
@@ -1816,26 +1816,41 @@
 %09return 
-0xFF8040CC
+DEFAULT_ENCHANTMENT_COLOR
 ;%0A%09%09%7D%0A%09%7D

Fix Quark colored runes support