BlackDereker
BlackDereker

Reputation: 41

Breaking Custom Block

I'm trying to break a custom block that i receive when i type the command "/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");

    }

Ignore the additional variables like the World and the PlayerInventory

So... I'm receiving the right block but when i break it... just dont do anything

Upvotes: 1

Views: 1122

Answers (1)

Victor2748
Victor2748

Reputation: 4199

What is CustomBlock? is it a variable or a Class? 2 things:

  1. a Block is just a position, you can not serialize it, or check if it equals to another block.
  2. block.equals() is a native Object's method, not overwriten by bukkit. It will simply check if one object is equal to another.

The best way to check ff a block is your "custom block", is to simply record every custom block's location, and check if the block is in one of those locations. 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
    }

}

Upvotes: 4

Related Questions