我的世界mod开发1.3第一个物品

Wzhrdx 发布于

制作一个物品分为两步:

1.创建一个物品,并添加材质

2.实例化并注册这个物品

那么由于我懒我就不介绍了,自己直接照抄xiao_bang的介绍了

在Minecraft中,有烈火般的烈焰棒(废话),咱们这次做来做个钻石颜色的棒子(真的想不出来要做什么了),这一次教程会一步一步地带着你完成制作新物品的全过程。

好现在我们来创建一个新的物品,如果你有经常翻看net.minecraft源码,在item包里面会看到在mc中遇到的各种物品,都继承了Item类,那么我们做的物品也要继承这个类(继承这个类的一般都是物品)

新建一个包com.wzhrdx.XenoAge.item,并在其中创建一个类ItemSilverNugget(注意注意类的名称前面推荐是Item,禁止乱搞,xiaobang路过)

创建一个物品

这里由于我们写的是XenoAge使用我们在这里创建银粒。新建java类item.ItemSilverNugget

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.wzhrdx.XenoAge.item;

import com.wzhrdx.XenoAge.XenoAge;

import net.minecraft.item.Item;

public class ItemSilverNugget extends Item {

public ItemSilverNugget() {

this.setMaxStackSize(64);

this.setTranslationKey(XenoAge.MODID + ".silvernugget");

this.setRegistryName("silver_nugget");

}

}
  • setTranslationKey这是新的方法,以前的方法为setUnlocalizedName()

setMaxStackSize()这里是设置最大堆叠数目,如果不特殊强调就是默认64(比如说我不写这个方法😀)

设置注册名字,当你在游戏中想要获得你的物品那就需要这个名字,比如在本教程你需要这样做give @a xenoage:silver_nugget,当然此时如果你启动的话,不出意外会报错

实例化并注册

新建java类item.ItemRegistryHandler

src/main/java/com/wzhrdx/XenoAge/item/ItemRegistryHandler.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.wzhrdx.XenoAge.item;

import net.minecraft.item.Item;

import net.minecraftforge.event.RegistryEvent;

import net.minecraftforge.fml.common.Mod;

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import net.minecraftforge.registries.IForgeRegistry;

@Mod.EventBusSubscriber

public class ItemRegistryHandler {

public static final ItemSilverNugget SILVER_NUGGET = new ItemSilverNugget();

@SubscribeEvent

public static void onRegistry(RegistryEvent.Register<Item> event)

{

IForgeRegistry<Item> registry = event.getRegistry();

registry.register(SILVER_NUGGET);

} //这里是实例话物品

绑定材质

ItemRegistryHandler添加一个方法,修改后如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.wzhrdx.XenoAge.item;

import net.minecraft.client.renderer.block.model.ModelResourceLocation;

import net.minecraft.item.Item;

import net.minecraftforge.client.event.ModelRegistryEvent;

import net.minecraftforge.client.model.ModelLoader;

import net.minecraftforge.event.RegistryEvent;

import net.minecraftforge.fml.common.Mod;

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import net.minecraftforge.fml.relauncher.Side;

import net.minecraftforge.fml.relauncher.SideOnly;

import net.minecraftforge.registries.IForgeRegistry;

@Mod.EventBusSubscriber

public class ItemRegistryHandler {

public static final ItemSilverNugget SILVER_NUGGET = new ItemSilverNugget();

@SubscribeEvent

public static void onRegistry(RegistryEvent.Register<Item> event)

{

IForgeRegistry<Item> registry = event.getRegistry();

registry.register(SILVER_NUGGET);

} //这里是实例话物品

@SubscribeEvent //监听net.minecraftforge.client.event.ModelRegistryEvent事件

@SideOnly(Side.CLIENT)

public static void onModelRegistry(ModelRegistryEvent event){

ModelLoader.setCustomModelResourceLocation(SILVER_NUGGET, 0,

new ModelResourceLocation(SILVER_NUGGET.getRegistryName(),"inventory"));

}

} //这里是绑定材质

创建包assets.xenoage.models.item,创建json文件silver_nugget.json

src/main/resources/assets/xenoage/models/item/silver_nugget.json

1
2
3
4
5
6
7
8
9
10
11
{

"parent": "item/generated",

"textures": {

"layer0": "xenoage:items/silver_nugget"

}

}

创建材质包位置src/main/resources/assets/xenoage/textures/items

将材质放入,材质的名字要和你的注册名字一样这里是silver_nugget.png,”dWVxvn.png

这里我们运行游戏输入/give @a xenoage:silver_nugget我们就会得到这个物品


练习

  • 练习1 创建一个名为silver_ingot的物品Java类名为ItemSilverNugget材质图片dWAfaD.png
    要求1:设置最大堆叠数目为64
    要求2:设置注册名为silver_ingot
    要求3:设置UnlocalizedNamemodid+#
    要求4:将其放入创造栏中

  • 练习2 注册、实例化物品并注册材质

  • 练习3 配置json文件使材质正常显示

答案1
 

package com.wzhrdx.XenoAge.item;

import com.wzhrdx.XenoAge.XenoAge;

import com.wzhrdx.XenoAge.creativetab.TabXenoAge;

import net.minecraft.item.Item;

public class ItemSilverIngot extends Item {

public ItemSilverIngot() {

this.setMaxStackSize(64);

this.setCreativeTab(TabXenoAge.TAB_XENO_AGE);

this.setTranslationKey(XenoAge.MODID + ".silveringot");

this.setRegistryName("silver_ingot");

}

}

答案2
 

//在ItemRegistryHandler对应位置下添加

public static Item SILVER_INGOT = new ItemSilverIngot();

registry.register(SILVER_INGOT);

registerModel(SILVER_INGOT);

答案3
 

1.创建assets/xenoage/models/item/silver_ingot.json

2.silver_ingot.json

{

"parent": "item/generated",

"textures": {

"layer0": "xenoage:items/silver_ingot"

}

}