我试图打破键入命令“ / 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之类的其他变量

所以...我收到了正确的方块,但是当我打破它时...什么都不做

最佳答案

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


Block只是一个位置,您无法序列化它,或检查它是否等于另一个块。
block.equals()是本机Object's方法,不会被bukkit覆盖。它只会检查一个对象是否等于另一个。


检查块的最佳方法是“自定义块”,即简单地记录每个自定义块的位置,并检查该块是否在这些位置之一中。例如:

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
    }

}

09-05 18:21