Problem remove items

Discussion in 'Plugin Development' started by Fish28, Jun 24, 2019.

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

    Fish28

    Hello,

    I am currently doing a shop plugin. When you click on the mouse wheel in an inventory of the shop on an item (pa example of the stone) it must remove all the blocks of stone that the player has in his inventory. The problem is that if he has an item / block (in this case the stone), in different slots, when I click on the stone in the shop inventory, it will remove the stone than a single slot instead of all the slots containing the stone. My code: https://pastebin.com/4rGDaUc3

    Thank you (sorry I use a translator)
     
  2. Offline

    Kars

    You have a break; inside your if statement. This means when a match is found, it will remove the match and stop the loop. This is why it only removes one. Remove the break.
     
  3. Offline

    Fish28

    Thank you but the message "§aVous avez effectué la vente avec succès !" is sent to the player as many times as there is slot with the item. I would like this message to be sent once

    I noticed another problem: For example if I have stacks of dirt in different slots and that in the inventory of shop I click on the dirt with the wheel of the mouse, it must give me money in function from what I sold. But in my example, it will give me the money of 64 * dirt price + 128 * dirt price instead of 64 * dirt price + 64 * dirt price because I have 64 dirt blocks in 2 different slots

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 24, 2019
  4. Offline

    KarimAKL

    Move the message outside the loop.
    I would guess that happens because your variable 'count' contains the amount of items from the last found ItemStack. Try changing 'count * price' to 'item.getAmount() * price'.
     
  5. Offline

    Fish28

    Thank you it works!

    I also made sure that when we click right on an item in the inventory of shop, it asks him in the chat how much the amount he wants this item. I still have the same problem: when the player wants to sell for example 1 stone and he has 1 stack of stone in 2 different slots, it will remove 1 stone from each slot containing stone instead of 1 stone for the total of the stone he has in his inventory (in this case, 1 stone out of the 128 of his inventory). My code: https://pastebin.com/EpszxGrH

    Thank you !

    Help ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 25, 2019
  6. Offline

    KarimAKL

    @Fish28 Try adding something like this:
    Code:Java
    1. //Outside the loop
    2. int toRemove = /*the amount of items you want to remove*/;
    3. int removed = 0;
    4. // Inside the loop
    5. if (removed < toRemove) {
    6. // Remove item
    7. removed++;
    8. } else break;

    That should check if it should remove any items, if it shouldn't remove any more items then it breaks out of the loop.
    I just came up with this on the spot so there is probably a better way. :p
     
  7. Offline

    Fish28

    Thanks but the amount of items I want to remove is in the loop
    Code:
    for (int slot: matches.keySet ()) {
    so I can't put the variables : "toRemove" and "removed" in my code?
     
  8. Offline

    KarimAKL

    @Fish28 Then loop the inventory for items to remove. :7
    Code:Java
    1. int toRemove = 0;
    2. for (inventory.getContents()) {
    3. if (item == null) continue;
    4. if (item.getType() != Material./*material to sell*/) continue;
    5. toRemove += item.getAmount();
    6. }
    7. int removed = 0;
    8. // Do your thing here
     
  9. Offline

    Fish28

    Yeah but to remove the blocks I have to be in the loop

    Code:
    for(ItemStack items : main.inventoryClick.sold.get(p).keySet()) {
                                           
                                                for (ItemStack item : p.getInventory().getContents()) {
                                                   
                                                    if (item == null) continue;
                                                   
                                                    if (item.getType() != items.getType() && item.getData().getData() != items.getData().getData()) continue;
                                                   
                                                    toRemove += item.getAmount();
                                                   
                                                }
                                           
                                            }
                                           
                                            int removed = 0;
                                           
                                            if (removed < toRemove) {
    
                                                p.sendMessage(main.prefix + "§aVous avez effectué la vente avec succès !");
    
                                               
                                               
                                                removed++;
                                           
                                            } else break;
     
  10. Offline

    KarimAKL

    @Fish28 Move the inventory loop and 'removed' variable outside your other loop.
     
  11. Offline

    Fish28

    But if I move the inventory loop out of the other loop, I will not be able to retrieve "items"
     
  12. Offline

    KarimAKL

    @Fish28 Ah, i didn't notice you got the material from 'items', my bad. :7
    Is the the material of all the itemstacks in the map the same? If so, why do you get the material from that? Isn't that the material you want to remove?
     
Thread Status:
Not open for further replies.

Share This Page