Solved Inventory weirdness

Discussion in 'Spigot Plugin Development' started by torpkev, Apr 15, 2020.

  1. [SOLVED] - My kludgy workaround works and doesn't seem to be causing any issues.

    I'm trying to open an inventory I generate based on a PersistenDataContainer on an itemstack, in my example, leather pants.

    I have my ItemStack[] and can generate the Inventory no problem, but to open it, my plan was to right click on the leather pants - and that works, but when I close it, the leather pants are gone, but not really..

    Code (Java):
      //In InventoryClickEvent
     if (event.getClick() == ClickType.RIGHT) {
                if (event.getSlot() >= 36 && event.getSlot() <= 39) {
                    ItemStack itemStack = event.getClickedInventory().getItem(event.getSlot()).clone();
                    if (itemStack != null) {
                       // get my key, pass it to some code that populates the inventory
                      event.setCancelled(true);
                       player.openInventory(myinv.getInventory());
                    }
                }
       }
    Ok, so that works.. the inventory shows up when i right click the leather pants

    upload_2020-4-15_9-7-12.png

    upload_2020-4-15_9-7-24.png

    Then problem comes when I close the inventory, the pants are gone

    upload_2020-4-15_9-8-14.png

    But if I left click in the empty pants slot, they appear on my cursor and can be placed back in there

    upload_2020-4-15_9-8-30.png

    I've tried cancelling the event a bit earlier
    I tried using a clone() of the pants itemstack just in case I was unsetting something
    I've tried setting the cloned copy of the itemstack back into their place after showing the inventory

    None of that works and I'm going slightly bonkers with this, so any assistance would be greatly appreciated.
     
    #1 torpkev, Apr 15, 2020
    Last edited: Apr 15, 2020
  2. I have kind of a kludgy workaround in place that works, but if anyone knows a proper fix, please let me know.

    I added an ItemStack and integer to the code that handles my inventory and passed the slot and leather pants to it.
    Then in InventoryCloseEvent, I'm checking if it's my inventory, if so, I grab the itemstack and slot and then set them then - that seems to let the item stick around without the weirdness.
     
  3. when you "close" it? do you mean when you open the new inventory? if so, open the new inventory on the next tick.
     
  4. I close the inventory (e or esc or whatever)

    Then click e again on the player which opens up my inventory.. and pants are gone.

    If that makes more sense
     
  5. so to clarify, in your inventoryclickevent, you open a new inventory. are the pants still there after the new inventory opens? its only gone once you close the inventory and view your own?
     
  6. The pants were equipped to the armor slot. Once I open the new inventory, I don't see the armor slot, but once I close the new inventory and open my regular inventory again, the pants are missing from the armor slot, UNLESS i click in the empty slot, then they appear.

    Its really weird.
     
  7. interesting. likely the playerinventory gets desynced on the client. have you seen what my suggestion would do? delay opening the next inventory by a tick when you click the pants so that the client has a chance to understand that the event was cancelled?
     
  8. I haven't tried it, I had already put my little workaround in there - I'll give it a whirl in the morning when I get a minute.

    Appreciate your help!