Fixed ItemRetriever

This commit is contained in:
Aidan C. Brady 2015-03-27 12:49:29 -04:00
parent a0af78ef46
commit fba7fb34f0

View file

@ -1,5 +1,6 @@
package mekanism.api;
import mekanism.common.Mekanism;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@ -12,11 +13,14 @@ import net.minecraft.item.ItemStack;
*/
public final class ItemRetriever
{
/** The 'Mekanism' class that items and blocks are retrieved from. */
private static Class Mekanism;
/** The 'MekanismItems' class that items are retrieved from. */
private static Class MekanismItems;
/** The 'MekanismBlocks' class that blocks are retrieved from. */
private static Class MekanismBlocks;
/**
* Attempts to retrieve an ItemStack of an item or block with the declared identifier.
* Attempts to retrieve an ItemStack of an item with the declared identifier.
*
* Mekanism identifiers follow an easy-to-remember pattern. All identifiers
* are identical to the String returned by 'getItemName().' None include spaces,
@ -26,10 +30,6 @@ public final class ItemRetriever
*
* ItemStack enrichedAlloy = ItemRetriever.getItem("EnrichedAlloy");
*
* The same also works for blocks.
*
* ItemStack refinedObsidian = ItemRetriever.getItem("RefinedObsidian");
*
* Note that for items or blocks that have specific metadata you will need to create
* a new ItemStack with that specified value, as this will only return an ItemStack
* with the meta value '0.'
@ -39,27 +39,23 @@ public final class ItemRetriever
* registered later in order to hook into other mods. In a rare circumstance you may
* have to add "after:Mekanism" in the @Mod 'dependencies' annotation.
*
* @param identifier - a String to be searched in the 'Mekanism' class
* @param identifier - a String to be searched in the 'MekanismItems' class
* @return an ItemStack of the declared identifier, otherwise null.
*/
public static ItemStack getItem(String identifier)
{
try {
if(Mekanism == null)
if(MekanismItems == null)
{
Mekanism = Class.forName("mekanism.common.Mekanism");
MekanismItems = Class.forName("mekanism.common.MekanismItems");
}
Object ret = Mekanism.getField(identifier).get(null);
Object ret = MekanismItems.getField(identifier).get(null);
if(ret instanceof Item)
{
return new ItemStack((Item)ret, 1);
}
else if(ret instanceof Block)
{
return new ItemStack((Block)ret, 1);
}
else {
return null;
}
@ -68,4 +64,50 @@ public final class ItemRetriever
return null;
}
}
/**
* Attempts to retrieve an ItemStack of a block with the declared identifier.
*
* Mekanism identifiers follow an easy-to-remember pattern. All identifiers
* 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
* characters (,./'=-_). Below is an example:
*
* ItemStack enrichedAlloy = ItemRetriever.getItem("EnrichedAlloy");
*
* Note that for items or blocks that have specific metadata you will need to create
* a new ItemStack with that specified value, as this will only return an ItemStack
* with the meta value '0.'
*
* Make sure you run this in or after FMLPostInitializationEvent runs, because most
* items are registered when FMLInitializationEvent runs. However, some items ARE
* registered later in order to hook into other mods. In a rare circumstance you may
* have to add "after:Mekanism" in the @Mod 'dependencies' annotation.
*
* @param identifier - a String to be searched in the 'MekanismBlocks' class
* @return an ItemStack of the declared identifier, otherwise null.
*/
public static ItemStack getBlock(String identifier)
{
try {
if(MekanismBlocks == null)
{
MekanismBlocks = Class.forName("mekanism.common.MekanismBlocks");
}
Object ret = MekanismBlocks.getField(identifier).get(null);
if(ret instanceof Block)
{
return new ItemStack((Block)ret, 1);
}
else {
return null;
}
} catch(Exception e) {
System.err.println("Error retrieving block with identifier '" + identifier + "': " + e.getMessage());
return null;
}
}
}