Right Click Event?

Discussion in 'Plugin Development' started by Bear53, Apr 20, 2015.

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

    Bear53

    Trying to make a right click event execute this command, I'm not sure if im doing this event right, just wondering
    Code:
    package me.Bear53.RechargeHub.Events;
    
    import java.util.ArrayList;
    
    import me.Bear53.RechargeHub.Main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class ShowPlayerEvent implements Listener {
        Main plugin;
    
        public ShowPlayerEvent(Main pl) {
            this.plugin = pl;
        }
    
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerUse(PlayerInteractEvent event) {
            Player p = event.getPlayer();
    
            ItemStack h = new ItemStack(Material.REDSTONE_TORCH_ON);
            ItemMeta hmeta = h.getItemMeta();
            hmeta.setDisplayName(ChatColor.GREEN + "Player Visibility "
                    + ChatColor.DARK_GRAY + "» " + ChatColor.YELLOW + "On");
            ArrayList<String> hlore = new ArrayList<String>();
            hlore.add(ChatColor.GRAY + "Right Click to toggle Players");
            h.setItemMeta(hmeta);
    
            ItemStack s = new ItemStack(Material.REDSTONE_TORCH_ON);
            ItemMeta smeta = s.getItemMeta();
            hmeta.setDisplayName(ChatColor.GREEN + "Player Visibility "
                    + ChatColor.DARK_GRAY + "» " + ChatColor.RED + "Off");
            ArrayList<String> slore = new ArrayList<String>();
            slore.add(ChatColor.GRAY + "Right Click to toggle Players");
            s.setItemMeta(smeta);
    
            if (p.getItemInHand().equals(Material.REDSTONE_TORCH_ON)) {
                Bukkit.dispatchCommand(p, "toggle");
                p.setItemInHand(s);
            } else if (p.getItemInHand().equals(Material.LEVER)) {
                Bukkit.dispatchCommand(p, "toggle");
                p.setItemInHand(h);
            }
        }
    }
    if someone could see if there is a problem located inside my code as i am getting no errors Thanks!
     
  2. Offline

    SuperOriginal

    You're trying to compare an ItemStack to a Material which will never be true. You need to get the Material value of the ItemStack and to get it you use the getType() method.
     
  3. Offline

    _Cookie_

    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    //Do stuff :)
    }
     
  4. Offline

    Bear53

    @_Cookie_ Would this be the correct way to use this?
    Code:
    @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerUse(PlayerInteractEvent event) {
            Player p = event.getPlayer();
    
            ItemStack h = new ItemStack(Material.REDSTONE_TORCH_ON);
            ItemMeta hmeta = h.getItemMeta();
            hmeta.setDisplayName(ChatColor.GREEN + "Player Visibility "
                    + ChatColor.DARK_GRAY + "» " + ChatColor.YELLOW + "On");
            ArrayList<String> hlore = new ArrayList<String>();
            hlore.add(ChatColor.GRAY + "Right Click to toggle Players");
            h.setItemMeta(hmeta);
    
            ItemStack s = new ItemStack(Material.REDSTONE_TORCH_ON);
            ItemMeta smeta = s.getItemMeta();
            hmeta.setDisplayName(ChatColor.GREEN + "Player Visibility "
                    + ChatColor.DARK_GRAY + "» " + ChatColor.RED + "Off");
            ArrayList<String> slore = new ArrayList<String>();
            slore.add(ChatColor.GRAY + "Right Click to toggle Players");
            s.setItemMeta(smeta);
            if (event.getAction() == Action.RIGHT_CLICK_AIR
                    || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (p.getItemInHand().equals(Material.REDSTONE_TORCH_ON)) {
                    Bukkit.dispatchCommand(p, "toggle");
                    p.setItemInHand(s);
                } else if (event.getAction() == Action.RIGHT_CLICK_AIR
                        || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    if (p.getItemInHand().equals(Material.LEVER)) {
                        Bukkit.dispatchCommand(p, "toggle");
                        p.setItemInHand(h);
                    }
                }
            }
        }
    }
     
  5. @Bear53 Looks fine to me, but you'll never know until you test it out.
     
  6. Offline

    Bear53

  7. Offline

    nj2miami

     
    CodePlaysMinecraft likes this.
Thread Status:
Not open for further replies.

Share This Page