Solved PlayerInteractEvent wool colors?

Discussion in 'Plugin Development' started by Krotass, Apr 16, 2014.

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

    Krotass

    Hey guys. I'm currently trying to test if a player right clicked with a certain wool color. I know how to give them this item but how do I test for it?

    (Main.prefix = string in my main.class file)

    Current code:
    Code:
    @EventHandler
    public void JoinTeam(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    if ((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)&& player.getItemInHand().getType() == Material.WOOL) {
    player.sendMessage(Main.prefix + ChatColor.YELLOW + "test");
    }
    }//end of JoinTeam
     
  2. Offline

    Konkz

    PHP:
        @SuppressWarnings("deprecation")
        @
    EventHandler
        
    public void joinTeam(PlayerInteractEvent e) {
            
    Player p e.getPlayer();
           
            if (
    e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (
    p.getItemInHand().getType() == Material.WOOL) {
                    switch(
    p.getItemInHand().getData().getData()) {
                    case (
    byte4:
                        
    p.sendMessage("It's yellow!");
                        break;
                   
                    case (
    byte12:
                        
    p.sendMessage("It's brown!");
                        break;
                    }
                }
            }
        }
    Code:
     0 0x0 Regular (White)
    1 0x1 Orange
    2 0x2 Magenta
    3 0x3 Light Blue
    4 0x4 Yellow
    5 0x5 Lime
    6 0x6 Pink
    7 0x7 Gray
    8 0x8 Light Gray
    9 0x9 Cyan
    10 0xA Purple
    11 0xB Blue
    12 0xC Brown
    13 0xD Green
    14 0xE Red
    15 0xF Black
    Krotass
     
  3. Offline

    coasterman10

    Get the MaterialData with getData() and cast it to Wool, then use getColor(), which returns a DyeColor.
    Code:java
    1. Wool w = (Wool) player.getItemInHand().getData();
    2. if (w.getColor() == DyeColor.YELLOW) {
    3. player.sendMessage("You clicked with yellow wool");
    4. }

    This is more future-proof since it makes use of API methods instead of using magic values which were deprecated for a reason. Please note that if you use this code anywhere else, you should make sure that the item's MaterialData is instanceof Wool first, or that the item's type is wool.
     
  4. Offline

    Konkz

    That works too I guess, plus then you don't have to use deprecated method. :)
     
  5. Offline

    Krotass

    Thanks for helping me guys. I can now continue with my plugin :D
     
Thread Status:
Not open for further replies.

Share This Page