Adding item to player's inventory results in the contents of a list being edited?

Discussion in 'Plugin Development' started by HeyAwesomePeople, Jul 16, 2016.

Thread Status:
Not open for further replies.
  1. This is going to make no sense at all but maybe there is something really simple that I'm missing here.

    Basically, when i add an item in to the player's inventory, the same item I just added in to his inventory seems to also be added to a hashmap the item is in. Before I add the item to the player's inventory, the map contains the itemstack with the amount being one. After I add it in, it returns two for the amount. I have not done a SINGLE call to itemStack.setAmount(), I have run a search for those calls throughout my code and I only set the amount to one.

    http://pastebin.com/raw/2uNzfdFn - That is a pastebin of my debug codes. Shop Contents is LITERALLY right before i add the item, and Shop Contents After is LITERALLY right after I add the item. Notice how the emerald at first was one, but is now two? I have done no calls to change that. It's become two in the config now too, and you can see that the item was put into the itemstack just seconds BEFORE it changes in the map, and there was no other call to the config after the itemstack amount changed.

    Here is a part of my code:

    Code:
            if (e.getCurrentItem().equals(getBuyItem())) {
                if (Utils.canInventoryAcceptItem(this.item, p.getInventory(), sizeBeingHandled)) {
                    if (!plugin.priceCalculator.canPlayerAffordItem(p, this.item, sizeBeingHandled)) {
                        p.sendMessage(ChatColor.RED + "[PrimeShop] You cannot afford this item!");
                        return;
                    }
                    for (int x = 0; x < sizeBeingHandled; x++) {
                        Bukkit.broadcastMessage("Shop Contents: " + this.getLastInventory().getShop().getItems().toString());
                        p.getInventory().addItem(this.item);
                        Bukkit.broadcastMessage("Shop Contents After: " + this.getLastInventory().getShop().getItems().toString());
                        //p.getWorld().dropItemNaturally(p.getLocation(), this.item);
                    }
                    p.sendMessage(ChatColor.GREEN + "[PrimeShop] You just purchased " + sizeBeingHandled + " '" + Utils.getItemName(this.item) + "' for " + plugin.priceCalculator.chargePlayer(p, this.item, sizeBeingHandled) + " money.");
                    plugin.itemManager.addBuys(this.item, sizeBeingHandled);
                } else {
                    p.sendMessage(ChatColor.RED + "You do not have enough room in your inventory to do this!");
                }
                updateOpTags();
            }
    This code is a part of an InventoryClickEvent. This inventory click event listens for clicks on items inside of an inventory which acts as a ShopGUI. I have multiple inventories that are opened and closed depending on the page you are on. I figured it'd be the best way to handle it. There is also a hashmap of items which links items to a given item id.

    Code:
    private BiMap<ItemStack, Integer> items = HashBiMap.create();
    I am using a BidirectionalMap so that I can search it either way, by id to get itemstack and by itemstack to get integer. Here is the confusing part. This map is ONLY changed when I run one of these two methods:

    Code:
        public void addItem(ItemStack item, Shop shop) {
            Integer nextGoodInt = getNextFreeInt();
            if (!items.containsKey(item)) {
                Bukkit.broadcastMessage("Put item " + item + " into position " + nextGoodInt);
                items.put(item, nextGoodInt);
            }
            saveSingleStack(nextGoodInt, item);
            shop.addItem(item);
            recipes.put(item, Bukkit.getRecipesFor(item));
        }
    
        private void loadAllItemStacks() {
            if (plugin.itemsConfig.getConfig().contains("primeshop_items")) {
                items.clear();
                for (String str : plugin.itemsConfig.getConfig().getConfigurationSection("primeshop_items").getKeys(false)) {
                    if (str.equalsIgnoreCase("null")) continue;
                    int itemId = Integer.parseInt(str);
                    ItemStack itemStack = plugin.itemsConfig.getConfig().getItemStack("primeshop_items." + itemId + ".item_serial");
                    items.put(itemStack, itemId);
                    recipes.put(itemStack, Bukkit.getRecipesFor(itemStack));
                }
            }
            Logger.logSuccess("Loaded all ItemStacks.");
        }
    But after I make the call to add the item into the inventory of the player, THE MAP IS EDITED. I've put debugs everywhere that I edit the map, and everywhere that I save the config. There is no place in my code that saves the config or sets information to it when the player has an item added to his inventory. The map only changes on load and when I add a new item into the shop.

    If I add an item into the shop, it's amount is 1. If I immediatly go and try and buy it, the amount becomes 2. It doesn't go past two no matter how many times I try to buy it, and this is because once the amount becomes 2, the call to the config no longer works. It is VERY VERY strange. Any help would be so appreciated.

    All my code: https://drive.google.com/file/d/0B6WLJEBg9I0vVWJ0TjZZWWxIckU/view?usp=sharing

    (Don't know if I'm allowed to link to zips of my code, if not, please have a mod just edit it out)
     
Thread Status:
Not open for further replies.

Share This Page