Solved removeItem(new ItemStack) Method

Discussion in 'Plugin Development' started by JBAvodas, Aug 30, 2015.

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

    JBAvodas

    Hello, Please understand I am still learning JAVA and spoon feeding wont help me! Please try to explain your code a little, Many thanks!

    At this current stage I am starting my creation into a Guild RPG Plugin for my server, Now I am not looking to get advanced right now as I am stuck at the bottom of the mountain right now and it is only the smallest thing that is affecting my progression.

    I want to remove 4x64 of the item from the players inventory after checking that he has it, and if he doesn't have it then I want it tell them you can't buy a guild yet.

    My code so far:
    Code:
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            Player player = (Player) sender;
            if (commandLabel.equalsIgnoreCase("Guild")) {
                if (args.length < 1) {
                    sender.sendMessage("You need to use one of the following:");
                    sender.sendMessage("/guild create <name>");
                    sender.sendMessage("/guild invite <name>");
                    sender.sendMessage("/guild remove <name>");
                } else if (args[0].equalsIgnoreCase("create")
                        && player.getInventory().contains(new ItemStack(Material.GOLD_NUGGET, 64 * 4))) {
                    sender.sendMessage("You just created a Guild, It costed you 256 Golden Nuggets!");
                    player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64 * 4));
                } else {
                    sender.sendMessage("You can't afford a Guild yet! Please go get 256 Gold Nuggets!");
    
                    return true;
                }
            }
            return false;
        }
     
  2. @JBAvodas
    Because bukkit checks if the inventory contains the exact stack, and doesn't care about amounts. What you can do is:
    1) Create an int and set it to the amount you want (in this case, 64 * 4 = 256)
    2) Loop through the inventory's contents
    3) Check if the item is not null due to empty slots
    4) Check if the item's type is a gold nugget, then subtract the item's amount from the number created in step 1
    5) After the loop, check if the number from step 1 is smaller than or equal to 0, if it is, remove the items using Inventory#removeItem(ItemStack) as that doesn't care about amount.
     
  3. Offline

    JBAvodas

    So, Would I do the following below my public class create an int as follows:
    Code:
    int guildcost = 256;
    Then would I do the following:
    Code:
    for(int : guildcost)
    {
       player.getInventory().contains(new ItemStack(Material.GOLD_NUGGET);
    }
    and if that is true then:
    Code:
    sender.sendMessage("");
    
    but if it is false then
    Code:
    } else {
    sender.sendMessage("");
    
    return true;
    }
    am I on the right track? If not can you please give me an example
     
  4. @JBAvodas
    Please tahg me next time :/.
    What I explained should be enough.
     
  5. Offline

    Kyorax

    "for(int : guildcost)" will not work.
    You are not looping through the items.
     
  6. Offline

    JBAvodas

    @megamichiel
    I am very new to java coding and over the last week am still learning, I am not the sort who will go sit down and watch 100s of videos though and my best way of learning is to do it as I go.
    I don't full understand what looping through items meant could you please give me an example of how something like this should be laid out? Thanks!
     
  7. Offline

    caderape

    @JBAvodas
    if (inventory.containsAtLeast(itemstack, itemstack.getamount()) ) {
    inventory.removeItem(itemstack)
    }

    This is the best way for remove itemstack. If you have many stack to remove, do a loops. Im not sure you can set an amount of 400 and remove it like that.
     
  8. Offline

    JBAvodas

    @caderape
    I understand all of that cader its the looping part I am stuck on. Can you give me an example of how the loop should look?

    EDIT: I worked out how to do it using your containsAtLeast method! Thanks so much Cader! for anybody who would be wondering this is what I have ended up using:
    Code:
    } else if (args[0].equalsIgnoreCase("create")
                        && player.getInventory().containsAtLeast(new ItemStack(Material.GOLD_NUGGET), guildcost)) {
                    sender.sendMessage(ChatColor.GOLD + "You just created a Guild, It costed you a 256 Gold Nuggets!");
                    player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
                    player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
                    player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
                    player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
                }
     
    Last edited: Aug 31, 2015
  9. Offline

    caderape

    @JBAvodas
    for (int i = 0; i < StackNumber; i++)
    if (player.getinventoy.containsAtleast(itemstack, itemstack.getamount)
    removeitem(itemstack);
     
Thread Status:
Not open for further replies.

Share This Page