Taking items off players! :)

Discussion in 'Plugin Development' started by NeoSilky, Sep 20, 2011.

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

    NeoSilky

    So, me and another guy have spent days doing a plugin, and we're nearly done! But i have a slight bug, when i use this line of code:
    Code:
            if(player.getInventory().contains(Material.getMaterial(5), 4)){
    It doesnt take the amount off the player, it just checks it, but if i use this code for example:
    Code:
            if(player.getInventory().contains(Material.COBBLESTONE, 8)){
    it works fine, and take the amount off the player (in this case, 8)

    So does anyone know how to fix this? Some materials do not have a written version of themselves so i have to rely on the material id :)

    Thanks!
     
  2. Offline

    DragonSoulSong

    It's not the "if" that's taking the items, but what's inside of it, right? I feel like what's inside the "if" might be the problem, not the "if" itself, but I can't tell from just the "if".
     
  3. Offline

    NeoSilky

    okay, so heres the section for the code:)
    Code:
        if (args[0].equalsIgnoreCase("woodplanks")) {
            if(player.getInventory().contains(Material.getMaterial(17), 1)){
            player.getInventory().remove(new ItemStack(Material.getMaterial(17), 1));
            player.getInventory().addItem(new ItemStack(Material.getMaterial(5), 4));
            player.sendMessage(ChatColor.BLUE + "[AutoItem] 4 WoodPlanks exchanged for 1 Wood.");
            } else { player.sendMessage("You do not have the required items!");
            }
            return true;
          }
    Others work if i specify the item with it's name, not ID :)
     
  4. Offline

    Nitnelave

    Does it work of you have a stack of more than one log? I think it checks for an exact match of the stack you said; exactly 1 log or 8 cobble.
     
  5. Offline

    NeoSilky

    @Nitnelave damn :/ and no, i have one stack of 64 :)
     
  6. Offline

    Nitnelave

    Just a question : you're aware that Material.COBBELSTONE is different than Material.getMaterial(5), aren't you? For the simple reason that cobblestone is 4, and 5 is wood planks?
     
  7. Offline

    NeoSilky

    yes, i know, i just couldn't be arsed to find the same quote bit, but its the same principle :p the thing is, it would be much easier if wooden planks was in the materials list, i looked, i can't find WOODEN_PLANK at all :/
     
  8. Offline

    Acrobot

    @NeoSilky
    It's actually "WOOD"
    And wood, as in log, is LOG
     
  9. Offline

    Nitnelave

    Ok, could you show us a bit of code you know isn't working, with a description of what it should do and what it does, and a piece that is wotking?

    Oh, and when I said that it checked for the exact amount, I meant for the remove part. If you have a stack of 64 and do remove (8), are you sure it works?

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

    NeoSilky

    Oh really? :D i didn't know that. That solves a lot of my problems, but some still doesn't work :(

    well that chunk of code i supplied earlier, it doesn't work exactly, basically i want it so that on a command, it would check the inventory, then if the materials are there, remove the correct amount then replace it with the item. It all works except for the ones that use a ID number instead of the name. Im not on my laptop atm, but because some arent there, i resorted to item ID's :)

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

    Nitnelave

    Well, you could always resort to using only IDs, even if the code is that much harder to read, but are you sure you're using the rigbt name/id?
     
  12. Offline

    NeoSilky

    is this line right?
    Code:
    player.getInventory().remove(new ItemStack(Material.getMaterial(17), 1))
    Is the Material.getMaterial the right phrase to use? :S
     
  13. Offline

    Nitnelave

    Sure, bit as I said, it will try to remove a stack of EXACTLY 1 log. Unless I'm wrong, you should find a stack that has at least 1 log, remove it then add a stack with one less in it.
     
  14. Offline

    NeoSilky

    Ahhh, makes sense, i'll look tomorrow :D thanks!

    Yes, you were entirely right btw, i'll try and figure it out tonight :)

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

    Celeixen

    I had a similar problem you want to use inventory.removeitem(itemstack)

    Inventory.remove only works if it matches the exact stack.
     
  16. Offline

    NeoSilky

    So would
    player.getInventory().removeItem(new ItemStack(Material.getMaterial(idhere), 5)));

    Work? :)
     
  17. Offline

    Nitnelave

    No.unless you are sure he has a stack of exactly 5. What I did in a plugin was to cycle through his inventory, count the number of the item type, remove them all, then add the difference. You'd have to check for data values, then. Or you could cycle through the inventory, find a big enough stack, remove it and add back the rest.
     
  18. Offline

    NeoSilky

    How would i find the biggest enough then remove it and add the difference? :/

    EDIT: it seems to work with the last guys post, even if there is more than one itemstack of the item :)
     
  19. Offline

    desht

    Take a look at https://github.com/desht/ScrollingM...me/desht/scrollingmenusign/CommandParser.java, in particular the chargeItems() method. (I've wrapped the item cost in a small Cost class there, but it should be pretty clear how to use it - just replace the getId(), getData() and getQuantity() calls with your item ID, data & quantity - or feel free to use the Cost class too). This method works regardless of how all your items are stacked.

    PHP:
    // try to remove 10 cobblestone from the player
    chargeItems(player, new Cost("4,10"));
    // try to remove 1 red wool
    chargeItems(player, new Cost("35:1,1");
    It doesn't check if the player has enough items but you can use the playerCanAfford() method in the same class to do that. The grantItems() method might also be useful, if you need to do the reverse and give the player some items.

    I adapted this code from CommandSigns, so credit must go to Edward Hand and/or Fluff for it.
     
  20. Offline

    NeoSilky

    It seems to work atm, thanks :D
     
  21. Offline

    Celeixen

    I though it would :)
     
Thread Status:
Not open for further replies.

Share This Page