Solved Why doesn't this Right Click Method work?

Discussion in 'Plugin Development' started by AmusingThrone, Sep 9, 2016.

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

    AmusingThrone

    Hi,

    So basically I am trying to check if an item is a Feather, and if it is, does it have the name "Vanish Cape". If it does, it will either vanish, or unvanish the player. I can't figure out why this doesn't work, I am not getting any errors in console, but this does not work in-game.

    Code:
    @EventHandler(priority=EventPriority.HIGH)
       
        public void onPlayerUse(PlayerInteractEvent e){
           
           
            Player p = e.getPlayer();
           
            if (p.getItemInHand() != null) {
               
                    if (p.getItemInHand().getType().equals(Material.FEATHER)) {      
                       
                        ItemStack item = p.getItemInHand();
                           
                        if (item.hasItemMeta()) {
                           
                            ItemMeta meta = item.getItemMeta();
                           
                            if (meta.hasDisplayName()) {
                               
                                if (meta.getDisplayName().contains("Vanish Cape")) {
                       
                                    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                                   
                                        if (invisibles.get(e.getPlayer()) == Boolean.FALSE) {
                 
                                            p.hidePlayer(p);
                                            invisibles.put(e.getPlayer(), false);
                                            p.sendMessage(ChatColor.BLUE + "Vanishing");
                 
                                        } else {
                 
                                            p.showPlayer(p);
                                            invisibles.put(e.getPlayer(), false);
                                            p.sendMessage(ChatColor.BLUE + "Un-Vanishing");
                 
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
        }
     
  2. Offline

    Zombie_Striker

    First: Debug. That is the only way you can figure out what line is the problem. Right after each if statement, send a message to the player. If the player sees it, that means that part of the code works.
    use == to compare enums.

    Are you sure the displayname contains this string? Debug.

    A) You duplicated the Air line. Remove one of them
    B) Use == to compare enums.

    A) Dont use == to compare booleans. A boolean is already a boolean. For this, remove "== false" and invert the statement (by adding a ! before the boolean)
    B) Are you sure the value that gets returned is equal to false? Debug.
     
  3. Offline

    AmusingThrone

    @Zombie_Striker
    So you are basically asking me to check if I named the item correctly in-game? This is a user created item, not player created.

    So would it be something like this?

    Code:
    if (invisibles.get(e.getPlayer()) !=Boolean.TRUE) {
                
                                            p.hidePlayer(p);
                                            invisibles.put(e.getPlayer(), false);
                                            p.sendMessage(ChatColor.BLUE + "Vanishing");
                
                                        } else {
                
                                            p.showPlayer(p);
                                            invisibles.put(e.getPlayer(), false);
                                            p.sendMessage(ChatColor.BLUE + "Un-Vanishing");
                
                                        }
    So I have debugged the plugin and come to the conclusion that literally nothing is working... For some reason, the listener is being skipped altogether... Nothing below my onEnable seems to be working. I have posted my entire class below:

    Code:
    package me.AmusingThrone.test
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class SFTTest extends JavaPlugin implements Listener {
      
        public Map<Player, Boolean> invisibles = new HashMap<Player, Boolean>();
      
        public void onEnable() {
            System.out.println("Loading libraries for plugin...");
        }
      
        @EventHandler(priority=EventPriority.HIGH)
      
        public void onPlayerUse(PlayerInteractEvent e){
          
          
            Player p = e.getPlayer();
          
            if (p.getItemInHand() != null) {
              
                p.sendMessage(ChatColor.BLUE + "Nothing in Hand!");
              
                    if (p.getItemInHand().getType() == (Material.FEATHER)) {     
                      
                        p.sendMessage(ChatColor.BLUE + "Feather Recognized!");
                      
                        ItemStack item = p.getItemInHand();
                          
                        if (item.hasItemMeta()) {
                          
                            p.sendMessage(ChatColor.BLUE + "Meta Recognized!");
                          
                            ItemMeta meta = item.getItemMeta();
                          
                            if (meta.hasDisplayName()) {
                              
                                if (meta.getDisplayName().contains("Vanish Cape")) {
                      
                                    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == (Action.RIGHT_CLICK_BLOCK)) {
                                  
                                        p.sendMessage(ChatColor.BLUE + "Button Pressed!");
                                      
                                        if (invisibles.get(e.getPlayer()) !=Boolean.TRUE) {
                
                                            p.hidePlayer(p);
                                            invisibles.put(e.getPlayer(), false);
                                            p.sendMessage(ChatColor.BLUE + "Vanishing");
                
                                        } else {
                
                                            p.showPlayer(p);
                                            invisibles.put(e.getPlayer(), false);
                                            p.sendMessage(ChatColor.BLUE + "Un-Vanishing");
                
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
          
      
      
    
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
          
            Player p = e.getPlayer();
            p.showPlayer(p);
            this.invisibles.put(e.getPlayer(), false);
            p.sendMessage(ChatColor.BLUE + "You have joined!");
          
        }
      
    }
     
    Last edited: Sep 9, 2016
  4. Offline

    Zombie_Striker

    @AmusingThrone
    The reason the event is not working is because you forgot to register the listener. Add the following line to the onEnable
    Code:
    getServer().getPluginManager().registerEvents(this,this);
    Also:
    • Keep your package lowercase. This is convention for all Java programs.
    • Remove the "System.out" line. Bukkit already logs your plugin so that line is not needed.
    • What you posted is not how to invert an if statement. It should be the following
      Code:
      if (!invisibles.get(e.getPlayer()) {
     
  5. Offline

    AmusingThrone

Thread Status:
Not open for further replies.

Share This Page