Do you need to get an Item (or Block) from a String ID? You do this by making use of GameRegistry.findItem(String, String), one of Minecraft Forge's useful little methods.
/**
* Look up a mod item in the global "named item list"
* @param modId The modid owning the item
* @param name The name of the item itself
* @return The item or null if not found
*/
public static Item findItem(String modId, String name)
{
return GameData.findItem(modId, name);
}
...But that requires the ModID and ItemID to NOT be a part of the same string. To do this, we need to cut it up into smaller segments.
This code takes a string like "minecraft:dirt" or "modID:itemName" and cuts it up by finding the ":" and then cutting the string on either side of that colon. If there are multiple colons, it ignores them in favor of the first. If it can't find a colon, it defaults to the modID "minecraft". It then takes the separated modID and itemName and asks GameRegistry to find it for them, and will return the retrieved result -- the item being searched for, or null.
/** Gets an Item from the specified ID string. Returns null if no item is found. */
public static Item getItem(String s)
{
String modId = "minecraft";
String name;
if (s.indexOf(":") != -1)
{
modId = s.substring(0, s.indexOf(":"));
}
name = s.substring(s.indexOf(":") + 1);
return GameRegistry.findItem(modId, name); // Will return null if no item is found.
}
A possible use for this could be to add this to your mod's config file so you can allow people to set their own items (my mod, Simple Flight, uses this to allow users to set a custom enchantment item).
You could also use this to check for the existence of items from other mods that have been registered with Forge already, if you wanted (...but that would require knowing that ID in the first place).
An example of the first, adapted from Simple Flight's source:
/** Configures the mod. */
public void configure(Configuration config)
{
config.load();
// [*snip*]
Item item = getItem( itemIdentityString );
if (item == null)
{
System.out.println("Item "+ itemIdentityString +" not found. Using default item instead.");
item = Items.feather; //Sets the default / fallback item.
}
this.configVariable = item;
// [*snip*]
config.save();
}
You can also use findBlock instead of findItem, it works basically the same way. (Though I have had no problems with getting and using Blocks from the getItem() method.)
So that's how you do it, if anyone was wondering. Hope you found this useful!
This is, like, really novice general-purpose Java knowledge. No offense to you or them, but if anybody finds this useful they're probably not experienced enough in Java to be modding in the first place.
Just my two cents.
Also, Modding Tutorials has it's own forum. Not that this has much to do with modding.
This is, like, really novice general-purpose Java knowledge. No offense to you or them, but if anybody finds this useful they're probably not experienced enough in Java to be modding in the first place.
Just my two cents.
Also, Modding Tutorials has it's own forum. Not that this has much to do with modding.
Returning a default value is also a bad idea when you're doing a data lookup. It should either return the correct data, or an error code (or just null). A crash is preferable to random behavior; they're far easier to debug. This is one reason why I like Java so much, it's obsessive use of null - we don't have to always check return codes
Well people making tutorials are supposed to have at least searched more than the average on the topic it tries to educate others...or so i think.
And the simplest solution is already used by vanilla in the Items class, anyways. I just reword it to be closer to Forge.
GameData.getItemRegistry().getObject(String)
is strictly equivalent to
Item.itemRegistry.getObject(String)
...But that requires the ModID and ItemID to NOT be a part of the same string. To do this, we need to cut it up into smaller segments.
This code takes a string like "minecraft:dirt" or "modID:itemName" and cuts it up by finding the ":" and then cutting the string on either side of that colon. If there are multiple colons, it ignores them in favor of the first. If it can't find a colon, it defaults to the modID "minecraft". It then takes the separated modID and itemName and asks GameRegistry to find it for them, and will return the retrieved result -- the item being searched for, or null.
A possible use for this could be to add this to your mod's config file so you can allow people to set their own items (my mod, Simple Flight, uses this to allow users to set a custom enchantment item).
You could also use this to check for the existence of items from other mods that have been registered with Forge already, if you wanted (...but that would require knowing that ID in the first place).
An example of the first, adapted from Simple Flight's source:
You can also use findBlock instead of findItem, it works basically the same way. (Though I have had no problems with getting and using Blocks from the getItem() method.)
So that's how you do it, if anyone was wondering. Hope you found this useful!
Just my two cents.
Also, Modding Tutorials has it's own forum. Not that this has much to do with modding.
I kinda find it funny when people do that. XD
Heh.
If I could be brutally honest a second time, that getItem method is kinda bad. It can be done in far less code:
Returning a default value is also a bad idea when you're doing a data lookup. It should either return the correct data, or an error code (or just null). A crash is preferable to random behavior; they're far easier to debug. This is one reason why I like Java so much, it's obsessive use of null - we don't have to always check return codes
GameData.getItemRegistry().getObject(String)
And...done.
Ha! Good to know. So this entire tutorial is solved with one line of code xD
I feel like I'm being a little nasty to OP but that's just funny.
And the simplest solution is already used by vanilla in the Items class, anyways. I just reword it to be closer to Forge.
GameData.getItemRegistry().getObject(String)
is strictly equivalent to
Item.itemRegistry.getObject(String)