Solved Gui Issue

Discussion in 'Plugin Development' started by xXLightbulbXx, Aug 16, 2015.

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

    xXLightbulbXx

    I can't figure out why when i type /class and click the apple it doesn't do anything, Please help me.
    EDIT:
    Code:
    package xXLightbulbXx.Classes;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Classes extends JavaPlugin implements Listener{
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
            saveConfig();
        }
        public void onDisable(){
            saveConfig();
        }
       
        private void openGui(Player player){
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GRAY + "" + ChatColor.BOLD + "Classes");
           
            ItemStack Ninja = new ItemStack(Material.APPLE);
            ItemMeta NinjaMeta = Ninja.getItemMeta();
            NinjaMeta.setDisplayName(ChatColor.GRAY + "" + ChatColor.BOLD + "Ninja");
            Ninja.setItemMeta(NinjaMeta);
           
            inv.setItem(3, Ninja);
            player.openInventory(inv);
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = (Player)sender;
           
            if(label.equalsIgnoreCase("class")){
                if(getConfig().getString("player."+player.getName()) == null){
                    getConfig().set("player."+player.getName(), player.getName());
                    saveConfig();
                }else{
                    if(getConfig().getString("ninja."+player.getName()) != null){
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 1));
                        player.sendMessage(ChatColor.GREEN + "Your class has been activated!");
                    }else{
                        openGui(player);
                    }
                }
            }
            return true;
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Classes"))
                return;
                Player player = (Player) e.getWhoClicked();
                e.setCancelled(true);
               
                if(e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR || e.getCurrentItem().hasItemMeta()){
                    player.closeInventory();
                    return;
                }
               
                if(e.getCurrentItem().equals(Material.APPLE) && e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.stripColor("Ninja"))){
                   
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 1));
                    player.sendMessage(ChatColor.GREEN + "Your class has been activated!");
                    getConfig().set("ninja."+player.getName(), player.getName());
                    saveConfig();
            }
        }
    }
    
     
    Last edited: Aug 16, 2015
  2. Offline

    khave

    How about showing your code?
     
  3. Offline

    CraftCreeper6

    @xXLightbulbXx
    Let me just quickly read your mind....


    Oh wait, I can't.
     
  4. Offline

    xXLightbulbXx

    O xD I thought i posted code, sorry
     
  5. Offline

    CraftCreeper6

    @xXLightbulbXx
    Does the Apple have a coloured name?

    EDIT: The answer is yes, instead of ChatColor.stripColor(""); use ChatColor.GRAY + ChatColor.BOLD
     
  6. Offline

    xXLightbulbXx

    Last edited: Aug 16, 2015
  7. @xXLightbulbXx
    Code:
    e.getCurrentItem().equals(Material.APPLE)&& e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.stripColor("Ninja"))
    There are 2 mistakes in this...
    1) e.getCurrentItem() returns an ItemStack, and you're comparing it to a Material which will never return true, use ItemStack#getType() to get the Material of the item
    2) ChatColor.stripColor("Ninja") just returns "Ninja", call stripColor on the displayname, because I believe you want to strip the color of that
     
  8. Offline

    xXLightbulbXx

    I tried this but still nothing....
    Code:
    if(e.getCurrentItem().getType() == Material.APPLE){
                   
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 1));
                    player.sendMessage(ChatColor.GREEN + "Your class has been activated!");
                    getConfig().set("ninja."+player.getName(), player.getName());
                    saveConfig();
            }
     
  9. Offline

    xXLightbulbXx

    @megamichiel
    Code:
    package xXLightbulbXx.Classes;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Classes extends JavaPlugin implements Listener{
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
            saveConfig();
        }
        public void onDisable(){
            saveConfig();
        }
      
        private void openGui(Player player){
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GRAY + "" + ChatColor.BOLD + "Classes");
          
            ItemStack Ninja = new ItemStack(Material.APPLE);
            ItemMeta NinjaMeta = Ninja.getItemMeta();
            NinjaMeta.setDisplayName(ChatColor.GRAY + "" + ChatColor.BOLD + "Ninja");
            Ninja.setItemMeta(NinjaMeta);
          
            inv.setItem(3, Ninja);
            player.openInventory(inv);
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = (Player)sender;
          
            if(label.equalsIgnoreCase("class")){
                if(getConfig().getString("player."+player.getName()) == null){
                    getConfig().set("player."+player.getName(), player.getName());
                    saveConfig();
                }else{
                    if(getConfig().getString("ninja."+player.getName()) != null){
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 1));
                        player.sendMessage(ChatColor.GREEN + "Your class has been activated!");
                    }else{
                        openGui(player);
                    }
                }
            }
            return true;
        }
      
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
            if(!e.getInventory().getName().equalsIgnoreCase(ChatColor.GRAY + "" + ChatColor.BOLD + "Classes"))
                return;
                Player player = (Player) e.getWhoClicked();
                e.setCancelled(true);
              
                if(e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR || e.getCurrentItem().hasItemMeta()){
                    player.closeInventory();
                    return;
                }
              
                if(e.getCurrentItem().getType() == Material.APPLE){
                  
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 1));
                    player.sendMessage(ChatColor.GREEN + "Your class has been activated!");
                    getConfig().set("ninja."+player.getName(), player.getName());
                    saveConfig();
            }
        }
    }
     
  10. Offline

    caderape

    @xXLightbulbXx Did you do a debug for see if you code is called ?

    e.getCurrentItem().hasItemMeta() you close the inventory if the item has a displayname
     
    Xtreme727 likes this.
  11. Offline

    xXLightbulbXx

    Thanks @caderape it worked! I just removed that line of code e.getCurrentItem().hasItemMeta()
     
  12. Offline

    _Error

    Please mark as solved.
     
Thread Status:
Not open for further replies.

Share This Page