Solved Items being placed in inventory twice?

Discussion in 'Spigot Plugin Development' started by XixiPlayZ, Aug 17, 2016.

  1. Relevant Code, and Problem:

    The problem is: The inventory items, which are taken from the configuration, are being placed twice.
    Code (Text):
    package me.xixiplaysa.menus;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import me.xixiplaysa.mfshop.MFHHShop;
    import net.md_5.bungee.api.ChatColor;

    public class SpawnersMenu {

        private MFHHShop instance = MFHHShop.getPlugin(MFHHShop.class);
        private Inventory inv;
        private String title;
        private int slots;
        private List<ItemStack> matty;
        public SpawnersMenu(){
            this.matty = new ArrayList<ItemStack>();
            this.setTitle(ChatColor.translateAlternateColorCodes('&',MFHHShop.getPlugin(MFHHShop.class).getConfig().getString("Menu.title")));
            this.setSlots(instance.getConfig().getInt("Menu.slots"));
            this.inv = Bukkit.createInventory(null,slots,ChatColor.translateAlternateColorCodes('&',title));
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getSlots() {
            return slots;
        }

        public void setSlots(int slots) {
            this.slots = slots;
        }
        public Inventory getInventory(){
            return inv;
        }
        public List<ItemStack> convItemsFConfig(){
            for(String s : instance.getConfig().getConfigurationSection("Menu.Items").getKeys(false)){
               
                Material mat = Material.matchMaterial(s);
                ItemStack istack = new ItemStack(mat);
                ItemMeta iMeta = istack.getItemMeta();
                istack.setDurability((short)instance.getConfig().getInt("Menu.Items."+s+".Durability"));
                iMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&',instance.getConfig().getString("Menu.Items."+s+".Name")));
                iMeta.setLore(Arrays.asList(color("&7Price: &6"+instance.getConfig().getInt("Menu.Items."+s+".Price")),color("&7Level required: &6" + instance.getConfig().getInt("Menu.Items."+s+".Level"))));
                istack.setItemMeta(iMeta);
                matty.add(istack);
                   
                    }
            return matty;
        }
        public String color(String s){
            return ChatColor.translateAlternateColorCodes('&', s);
        }
        public void setItems(){
            if(convItemsFConfig().size() < instance.getConfig().getInt("Menu.slots")){
                for(ItemStack i : convItemsFConfig()){
                    if(!i.equals(null)){
                      getInventory().addItem(i);
                     
                    }
            }
            }
            else {
                System.out.println("Error in MFHHShop. The gui items list are higher than the inventory slot size, contact Terminating for assistance.");
            }
            }
    }
     
     
  2. Hard to make up the exact issue, but the only method in that code that actually adds items to an inventory is the SpawnersMenu#setItems() method as that contains the Inventory#addItem() method. Although the SpawnersMenu#setItems() method appears to be not implemented in the provided code by taking a quick look at it. Depending on what you mean with items being added double, check if the SpawnersMenu#setItems() method gets fired twice somewhere else in the code that you didn't provide here.

    Once again, I quickly scanned the code and I could be wrong.
     
  3. IF, what you're trying to say is the method isn't used here, that's because it's used here:

    Code (Text):
    SpawnersMenu menu = new SpawnersMenu();
               menu.setItems();
               player.openInventory(menu.getInventory());
     
  4. In setItems() you call convItemsFConfig() twice, try to call it once and then use getInventory() like this:
    Code (Text):

    //First change this to return void, it adds items into matty, you don't need to return it
    public void convItemsFConfig(){
            for(String s : instance.getConfig().getConfigurationSection("Menu.Items").getKeys(false)){
             
                Material mat = Material.matchMaterial(s);
                ItemStack istack = new ItemStack(mat);
                ItemMeta iMeta = istack.getItemMeta();
                istack.setDurability((short)instance.getConfig().getInt("Menu.Items."+s+".Durability"));
                iMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&',instance.getConfig().getString("Menu.Items."+s+".Name")));
                iMeta.setLore(Arrays.asList(color("&7Price: &6"+instance.getConfig().getInt("Menu.Items."+s+".Price")),color("&7Level required: &6" + instance.getConfig().getInt("Menu.Items."+s+".Level"))));
                istack.setItemMeta(iMeta);
                matty.add(istack);
                 
                    }
        }

    //Now call that method once and then access your list
    public void setItems(){
            convItemsFConfig();
            if(this.matty.size() < instance.getConfig().getInt("Menu.slots")){
                for(ItemStack i : this.matty()){
                    if(!i.equals(null)){
                      getInventory().addItem(i);
                   
                    }
            }
            }
            else {
                System.out.println("Error in MFHHShop. The gui items list are higher than the inventory slot size, contact Terminating for assistance.");
            }
    }


     
    Edit: to be sure you can add a matty.clear() at the begin of convItemsFConfig() method.
     
    • Agree Agree x 1
  5. You should also import ChatColor from org.bukkit instead of net.md_5, because it will result in an error on a non-spigot server.
     
  6. I agree
     
  7. It's a private plugin.
     
  8. Then it probably won't cause problems, but it's generally a bad practice to import it from net.md_5
     
    • Agree Agree x 1
  9. I know. I dont do that to public plugins though.
     
  10. It did not work.

    Now it's empty ;-;
     
  11. Can you post your new code?
     
  12. Code (Text):
    package me.xixiplaysa.menus;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import me.xixiplaysa.mfshop.MFHHShop;
    import net.md_5.bungee.api.ChatColor;

    public class SpawnersMenu {

        private MFHHShop instance = MFHHShop.getPlugin(MFHHShop.class);
        private Inventory inv;
        private String title;
        private int slots;
        private List<ItemStack> matty;
        public SpawnersMenu(){
            this.matty = new ArrayList<ItemStack>();
            this.setTitle(ChatColor.translateAlternateColorCodes('&',MFHHShop.getPlugin(MFHHShop.class).getConfig().getString("Menu.title")));
            this.setSlots(instance.getConfig().getInt("Menu.slots"));
            this.inv = Bukkit.createInventory(null,slots,ChatColor.translateAlternateColorCodes('&',title));
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getSlots() {
            return slots;
        }

        public void setSlots(int slots) {
            this.slots = slots;
        }
        public Inventory getInventory(){
            return inv;
        }
        public void convItemsFConfig(){
            matty.clear();
            for(String s : instance.getConfig().getConfigurationSection("Menu.Items").getKeys(false)){
             
                Material mat = Material.matchMaterial(s);
                ItemStack istack = new ItemStack(mat);
                ItemMeta iMeta = istack.getItemMeta();
                istack.setDurability((short)instance.getConfig().getInt("Menu.Items."+s+".Durability"));
                iMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&',instance.getConfig().getString("Menu.Items."+s+".Name")));
                iMeta.setLore(Arrays.asList(color("&7Price: &6"+instance.getConfig().getInt("Menu.Items."+s+".Price")),color("&7Level required: &6" + instance.getConfig().getInt("Menu.Items."+s+".Level"))));
                istack.setItemMeta(iMeta);
                matty.add(istack);
                    }
        }
        public String color(String s){
            return ChatColor.translateAlternateColorCodes('&', s);
        }
        public void setItems(){
            if(matty.size() < instance.getConfig().getInt("Menu.slots")){
                for(ItemStack i : matty){
                    if(!i.equals(null)){
                      getInventory().addItem(i);
                   
                    }
                 }
            }
            else {
                System.out.println("Error in MFHHShop. The gui items list are higher than the inventory slot size, contact Terminating for assistance.");
            }
            }
    }
     
     
  13. @XixiPlayZ now you didn't call convItemsFConfig()
     
    • Funny Funny x 1
  14. Haha, what a stupid move from me! Thank you!
     
  15. If it is solved mark this thread as solved.