Solved .contains(ItemStack) not working if more than 1 item in a stack

Discussion in 'Plugin Development' started by bodhistrontg, Dec 22, 2013.

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

    bodhistrontg

    Code:java
    1. @EventHandler
    2. public void onWeaponUse(PlayerInteractEvent e){
    3. Player p = (Player) e.getPlayer();
    4. if(!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    5. if(!(e.getItem().equals(mpl))) return;
    6. if(!(p.getInventory().contains(a.smgAmmo))){
    7. p.sendMessage("No smgAmmo found!");
    8. return;
    9. }
    10. p.getInventory().remove(a.smgAmmo);
    11. e.getPlayer().launchProjectile(Egg.class);
    12. }

    a.smgAmmo is an ItemStack (Egg).
    Now it will do No smgAmmo found! if you have more than one egg in a stack if you spread them out it will shoot but it removes all of them how do can i fix that you can have multiple in a stack and it will only take one from that stack
    Lol thanks
     
  2. Offline

    Ronbo

    remove() in the Inventory class simply removes ALL items of a type (weird I suppose, but it has its uses).

    Try using
    removeItem(new ItemStack(Material.EGG, 1));
    instead.

    bodhistrontg
     
    bodhistrontg likes this.
  3. Offline

    bodhistrontg

    Do you know about .contains and why it doesnt work if there is multiple eggs in a stack? Thanks
     
  4. Offline

    xTigerRebornx

    bodhistrontg Probably because the itemstack that you are checking for is defined for a certain number?
     
  5. Offline

    bodhistrontg

    Yes so i should do p.getInventory().contains(new ItemStack(Material.EGG)); in stead of new ItemStack(Material.EGG, 1)
     
  6. Offline

    xTigerRebornx

  7. Offline

    Ronbo

    bodhistrontg
    There are many different versions of contains()
    By doing contains(a.smgAmmo), you are checking for the exact itemstack of size 1. Is that what you want to do? Nope. Think of it this way...
    1) You need to check if there are ANY ammos in the inventory
    2) Remove ONE of the ammos

    Thus, your first contains() could be changed to be contains(a.smgAmmo.getType()).
    This works because Inventory also has a contains(Material) method. You only need to check if the material exists at all, since if it exists there is at least one, which is all you need for a shot.

    Edit: You posted while I was typing this, but while the way you posted works, it isn't very flexible - by using contains(Material.EGG) you are restricting it to checking for eggs. If your ammo has a different type it wouldn't work. So, use a.smgAmmo.getType() instead :)
     
  8. Offline

    bodhistrontg

    Nope

    Well i have a diffrend Material for each type of ammo like smg is eggs pistols are snowballs...
    and
    This is in my Ammo class (a) and this is how i create my ammos should i do it diffrently
    Code:java
    1. package me.bodhiIsEpic.bukkit;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.inventory.ItemStack;
    8. import org.bukkit.inventory.meta.ItemMeta;
    9.  
    10.  
    11. public class Ammo {
    12. public ItemStack smgAmmo;
    13. public ItemStack pistolAmmo;
    14.  
    15. public void newAmmo()
    16. {
    17. smgAmmo = createItem(new ItemStack(Material.EGG, 1), ChatColor.AQUA + "SMG Ammo", "Ammo for all SMG type guns!");
    18. pistolAmmo = createItem(new ItemStack(Material.SNOW_BALL, 1), ChatColor.AQUA + "Pistol Ammo", "Ammo for all Pistol type guns!");
    19. }
    20.  
    21. private ItemStack createItem(ItemStack is, String name, String note) {
    22. ItemStack i = new ItemStack(is);
    23. ItemMeta im = i.getItemMeta();
    24. im.setDisplayName(name);
    25. im.setLore(Arrays.asList(new String[] { ChatColor.LIGHT_PURPLE + note }));
    26. i.setItemMeta(im);
    27. return i;
    28. }
    29.  
    30. Ammo(){
    31. newAmmo();
    32. }
    33.  
    34. }


    Lol nvm just got it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page