提供: Minecraft Modding Wiki
移動先: 案内検索

この記事は1.6の流体追加を読んだ事を前提としています。

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

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

1.6で追加した流体をバケツで扱う[編集]

ソースコード[編集]

  • FillBucketHandler.java(SpaceToad氏のBCでの実装の一部改変)
/**
 * Copyright (c) SpaceToad, 2011 http://www.mod-buildcraft.com
 *
 * BuildCraft is distributed under the terms of the Minecraft Mod Public License
 * 1.0, or MMPL. Please check the contents of the license located in
 * http://www.mod-buildcraft.com/MMPL-1.0.txt
 */
package samplefluid;

import java.util.HashMap;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import net.minecraftforge.event.Event.Result;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.player.FillBucketEvent;

public class FillBucketHandler {

        public static FillBucketHandler INSTANCE = new FillBucketHandler();
        public Map<Block, Item> buckets = new HashMap<Block, Item>();

        private BucketHandler() {
        }

        @ForgeSubscribe
        public void onBucketFill(FillBucketEvent event) {

                ItemStack result = fillCustomBucket(event.world, event.target);

                if (result == null)
                        return;

                event.result = result;
                event.setResult(Result.ALLOW);
        }

        private ItemStack fillCustomBucket(World world, MovingObjectPosition pos) {
        //BlockIDを取得
                int blockID = world.getBlockId(pos.blockX, pos.blockY, pos.blockZ);
        //BlockIDから登録した液体バケツを取得
                Item bucket = buckets.get(Block.blocksList[blockID]);
                if (bucket != null && world.getBlockMetadata(pos.blockX, pos.blockY, pos.blockZ) == 0) {
                        world.setBlock(pos.blockX, pos.blockY, pos.blockZ, 0);
                        return new ItemStack(bucket);//液体バケツを返却
                } else
                        return null;

        }
}
  • SampleFluid.javaの修正部分
@EventHandler
	public void load(FMLInitializationEvent event) {
                //この行を
		//FluidContainerRegistry.registerFluidContainer(sampleFluid, filledContainer);]
                //こちらに入れ替える。
		FluidContainerRegistry.registerFluidContainer(sampleFluid, filledContainer,  new ItemStack(Items.bucket));

                //ここから追加
            FillBucketHandler.INSTANCE.buckets.put(sampleFluidBlock, itemSampleFluid);
            MinecraftForge.EVENT_BUS.register(FillBucketHandler.INSTANCE);
                //ここまで追加
	}

解説[編集]

loadメソッド内の変更点[編集]

FluidContainerRegistry.registerFluidContainer(sampleFluid, filledContainer,  new ItemStack(Items.bucket));

液体ブロック設置時に空のバケツが返却されるように、登録。IC2の空のセルとかを返却させることも出来る。

    FillBucketHandler.INSTANCE.buckets.put(sampleFluidBlock, itemSampleFluid);
            MinecraftForge.EVENT_BUS.register(FillBucketHandler.INSTANCE);

バケツハンドラー[編集]

バニラのバケツの処理が特殊なので、ハンドラーを登録し、ハンドラーに液体ブロックと液体入りバケツを登録。

private ItemStack fillCustomBucket(World world, MovingObjectPosition pos) {
        //BlockIDを取得
                int blockID = world.getBlockId(pos.blockX, pos.blockY, pos.blockZ);
        //BlockIDから登録した液体バケツを取得
                Item bucket = buckets.get(Block.blocksList[blockID]);
                if (bucket != null && world.getBlockMetadata(pos.blockX, pos.blockY, pos.blockZ) == 0) {
                        world.setBlock(pos.blockX, pos.blockY, pos.blockZ, 0);
                        return new ItemStack(bucket);//液体バケツを返却
                } else
                        return null;

        }

空バケツの右クリックの座標から液体ブロックを取得し、液体バケツを返却

@ForgeSubscribe
        public void onBucketFill(FillBucketEvent event) {

                ItemStack result = fillCustomBucket(event.world, event.target);

                if (result == null)
                        return;

                event.result = result;
                event.setResult(Result.ALLOW);
        }

バケツのイベントから、ターゲット座標を取得し、fillCustomBucketメソッドに送り、返ってきた液体バケツのItemStackを結果に返す。 これにより、空バケツが液体バケツになる。