2012-11-05 20:29:04 +01:00
|
|
|
package mekanism.api;
|
2012-10-14 03:48:29 +02:00
|
|
|
|
2013-03-11 18:49:01 +01:00
|
|
|
import net.minecraft.block.Block;
|
2012-12-20 22:53:39 +01:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2012-10-14 03:48:29 +02:00
|
|
|
|
|
|
|
/**
|
2012-11-05 20:29:04 +01:00
|
|
|
* Use this class's 'getItem()' method to retrieve ItemStacks from the 'Mekanism'
|
2012-10-14 03:48:29 +02:00
|
|
|
* class.
|
|
|
|
* @author AidanBrady
|
|
|
|
*
|
|
|
|
*/
|
2014-03-08 02:00:25 +01:00
|
|
|
public final class ItemRetriever
|
2012-10-14 03:48:29 +02:00
|
|
|
{
|
2012-11-05 20:29:04 +01:00
|
|
|
/** The 'Mekanism' class that items and blocks are retrieved from. */
|
|
|
|
private static Class Mekanism;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2012-10-14 03:48:29 +02:00
|
|
|
/**
|
|
|
|
* Attempts to retrieve an ItemStack of an item or block with the declared identifier.
|
2014-03-08 02:00:25 +01:00
|
|
|
*
|
2012-11-05 20:29:04 +01:00
|
|
|
* Mekanism identifiers follow an easy-to-remember pattern. All identifiers
|
2014-03-08 02:00:25 +01:00
|
|
|
* are identical to the String returned by 'getItemName().' None include spaces,
|
|
|
|
* and all start with a capital letter. The name that shows up in-game can be
|
|
|
|
* stripped down to identifier form by removing spaces and all non-alphabetic
|
2013-10-15 05:36:07 +02:00
|
|
|
* characters (,./'=-_). Below is an example:
|
2014-03-08 02:00:25 +01:00
|
|
|
*
|
2012-10-14 03:48:29 +02:00
|
|
|
* ItemStack enrichedAlloy = ItemRetriever.getItem("EnrichedAlloy");
|
2014-03-08 02:00:25 +01:00
|
|
|
*
|
2012-10-14 03:48:29 +02:00
|
|
|
* The same also works for blocks.
|
2014-03-08 02:00:25 +01:00
|
|
|
*
|
2012-10-14 03:48:29 +02:00
|
|
|
* ItemStack refinedObsidian = ItemRetriever.getItem("RefinedObsidian");
|
2014-03-08 02:00:25 +01:00
|
|
|
*
|
|
|
|
* Note that for items or blocks that have specific metadata you will need to create
|
2013-12-23 23:06:22 +01:00
|
|
|
* a new ItemStack with that specified value, as this will only return an ItemStack
|
|
|
|
* with the meta value '0.'
|
2014-03-08 02:00:25 +01:00
|
|
|
*
|
2012-10-14 03:48:29 +02:00
|
|
|
* Make sure you run this in or after FMLPostInitializationEvent runs, because most
|
2014-03-08 02:00:25 +01:00
|
|
|
* items are registered when FMLInitializationEvent runs. However, some items ARE
|
2012-10-14 03:48:29 +02:00
|
|
|
* registered later in order to hook into other mods. In a rare circumstance you may
|
2012-11-05 20:29:04 +01:00
|
|
|
* have to add "after:Mekanism" in the @Mod 'dependencies' annotation.
|
2014-03-08 02:00:25 +01:00
|
|
|
*
|
2012-11-05 20:29:04 +01:00
|
|
|
* @param identifier - a String to be searched in the 'Mekanism' class
|
2012-10-14 03:48:29 +02:00
|
|
|
* @return an ItemStack of the declared identifier, otherwise null.
|
|
|
|
*/
|
|
|
|
public static ItemStack getItem(String identifier)
|
|
|
|
{
|
|
|
|
try {
|
2012-11-05 20:29:04 +01:00
|
|
|
if(Mekanism == null)
|
2012-10-14 03:48:29 +02:00
|
|
|
{
|
2012-11-05 20:29:04 +01:00
|
|
|
Mekanism = Class.forName("mekanism.common.Mekanism");
|
2012-10-14 03:48:29 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2012-11-05 20:29:04 +01:00
|
|
|
Object ret = Mekanism.getField(identifier).get(null);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2012-10-14 03:48:29 +02:00
|
|
|
if(ret instanceof Item)
|
|
|
|
{
|
|
|
|
return new ItemStack((Item)ret, 1);
|
|
|
|
}
|
2013-03-11 18:49:01 +01:00
|
|
|
else if(ret instanceof Block)
|
|
|
|
{
|
|
|
|
return new ItemStack((Block)ret, 1);
|
|
|
|
}
|
2012-10-14 03:48:29 +02:00
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch(Exception e) {
|
2012-11-05 20:29:04 +01:00
|
|
|
System.err.println("[Mekanism] Error retrieving item with identifier '" + identifier + "': " + e.getMessage());
|
2012-10-14 03:48:29 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|