打破自定义块

时间:2014-12-22 04:55:34

标签: java eclipse plugins minecraft bukkit

我正在尝试打破我在输入命令“/ customblock”

时收到的自定义块
@EventHandler
public void onBlockBreak(BlockBreakEvent broke){




    Player player = broke.getPlayer();
    PlayerInventory inventory = broke.getPlayer().getInventory();
    World world = player.getWorld();
    Material block = broke.getBlock().getType();


    if(block.equals(CustomBlock)){

        player.sendMessage("Test");

    }

忽略其他变量,例如World和PlayerInventory

所以...我正在接受正确的阻止,但是当我打破它......就是什么都不做

1 个答案:

答案 0 :(得分:4)

什么是CustomBlock?它是变量还是类? 2件事:

  1. Block只是一个位置,您无法将其序列化,或检查它是否等于另一个块。
  2. block.equals()是一种原生的Object's方法,不会被bukkit覆盖。它只会检查一个对象是否与另一个对象相同。
  3. 检查块的最佳方法是"自定义块",只需记录每个自定义块的位置,并检查块是否位于其中一个位置。 E.g:

    public List<Location> customBlocks = new ArrayList<Location>();
    
    //... in the block place event add the block's location to the list
    
    @EventHandler
    public void onBlockBreak(BlockBreakEvent broke){
    
        Player player = broke.getPlayer();
        PlayerInventory inventory = broke.getPlayer().getInventory();
        World world = player.getWorld();
        Material block = broke.getBlock().getType();
    
    
        if(customBlocks.contains(block.getLocation())){
            //custom block
            block.setType(Material.AIR); //destroy the block
        }
    
    }