Solved InventoryClickEvent

Discussion in 'Plugin Development' started by CreeperShift, May 2, 2013.

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

    CreeperShift

    Hey, I'm currently trying to combine 2 items via a custom furnace inventory, however I can't seem to get the item to be displayed right.

    (eggINV is a HashMap which contains both the Location & the Inventory / checkitem(ItemStack, ItemStack); is a method which checks to which item they should be combined to, currently only returns an ItemStack of Apple for testing purposes)
    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
     
            Inventory openInv = event.getInventory();
     
            if (eggINV.containsValue(openInv) == true) {
     
     
                    if (openInv.getItem(0) != null && openInv.getItem(1) != null) {
     
                        openInv.setItem(2,
                                checkItems(openInv.getItem(0), openInv.getItem(1)));
     
                    }
     
                    if (openInv.getItem(2) != null && openInv.getItem(1) == null
                            || openInv.getItem(0) == null) {
                        openInv.setItem(2, null);
     
                    }
     
                    if (event.getSlot() == 2 && openInv.getItem(2) != null) {
     
                        openInv.setItem(0, null);
                        openInv.setItem(1, null);
     
                    }
                    ((Player) event.getWhoClicked()).sendMessage("Slot: "
                            + event.getSlot() + " and RawSlot: "
                            + event.getRawSlot());
     
                    final Player p = (Player) event.getWhoClicked();
                    this.getServer().getScheduler()
                            .scheduleSyncDelayedTask(this, new Runnable() {
                                @Override
                                public void run() {
     
                                    p.updateInventory();
                                }
                            }, 1);
     
             
            }
     
        }
    Now it currently works fine but displays wrong, look at the attached youtube video so you know what I mean:

    Explanation:
    Basically for testing purposes I can put any 2 items into the upper and lower slot, and it will create a 3rd item. However, it's invisible! (At 0:04 you can see that there is no item, yet when I click on it I have it in my hand)

    Now at 0:09 I place 2 items in it again and take out the lower item, now the 3rd item appears, but it's actually not there anymore. As soon as I place the lower item again, the 3rd item disappears even though it's there! Now if I place another item, it shows up.

    What the issues is, it's updating on the next click, even tho it should update when I put the item in :S.

    Put both items in, no update happens. When I remove one, it updates and shows the resulting item, even tho it's not there anymore and needs to be updated. Now when I click again, it shows no resulting item, even tho it's there, simply because it updated to the action which happened before.


    Edit: Solved it with a workaround, nowhere near perfect but it's not noticeable ingame.
    If anyone else is having the same issues, try getting/checking items a tick later:
    Code:
    if (event.getSlot() == 0 && openInv.getItem(1) != null
                            && openInv.getItem(0) == null) {
                        ((Player) event.getWhoClicked())
                                .sendMessage("Slot 2 was filled because 0 & 1 was there");
                        this.getServer().getScheduler()
                                .scheduleSyncDelayedTask(this, new Runnable() {
                                    @Override
                                    public void run() {
     
                                        openInv.setItem(
                                                2,
                                                checkItems(openInv.getItem(0),
                                                        openInv.getItem(1)));
                                    }
                                }, 1);
     
                    }
     
                    if (event.getSlot() == 1 && openInv.getItem(0) != null
                            && openInv.getItem(1) == null) {
                        ((Player) event.getWhoClicked())
                                .sendMessage("Slot 2 was filled because 0 & 1 was there");
                        this.getServer().getScheduler()
                                .scheduleSyncDelayedTask(this, new Runnable() {
                                    @Override
                                    public void run() {
     
                                        openInv.setItem(
                                                2,
                                                checkItems(openInv.getItem(0),
                                                        openInv.getItem(1)));
                                    }
                                }, 1);
     
                    }
     
                    if (event.getSlot() == 1 && openInv.getItem(0) != null
                            && openInv.getItem(1) != null) {
                        ((Player) event.getWhoClicked())
                                .sendMessage("Slot 2 was removed");
     
                        openInv.setItem(2, null);
                    }
     
                    if (event.getSlot() == 0 && openInv.getItem(1) != null
                            && openInv.getItem(0) != null) {
                        ((Player) event.getWhoClicked())
                                .sendMessage("Slot 2 was removed");
     
                        openInv.setItem(2, null);
                    }
     
                    if (event.getSlot() == 2 && openInv.getItem(2) != null) {
     
                        openInv.setItem(0, null);
                        openInv.setItem(1, null);
     
                        ((Player) event.getWhoClicked())
                                .sendMessage("Slots 0 & 1 were removed because 2 was taken out");
     
                    }
     
                    final Player p = (Player) event.getWhoClicked();
                    this.getServer().getScheduler()
                            .scheduleSyncDelayedTask(this, new Runnable() {
                                @Override
                                public void run() {
     
                                    p.updateInventory();
                                }
                            }, 1);
     
                }
            }
     
  2. Offline

    Malikk

    Why are you updating in a delayed task?

    Also, you should check the updateInventory actually works for furnaces, chests, etc. I've never used it outside of the player's own inventory, and there's a chance that there's a different way you have to update it for those types of inventories.

    If not, you could do some hackish things like moving items around yourself, and then moving them back to force it to update after the player has made their changes.
     
  3. Offline

    devilquak

    CreeperShift

    If everything works except the item showing up correctly, it could just be a problem on Bukkit's end. I've had trouble in the past with item updating as well, and the updateInventory method, I understand, has had a bumpy history.

    Try it without updating the inventory, then or without a delay, or even just a longer delay if nothing else works.

    Also, remember to debug so you know what is firing when it's supposed to.
     
  4. Offline

    CreeperShift

    Because from my experience with my older plugin, updating the inventory without a delay doesn't actually change anything :/. After a tick delay it does what I want (in case with my other plugin, fix shift-clicking custom recipes)


    Yeah that's the issue I'm having. I've searched for at least half an hour and could not find an answer to that. I'm really not sure it even has any effect on that inventory. But I have no idea how to update an inventory except the player one :S.

    How would I do it the hackish way? As in move items around? I'm not exactly sure what you mean by that :)

    Dang, I knew that the Inventory stuff was a bit wonky, but I didn't think it was this bad.

    But thanks for the reminder, I actually simply assumed it was a visual bug/glitch but never verified this through extensive debugging lines, could be that the complete task is one "click" delayed. Let me check that. However IF it is, how would I fix that :S
     
  5. Offline

    CreeperShift

    Did you even read? :O

    Okay I come to the conclusion that this event is gay.

    Basically I tried what devilquak suggested and put 234234 debug messages in it, switched around stuff like crazy and still nothing would work as I was getting inconsistent results. So now I simply went for:

    Code:
                    if (event.getSlot() == 0 || event.getSlot() == 1
                            || event.getSlot() == 2) {
     
                        ((Player) event.getWhoClicked()).sendMessage("Slot was: "
                                + event.getSlot() + ", CurrentItem was: "
                                + event.getCurrentItem() + ", Slot 0 was: "
                                + openInv.getItem(0) + ", Slot 1 was: "
                                + openInv.getItem(1) + ", Slot 2 was: "
                                + openInv.getItem(2));
                    }
    And guess what, when you put an item in,
    CurrentItem is null
    and the inventory.getItem is null

    -.-

    When you take it out, it shows which item was in it BEFORE. What sense does that make. Shouldn't it rather be showing the item in the slot when I put it in, and null when I take it out. As you can simply get the cursorItem for when you are getting it out of the slot.

    Now I really don't know what to do, because I NEED to know the item in the slot, and "null" isn't helping me in any way :(.

    I've worked around this, it's still stupid but it works flawless now. Solved.

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

Share This Page