Solved Weird bugs with itemstacks in my plugin

Discussion in 'Plugin Development' started by stimoze, Dec 8, 2017.

Thread Status:
Not open for further replies.
  1. Hey! My plugin is based on another Bukkit plugin, i'm trying to expand the features of this plugin. (original idea: mrgeneralq)
    Basically this plugin can save your items when you die. (one of the item goes away but your other things will stay there)

    So, let's get to the point.

    Conception: You have an itemstack, which is a blazerod.
    Once you hit void level, one of the itemstack goes away.

    Problem:
    Well, it works, but only if you have one of the item.
    If you have multiple pieces of this, all your items go away.

    It always works perfectly if i have one of it.
    It sometimes works if i put it to the last inventory slot(the one after the last non-null slot)
    Code:
                                    if (allItem.isSimilar(saveRod))
                                    {
                                        if (e.getPlayer().getInventory().contains(saveRod)){
                                            e.getPlayer().setFallDistance(0);
                                            e.getPlayer().teleport(getLocation(lp.getWorld()));
                                            e.getPlayer().sendMessage(conv(getPrefix() + " " + getMessage()));
                                              if (allItem.getAmount() == 1)
                                              {
                                                e.getPlayer().getInventory().remove(saveRod);
                                              }
                                              else
                                              {
                                                allItem.setAmount(allItem.getAmount()-1);
                                              }
                                        }else{
                                            e.getPlayer().getInventory().clear();
                                            e.getPlayer().setFallDistance(0);
                                            e.getPlayer().teleport(getLocation(lp.getWorld()));
                                            e.getPlayer().sendMessage(conv(getPrefix() + " " + getDenyMessage()));
                                        }
                                   }
    ps: sorry for the messed up code piece
     
  2. Offline

    Zombie_Striker

    @stimoze
    There are multiple ways to do this. The easiest way to remove just one item would be the following:
    1. Create a for int loop. Start at 0 and move up to 36 (the max player inventory).
    2. Use Inventory#getItem(...) to get the item at that slot.
    3. If the itemstack is not null, and if the item is what you are looking for.
    4. Check the amount. If it is 1, just remove the itemstack.
    5. If it is more than one, set the itemstacks amount to be equal to itself -1. Then, use Inventory#set(slot, itemstack) to re-set the item. This will update that slot so it will now have one less item in it.
     
  3. what method should i use after getItem() ? equals or isSimilar?

    update: i was trying some other codes, thats the best i can get now

    Code:
                                  for (int i = 0; i < 36; i++)
                                  {
                                      Inventory inv = e.getPlayer().getInventory();
                                      ItemStack items = e.getPlayer().getInventory().getItem(i);
                                          if(items.equals(saveRod)){
                                              if(inv.contains(saveRod)){
                                                  if (saveRod.getAmount() == 1){
                                                      inv.remove(saveRod);
                                                      e.getPlayer().setFallDistance(0);
                                                       e.getPlayer().teleport(getLocation(lp.getWorld()));
                                                       e.getPlayer().sendMessage(conv(getPrefix() + " " + getMessage()));
                                                  }else{
                                                      saveRod.setAmount(saveRod.getAmount() - 1);
                                                      e.getPlayer().setFallDistance(0);
                                                       e.getPlayer().teleport(getLocation(lp.getWorld()));
                                                       e.getPlayer().sendMessage(conv(getPrefix() + " " + getMessage()));
                                                  }
                                              }else{
                                                     inv.clear();
                                                   e.getPlayer().setFallDistance(0);
                                                   e.getPlayer().teleport(getLocation(lp.getWorld()));
                                                   e.getPlayer().sendMessage(conv(getPrefix() + " " + getDenyMessage()));
                                                }
                                       }
                                  }    
     
    Last edited: Dec 8, 2017
  4. Offline

    Caderape2

    @stimoze Use the method removeItem(), not remove().
     
  5. Code:
                                for (ItemStack items : inv.getContents())
                                {
                                          if(items.isSimilar(saveRod)){
                                             inv.removeItem(saveRod);
                                             e.getPlayer().setFallDistance(0);
                                             e.getPlayer().teleport(getLocation(lp.getWorld()));
                                             e.getPlayer().sendMessage(conv(getPrefix() + " " + getMessage()));
                                          }else{
                                             inv.clear();
                                             e.getPlayer().setFallDistance(0);
                                             e.getPlayer().teleport(getLocation(lp.getWorld()));
                                             e.getPlayer().sendMessage(conv(getPrefix() + " " + getDenyMessage()));
                                          }
                                }
    
    Now if i have multiple of saveRod, it only removes 1, but ONLY WORKS if i have it in the first slot and no other items in my inventory.

    FINALLY! I got it working!
    Code:
                                          if(inv.containsAtLeast(saveRod, 1)){
                                             inv.removeItem(saveRod);
                                             e.getPlayer().setFallDistance(0);
                                             e.getPlayer().teleport(getLocation(lp.getWorld()));
                                             e.getPlayer().sendMessage(conv(getPrefix() + " " + getMessage()));
                                          }else{
                                             inv.clear();
                                             e.getPlayer().setFallDistance(0);
                                             e.getPlayer().teleport(getLocation(lp.getWorld()));
                                             e.getPlayer().sendMessage(conv(getPrefix() + " " + getDenyMessage()));
                                          }  
    this is the working code!

    had to use containsAtLeast() instead of contains()

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

Share This Page