Getting inventory on close

Discussion in 'Spigot Plugin Development' started by Kyllian, Apr 15, 2017.

  1. Hey.

    I'm trying to get the inventory that the player closed. But I don't think this is possible because the inventory is closed already (i guess)

    This is my code (none of my debug messages are sent)
    Code (Text):
    package me.kyllian.minefly.listeners;

    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryCloseEvent;

    import me.kyllian.minefly.utils.AdGui;
    import me.kyllian.minefly.utils.Data;

    public class OnInventoryCloseEvent implements Listener {

        private AdGui ag = new AdGui();
        private Data data = Data.getInstance();

        @EventHandler(priority = EventPriority.HIGHEST)
        public void onInventoryClose(InventoryCloseEvent e) {
            if (e.getInventory().equals(ag.adGui)) {
                Bukkit.broadcastMessage("MATCG");
                if (e.getPlayer() instanceof Player) {
                    Player p = (Player) e.getPlayer();
                    UUID uuid = p.getUniqueId();
                    if (data.timeLeft.get(uuid) != -1 && Data.getData(p).toExecute != null) {
                        Bukkit.broadcastMessage("okeoekoekek");
                        p.openInventory(e.getInventory());
                    }
                }
            }
        }
    }
     
     
  2. Edit:

    This is the code now. But it keeps spamming errors + my debug messages. why?

    Code (Text):
    package me.kyllian.minefly.listeners;

    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryCloseEvent;

    import me.kyllian.minefly.utils.AdGui;
    import me.kyllian.minefly.utils.Data;

    public class OnInventoryCloseEvent implements Listener {

        private AdGui ag = AdGui.getInstance();
        private Data data = Data.getInstance();

        @EventHandler
        public void onInventoryClose(InventoryCloseEvent e) {
            if (e.getInventory().equals(ag.adGui)) {
                Bukkit.broadcastMessage("MATCG");
                if (e.getPlayer() instanceof Player) {
                    Player p = (Player) e.getPlayer();
                    UUID uuid = p.getUniqueId();
                    if (data.timeLeft.get(uuid) != -1 && Data.getData(p).toExecute != null) {
                        Bukkit.broadcastMessage("okeoekoekek");
                        p.openInventory(ag.adGui);
                        return;
                    }
                }
            }
        }
    }
     
     
  3. What are the errors?
     
  4. Its so large I cant even copy it. Probably a stackoverflow error or something. Ill look when I return to my pc
     
  5. It's probably the same error over and over again.

    I don't think this would work, but maybe try canceling the event?
    I would also look into whether the openInventory method calls the InventoryCloseEvent if there's already an open inventory.
     
  6. Strahan

    Benefactor

    Instead of reopening whilst in the close event, try using the scheduler.