提供: Minecraft Modding Wiki
移動先: 案内検索
(段落修正)
(setCustomModelResourceLocationはpreInitで呼び出すべきでないので、ModelRegistryEvent に移動)
 
(他の1人の利用者による、間の1版が非表示)
78行目: 78行目:
 
     }
 
     }
  
     @EventHandler
+
    /**
     private void preInit(FMLPreInitializationEvent event){
+
    * モデル登録用イベント
         if (event.getSide() == Side.CLIENT)
+
    * preInitなどでは正しく登録できない場合があるので注意
            ModelLoader.setCustomModelResourceLocation(ITEMS.sample_item, 0, new ModelResourceLocation(ITEMS.sample_item.getRegistryName(), "inventory"));
+
    */
 +
     @SubscribeEvent
 +
     public static void registerModels(ModelRegistryEvent event){
 +
 
 +
         ModelLoader.setCustomModelResourceLocation(ITEMS.sample_item, 0, new ModelResourceLocation(ITEMS.sample_item.getRegistryName(), "inventory"));
 +
 
 
     }
 
     }
 
}
 
}
96行目: 101行目:
 
描画色が変更されるのは、アイテムモデル(assets/models/item/<item_model>.json)側で指定した「layer0」のテクスチャである。
 
描画色が変更されるのは、アイテムモデル(assets/models/item/<item_model>.json)側で指定した「layer0」のテクスチャである。
  
バニラポーションは、layer1でボトルのテクスチャ、layer0で中の液体のテクスチャを指定し、ItemColorsにポーションアイテムを登録、ポーションの色をnet.minecraft.potion.PotionUtils#getColorで求めている。
+
バニラポーションは、layer1でボトルのテクスチャ、layer0で中の液体のテクスチャを指定し、ItemColorsにポーションアイテムを登録、ポーションの色をnet.minecraft.potion.PotionUtils#getColorで求めている。ポーションのアイテムモデルは「assets/minecraft/models/item/bottle_drinkable.json」などである。
 +
 
 +
