Solved if player has one out of a list of items. take one

Discussion in 'Plugin Development' started by khave, Feb 1, 2014.

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

    khave

    Hello again.

    So basically there's a list of items and then if the player has some of the items then take ONLY one item out of that list. So like: If the list of items contains apple, sapling, coal. The player has said items. What I want it to do is then take only ONE of those items e.g take the apple and then leave the sapling and the coal. If the player then has a grass block and a piston then only take the piston and leave the grass block, but if he didn't have the piston then it should've taken the grass block.

    It's very hard to explain, but I hope some of you understand it ^^

    Thank you!

    Don't leave me hanging. Bump xC

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  2. To remove ALL apples IF the player has one:

    Code:java
    1. if (player.getInventory().contains(new ItemStack(Material.APPLE))){
    2. player.getInventory().remove(new ItemStack(Material.APPLE));
    3. }


    To only remove 1 of the several stacks of apples, either use the slot int, removing the first, or using Java Math Random to remove an apple in a random slot.
     
  3. Offline

    khave


    But what I want is to remove one item out of a list of items. I know how to remove the apple, but what if people have an apple, a bread and a cake. I want it to only remove one of those items, BUT if he didn't have an apple then it would remove one of the other items
     
  4. You could iterate over your list of those items, be it the item type of the actual item stack, check if the player has one of it in the inventory, remove it and then just exit the loop.
     
  5. Offline

    khave


    I understand your logic, but how would I go about doing that?
    Like this?:
    Code:java
    1. Material[] energy = { Material.SAPLING, Material.RED_ROSE, Material.CACTUS, Material.BROWN_MUSHROOM, Material.COAL , Material.YELLOW_FLOWER, Material.SUGAR_CANE, Material.SEEDS};
    2.  
    3. for(Material m : energy) {
    4. if(p.getInventory().contains(m)) {
    5.  
    6. }
    7. }


    ow would I go about stopping it when it finds one of said items?
     
  6. Offline

    khave


    Ooooh yea, sorry didn't think of that! Thanks

    EDIT:
    How come this doesn't work?

    Code:java
    1. public void takeEnergy(Player p){
    2. Material[] energy = { Material.SAPLING, Material.RED_ROSE, Material.CACTUS, Material.BROWN_MUSHROOM, Material.COAL , Material.YELLOW_FLOWER, Material.SUGAR_CANE, Material.SEEDS};
    3.  
    4. for(Material m : energy) {
    5. if(p.getInventory().contains(m)) {
    6. hasEnergy = true;
    7. break;
    8. }else{
    9. hasEnergy = false;
    10. }
    11. if(hasEnergy){
    12. p.getInventory().remove(m);
    13. }
    14. }
     
  7. Offline

    Engfr546



    Try


    PHP:
    public void takeEnergy(Player p){
    Material[] energy = { Material.SAPLINGMaterial.RED_ROSEMaterial.CACTUSMaterial.BROWN_MUSHROOMMaterial.COAL Material.YELLOW_FLOWERMaterial.SUGAR_CANEMaterial.SEEDS};
     
    for(
    Material m energy) {
    if(
    p.getInventory().contains(m)) {
    hasEnergy true;
    break;
    }else{
    hasEnergy false;
    }
    if(
    hasEnergy == true){
    p.getInventory().remove(m);
    }
    }
    but, this will remove all items (I think)
     
  8. Offline

    khave


    Nope it doesn't work :/ Thanks anyway.
     
  9. You're checking if hasEnergy is true inside the loop, however you immediately exit the loop when you set it to true. so either do the if content directly when you set hasEnergy or do it outside of the loop.
     
  10. Offline

    khave


    But if I put it outside the loop it doesn't know what Material m is. So how will I remove the item found?
     
  11. Then do it this way:
     
  12. Offline

    khave


    Sorry I don't quite get it :oops:

    Edit:
    You mean like this:
    Code:java
    1. public void takeEnergy(Player p){
    2. Material[] energy = { Material.SAPLING, Material.RED_ROSE, Material.CACTUS, Material.BROWN_MUSHROOM, Material.COAL , Material.YELLOW_FLOWER, Material.SUGAR_CANE, Material.SEEDS};
    3.  
    4. for(Material m : energy) {
    5. if(p.getInventory().contains(m)) {
    6. p.getInventory().remove(m);
    7. hasEnergy = true;
    8. break;
    9. }else{
    10. hasEnergy = false;
    11. }
    12. }
    13. }


    ?
     
  13. I guess that would work, just try it out.
     
  14. Offline

    khave


    Arg, it did not work. Maybe I should just give up doing it this way.

    Edit:
    It got it to work, but it takes the whole stack of the item. Is there anyway for it to only take perhaps one?
     
  15. When the amount is 1 still remove it, else just set the amount one less.
     
  16. Offline

    khave

    Sorry for being a bit thick headed, but could you explain that again, or give me an example?
     
  17. Offline

    khave


    Was it this you were thinking?:
    Code:java
    1. for(Material m : energy) {
    2. if(p.getInventory().contains(m)) {
    3. hasEnergy = true;
    4. ItemStack is = new ItemStack(m);
    5. if(is.getAmount() == 1){
    6. p.getInventory().remove(is);
    7. }else{
    8. p.getInventory().remove(is.getAmount() - 1);
    9. }
    10. p.updateInventory();
    11. break;
    12. }else{
    13. hasEnergy = false;
    14. }
    15. }


    That doesn't seem to work, though.
     
  18. Not exactly, since when you create a new instance of the itemstack it won't affect the itemstack which is currently in your inventory. I would prefer looping though all the inventory items and for each item check if it's one of your materials, if so, change the amount or remove it (because of this, you're better of using an iterator).
     
  19. Offline

    khave


    Yea, now I'm completely lost. Sorry xC
     
  20. Code:
    iterator = inventory.iterator
    while iterator.hasNext
    {
      item = iterator.next
      found = false
      foreach type in myMaterials
      {
          is item.ofType type ?
          {
              found = true
              is item.amount == 1 ?
                  iterator.remove
              otherwise
                  item.amount = item.amount - 1
              break
           }
         
          is found ?
              break
      }
    }
     
  21. Offline

    khave


    Code:java
    1. Iterator i = p.getInventory().iterator();
    2. while(i.hasNext()){
    3. ItemStack mat = (ItemStack) i.next();
    4. //For each type? What
    5. //hasEnergy = true;
    6. if(mat.getAmount() == 1){
    7. i.remove();
    8. }else{
    9. p.getInventory().remove(mat.getAmount() - 1);
    10. break;
    11. }
    12. if(hasEnergy == true){
    13. break;
    14. }
    15. }


    This is what I could get out of it. Some things I'm a bit confused by. Is it right? And what's the for Each type thing?
    Also I really appreciate that you want to go through all this with me! I'm very grateful.
     
  22. Foreach is well.. for each. So for each x in a collection/array do the given thing.
     
  23. Offline

    khave

    Eeeeeh, what? You mean a for(mat.getType()){ ??
     
  24. for(Material mat : yourmaterials) this is technically a foreach.
     
  25. Offline

    khave


    Code:java
    1. public void takeEnergy(Player p){
    2. Material[] energy = { Material.SAPLING, Material.RED_ROSE, Material.CACTUS, Material.BROWN_MUSHROOM, Material.COAL , Material.YELLOW_FLOWER, Material.SUGAR_CANE, Material.SEEDS};
    3. hasEnergy = false;
    4. Iterator i = p.getInventory().iterator();
    5. while(i.hasNext()){
    6. ItemStack mat = (ItemStack) i.next();
    7. for(Material ma: energy){
    8. hasEnergy = true;
    9. if(mat.getAmount() == 1){
    10. i.remove();
    11. }else{
    12. p.getInventory().remove(mat.getAmount() - 1);
    13. break;
    14. }
    15. }
    16. if(hasEnergy == true){
    17. break;
    18. }
    19. }
    20. }


    Sorry for answering late, but this doesn't seem to work :(
     
  26. Because you forgot something, because now it sets 'hasEngergy' true, without checking the type/material of it.
     
  27. Offline

    khave

    Code:
        public void takeEnergy(Player p){
            Material[] energy = { Material.SAPLING, Material.RED_ROSE, Material.CACTUS, Material.BROWN_MUSHROOM, Material.COAL , Material.YELLOW_FLOWER, Material.SUGAR_CANE, Material.SEEDS};
            hasEnergy = false;
            Iterator i = p.getInventory().iterator();
            while(i.hasNext()){
                ItemStack mat = (ItemStack) i.next();
                for(Material ma: energy){
                if(mat.getAmount() == 1){
                    hasEnergy = true; //This
                    i.remove();
                }else{
                    hasEnergy = true; //This
                  p.getInventory().remove(mat.getAmount() - 1);
                  break;
                }
                }
                if(hasEnergy == true){
                    break;
                }
            }
        }
    Tried to do this, but it didn't work D:
     
  28. Nononono. Why are you getting the material amount?
    Think about what you want. You want to check if the current itemstack has one of the given materials. The check for the amount comes later when you actually know that it is of one of your materials.
     
Thread Status:
Not open for further replies.

Share This Page