On right click

Discussion in 'Spigot Plugin Development' started by alessio2010, Feb 13, 2018.

  1. Code (Text):
       
    if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
                    if (player.getItemInHand().equals(Material.BLAZE_ROD)) {
                        if (e.getAction() == Action.RIGHT_CLICK_AIR) {
                            player.sendMessage(ChatColor.GREEN + "hi");
                           
                        }
                    }
     

    Why doesn't this work ?
     
  2. Are you right clicking air.. are you getting any errors? Try
    Code (Text):
     if (player.getItemInHand().getType() ==Material.BLAZE_ROD) {
     
  3. ScarabCoder

    Resource Staff

    You're comparing an ItemStack object with a Material enum, which won't work unless ItemStack overrided equals to support that (which it doesn't).
     
    • Winner Winner x 1
  4. .
    Is this in the player interact event, plz provide more code. Also the third line of the code is not necessary
     
  5. Are you sure that returns a Material?
     
  6. How should it be then ?
     
  7. Try player.getItemInMainHand().getType() == Material.BLAZE_ROD
     
    • Agree Agree x 1
  8. ScarabCoder

    Resource Staff

    What @RevampedPvp said. You want to compare the type of the ItemStack, not the ItemStack object.
     
  9. This is my third day coding, I'm learning stuff! YAY!
     
  10. I tried it with getiteminhand not main hand. I’m not at my PC anymore so i can’t try main hamd
    But inhand did not work so if it’s the same thing it won’t work
     
  11. Main hand should work
     
  12. I’ll try it out
     
  13. How many blaze rods do you have, and get rid of the 2nd action underneath the code that isn't working
     
  14. ScarabCoder

    Resource Staff

    Can you post your updated code?
     
  15. Try this code, It should
    Code (Text):
    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
        Player p = event.getPlayer();
     
        if(!(e.getAction() Action.RIGHT_CLICK_AIR))
          if(p.getItemInHand() == Material.BLAZE_ROD){
            player.sendMessage(ChatColor.GREEN + "Hi")
        }
        else if(p.getItemInHand() == Material.BLAZE_ROD){
            //Do whatever
        }
    }
     
     
  16. Code (Java):
    if (event.getAction() != Action.RIGHT_CLICK_AIR) {
         return;
    }
    if (player.getItemInHand().getType() == Material.BLAZE_ROD) {
         player.sendMessage(ChatColor.GREEN + "hi");
    }
     
    • Agree Agree x 2
  17. This code will work, thanks @FrostedSnowman, here you go @alessio2010
     
  18. ScarabCoder

    Resource Staff

    Don't use the HIGH event priority unless it's required for some reason.
     
    • Agree Agree x 1
  19. None of these work.
    Code (Text):

    package me.alessio2010.ineedhealing;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;


    public class MedicListeners implements Listener {

        @EventHandler
        public void onSignChange(SignChangeEvent e) {
            if (e.getLine(0).equalsIgnoreCase("[Heal]")) {
                e.setLine(0, "§6[§4Heal§6]");
            }
        }
       
       
        @EventHandler
        public void onPlayerInteract1(PlayerInteractEvent event) {
            Player player = event.getPlayer();
        if (event.getAction() != Action.RIGHT_CLICK_AIR) {
             return;
        }
        if (player.getItemInHand().getType() == Material.BLAZE_ROD) {
             player.sendMessage(ChatColor.GREEN + "hi");
        }
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            if (e.getClickedBlock().getState() instanceof Sign) {
                Sign s =(Sign) e.getClickedBlock().getState();
                if (s.getLine(0).equalsIgnoreCase("§6[§4Heal§6]")) {
                    e.getPlayer().setHealth(20);
                    e.getPlayer().sendMessage("You were healed!");
                   
                   
                   
                   
                    }
                }
            }
        }
       
     
    FULL CODE HERE
     

Share This Page