Solved getting an item, amount, and short from inventory

Discussion in 'Plugin Development' started by tylerthecreeper1, Mar 4, 2016.

Thread Status:
Not open for further replies.
  1. I'm making a shop plugin, and I have this method to sell items to the shop..


    Code:
        // sell item
        public static void sellItem(Player p, Material mat, int amt, short s, int price) {
            ItemStack is = new ItemStack(mat,s);
            if(p.getInventory().contains(is,amt)) {
                p.getInventory().removeItem(is);
                p.updateInventory();
                CloudyShop.econ.depositPlayer(p, price);
                p.playSound(p.getLocation(), Sound.SUCCESSFUL_HIT, 5, 5);
                Utils.sendPlayerMsg(p, ChatColor.GREEN + "You sold " + ChatColor.YELLOW + amt + " " + mat + ChatColor.GREEN +
                        " for " + ChatColor.YELLOW + "$" + price + ChatColor.GREEN + "!");
            } else {
                Utils.sendPlayerMsg(p, ChatColor.RED + "You don't have enough " + mat + " to sell!");
                p.playSound(p.getLocation(), Sound.VILLAGER_NO, 5, 5);
            }
        }
    I'm trying right now to check for the data (short) on spruce wood, the method works in general but I added short to it so I can get the data for wood types, and I can't sell the item unless there's an itemstack of the wood type in my inventory with 10 as the amount. 10 is the amount I'm trying to sell the item in groups of.

    Any help on how I could get this to work would be greatly appreciated. Been stuck on this for about an hour, tried a bunch of different things.
     
  2. Offline

    Lordloss

    First, there is no constructor of ItemStack(Material, short), so i think it interpretes your ItemStack is =new ItemStack(mat,s); as (Material, amount). Use ItemStack(Material, amount, short) instead.

    This means in expample, if you tried to create an ItemStack with 1x wood data value 4, it creates instead an ItemStack with 4x wood data value 0. Maybe thats your problem :)
     
  3. Thanks, I believe I tried that already but just in case not, I'll try it. However the s is defined as short, so it shouldn't think it's the amount (it's not just an int)

    I still have this issue :/ It's only selling the items if the inventory contains an itemstack of 10 of that log type, because i check for the amount. When I have the 10 it will take 10 from all the stacks of logs in my inventory until i have less than 10 left.

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

    Lordloss

    @tylerthecreeper1
    "just" an int is maybe the wrong word for this, the constructor (Material, Amount) looks for an int. Short is smaller than that so it can be used as Integer without problems. Other way around it just works if you cast it to a short. And if your IDE dont throws any errors on that line, it does fit a constructor. And because there isnt a ItemStack(Material, short) which would it be?

    The next thing is, contains(ItemStack, amount) is not doing what you expect. What you are looking for is containsAtLeast(ItemStack, amount). This is because: "contains(ItemStack, amount) = Checks if the inventory contains at least the minimum amount specified of exactly matching ItemStacks. An ItemStack only counts if both the type and the amount of the stack match."
    containsAtLeast sums all ItemStacks in your inv up and looks if the amount is right. Also if you put them in many smaller ItemStacks.

    It removes all of the given ItemStacks because of "removeItem = It will try to remove 'as much as possible' from the types and amounts you give as arguments."

    For better understanding look at Inventory and ItemStack.
     
Thread Status:
Not open for further replies.

Share This Page