Getting wool color

Discussion in 'Plugin Development' started by orikenig, Feb 5, 2013.

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

    orikenig

    Want to check if the player break a red wool and then do a code.​
    Do it on "BlockBreakEvent".​
     
  2. orikenig
    Check if the block id type is wool. Then get the item data and compare its data value. A google search will show you which id corresponds to which colour of wool.
     
  3. The data value is the color.

    If you want easy colors you can use:
    Code:
    DyeColor color = DyeColor.getByWoolData(block.getData());
    
    if(color == DyeColor.RED)
    {
        // ...
    }
     
  4. Offline

    orikenig

    I tried to use your code and did it like that:
    Code:
    Block b = e.getBlock();
    DyeColor Color = DyeColor.getByWoolData(b.getData());
    Player p = e.getPlayer();
    if(e.getBlock().equals(b)){
    if(Color == DyeColor.RED){
      p.sendMessage("Hi!");
            }
            }
    And it doesn't working.
     
  5. Digi
    Didn't know about that class, neat ;).
     
  6. Offline

    lycano

    orikenig you want to get the state of that block and then cast it to Material Wool. After that just do a getColor() on that object and see if it has the right color. I would use switch for that as you may want to do different things when getting different colors.


    Code:
        @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void onBlockBreak(BlockBreakEvent event) {
            Block b = event.getBlock();
    
            if (!b.equals(Material.WOOL))
                return;
    
            DyeColor woolColor = ((Wool) b.getState()).getColor();
            switch (woolColor) {
                case RED:
                    //red code here
                    break;
                case GREEN:
                    //green code here
                    break;
            }
        }
    
     
  7. Offline

    orikenig

    I dont understand it.
    I just want to check if the player break a red wool.
     
  8. Offline

    lycano

    Ok, the basics then.

    I hope you know what a listener is if not please checkout the wiki page first and read about it.

    in that code example you have an event.

    That event describes the BlockBreakEvent. You can get the affected block with event.getBlock() and the player by event.getPlayer();

    Each block has a state. The state of that affected block can be fetched by using event.getBlock().getState(). It holds basic informations like the position of that block and so on. (Have a look at the apidocs http://jd.bukkit.org/apidocs/)

    Anyways ... my code example wouldn't work i believe as Wool does not implement Metadatable. (didn't checked it just did it from my mind and i assumed it would be the same as when playing with Sings but ....)

    Sry that i misleaded you.
     
  9. Offline

    ZachBora

    lycano I don't think you can compare a Block with Material. Perhaps with block.getType()

    at the very basic level you could do this and it would work until red changes id.
    Code:
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    public void onBlockBreak(BlockBreakEvent event) {
        Block b = event.getBlock();
     
        if(b.getType() == Material.WOOL && b.getData().getData() == 14)
        {
          //do stuff
        }
    }
    
     
  10. Offline

    keensta

    Do a check the block to check if its wool if it is do.

    Wool wool = (Wool) ev.getBlock().getData();
    If(wool.getColor() == DyeColor.RED) {
    }

    This code hasn't been tested. I have used it for an item but never blocks I don't know if wool will cast a block you have to check that as well
     
Thread Status:
Not open for further replies.

Share This Page