Advertisement
Guest User

MFR Compatibility for Magical Crops

a guest
Feb 1st, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. package mechaet.mfr.compat.magicalcrops;
  2.  
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5.  
  6. import net.minecraft.block.Block;
  7. import net.minecraft.item.Item;
  8. import net.minecraftforge.common.IPlantable;
  9. import powercrystals.minefactoryreloaded.MFRRegistry;
  10. import powercrystals.minefactoryreloaded.farmables.plantables.PlantableCropPlant;
  11. import powercrystals.minefactoryreloaded.farmables.harvestables.HarvestableCropPlant;
  12. import cpw.mods.fml.common.Mod;
  13. import cpw.mods.fml.common.Mod.EventHandler;
  14. import cpw.mods.fml.common.Mod.Instance;
  15. import cpw.mods.fml.common.event.FMLInitializationEvent;
  16. import cpw.mods.fml.common.event.FMLPostInitializationEvent;
  17. import cpw.mods.fml.common.event.FMLPreInitializationEvent;
  18. import cpw.mods.fml.common.network.NetworkMod;
  19.  
  20. @Mod(modid = MFRCompatMagicalCrops._ID, name = MFRCompatMagicalCrops._NAME, version = MFRCompatMagicalCrops._VERSION, dependencies = MFRCompatMagicalCrops._DEPENDENCIES)
  21. @NetworkMod(clientSideRequired = false, serverSideRequired = false)
  22. public class MFRCompatMagicalCrops
  23. {
  24.  
  25.     public static final String _ID = "MineFactoryReloaded|MFRCompatMagicalCrops";
  26.     public static final String _NAME = "MFR Compat: MagicalCrops";
  27.     public static final String _VERSION = "0.1a";
  28.     public static final String _DEPENDENCIES = "after:MineFactoryReloaded;after:magicalcrops";
  29.  
  30.     @Instance(_ID)
  31.     public static MFRCompatMagicalCrops instance;
  32.     private Logger _log;
  33.  
  34.     @EventHandler
  35.     public void preInit(FMLPreInitializationEvent event)
  36.     {
  37.         _log = event.getModLog();
  38.     }
  39.  
  40.     private void registerMFR() throws Exception
  41.     {
  42.         // Registering materials
  43.         String[] mMaterials =
  44.         {
  45.                 "Alumin",
  46.                 "Blaze",
  47.                 "Coal",
  48.                 "Copper",
  49.                 "Diamond",
  50.                 "Dye",
  51.                 "Emerald",
  52.                 "Ender",
  53.                 "Essence",
  54.                 "Glowstone",
  55.                 "Gold",
  56.                 "Iron",
  57.                 "Lapis",
  58.                 "Lead",
  59.                 "Nether",
  60.                 "Obsidian",
  61.                 "Peridot",
  62.                 "Quartz",
  63.                 "Redstone",
  64.                 "Ruby",
  65.                 "Sapphire",
  66.                 "Silver",
  67.                 "Tin",
  68.                 "XP",
  69.         };
  70.         registerMFR(mMaterials, "m");
  71.  
  72.         // Registering elements
  73.         String[] nMaterials =
  74.         {
  75.                 "Bberry",
  76.                 "Chil",
  77.                 "Cucum",
  78.                 "Grape",
  79.                 "Rberry",
  80.                 "Sberry",
  81.                 "Sweetcorn",
  82.                 "Tomato",
  83.         };
  84.         registerMFR(nMaterials, "");
  85.  
  86.         // Registering elements
  87.         String[] eMaterials =
  88.         {
  89.                 "Water",
  90.                 "Fire",
  91.                 "Earth",
  92.                 "Air",
  93.         };
  94.         registerMFR(eMaterials, "e");
  95.  
  96.         // Registering your soul
  97.         String[] soulMaterials =
  98.         {
  99.                 "Cow",
  100.                 "Creeper",
  101.                 "Ghast",
  102.                 "Magma",
  103.                 "Skeleton",
  104.                 "Slime",
  105.                 "Spider",
  106.         };
  107.         registerMFR(soulMaterials, "soul");
  108.     }
  109.  
  110.     private void registerMFR(String[] materials, String prefix) throws Exception
  111.     {
  112.         // Base class of the MC mod
  113.         Class cropsModClass = Class.forName("magicalcrops.mod_mCrops");
  114.  
  115.         // Loop through all the crops that were passed in and add them to the
  116.         // list.
  117.         for (String material : materials)
  118.         {
  119.             // Shared
  120.             String cropName;
  121.             String seedName;
  122.  
  123.             if (prefix.isEmpty())
  124.             {
  125.                 cropName = "crop" + material;
  126.                 seedName = "seed" + material;
  127.  
  128.             }
  129.             else
  130.             {
  131.                 cropName = prefix + "Crop" + material;
  132.                 seedName = prefix.charAt(0) + "Seeds" + material;
  133.             }
  134.  
  135.             Block crop = (Block) cropsModClass.getField(cropName).get(null);
  136.             Item seed = (Item) cropsModClass.getField(seedName).get(null);
  137.  
  138.             if (crop == null || seed == null)
  139.             {
  140.                 _log.warning("Unable to find crop " + cropName + " by reflection.  Maybe that crop is disabled or something?");
  141.                 continue;
  142.             }
  143.  
  144.             // Planter
  145.             if (!(seed instanceof IPlantable))
  146.             {
  147.                 throw new IllegalArgumentException("Seed " + seed.getUnlocalizedName() + " is not IPlantable.  Type is " + seed.getClass());
  148.             }
  149.  
  150.             // Using MFR code to register for Planter
  151.             MFRRegistry.registerPlantable(new PlantableCropPlant(seed.itemID, crop.blockID));
  152.  
  153.             // Using MFR code to register for Harvester
  154.             MFRRegistry.registerHarvestable(new HarvestableCropPlant(crop.blockID, 7));
  155.  
  156.             _log.finer("Registered crop " + cropName + " with the MFR planter and harvester.");
  157.         }
  158.     }
  159.  
  160.     @EventHandler
  161.     public void init(FMLInitializationEvent event) {
  162.  
  163.         try
  164.         {
  165.             _log.log(Level.INFO, "Loading MFR Compatibility for Magical Crops");
  166.  
  167.             // Doing this in post-init to minimize problems with Magical Crops
  168.             // not having finished init yet.
  169.             registerMFR();
  170.  
  171.             _log.log(Level.INFO, "MFR Compatibility for Magical Crops successfully initialized");
  172.  
  173.         } catch (Exception exception)
  174.         {
  175.             exception.printStackTrace();
  176.         }
  177.  
  178.     }
  179.  
  180.     @EventHandler
  181.     public static void postInit(FMLPostInitializationEvent event)
  182.     {
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement