新用户登入 登录
萌子岛 返回首页

wangtao03的个人空间 https://www.dommdo.com/?315352 [收藏] [复制] [RSS]

日志

2013-01-23

已有 226 次阅读2013-1-23 11:54 |个人分类:等待翻译

Goals
  • To create new items
  • To configure items
[edit]Prerequisites
  • Forge installed
  • Basic modding
[edit]Item Basics

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:

  • Item ID
  • Stack Size
  • Icon Index
  • Internal Name
  • In Game Name
  • Creative Tab
[edit]Generic Item

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:

package tutorial.generic;

import net.minecraft.item.Item;

public class GenericItem extends Item {

        public GenericItem(int id) {
                super(id);
                // TODO Auto-generated constructor stub
        }

}

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.

private final static Item genericItem = new GenericItem(5000);

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:

int MyItemID = genericItem.itemID;
[edit]Configuring GenericItem

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 Configuration

In the constructor, add the follow block of configuration.

// Constructor Configuration
setMaxStackSize(64);
setCreativeTab(CreativeTabs.tabMisc);
setIconIndex(0);
setItemName("genericItem");

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 Configuration

Both 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.

private static final Item genericIngot = newGenericItem(5001).setMaxStackSize(16).setCreativeTab(CreativeTabs.tabMisc).setIconIndex(1).setItemName("genericIngot");

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.

private static final Item genericIngot = new GenericItem(5001).setMaxStackSize(16).setIconIndex(1).setItemName("genericIngot");
[edit]Adding Additonal Constructors

Another option is adding these configurable properties as parameters on the constructor. For example, we can add this constructor to GenericItem.

public GenericItem(int id, int maxStackSize, CreativeTabs tab, int texture, String name) {
        super(id);
        setMaxStackSize(maxStackSize);
        setCreativeTab(tab);
        setIconIndex(texture);
        setItemName(name);
}

Then to create the genericIngot, we could add the field like this instead.

public static final Item genericIngot = new GenericItem(500116, CreativeTabs.tabMisc1"genericIngot");

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 Name

Notice 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.

LanguageRegistry.addName(Object toName, String readableName);

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.

LanguageRegistry.addName(genericItem, "Generic Item");
LanguageRegistry.addName(genericIngot, "Generic Ingot");

Now when people pick up a generic ingot, the tooltip will show "Generic Ingot".

[edit]Icon

See Icons and Textures, either now, or at the end of this tutorial.

[edit]Finishing Up

At 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 Class
package tutorial.generic;

// This Import list will grow longer with each additional tutorial.
// It's not pruned between full class postings, unlike other tutorial code.
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.Mod.PostInit;
import cpw.mods.fml.common.Mod.PreInit;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid="Generic", name="Generic", version="0.0.0")
@NetworkMod(clientSideRequired=true, serverSideRequired=false)
public class Generic {
        
        
        @Instance("Generic")
        public static Generic instance;

        private final static Item genericItem = new GenericItem(5000);
        public final static Item genericIngot = new GenericItem(5001)
                .setMaxStackSize(16).setIconIndex(1).setItemName("genericIngot");

        @SidedProxy(clientSide="tutorial.generic.client.ClientProxy",
                        serverSide="tutorial.generic.CommonProxy")
        public static CommonProxy proxy;
        
        @PreInit
        public void preInit(FMLPreInitializationEvent event) {
                // Stub Method
        }
        
        @Init
        public void load(FMLInitializationEvent event) {
                LanguageRegistry.addName(genericItem, "Generic Item");
                LanguageRegistry.addName(genericIngot, "Generic Ingot");
        }
        
        @PostInit
        public void postInit(FMLPostInitializationEvent event) {
                // Stub Method
        }
}
[edit]GenericItem Class
package tutorial.generic;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;

public class GenericItem extends Item {

        public GenericItem(int id) {
                super(id);
                
                // Constructor Configuration
                maxStackSize = 64;
                setCreativeTab(CreativeTabs.tabMisc);
                setIconIndex(0);
                setItemName("genericItem");
        }
        
        public String getTextureFile() {
                return CommonProxy.ITEMS_PNG;
        }
}

路过

雷人

握手

鲜花

鸡蛋

全部作者的其他最新日志

评论 (0 个评论)

小黑屋|萌子岛

GMT+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!

返回顶部