From 51f597321d5c6a3b91953ff73e5e183f20cbedf2 Mon Sep 17 00:00:00 2001
From: Graham Hughes <graham@sigwinch.org>
Date: Wed, 21 Jun 2017 18:46:42 -0700
Subject: [PATCH] Add crop support for Pam's Harvestcraft and Roots herbs
 (#1164)

* Add harvest ritual support for Pam's crops.

* Add roots crops.

* Per @TehNut, use BM instance logger.

* Call getMaxAge rather than hardcode 3.

(cherry picked from commit 061850e)
---
 .../harvest/HarvestHandlerPlantable.java      | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerPlantable.java b/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerPlantable.java
index a5152b27..141c8b56 100644
--- a/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerPlantable.java
+++ b/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerPlantable.java
@@ -1,9 +1,15 @@
 package WayofTime.bloodmagic.ritual.harvest;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+
+import WayofTime.bloodmagic.BloodMagic;
 import WayofTime.bloodmagic.api.BlockStack;
 import WayofTime.bloodmagic.api.iface.IHarvestHandler;
 import WayofTime.bloodmagic.api.registry.HarvestRegistry;
 import net.minecraft.block.Block;
+import net.minecraft.block.BlockCrops;
 import net.minecraft.entity.item.EntityItem;
 import net.minecraft.init.Blocks;
 import net.minecraft.item.ItemStack;
@@ -13,6 +19,7 @@ import net.minecraft.world.World;
 import net.minecraftforge.common.IPlantable;
 import net.minecraftforge.fml.common.Loader;
 import net.minecraftforge.fml.common.registry.ForgeRegistries;
+import net.minecraftforge.fml.relauncher.ReflectionHelper;
 
 import java.util.List;
 
@@ -38,6 +45,15 @@ public class HarvestHandlerPlantable implements IHarvestHandler
 
         addThirdPartyCrop("extrautils2", "redorchid", 6);
         addThirdPartyCrop("extrautils2", "enderlily", 7);
+
+        addThirdPartyCrop("roots", "moonglow", 7);
+        addThirdPartyCrop("roots", "terra_moss", 7);
+        addThirdPartyCrop("roots", "pereskia", 7);
+        addThirdPartyCrop("roots", "wildroot", 7);
+        addThirdPartyCrop("roots", "aubergine", 7);
+        addThirdPartyCrop("roots", "spirit_herb", 7);
+
+        addPamCrops();
     }
 
     @Override
@@ -98,4 +114,37 @@ public class HarvestHandlerPlantable implements IHarvestHandler
         if (block != null && block != Blocks.AIR)
             HarvestRegistry.registerStandardCrop(block, matureMeta);
     }
+
+    private static void addPamCrops()
+    {
+        if (!Loader.isModLoaded("harvestcraft"))
+            return;
+
+        try {
+            ClassLoader loader = HarvestHandlerPlantable.class.getClassLoader();
+            String className = "com.pam.harvestcraft.blocks.CropRegistry";
+            Class<?> registry = ReflectionHelper.getClass(loader, className);
+            Field names = ReflectionHelper.findField(registry, "cropNames");
+            Method getCrop = registry.getMethod("getCrop", String.class);
+            for (String name : (String[])names.get(null)) {
+                BlockCrops crop = (BlockCrops) getCrop.invoke(null, name);
+                HarvestRegistry.registerStandardCrop(crop, crop.getMaxAge());
+            }
+        } catch (NoSuchMethodException e) {
+            BloodMagic.instance.getLogger().error("HarvestCraft integration cancelled; unable to find crop name mapper");
+            return;
+        } catch (IllegalAccessException e) {
+            BloodMagic.instance.getLogger().error("HarvestCraft integration cancelled; crop name lookup broke");
+            return;
+        } catch (InvocationTargetException e) {
+            BloodMagic.instance.getLogger().error("HarvestCraft integration cancelled; crop name lookup broke");
+            return;
+        } catch (ReflectionHelper.UnableToFindClassException e) {
+            BloodMagic.instance.getLogger().error("HarvestCraft integration cancelled; unable to find crop registry");
+            return;
+        } catch (ReflectionHelper.UnableToFindFieldException e) {
+            BloodMagic.instance.getLogger().error("HarvestCraft integration cancelled; unable to find crop names in registry");
+            return;
+        }
+    }
 }