提供: Minecraft Modding Wiki
2015年12月30日 (水) 23:50時点におけるA.K. (トーク | 投稿記録)による版 (ソースコード)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

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

この記事はMCPのMappingが"stable_16"であることを前提としています。


Stone pickaxe.png
中級者向けのチュートリアルです。



IRecipe利用によるレシピ追加[編集]

IRecipeを利用した少し複雑なレシピ追加方法。
(単純なレシピは1.7と同様の方法で実現できる)

ソースコード[編集]

  • SampleRecipeCore.java
package等省略

@Mod(modid = "irecipesample",
     acceptedMinecraftVersions = "[1.8,1.8.9]")
public class SampleRecipeCore{
    @EventHandler
    public void init(FMLInitializationEvent event){
        RecipeSorter.register("irecipesample:sample",SampleRecipe.class, RecipeSorter.Category.SHAPELESS,"after:minecraft:shapeless");
        GameRegistry.addRecipe(new SampleRecipe());
    }
}
  • SampleRecipe.java
package等省略;

public class SampleRecipe implements IRecipe{
    private ItemStack recipeItem = new ItemStack(Blocks.dirt);
    private ItemStack outItem = new ItemStack(Items.diamond);

    public boolean matches(InventoryCrafting inv, World worldIn){
        boolean allEmpty=true;
        for(int h = 0; h < inv.getHeight(); h++){
            for(int w = 0; w < inv.getWidth(); w++){
                ItemStack current = inv.getStackInRowAndColumn(h,w);
                if(current==null){
                    allEmpty &= true;
                    continue;
                }else allEmpty &= false;
                if (!(current.getItem() == recipeItem.getItem() && (recipeItem.getMetadata() == 32767 || current.getMetadata() == recipeItem.getMetadata()))){
                    return false;
                }
            }
        }
        return !allEmpty;
    }

    public ItemStack getCraftingResult(InventoryCrafting inv){
        ItemStack out = getRecipeOutput().copy();
        for(int h = 0; h < inv.getHeight(); h++){
            for(int w = 0; w < inv.getWidth(); w++){
                if(inv.getStackInRowAndColumn(h,w)!=null)out.stackSize++;
            }
        }
        out.stackSize--;
        return out;
    }

    public int getRecipeSize(){
        return 10;
    }

    public ItemStack getRecipeOutput(){
        return outItem;
    }

    public ItemStack[] getRemainingItems(InventoryCrafting inv){
        ItemStack[] aitemstack = new ItemStack[inv.getSizeInventory()];

        for (int i = 0; i < aitemstack.length; ++i)
        {
            ItemStack itemstack = inv.getStackInSlot(i);
            aitemstack[i] = net.minecraftforge.common.ForgeHooks.getContainerItem(itemstack);
        }

        return aitemstack;
    }
}

解説[編集]

SampleRecipeCore.java[編集]

RecipeSorter.register("irecipesample:sample", SampleRecipe.class, RecipeSorter.Category.SHAPELESS,"after:minecraft:shapeless");
GameRegistry.addRecipe(new SampleRecipe());

RecipeSorterにIRecipe実装クラスを登録した後GameRegistry#addRecipeでレシピを登録。
RecipeSorter#registerの第一引数は他と登録名が被らないようにModID等一意なものを含める。
また、第四引数は"after"であればあるほど優先度が低い。 詳しくはRecipeSorterのコンストラクタを見ると良い。

SampleRecipe.java[編集]

matchesで対象であるかを判定し、getCraftingResultで結果を返すというのが基本的な流れ。
ここではクラフティングテーブル内が全て土であることを確認し、土の量によって結果であるダイアモンドの量を増減させて返している。