Event Don't work at all

Discussion in 'Spigot Plugin Development' started by DeadlyDeath001, May 6, 2015.

  1. How would I make this code work.
    I want the compass to Right Click and open up the Inventory I made but it is not working
    This Is Just a separate class. This is all the code in the class u see in this image
    Error:
    [​IMG]
    Code (Text):
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            Action action = event.getAction();
            ItemStack item = event.getItem();
            if(action != Action.RIGHT_CLICK_AIR || action != Action.RIGHT_CLICK_BLOCK){
                return;
            }
            if(item.getType() != Material.COMPASS){
                return;        
            }
            if(item.getItemMeta().getDisplayName().equalsIgnoreCase("§eClass §lSelector")){
                event.setCancelled(true);
                player.openInventory(inv);
            }
        }

    }
     
     
  2. Updated but Still don't work :(
    Code (Text):
    package com.DeadlyDeath001.warfare.listeners;

    import org.bukkit.Material;
    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.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;

    import com.DeadlyDeath001.warfare.classes.ClassCompass;

    public class PlayerInteract implements Listener {
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            Action action = event.getAction();
            ItemStack item = event.getItem();
            if(action != Action.RIGHT_CLICK_AIR || action != Action.RIGHT_CLICK_BLOCK){
                return;
            }
            if(item.getType() == Material.COMPASS){
                return;          
            }    
            if(item.getItemMeta().getDisplayName().equalsIgnoreCase("§eClass §lSelector")){
                event.setCancelled(true);
                ClassCompass.openClassCompass(player);
        }
        }
    }
     
  3. Would this be the right code?
    Code (Text):
    ClassCompass.openClassCompass(player);
     
  4. This is my second class
    Code (Text):
    public class ClassCompass {
       
    [CODE]public class ClassCompass {
       
        public static void openClassCompass(Player player){
            Inventory inv = Bukkit.createInventory(null, 9, "§8Pick your class!");
    // Stuff
     
  5. you registered the listener class?
     
  6. 1. Are you getting any errors now?

    2. Are you actually opening the inventory or are you only creating it?
     
  7. That would create the inventory, open it with player.openInventory(inv);
    Why not instead of if(item.getItemMeta().getDisplayName().equalsIgnoreCase("§eClass §lSelector")){
    Do if(ChatColor.stripColor(item.getItemMeta().getDisplayName()).equalsIgnoreCase("Class Selector")){


    Might have typos, on my phone.

    EDIT: The error in the first class is there because "inv" is not a variable there. . .
     
  8. Code (Text):
            Bukkit.getPluginManager().registerEvents(new PlayerInteract(), this);
    Yeah I did register it
     
  9. Code (Text):
     if(ChatColor.stripColor(item.getItemMeta().getDisplayName()).equalsIgnoreCase("Class Selector")){
    Right Clicking compass Did Not Work.
     
  10. Are you sure the in game item has the right name?
     
  11. In the method openClassInventory do you have player.openInventory(inv);? And in the first class remove player.openInventory(inv); and add ClassCompass.openClassInventory(inv).
    Why don't you make the inventory in the same class by the way?
     
  12. Code (Text):
        public ItemStack getClassCompass(){
            ItemStack com = new ItemStack(Material.COMPASS);
            ItemMeta comMeta = com.getItemMeta();
            comMeta.setDisplayName("§eClass §lSelector");
            com.setItemMeta(comMeta);
            return com;
        }
       

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event){
            Player player = event.getPlayer();
            event.setJoinMessage("§5[Warfare] §3Player " + player.getDisplayName() + " has joined us!");
            player.sendMessage("§5[Warfare] §3Welcome to the fight, young warrior!");
            player.getInventory().addItem(getClassCompass());
        }
    }
     
    [​IMG]
    Yes I'm 100% positive
     
  13. Are you using any debug messages? Do you know where it is failing? Are you always just clicking air? Have you tried right clicking against a block to check the second condition? IIRC one of the clicks when clicked against air just cancels the event and never actually fires.
     
  14. This will always be true and therefore always ends the method execution.
     
    • Agree Agree x 1
    • Informative Informative x 1
  15. The dreaded && || switcharoo
     
  16. inv cannot be resolved to a variable
    [​IMG]
    New Code: (I Combined Inventory With RightClick Invent)
    Code (Text):
    package com.DeadlyDeath001.warfare.listeners;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    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.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import com.DeadlyDeath001.warfare.classes.ClassCompass;
    import com.DeadlyDeath001.warfare.classes.EquipPlayer;

    public class PlayerInteract implements Listener {
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event){
            Player player = (Player) event.getWhoClicked();
            Inventory inv = event.getInventory();
            if(inv.getName().equalsIgnoreCase("§8Pick your class!")){
                if(event.getCurrentItem() == null | event.getCurrentItem().getType() == Material.AIR || !event.getCurrentItem().hasItemMeta()){
                    player.closeInventory();
                }else{
                    if(event.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§dAssault")){
                        player.closeInventory();
                        EquipPlayer.equipAssaultClass(player);
                    }
                    if(event.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§eArcher")){
                        player.closeInventory();
                        EquipPlayer.equipArcherClass(player);
                    }  
                    if(event.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§cStormer")){
                        player.closeInventory();
                        EquipPlayer.equipStormerClass(player);
                    }
                    if(event.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§6Pyro")){
                        player.closeInventory();
                        if(player.hasPermission("matrixffa.pyro")){
                            EquipPlayer.equipPyroClass(player);
                        }else{
                            player.sendMessage("§5[Warfare] §3You need to buy a premium rank to unlock this class. Visit §ahttp://thematrixmc.buycraft.net §3to unlock.");
                        }
                    }
                    if(event.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§bTank")){
                        player.closeInventory();
                        if(player.hasPermission("matrixffa.tank")){
                            EquipPlayer.equipPyroClass(player);
                        }else{
                            player.sendMessage("§5[Warfare] §3You need to buy a premium rank to unlock this class. Visit §ahttp://thematrixmc.buycraft.net §3to unlock.");
                        }
                    }
                    if(event.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§aWarrior")){
                        player.closeInventory();
                        if(player.hasPermission("matrixffa.warrior")){
                            EquipPlayer.equipPyroClass(player);
                        }else{
                            player.sendMessage("§5[Warfare] §3You need to buy a premium rank to unlock this class. Visit §ahttp://thematrixmc.buycraft.net §3to unlock.");
                        }
                    }
                }
            }
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            Action action = event.getAction();
            ItemStack item = event.getItem();
            if(action != Action.RIGHT_CLICK_AIR || action != Action.RIGHT_CLICK_BLOCK){
                return;
            }
            if(item.getType() == Material.COMPASS){
                return;          
            }    
            if(ChatColor.stripColor(item.getItemMeta().getDisplayName()).equalsIgnoreCase("Class Selector")){
                event.setCancelled(true);
                ClassCompass.openClassInventory(inv);
            }
        }
        }
     
  17. Updated:
    This Code:
    Code (Text):
    if(event.getAction() == Action.RIGHT_CLICK_BLOCK | event.getAction() == Action.RIGHT_CLICK_AIR){
     

  18. You do not assign the inventory variable anywhere? and you still have the || operator error which will always fail to execute.

    Also, you needed to change the '||' operator to '&&' to get the desired functionality.
     
  19. But How do I Fix This:
    [​IMG]
    inv cannot be resolved to a variable
     
  20. So I just copy and paste
    Code (Text):
    Inventory inv = event.getInventory();
    To the second event?
    Updated Code:
    Code (Text):
    if(event.getAction() == Action.RIGHT_CLICK_BLOCK | event.getAction() == Action.RIGHT_CLICK_AIR){