提供: Minecraft Modding Wiki
移動先: 案内検索
(タグの取得)
 
107行目: 107行目:
 
   super.fromTag(tag);
 
   super.fromTag(tag);
 
   //numberタグを取得し、num1に挿入
 
   //numberタグを取得し、num1に挿入
   number = tag.getInt("number");
+
   num1 = tag.getInt("number");
 
}
 
}
 
</source>
 
</source>

2021年8月23日 (月) 15:19時点における最新版

この記事は"Fabric API 0.3.2 build 230~"を前提MODとしています。

Stone pickaxe.png
中級者向けのチュートリアルです。
C block.png
Blockに関係のあるチュートリアルです。

ブロック追加のソースコード2からまず、ブロックを追加してください。 無機能なブロックエンティティ(タイルエンティティ)を追加します。

動作確認済みのバージョン:1.16.3

ブロックエンティティの追加[編集]

ソースコード[編集]

  • SampleMod.java
package com.example.block;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.block.FabricBlockSettings;
import net.minecraft.item.Item;
import net.minecraft.block.Block;
import net.minecraft.block.Material;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.BlockItem;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class SampleMod implements ModInitializer
    {
        /**
         * ModID文字列
         */
        public static final String MOD_ID = "samplemod";

        //ブロック作成
        public static final Block SAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build());

        //ブロックエンティティ
        public static BlockEntityType<SampleBlockEntity> SAMPLE_BLOCK_ENTITY;

        @Override
        public void onInitialize()
        {
            //ブロック登録
            Registry.register(Registry.BLOCK, new Identifier(MOD_ID, "sample_block"), SAMPLE_BLOCK);
            //ブロックアイテムの登録
            Registry.register(Registry.ITEM, new Identifier(MOD_ID, "sample_block"), new BlockItem(SAMPLE_BLOCK, new Item.Settings().group(ItemGroup.MISC)));
            //ブロックエンティティの登録
            SAMPLE_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, MOD_ID+":sample_block_entity", BlockEntityType.Builder.create(SampleBlockEntity::new, SAMPLE_BLOCK).build(null));
        }
    }
}
  • SampleBlock.java
package com.example.block;

import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.world.BlockView;

public class SampleBlock extends Block implements BlockEntityProvider
{
    public SampleBlock(Settings settings)
    {
        super(settings);
    }

    @Override
    //ブロックとブロックエンティティを接続
    public BlockEntity createBlockEntity(BlockView blockView) {
       return new SampleBlockEntity();
    }
}
  • SampleBlockEntity.java
package com.example.block;

import net.minecraft.block.entity.BlockEntity;
//タグを追加しないなら削除
import net.minecraft.nbt.CompoundTag;
//----

public class SampleBlockEntity extends BlockEntity {
    //ブロックエンティティを追加してますが、まだ機能はありません。
    public SampleBlockEntity() {
        super(Main.SAMPLE_BLOCK_ENTITY);
    }
    //タグを追加しないなら削除
    //このメソッドはただブロックエンティティのタグに整数を保存しているだけなので特に必要ないです。
    public CompoundTag toTag(CompoundTag tag) {
        super.toTag(tag);
        //数字をタグに保存
        tag.putInt("number", 1);

        return tag;
    }
    //----
}

タグの取得[編集]

//変数num1を初期化
private int num1;

//タグを取得
public void fromTag(CompoundTag tag) {
   super.fromTag(tag);
   //numberタグを取得し、num1に挿入
   num1 = tag.getInt("number");
}