Solved InventoryClickEvent remove item clicked

Discussion in 'Plugin Development' started by Serilum, May 3, 2013.

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

    Serilum

    Hiya, I've been coping with this problem and can't seem to find a solution to it. Basically I am trying to remove the item (or part of it) from the inventory of the player. This code will probably make more sense:

    Code:JAVA
    1.  
    2. @EventHandler
    3. public void onClick(InventoryClickEvent event) {
    4. event.setCanceled(true);
    5. if(event.isRightClick()) {
    6. int amount = 1;
    7. if(item.getDurability() > 0) {
    8. player.sendMessage(Message.Shop + "" + Color.Information + "Cannot sell damaged items.");
    9. return;
    10. }
    11. int id = item.getTypeId();
    12. Double price = sMcCombat.itemConfig.getDouble(id + ".price");
    13. if(price == 0) {
    14. player.sendMessage(Message.Shop + "" + Color.Information + "That item can currently not be sold!");
    15. return;
    16. }
    17. if(event.isShiftClick()) {
    18. amount = item.getAmount();
    19. }
    20. Account account = sMcCombat.getAPI().getAccount(player.getName());
    21. double total = amount*price;
    22. ItemStack ritem = new ItemStack(id, amount, item.getData().getData());
    23. player.getInventory().remove(ritem);
    24. player.updateInventory();
    25. account.deposit(total);
    26. String name = Util.itemName(item);
    27. player.sendMessage(Message.Shop + "" + Color.Information + "You have sold " + amount + " of " + name + " for " + total + ".");
    28. return;
    29. }
    30. }
    31.  

    Don't mind the specific code, as in Double price or Account account.

    If a player Shift-Clicks, it does work. But as soon as they right-click the item is "picked up" and removing it from the inventory is impossible. Any ideas on how to fix this?
     
  2. Offline

    MP5K

    Hello Serilum,
    try to check if the item is in the Invetory if yes reduce the amount by 1 of if the amount is 1 remove it
     
  3. Offline

    Serilum

    The problem is that whenever a player right-clicks the item it "splits" the stack, which is ofcourse default in minecraft. The 'event.setCanceled(true)' does work, but there is a delay and actually does split. Because of that I can't seem to reduce amounts or remove the item
     
  4. Try this if you already haven't used it
    Code:
    inventory.removeItem(new ItemStack (Mat, Int));
     
  5. Offline

    Serilum

    That works, but that will remove everything of that item. I can't actually set an amount of how many I want to remove, which is currently the problem.

    Basically the Shift-Click works with removing everything, but with the right-click I can't seem to change the ItemStack clicked.

    Oh hey, your edit worked. I think I'm able to work this out! Thanks.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  6. Serilum likes this.
  7. Offline

    Serilum

    Not exactly sure what you meant, but I removed that line and went with the:
    player.getInventory().removeItem(new ItemStack(id, amount));
    as you stated :)
     
    facepalm likes this.
Thread Status:
Not open for further replies.

Share This Page