Mod制作日記 - クリエイティブタブ
アップロード:2024/12/05 最終更新:2024/12/05
前回の続き。
カスタムクリエイティブタブの追加と、バニラのクリエイティブタブにアイテムを追加する方法をまとめてやっていきます。
目次 (折りたたみ可)
クラスの作成
「com.(作者名).(ModのID).CreativeTabs」クラスを作ります。
CreativeTabs.java
package com.masuec.my_mod;
import com.masuec.my_mod.registers.BlockRegister;
import com.masuec.my_mod.registers.ItemRegister;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.Items;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.RegistryObject;
public class CreativeTabs {
public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister
.create(Registries.CREATIVE_MODE_TAB, MyMod.MODID);
public static final RegistryObject<CreativeModeTab> MY_MOD_TAB = CREATIVE_MODE_TABS.register(
"my_mod", // タブのID
() -> CreativeModeTab.builder()
.icon(() -> Items.DIAMOND.getDefaultInstance()) // アイコンになるアイテム
.title(Component.translatable("crative_tab.name")) // タブの表示名
.displayItems((params, output) -> {
// 載せるアイテムをここに
// テストアイテム
output.accept(ItemRegister.TEST_ITEM.get());
// バニラのアイテムはこう
output.accept(Items.DIAMOND);
// テストブロックのアイテム
output.accept(BlockRegister.TEST_BLOCK_ITEM.get());
})
.build()
);
public static void register(IEventBus evBus) {
CREATIVE_MODE_TABS.register(evBus);
}
// バニラのクリエイティブタブへ追加する
public static void addVanillaTab(BuildCreativeModeTabContentsEvent ev) {
// 「建築ブロック」タブに追加
if(ev.getTabKey() == CreativeModeTabs.BUILDING_BLOCKS) {
// テストブロックのアイテム
ev.accept(BlockRegister.TEST_BLOCK_ITEM);
}
}
}
バニラのクリエイティブタブ
値 | 概要 |
---|---|
BUILDING_BLOCKS | 建築ブロック |
COLORED_BLOCKS | 色付きブロック |
NATURAL_BLOCKS | 天然ブロック |
FUNCTIONAL_BLOCKS | 機能的ブロック |
REDSTONE_BLOCKS | レッドストーン系ブロック |
TOOLS_AND_UTILITIES | 道具と実用品 |
COMBAT | 戦闘 |
FOOD_AND_DRINKS | 食べ物と飲み物 |
INGREDIENTS | 材料 |
SPAWN_EGGS | スポーンエッグ |
OP_BLOCKS | 管理者用アイテム |
メインのクラス
「MyMod」クラスに戻って、以下の部分を加えます。
public MyMod() {
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
modEventBus.addListener(this::commonSetup);
// 各コンテンツの登録
ItemRegister.register(modEventBus);
BlockRegister.register(modEventBus);
CreativeTabs.register(modEventBus); // これ
MinecraftForge.EVENT_BUS.register(this);
// これ
modEventBus.addListener(CreativeTabs::addVanillaTab);
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, Config.SPEC);
}