Quick way to check if block is wooden?

Discussion in 'Plugin Development' started by Chaoscrasher, Jun 15, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Chaoscrasher

    Hello guys,

    So I'm trying to impose a restriction, that makes it impossible for wooden blocks to be "mined" without an axe.

    Now I could do:

    Code:java
    1. if (Bock.getMaterial().equals(WOOD) || Block.getMaterial().equals(WOOD_STAIRS) ||...)
    2. {
    3. if (p.getItemInHand().getMaterial().toString().contains("AXE"))
    4. BlockBreakEvent.setCancelled(true);
    5. }


    but I find that a bit... inefficient, and this also won't account for future wooden blocks that aren't released yet.

    Isn't there a simpler/better way? Unfortunately not all wooden block materials have the word "Wood" in their name, even though this is the case if you look at them from within Minecraft. (All wooden blocks appear if you search for "wood" in the creative mode window), so the thing I did with the axe won't work there either.

    Would love to hear if you know of a solution :)
     
  2. Offline

    TheEntropy

    You could fill an ArrayList with the appropriate blocks, then check if that ArrayList contains the block:

    Code:
    ArrayList<Material> blocks = new ArrayList<Material>();
            
            blocks.add(Material.WOOD);
            // ...add all the appropriate blocks
            
            // check if the arraylist contains the b
            if (blocks.contains(event.getBlock()) {
                
                // do stuff
                
            }
    
     
  3. Offline

    1Rogue

    It's not inefficient, just incredibly verbose and unmaintainable. The List/Set suggestion is good.
     
  4. Offline

    Smerfa

    or array?
    private static final Material[] WOOD_BLOCKS = {Material.WOOD, ...};
     
Thread Status:
Not open for further replies.

Share This Page