提供: Minecraft Modding Wiki
移動先: 案内検索

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

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

村人職業の追加[編集]

村人の職業と肩書の追加方法

ここでは村人の職業と肩書の追加を行うが、特別にスポーン処理は追加しないので村に出現したりはせず、スポーンエッグからのランダムスポーンのみである。
村への村人の追加は、1.9の村構造物追加(未作成)を参照のこと。
※12.6.0.1863現在、既存の職業への肩書追加はできない模様。

ソースコード[編集]

SampleMod.java

package com.example.profession;

import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.entity.passive.EntityVillager.EmeraldForItems;
import net.minecraft.entity.passive.EntityVillager.ITradeList;
import net.minecraft.entity.passive.EntityVillager.ListItemForEmeralds;
import net.minecraft.entity.passive.EntityVillager.PriceInfo;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.common.registry.VillagerRegistry.VillagerCareer;
import net.minecraftforge.fml.common.registry.VillagerRegistry.VillagerProfession;

@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)
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.9-12.16.0.1853,)";
    /** 起動出来るMinecraft本体のバージョン。記法はMavenのVersion Range Specificationを検索すること。 */
    public static final String MOD_ACCEPTED_MC_VERSIONS = "[1.9]";
    /** 追加したいアイテムのインスタンスを格納する変数。レシピ等で利用。 */
    public static VillagerProfession sampleProfession;

    @EventHandler
    public void preInit(FMLPreInitializationEvent event){
        sampleProfession = new SampleProfession();
        ((SampleProfession) sampleProfession).init(
                new SampleCareer(sampleProfession, "1",
                        new EntityVillager.ITradeList[][]{
                                {
                                        new EmeraldForItems(Items.wheat, new PriceInfo(18, 22)),
                                        new EmeraldForItems(Items.potato, new PriceInfo(15, 19)),
                                        new EmeraldForItems(Items.carrot, new PriceInfo(15, 19)),
                                        new ListItemForEmeralds(Items.bread, new PriceInfo(-4, -2))
                                },
                                {
                                        new EmeraldForItems(Item.getItemFromBlock(Blocks.pumpkin), new PriceInfo(8, 13)),
                                        new ListItemForEmeralds(Items.pumpkin_pie, new PriceInfo(-3, -2))
                                },
                                {
                                        new EmeraldForItems(Item.getItemFromBlock(Blocks.melon_block), new PriceInfo(7, 12)),
                                        new ListItemForEmeralds(Items.apple, new PriceInfo(-5, -7))
                                },
                                {
                                        new ListItemForEmeralds(Items.cookie, new PriceInfo(-6, -10)),
                                        new ListItemForEmeralds(Items.cake, new PriceInfo(1, 1))
                                }
                        }
                ),
                new SampleCareer(sampleProfession, "2",
                        new EntityVillager.ITradeList[][]{
                                {
                                        new EmeraldForItems(Items.string, new PriceInfo(15, 20)),
                                        new EmeraldForItems(Items.coal, new PriceInfo(16, 24)),
                                        new ItemAndEmeraldToItem(Items.fish, new PriceInfo(6, 6), Items.cooked_fish, new PriceInfo(6, 6))
                                },
                                {
                                        new ListEnchantedItemForEmeralds(Items.fishing_rod, new PriceInfo(7, 8))
                                }
                        }
                )
        );
        //職業の登録。登録文字列はMOD内で被らなければ何でも良い。
        GameRegistry.register(sampleProfession, new ResourceLocation(MOD_ID, "sampleprofession"));
    }
}

SampleProfession.java

package com.example.profession;

import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.registry.VillagerRegistry.VillagerCareer;
import net.minecraftforge.fml.common.registry.VillagerRegistry.VillagerProfession;

import java.util.Random;

import static com.example.profession.SampleMod.*;

public class SampleProfession extends VillagerProfession{
    private VillagerCareer[] careers;

    public SampleProfession(){
        super(new ResourceLocation(MOD_ID,"sample").toString(), new ResourceLocation(MOD_ID,"sample").toString());
    }

    public void init(VillagerCareer... careers){
        this.careers = careers;
    }

    @Override
    public VillagerCareer getCareer(int id){
        return careers[id];
    }

    @Override
    public int getRandomCareer(Random rand){
        return rand.nextInt(careers.length);
    }
}

SampleCareer.java

package com.example.profession;

import net.minecraft.entity.passive.EntityVillager.ITradeList;
import net.minecraftforge.fml.common.registry.VillagerRegistry.VillagerCareer;
import net.minecraftforge.fml.common.registry.VillagerRegistry.VillagerProfession;

public class SampleCareer extends VillagerCareer{
    private final ITradeList[][] trades;

    public SampleCareer(VillagerProfession parent, String name, ITradeList[][] trades){
        super(parent, name);
        this.trades = trades;
    }

    @Override
    public ITradeList[][] getTrades(){
        return trades;
    }
}

解説[編集]

執筆中