|
Items are quite common in Minecraft. Dyes, tools, and mob drops are but some of the items in Minecraft, and I'm sure you have a few you would like to add.
All items have the following in common:
So, first thing is first. Let's get a completely generic, does nothing item created. If you haven't guessed the name, this item is called GenericItem.
In the base mod, we need to declare the items existence, and give it an item id. The minimum for item ids is 0 and the maximum is 32000. Find unused item IDs on the forum. GenericItem will use id 5000, and each new item will use one id higher.
All items subclass Item or a subclass of Item. The only constructor of Item is protected, so new Item(5000) doesn't work. Create the tutorial.generic.GenericItem class and make sure it subclasses net.minecraft.item.Item. Make sure that Constructors from Superclass is checked. If you did all of that properly, you'll get exactly this:
If you didn't, I suggest deleting the class, and trying again until you do. Sure, you can copypaste code, but knowing how to use your tools properly is important.
The first thing to do is to remove the comment. It's a perfectly good auto-generated constructor stub, and doesn't need to be changed.
If it isn't completely obvious, the id passed in the constructor is the item id. It's the only thing all items have in common.
The way this class exists, you can subclass it as if it were Item, and everything would work. The only benefit it has over Item right now is, is that it has a public constructor. Let's take advantage of that in the base mod file.
Add a field to Generic that creates a new GenericItem with item id 5000. As you can see, the constructor's first parameter is the item id.
Special Note: The ID you used above will get shifted internally so do not count on it to be the same ID you pass in, Best practice is to get the ID when required by accessing your static instance of the item as follows.
Example:
You now have an item, but it will use texture 0 of the Minecraft items sprite sheet. We want to use our own sprite sheet and choose which texture we want. We also want to have this item show up in creative mode. We also want to give it a name.
These are all done by methods in Item that return itself afterwards. This allows for chaining them together. Remember though, that it returns Item, and you might need to recast it back to the type you actually want. You can also call these methods in the constructor. Both ways will be shown.
[edit]Constructor ConfigurationIn the constructor, add the follow block of configuration.
setMaxStackSize is a method that determines the maximum number of this item can go in one stack. Remember, all items in a stack share the same damage value, and if the item has a damage value for reasons other than metadata, this field should be set to 1. Another common value is 16.
setCreativeTab is a method that takes a CreativeTabs, which has its entire list in CreativeTabs. Since generic items fit nowhere else, it is thrown into the miscellaneous tab.
setIconIndex takes an integer and that specifies which index to use on the sprite sheet. The sprite sheet used will still have to be specified.
setItemName takes a String, and sets the internal name for the item. Camel casing the name of the item is simple enough.
[edit]Chaining ConfigurationBoth blocks and items allow for method chaining. Most of the setters used in the constructor return the reference to the block (versus returning void), allowing another method to be called on the same line. Vanilla Minecraft items and blocks make heavy usage of method chaining for setting properties.
To show chaining, a new GenericItem will be used called genericIngot. Generic Ingots will be used in future tutorials and be dropped by Generic Ore.
Create a new GenericItem and after constructing, call each of the methods from the constructor configuration section on it all on the same. The length of the line will be long.
The chained property setting overrides setting properties in the constructor. This is because these methods are called after the constructor is finished.
Using chained configuration, you can set default values in the constructor and modifying changes on the item declaration. Where the constructor already sets the proper default value, you do not need to reset the value. For example, the creative tab in the constructor is already what we want, so we do not need to reset it, making our line shorter.
Another option is adding these configurable properties as parameters on the constructor. For example, we can add this constructor to GenericItem.
Then to create the genericIngot, we could add the field like this instead.
It is up to you to decide which form best works for you. Since the chaining method is common, future tutorials will end up doing that.
[edit]Public NameNotice that setName doesn't take a human readable name. If you hovered over these items in inventory, you'll get no name for these items. To fix this, we have to register the public name with the LanguageRegistry. The method we care about is addName.
The Object can be an Item, and ItemStack, or a Block. In this case, we are passing an Item. In the load method of Generic, add the following two lines.
Now when people pick up a generic ingot, the tooltip will show "Generic Ingot".
[edit]IconSee Icons and Textures, either now, or at the end of this tutorial.
[edit]Finishing UpAt this point, we have not one, but two items that don't do anything. Since they are items, you can use them like you would Item.diamond or any other item. Use them in crafting recipes. Have them in dungeon chests. Maybe a mob will drop it? It's all up to you.
Also, if you want to actually hold your item, run the client, and create a creative world. Open the miscellaneous tab, and you'll find your items. Using my sprites, you'll see the numbers 0 and 1, which are basically the icon index. The ingot will get a real icon soon.
[edit]Generic ClassGMT+8, 2025-2-10 22:15 , Processed in 0.050294 second(s), 22 queries , Gzip On, MemCache On.
Copyright 2020 atollmoe©.a2.1.0 All rights reserved. 9+
Copyright 2009 supported by zhixuan© oeo© oko© All rights reserved.Thank you!