Solved How to use cactus green in PlayerInteract

Discussion in 'Plugin Development' started by theon, Sep 14, 2014.

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

    theon

    I have been having trouble making it that when someone right clicked cactus green it gives them effects.
    Here is my code I don't know why its not working. Thank you.
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e){
    3. Player p = e.getPlayer();
    4. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    5. if(e.getMaterial() == Material.SUGAR){
    6. p.addPotionEffect(new PotionEffect((PotionEffectType.SPEED), 900, 2));
    7. p.addPotionEffect(new PotionEffect((PotionEffectType.JUMP), 900, 2));
    8. p.addPotionEffect(new PotionEffect((PotionEffectType.FAST_DIGGING), 900, 2));
    9. }
    10. else if(e.getMaterial() == Material.QUARTZ){
    11. p.addPotionEffect(new PotionEffect((PotionEffectType.INCREASE_DAMAGE), 1800, 1));
    12. p.addPotionEffect(new PotionEffect((PotionEffectType.DAMAGE_RESISTANCE), 1800, 1));
    13.  
    14. } else if(e.getClickedBlock().equals(new ItemStack(Material.INK_SACK,1, (short) 2))){
    15. p.addPotionEffect(new PotionEffect((PotionEffectType.SLOW_DIGGING), 1800, 1));
    16. p.addPotionEffect(new PotionEffect((PotionEffectType.SLOW), 1800, 1));
    17. p.addPotionEffect(new PotionEffect((PotionEffectType.CONFUSION), 1800, 0));
    18. }
    19. }
    20. }
     
  2. theon
    Well first of all, getClickedBlock() returns the block you clicked in the world, not the item in your hand. Use getItem() instead.

    Also, I believe you have go check the item durability rather than using a data value in the ItemStack constructor.
     
  3. Offline

    theon

    I tried this and it didn't work either.
    Code:java
    1. } else if(e.getItem() == new ItemStack(Material.INK_SACK)){
    2. if(e.getItem().getDurability() == 2)
    3. p.addPotionEffect(new PotionEffect((PotionEffectType.SLOW_DIGGING), 1800, 1));
    4. p.addPotionEffect(new PotionEffect((PotionEffectType.SLOW), 1800, 1));
    5. p.addPotionEffect(new PotionEffect((PotionEffectType.CONFUSION), 1800, 0));
    6. }
     
  4. Offline

    masons123456

    theon Instead of comparing your item with another itemstack, compare the Material and the data.
    Code:
    ItemStack item = e.getItem();
    if (item.getType() == Material.INK_SACK && item.getDurability() == 2) {
    }
    Alternatively, you could compare the ItemStack using:
    Code:
    ItemStack item = e.getItem();
    if (item.isSimilar(new ItemStack(Material.INK_SACK, 1, (short) 2))) {
    }
     
  5. Offline

    theon

    Oh, thanks a lot it works now!
     
  6. Offline

    Jaaakee224

    Please set this thread to Solved if all your questions are answered. :)
     
Thread Status:
Not open for further replies.

Share This Page