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

この記事は"ModLoader"を前提MODとしています。

ItemStack(アイテムを管理している部分)にエンチャント等の情報を保存するためにNBTを使用するメソッドがあります。
今回はそれを使用してTNTを遠隔で着火するアイテムを追加します。
なお、MOD部のソースコードは省略しています。

ソースコード

package net.minecraft.src;

import java.util.List;

public class ItemTNTRemote extends Item
{
	public int posx,posy,posz;

	public ItemHoge(int id)
	{
		super(id);
		//スタック不可能にする。
		maxStackSize = 1;
	}

	//ブロックを右クリックした時に呼ばれます。
	//1.3.x以降で使用する場合は引数を(ItemStack itemstack, EntityPlayer entityplayer, World world, int x, int y, int z, int side, float p, float q, float r)に変えて下さい。
	//1.3.xのみメソッド名がtryPlaceIntoWorldなので注意
	@Override
	public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer, World world, int x, int y, int z, int side)
	{
		if(!world.isRemote)
		{
			//NBTタグを取得します。
			NBTTagCompound nbt = itemstack.getTagCompound();
			if(nbt == null)
			{
				nbt = new NBTTagCompound();
				itemstack.setTagCompound(nbt);
			}
			//対象ブロックがTNTの場合は位置を記録し、それ以外の場合は着火処理を行う。
			if(world.getBlockId(x, y, z) == Block.tnt.blockID)
			{
				nbt.setInteger("PosX", x);
				nbt.setInteger("PosY", y);
				nbt.setInteger("PosZ", z);
			}
			else
			{
				onItemRightClick(itemstack, world, entityplayer);
			}
		}
		return true;
	}

	//空中を右クリックした場合に呼ばれます。
	@Override
	public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer)
	{
		if(!world.isRemote)
		{
			NBTTagCompound nbt = itemstack.getTagCompound();
			if(nbt == null)
				return itemstack;
			posx = nbt.getInteger("PosX");
			posy = nbt.getInteger("PosY");
			posz = nbt.getInteger("PosZ");
			if(world.getBlockId(posx, posy, posz) == Block.tnt.blockID)
			{
				Block.tnt.onBlockDestroyedByPlayer(world, posx, posy, posz, 1);
				world.setBlockWithNotify(posx, posy, posz, 0);
			}
		}
		return itemstack;
	}

	//アイテム名の表示の下に記録された位置を表示させます。
	@Override
	public void addInformation(ItemStack itemstack, List list)
	{
		NBTTagCompound nbt = itemstack.getTagCompound();
		if(nbt == null)
			return;
		int posx,posy,posz;
		posx = nbt.getInteger("PosX");
		posy = nbt.getInteger("PosY");
		posz = nbt.getInteger("PosZ");
		list.add("[" + posx + "," + posy + "," + posz + "]");
	}

}

ソースの解説

onItemUse

NBTTagCompound nbt = itemstack.getTagCompound();
if(nbt == null)
{
	nbt = new NBTTagCompound();
	itemstack.setTagCompound(nbt);
}
  • ここでアイテムからNBTタグを取得します。
    • getTagCompoundはアイテムにNBTタグが存在しない場合nullを返すようになっており、そのままではNullPointerExceptionが発生します。
    • なのでnullが返ってきた場合は、新しくNBTタグを作成し、アイテムに登録します。
if(world.getBlockId(x, y, z) == Block.tnt.blockID)
{
	nbt.setInteger("PosX", x);
	nbt.setInteger("PosY", y);
	nbt.setInteger("PosZ", z);
}
else
{
	onItemRightClick(itemstack, world, entityplayer);
}
  • ここでは右クリックしたブロックがTNTの場合NBTに座標情報を記録し、そうでない場合は、onItemRightClickを呼び出しています。

onItemRightClick

NBTTagCompound nbt = itemstack.getTagCompound();
if(nbt == null)
	return itemstack;
posx = nbt.getInteger("PosX");
posy = nbt.getInteger("PosY");
posz = nbt.getInteger("PosZ");
if(world.getBlockId(posx, posy, posz) == Block.tnt.blockID){
	Block.tnt.onBlockDestroyedByPlayer(world, posx, posy, posz, 1);
	world.setBlockWithNotify(posx, posy, posz, 0);
}
  • ここでは、NBTタグから座標情報を読み出し、その位置にTNTが存在すればTNTの着火処理メソッドを呼び出します。

addInformation

NBTTagCompound nbt = itemstack.getTagCompound();
if(nbt == null)
	return;
int posx,posy,posz;
posx = nbt.getInteger("PosX");
posy = nbt.getInteger("PosY");
posz = nbt.getInteger("PosZ");
list.add("[" + posx + "," + posy + "," + posz + "]");
  • ここでは、NBTタグから座標情報を読み出し、表示情報リストに追加しています。

メソッド解説

  • ItemやItemStack内には大量にメソッドがあるので今回は、直接関係のあるメソッドだけ紹介します。

Item

メソッド名説明
boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int i, int j, int k, int l)ブロックを右クリックした時に呼ばれる
ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)(onItemUseがfalseを返している時に)何も起きないブロックを右クリックした時やブロックにカーソルが合わさっていない場合に右クリックした時呼ばれる。
void addInformation(ItemStack itemstack, List list)アイテムの表示に独自情報(例:エンチャント)を追加するときに使う

ItemStack

メソッド名説明
NBTTagCompound getTagCompound()アイテムのNBTタグを取得する
void setTagCompound(NBTTagCompound par1NBTTagCompound)アイテムにNBTタグを登録する
List getItemNameandInformation()アイテムにカーソルを合わせた時に表示される情報のリストを取得する。
先述したaddInformationはここから呼ばれている

NBTTagCompound

メソッド名説明
void setByte(String par1Str, byte par2)Byte型の変数を記録する。
void setShort(String par1Str, short par2)Short型の変数を記録する。
void setInteger(String par1Str, int par2)Integer型の変数を記録する。
void setLong(String par1Str, long par2)Long型の変数を記録する。
void setFloat(String par1Str, float par2)Float型の変数を記録する。
void setDouble(String par1Str, double par2)Double型の変数を記録する。
void setString(String par1Str, String par2Str)String型の変数を記録する。
void setByteArray(String par1Str, byte par2ArrayOfByte[])Byte型の配列を記録する。
void setIntArray(String par1Str, int par2ArrayOfInteger[])Int型の配列を記録する。
void setBoolean(String par1Str, boolean par2)Boolean型の変数を記録する。
byte getByte(String par1Str)Byte型の変数を取得する。
short getShort(String par1Str)Short型の変数を取得する。
int getInteger(String par1Str)Integer型の変数を取得する。
long getLong(String par1Str)Long型の変数を取得する。
float getFloat(String par1Str)Float型の変数を取得する。
double getDouble(String par1Str)Double型の変数を取得する。
String getString(String par1Str)String型の変数を取得する。
byte[] getByteArray(String par1Str)Byte型の配列を取得する。
int[] getIntArray(String par1Str)Integer型の配列を取得する。
boolean getBoolean(String par1Str)Boolean型の変数を取得する。
  • NBTタグの変数の型を後から変更するとエラーが発生し、最悪の場合ワールドデータが破損します。十分注意して下さい。