目次
概要
特殊な機能を持たないブロックを追加します。リソースの自動生成で追加した内容を拡張し、ブロックのリソースも追加します。
動作確認
2023年12月13日
- 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 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()));
}
}
ExampleTNTBlocks
及びExampleTNTBlockStateProvider
に処理を渡します。それぞれExampleTNTItems
及びExampleTNTItemModelProvider
と同じように追加します。
ExampleTNTBlocks.java
ExampleTNT/src/main/java/com/tntmodders/exampletnt/ExampleTNTBlocks.java
package com.tntmodders.exampletnt;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
public class ExampleTNTBlocks {
private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, ExampleTNT.MOD_ID);
public static final RegistryObject<Block> LARGE_TNT = BLOCKS.register("large_tnt", () -> new Block(BlockBehaviour.Properties.of(Material.EXPLOSIVE).lightLevel(value -> 15)));
public static void register(IEventBus eventBus) {
BLOCKS.register(eventBus);
}
}
ブロックの追加処理をアイテムと同様に実装します。BlockBehaviour.Properties
でブロックごとの一般的な性質を追加できます。今回は明るさを追加しています。
ExampleTNTItems.java
ExampleTNT/src/main/java/com/tntmodders/exampletnt/ExampleTNTItems.java
package com.tntmodders.exampletnt;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Rarity;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
public class ExampleTNTItems {
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, ExampleTNT.MOD_ID);
public static final RegistryObject<Item> SMALL_TNT = ITEMS.register("small_tnt", () -> new Item(new Item.Properties().rarity(Rarity.EPIC)));
public static final RegistryObject<Item> LARGE_TNT = ITEMS.register("large_tnt", () -> new BlockItem(ExampleTNTBlocks.LARGE_TNT.get(), new Item.Properties()));
public static void register(IEventBus eventBus) {
ITEMS.register(eventBus);
}
}
クリエイティブタブに追加するためにブロックのアイテムを追加しています。new BlockItem()
を利用することで、ブロックのアイテムを簡単に追加することができます。
ExampleTNTEvents.java
ExampleTNT/src/main/java/com/tntmodders/exampletnt/ExampleTNTEvents.java
package com.tntmodders.exampletnt;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraftforge.event.CreativeModeTabEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@Mod.EventBusSubscriber(modid = ExampleTNT.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ExampleTNTEvents {
@SubscribeEvent
public static void creativeTabsBuildEvent(CreativeModeTabEvent.BuildContents event) {
if (event.getTab() == CreativeModeTabs.TOOLS_AND_UTILITIES) {
event.accept(ExampleTNTItems.SMALL_TNT.get());
} else if (event.getTab() == CreativeModeTabs.REDSTONE_BLOCKS) {
event.accept(ExampleTNTBlocks.LARGE_TNT.get());
}
}
}
アイテムの追加と同様に、クリエイティブタブにブロックを追加します。
ExampleTNTLangProvider.java
ExampleTNT/src/main/java/com/tntmodders/exampletnt/provider/ExampleTNTLangProvider.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.minecraftforge.common.data.LanguageProvider;
public abstract class ExampleTNTLangProvider extends LanguageProvider {
public ExampleTNTLangProvider(PackOutput output, String locale) {
super(output, ExampleTNT.MOD_ID, locale);
}
public static class ExampleTNTLangJP extends ExampleTNTLangProvider {
public ExampleTNTLangJP(PackOutput output) {
super(output, "ja_jp");
}
@Override
protected void addTranslations() {
this.add(ExampleTNTItems.SMALL_TNT.get(), "小型TNT");
this.add(ExampleTNTBlocks.LARGE_TNT.get(), "大型TNT");
}
}
public static class ExampleTNTLangUS extends ExampleTNTLangProvider {
public ExampleTNTLangUS(PackOutput output) {
super(output, "en_us");
}
@Override
protected void addTranslations() {
this.add(ExampleTNTItems.SMALL_TNT.get(), "Small TNT");
this.add(ExampleTNTBlocks.LARGE_TNT.get(), "Large TNT");
}
}
}
アイテムの追加と同様に、ブロックの翻訳を追加してます。
large_tnt.png
ExampleTNT/src/main/resources/assets/exampletnt/textures/block/large_tnt.png
テクスチャファイル
テクスチャファイルを上記ディレクトリに設置して下さい。16x16のpngファイルで作成して下さい。ファイル名はアイテムの登録名と同じ名称にすることをおすすめします。
ExampleTNTBlockStateProvider.java
ExampleTNT/src/main/java/com/tntmodders/exampletnt/provider/ExampleTNTBlockStateProvider.java
package com.tntmodders.exampletnt.provider;
import com.tntmodders.exampletnt.ExampleTNT;
import com.tntmodders.exampletnt.ExampleTNTBlocks;
import net.minecraft.data.PackOutput;
import net.minecraftforge.client.model.generators.BlockStateProvider;
import net.minecraftforge.common.data.ExistingFileHelper;
public class ExampleTNTBlockStateProvider extends BlockStateProvider {
public ExampleTNTBlockStateProvider(PackOutput output, ExistingFileHelper exFileHelper) {
super(output, ExampleTNT.MOD_ID, exFileHelper);
}
@Override
protected void registerStatesAndModels() {
this.simpleBlockWithItem(ExampleTNTBlocks.LARGE_TNT.get(), this.cubeAll(ExampleTNTBlocks.LARGE_TNT.get()));
}
}
ブロックのモデルファイルを生成します。registerStatesAndModels()
内に、モデルの追加方法を指定して記述します。
今回は単純な6面同テクスチャモデルを利用するため、simpleBlockWithItem(Block block, ModelFile file)
を親クラスから呼び出しています。
ModelFile
は、メソッドcubeAll(Block block)
を利用して生成しています。
関連クラス
net.minecraft.world.level.block.Block
:バニラのブロックが記述されています。
net.minecraft.world.item.BlockItem
:ブロックのアイテムに関しての内容が記述されています。
net.minecraftforge.client.model.generators.BlockStateProvider
:ブロックのモデル生成に関しての内容が記述されています。
リンク
次:イベントの追加