目次
概要
ブロック・アイテムを用いたレシピを追加します。
動作確認
2023年12月16日
- Minecraft 1.19.4
- Forge 45.2.0
解説
ExampleTNT.java
ExampleTNT/src/main/java/com/tntmodders/exampletnt/ExampleTNT.java
package com.tntmodders.exampletnt;
import com.tntmodders.exampletnt.provider.ExampleTNTBlockStateProvider;
import com.tntmodders.exampletnt.provider.ExampleTNTItemModelProvider;
import com.tntmodders.exampletnt.provider.ExampleTNTLangProvider;
import com.tntmodders.exampletnt.provider.ExampleTNTRecipeProvider;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.PackOutput;
import net.minecraftforge.common.data.ExistingFileHelper;
import net.minecraftforge.data.event.GatherDataEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@Mod(ExampleTNT.MOD_ID)
public class ExampleTNT {
public static final String MOD_ID = "exampletnt";
public ExampleTNT() {
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
modEventBus.addListener(this::registerProviders);
ExampleTNTBlocks.register(modEventBus);
ExampleTNTItems.register(modEventBus);
}
private void registerProviders(GatherDataEvent event) {
DataGenerator gen = event.getGenerator();
PackOutput packOutput = gen.getPackOutput();
ExistingFileHelper fileHelper = event.getExistingFileHelper();
gen.addProvider(event.includeClient(), new ExampleTNTItemModelProvider(packOutput, fileHelper));
gen.addProvider(event.includeClient(), new ExampleTNTBlockStateProvider(packOutput, fileHelper));
gen.addProvider(event.includeClient(), new ExampleTNTLangProvider.ExampleTNTLangUS(gen.getPackOutput()));
gen.addProvider(event.includeClient(), new ExampleTNTLangProvider.ExampleTNTLangJP(gen.getPackOutput()));
gen.addProvider(event.includeServer(), new ExampleTNTRecipeProvider(gen.getPackOutput()));
}
}
Providerを追加します。リソースの自動生成 (1.19.4)で追加したリソースと異なり、event.includeServer()
を第一引数に指定することで保存場所がgenerated/resources/data/exampletnt/
になります。
レシピの生成はProviderを記述した後、runDataを実行して行って下さい。
ExampleTNTRecipeProvider.java
ExampleTNT/src/main/java/com/tntmodders/exampletnt/provider/ExampleTNTRecipeProvider.java
package com.tntmodders.exampletnt.provider;
import com.tntmodders.exampletnt.ExampleTNT;
import com.tntmodders.exampletnt.ExampleTNTBlocks;
import com.tntmodders.exampletnt.ExampleTNTItems;
import net.minecraft.data.PackOutput;
import net.minecraft.data.recipes.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import java.util.function.Consumer;
public class ExampleTNTRecipeProvider extends RecipeProvider {
public ExampleTNTRecipeProvider(PackOutput output) {
super(output);
}
@Override
protected void buildRecipes(Consumer<FinishedRecipe> consumer) {
ShapelessRecipeBuilder.shapeless(RecipeCategory.TOOLS, ExampleTNTItems.SMALL_TNT.get(), 3)
.requires(ExampleTNTBlocks.LARGE_TNT.get())
.group("exampletnt")
.unlockedBy("has_large_tnt", has(ExampleTNTBlocks.LARGE_TNT.get()))
.save(consumer, new ResourceLocation(ExampleTNT.MOD_ID, "small_tnt"));
ShapedRecipeBuilder.shaped(RecipeCategory.BUILDING_BLOCKS, ExampleTNTBlocks.LARGE_TNT.get(), 2)
.define('#', Blocks.TNT)
.define('G', Items.GUNPOWDER)
.pattern(" # ").pattern("GGG").pattern(" # ")
.group("exampletnt")
.unlockedBy("has_tnt", has(Blocks.TNT))
.save(consumer, new ResourceLocation(ExampleTNT.MOD_ID, "large_tnt"));
}
}
メソッドbuildRecipes
でレシピを登録できます。今回は、ShapelessRecipeBuilder
とShapedRecipeBuilder
をそれぞれ利用して、クラフトレシピを登録します。
net.minecraft.data.recipes
には他にも精錬や石切台のレシピが存在し、今回のチュートリアルと同様の方法で登録が可能です。
ShapelessRecipeBuilder
不定形レシピの登録に使います。shapeless
の第一引数にはレシピのカテゴリを登録し、次いで第二引数に出力するアイテムを指定します。第三引数として出力個数も指定できます。
requires
で必要なアイテムを指定します。このメソッドは複数回使うことができ、必要なアイテムを何種類でも指定することが可能です。
group
でグループ名を指定できます。同じグループのレシピはレシピブックでまとめて表示されます。
unlockedBy
では、レシピの解禁条件を指定できます。第一引数にcriteria名、第二引数に条件を設定します。なお、レシピの解禁条件が存在しない場合jsonファイルの書き出しには失敗します。
最後にsave
でResourceLocationを設定し、レシピをセーブすることができます。
ShapedRecipeBuilder
定形レシピの登録に使います。shapeless
の代わりにshaped
メソッドを利用します。引数の順序は同様に利用できます。
define
では、特定のItemLikeに文字を割当てられます。このメソッドは複数回使うことができますが、一つのレシピ中で同じ文字を別のItemLikeに紐付けることはできません。
pattern
メソッドを用いて、define
で紐づけた文字の並びで作業台での配置を指示します。pattern
メソッド一つで作業台の上から1行ずつ指定することができます。空白はスペースを用いて記述して下さい。
pattern
メソッドは必ずしも3行設定する必要はなく、また列についても省略することができます。例えば、列に関しては、ドアのレシピは以下のように指定できます。
.pattern("##").pattern("##").pattern("##")
空白を挿入せず一行に2つずつアイテムを配置することで、作業台の右寄りでも左寄りでもレシピが認識されます。
unlockedBy
及びsave
はbuildRecipes
と同様に指定して下さい。
リンク
前:ビルド