アイテムモデル内で「face」(faces/northなど。[http://minecraft.gamepedia.com/Model#Item_models Minecraft Wiki/Model#Item_Models])に「tintindex」を指定すると、第二引数に渡される。
  
 
<source lang="java" line>
 
<source lang="java" line>
 
@Mod.EventHandler
 
@Mod.EventHandler
 
public void init(FMLInitializationEvent event) {
 
public void init(FMLInitializationEvent event) {
     Minecraft.getMinecraft().getItemColors().registerItemColorHandler((stack, tintIndex) -> { // new IItemColor() {
+
     Minecraft.getMinecraft().getItemColors().registerItemColorHandler(new IItemColor() {
 
         @Override
 
         @Override
 
         public int getColorFromItemstack(ItemStack stack, int tintIndex) {
 
         public int getColorFromItemstack(ItemStack stack, int tintIndex) {

2017年11月27日 (月) 14:09時点における最新版

この記事は"Minecraft Forge Universal 13.19.0.xxx~"を前提MODとしています。

Wood pickaxe.png
初心者向けのチュートリアルです。
C item.png
Itemに関係のあるチュートリアルです。

アイテムの追加[編集]

簡単な無機能アイテムの追加[編集]

ソースコード[編集]

SampleMod.java

package com.example.item;

import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.GameRegistry.ObjectHolder;
import net.minecraftforge.fml.relauncher.Side;

@Mod(modid = SampleMod.MOD_ID, name = SampleMod.MOD_NAME, version = SampleMod.MOD_VERSION, dependencies = SampleMod.MOD_DEPENDENCIES, acceptedMinecraftVersions = SampleMod.MOD_ACCEPTED_MC_VERSIONS, useMetadata = true)
@EventBusSubscriber
public class SampleMod{
    /**
     * ModID文字列
     */
    public static final String MOD_ID = "samplemod";

    /**
     * MOD名称
     */
    public static final String MOD_NAME = "SampleMod";

    /**
     * MODのバージョン
     */
    public static final String MOD_VERSION = "0.0.1";

    /**
     * 前に読み込まれるべき前提MODをバージョン込みで指定
     */
    public static final String MOD_DEPENDENCIES = "required-after:forge@[1.11-13.19.0.2130,)";

    /**
     * 起動出来るMinecraft本体のバージョン。記法はMavenのVersion Range Specificationを検索すること。
     */
    public static final String MOD_ACCEPTED_MC_VERSIONS = "[1.11]";

    /**
     * アイテム保持用内部クラス
     */
    @ObjectHolder(MOD_ID)
    public static class ITEMS{
        public static final Item sample_item = null;
    }

    /**
     * アイテム登録用イベント
     */
    @SubscribeEvent
    protected static void registerItems(RegistryEvent.Register<Item> event){
        event.getRegistry().registerAll(
                new Item()
                    .setRegistryName(MOD_ID, "sample_item")/*登録名の設定*/
                    .setCreativeTab(CreativeTabs.MISC)/*クリエイティブのタブ*/
                    .setUnlocalizedName("sample")/*翻訳キーの設定*/
                    /*.setHasSubtypes(true)*//*ダメージ値等で複数の種類のアイテムを分けているかどうか。デフォルトfalse*/
                    /*.setMaxDamage(256)*//*耐久値の設定。デフォルト0*/
                    /*.setFull3D()*//*3D表示で描画させる。ツールや骨、棒等。*/
                    /*.setContainerItem(Items.stick)*//*クラフト時にアイテムを返却できるようにしている際の返却アイテムの指定。*/
                    /*.setPotionEffect(PotionHelper.ghastTearEffect)*//*指定文字列に対応した素材として醸造台で使える。PotionHelper参照のこと。*/
                    /*.setNoRepair()*//*修理レシピを削除し、金床での修繕を出来なくする*/
                    /*.setMaxStackSize(64)*//*スタックできる量。デフォルト64*/
        );
    }

    /**
     * モデル登録用イベント
     * preInitなどでは正しく登録できない場合があるので注意
     */
    @SubscribeEvent
    public static void registerModels(ModelRegistryEvent event){

        ModelLoader.setCustomModelResourceLocation(ITEMS.sample_item, 0, new ModelResourceLocation(ITEMS.sample_item.getRegistryName(), "inventory"));

    }
}

解説[編集]

以前とは違い、登録用イベントが発火されるようになった。
アイテムのインスタンス保持用のクラスはForgeの中の人の手法に沿ったものであるが、別に従来の方法でも問題ない。ここでは細かな解説をしない。
LangやModel用Jsonについては過去のチュートリアルを参照のこと。

アイテムの色を変える[編集]

ItemColors#registerItemColorHandlerにハンドラを登録することでアイテムの描画色を変更することができる(glColorに相当)。ItemColorsは、Minecraft#getItemColorsで得られる(バニラの初期化が終わるinit以降に使用すること。preInitではnull)。

描画色が変更されるのは、アイテムモデル(assets/models/item/<item_model>.json)側で指定した「layer0」のテクスチャである。

バニラポーションは、layer1でボトルのテクスチャ、layer0で中の液体のテクスチャを指定し、ItemColorsにポーションアイテムを登録、ポーションの色をnet.minecraft.potion.PotionUtils#getColorで求めている。ポーションのアイテムモデルは「assets/minecraft/models/item/bottle_drinkable.json」などである。

アイテムモデル内で「face」(faces/northなど。Minecraft Wiki/Model#Item_Models)に「tintindex」を指定すると、第二引数に渡される。

@Mod.EventHandler
public void init(FMLInitializationEvent event) {
    Minecraft.getMinecraft().getItemColors().registerItemColorHandler(new IItemColor() {
        @Override
        public int getColorFromItemstack(ItemStack stack, int tintIndex) {
            return 0x00FF00; // 色のARGB値を返す。この場合、緑がかって描画される
        }
    }, Items.sample_item);
}