提供: Minecraft Modding Wiki
2016年2月10日 (水) 12:28時点におけるMiyabi (トーク | 投稿記録)による版
移動先: 案内検索

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

C none.png
TileEntityに関係のあるチュートリアルです。


この記事は http://forum.minecraftuser.jp/viewtopic.php?f=39&t=29948 での質問の結果を流用しており、
コード内のコメントを削除されていて、解説として不適切です。
修正されるまで参考にしないで下さい。

チェストにアイテムを入れる

今回は右クリックされると、一つ上のチェスト(インベントリを持つTileEntity) にアイテムを入れる。

package sample;

//import省略

public class chestinitem extends Block {
@SideOnly(Side.CLIENT)
    private IIcon TopIcon;

    @SideOnly(Side.CLIENT)
    private IIcon SideIcon;

public open() {
        super(Material.rock);
        //省略
    }
@Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float posX, float posY, float posZ){
if (!world.isRemote)
            {
               Block block = world.getBlock(x, y+1, z);
               int meta = world.getBlockMetadata(x, y+1, z);


               if (block.hasTileEntity(meta))
               {
                  TileEntity tile = world.getTileEntity(x, y+1, z);

                  if (tile != null)
                  {

                     if (tile instanceof IInventory)
                     {
                        IInventory inventory = (IInventory)tile;


                        for (int pl = 0; pl < inventory.getSizeInventory(); ++pl)
                        {
                           ItemStack itemstack = inventory.getStackInSlot(pl);


                           if (itemstack == null)
                           {


                              inventory.setInventorySlotContents(pl, new ItemStack(items.diamond));//ダイヤを一個入れる
                              return true;

                           }
                        }
                     }
                  }
               }
            }

解説

変数plにTileEntity毎のインベントリ情報を入れ、
空いているスロットに入れている。