Problem/Bug Iventory GUI wardrobe help

Discussion in 'Plugin Help/Development/Requests' started by Hectorg98, Jun 21, 2015.

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

    Hectorg98

    Here is my code, my plugin yml is good, when i click diamond chestplate in game it opens up the menu with the items but when I click one of the items it closes the inventory and doesnt do anything help?

    Code:
    package me.Hectorg98.wardrobe;
    
    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.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class wardrobe extends JavaPlugin implements Listener {
       
        public void onEnable(){
            getServer().getPluginManager().registerEvents((Listener) this, this);
        }
       
        public void openGUI(Player player){
            Inventory inv = Bukkit.createInventory(null, 54, ChatColor.DARK_BLUE + "Wardrobe");
           
            ItemStack goldhelmet = new ItemStack(Material.GOLD_HELMET);
            ItemMeta goldhelmetMeta = goldhelmet.getItemMeta();
            ItemStack goldchestplate = new ItemStack(Material.GOLD_CHESTPLATE);
            ItemMeta goldchestplateMeta = goldchestplate.getItemMeta();
            ItemStack goldleggings = new ItemStack(Material.GOLD_LEGGINGS);
            ItemMeta goldleggingsMeta = goldleggings.getItemMeta();
            ItemStack goldboots = new ItemStack(Material.GOLD_BOOTS);
            ItemMeta goldbootsMeta = goldboots.getItemMeta();
            ItemStack diamondhelmet = new ItemStack(Material.DIAMOND_HELMET);
            ItemMeta diamondhelmetMeta = diamondhelmet.getItemMeta();
            ItemStack diamondchestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
            ItemMeta diamondchestplateMeta = diamondchestplate.getItemMeta();
            ItemStack diamondleggings = new ItemStack(Material.DIAMOND_LEGGINGS);
            ItemMeta diamondleggingsMeta = diamondleggings.getItemMeta();
            ItemStack diamondboots = new ItemStack(Material.DIAMOND_BOOTS);
            ItemMeta diamondbootsMeta = diamondboots.getItemMeta();
           
           
           
            goldhelmetMeta.setDisplayName(ChatColor.GOLD + "Gold Helmet");
            goldchestplateMeta.setDisplayName(ChatColor.GOLD + "Gold Chestplate");
            goldleggingsMeta.setDisplayName(ChatColor.GOLD + "Gold Leggins");
            goldbootsMeta.setDisplayName(ChatColor.GOLD + "Gold Boots");
            diamondhelmetMeta.setDisplayName(ChatColor.AQUA + "Diamond Helmet");
            diamondchestplateMeta.setDisplayName(ChatColor.AQUA + "Diamond Chestplate");
            diamondleggingsMeta.setDisplayName(ChatColor.AQUA + "Diamond Leggings");
            diamondbootsMeta.setDisplayName(ChatColor.AQUA + "Diamond Boots");
           
           
            inv.setItem(1, goldhelmet);
            inv.setItem(2, goldchestplate);
            inv.setItem(3, goldleggings);
            inv.setItem(4, goldboots);
            inv.setItem(5, diamondhelmet);
            inv.setItem(6, diamondchestplate);
            inv.setItem(7, diamondleggings);
            inv.setItem(8, diamondboots);
           
            player.openInventory(inv);
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if(!ChatColor.stripColor(event.getInventory().getName())
                    .equalsIgnoreCase("Wardrobe"))
                return;
            Player player = (Player) event.getWhoClicked();
            event.setCancelled(true);
           
            if(event.getCurrentItem() == null
                    || event.getCurrentItem().getType()==Material.AIR
                    ||!event.getCurrentItem().hasItemMeta()){
                player.closeInventory();
                return;}
    
            switch(event.getCurrentItem().getType()) {
            case GOLD_CHESTPLATE:
                if(!player.hasPermission("wardrobe.goldchestplate"));
                player.sendMessage(ChatColor.RED + "You do not have permission to wear this!");
            if(player.hasPermission("wardrobe.goldchestplate"));
                ItemStack goldchestplate = new ItemStack(Material.GOLD_CHESTPLATE);
                player.getInventory().setHelmet(goldchestplate);
            case GOLD_HELMET:
                if(!player.hasPermission("wardrobe.goldhelmet"));
                player.sendMessage(ChatColor.RED + "You do not have permission to wear this!");
            if(player.hasPermission("wardrobe.goldhelmet"));
                ItemStack goldhelmet = new ItemStack(Material.GOLD_HELMET);
                player.getInventory().setHelmet(goldhelmet);
            case GOLD_LEGGINGS:
                if(!player.hasPermission("wardrobe.goldleggings"));
                    player.sendMessage(ChatColor.RED + "You do not have permission to wear this!");
                if(player.hasPermission("wardrobe.goldleggings"));
                ItemStack goldleggings = new ItemStack(Material.GOLD_LEGGINGS);
                player.getInventory().setLeggings(goldleggings);
            case GOLD_BOOTS:
                if(!player.hasPermission("wardrobe.goldboots"));
                    player.sendMessage(ChatColor.RED + "You do not have permssion to wear this!");
                if(player.hasPermission("wardrobe.goldboots"));
                ItemStack goldboots = new ItemStack(Material.GOLD_BOOTS);
                player.getInventory().setBoots(goldboots);
            case DIAMOND_HELMET:
                if(!player.hasPermission("wardrobe.diamondhelmet"));
                    player.sendMessage(ChatColor.RED + "You do not have permission to wear this!");
                if(player.hasPermission("wardrobe.diamondhelmet"));
                ItemStack diamondhelmet = new ItemStack(Material.DIAMOND_HELMET);
                player.getInventory().setHelmet(diamondhelmet);
            case DIAMOND_CHESTPLATE:
                if(!player.hasPermission("wardrobe.diamondchestplate"));
                    player.sendMessage(ChatColor.RED + "You do not have permission to wear this!");
                if(player.hasPermission("wardrobe.diamondchestplate"));
                ItemStack diamondchestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
                player.getInventory().setChestplate(diamondchestplate);
            case DIAMOND_LEGGINGS:
                if(!player.hasPermission("wardrobe.diamondleggings"));
                    player.sendMessage(ChatColor.RED + "You do not have permssion to wear this!");
                if(player.hasPermission("wardrobe.diamondleggings"));
                ItemStack diamondleggings = new ItemStack(Material.DIAMOND_LEGGINGS);
                player.getInventory().setLeggings(diamondleggings);
            case DIAMOND_BOOTS:
                if(!player.hasPermission("wardrobe.diamondboots"));
                    player.sendMessage(ChatColor.RED + "You do not have permssion to wear this!");
                if(player.hasPermission("wardrobe.diamondboots"));
                ItemStack diamondboots = new ItemStack(Material.DIAMOND_BOOTS);
                player.getInventory().setBoots(diamondboots);
               
               
    
           
               
               
           
            }
           
            }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            event.getPlayer().getInventory()
                    .addItem(new ItemStack(Material.DIAMOND_CHESTPLATE));
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Action a = event.getAction();
            ItemStack is = event.getItem();
           
            if(a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
                return;
            if (is.getType() == Material.DIAMOND_CHESTPLATE)
                openGUI(event.getPlayer());
     
  2. Offline

    Boomer

    I think you need to call a playerInventoryUpdate method (I forget its exact name, but it is one that has been marked as depricated since like version 1.0.0 but an alternative was never produced) after setting the armor

    Edit:
    .updateInventory(); is a player-object method.
     
  3. Offline

    Hectorg98

  4. Offline

    Boomer

    okay first some simplification - since it will cutdown on expensive overhead time --
    If (!some condition)
    something here;
    if (some condition) {
    other
    stuff
    here
    }

    Is simply an if/else condition, "some condition" evaluated once - hasPermission() lookup - and you either do one or the other block of code depending on the result. That will simplify and reduce redundant work on all your methods. Except, your code isn't even actually doing that -- you have IF (somecondition); <The semicolin is important , as you have no command following the IF test before that semicolin, so... no command is processed, it simply moves to the next line.

    So be it true or false, you're going to be executing a lot of things redundantly.

    You also should {} off some of that logic, since as-is right now, if you dont have permissions for a given thing, yet click on that (ie, click on the diamondchestplate but dont have diamond chestplate permissions) its going to create a diamondchestplate no matter what, and apply it. IF for some reason I am wrong, and IF(blah); still executes only the next statement such as the sendMessage , then not having permissions will not create an itemstack object, yet, the next line would be executed without permissions, using an object that doesn't exist.

    Code:
            case DIAMOND_CHESTPLATE:
                if(!player.hasPermission("wardrobe.diamondchestplate"));
                    player.sendMessage(ChatColor.RED + "You do not have permission to wear this!");
                if(player.hasPermission("wardrobe.diamondchestplate"));
                ItemStack diamondchestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
                player.getInventory().setChestplate(diamondchestplate);
    
    It should be more like

    Code:
            case DIAMOND_CHESTPLATE:
                if(!player.hasPermission("wardrobe.diamondchestplate")){
                    player.sendMessage(ChatColor.RED + "You do not have permission to wear this!");
                } else {
                    ItemStack diamondchestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
                    player.getInventory().setChestplate(diamondchestplate);
                    player.updateInventory();
                }
    
    With such braced, simplified conditional structure, you can add additional debugging (sendMessage or logger etc statements) to confirm that it is actually inside of the block where it has created a new chestplate, and what the value of getChestplate() is after setting it to the diamond, etc

    You need to clean things up in order to make the logic better for being able to read the code, and to be able to do proper debugging points, in order to see where it is "properly" failing.
     
  5. Offline

    Hectorg98

    Ill be trying it soon @Boomer

    So, it doesnt work, when I click the diamond chestplate/ diamond leggings it doesnt equip anything and doesnt say anything even if I dont have the permssion @Boomer

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  6. Offline

    Boomer

    work backwards then, throw in debugging stuff to check if it makes it into that event, into a function, past a first IF statement... see where it isnt working -- just cause the client is bringing up what you expect, doesn't mean the server is registering it. You want to narrow it down to "I click this, i see this debug comment(s) to here... but not beyond this point here ..." then, that will narrow our focus

    ie.. does it trigger that CASE selection.
    back further, does it reach just prior to doing the selection
    back further, does it enter the function/method
     
Thread Status:
Not open for further replies.

Share This Page