Solved Inventory Opening from another open inventory.

Discussion in 'Spigot Plugin Development' started by AquaticLabs, May 2, 2017.

  1. Code (Text):
     @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player p = ((Player) e).getPlayer();

            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Trails")) {
                e.setCancelled(true);

              (The Problem is here)
        //New Menu Show
         TrailSelector.show(p);
        }
    }
    When doing this I can't open the inventory because the player can't be referenced that way, Ive Tried OfflinePlayer as well with the same luck.

    The problem is when the Event "InventoryClickEvent" gets triggered It does not grab the player clicking. Any workarounds??

    Basically I want to Open another custom Inventory inside of a custom Inventory.
     
  2. Try

    Code (Text):
    Player p = e.getPlayer();
     
  3. You can't get Player in this Event I don't know why
     
  4. Oh I forgot it was
    Code (Text):
    Player p = (Player) e.getWhoClicked();
     
  5. TrailSelector.show(p); Cannot Reference that

    Code (Text):
        public void show(Player p) {
            p.openInventory(inv);
        }
     
  6. Get you a error in the console?
     
  7. This is at Line 19
    Code (Text):
    Player p = ((Player) e).getPlayer();

    [23:02:01 ERROR]: Could not pass event InventoryClickEvent to HubUtils v1.0

    org.bukkit.event.EventException

    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_121]

    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_121]

    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [Spigot.jar:git-Spigot-21fe707-e1ebe52]

    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]

    Caused by: java.lang.ClassCastException: org.bukkit.event.inventory.InventoryClickEvent cannot be cast to org.bukkit.entity.Player

    at me.extremesnow.Inventory.TrailorAuraItems.onInventoryClick(TrailorAuraItems.java:19) ~[?:?]

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_121]

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]

    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]

    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]

    ... 15 more


    also, Yes I'm extremesnow xD
     
  8. Replace that is on line 19 with
    Code (Text):
    Player p = (Player) e.getWhoClicked();
     
  9. Aight, now nothing happens, and no Error :p
     
  10. Try replacing
    Code (Text):
     e.setCancelled(true);

              (The Problem is here)
        //New Menu Show
         TrailSelector.show(p);
    With
    Code (Text):
     TrailSelector.show(p);
    e.setCancelled(true);
     
     
  11. Negative, If you want to know more how this works

    I have Items in the Hotbar and once you click them it opens an inventory (First Inventory)
    this inventory checks (do you want trail particles or aura particles)
    I have the 2 items checkin away yet once there clicked on now nothing happens
    (I have the next Inventories ready to open) they just apparently need to get called correctly


    EDIT: SOLVED
    Found my problem I was checking if the name of the inventory was (the wrong menus name)
     
  12. And if you add after
    Code (Text):
     if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Trails")) {
    This
    Code (Text):
    p.sendMessage("Opening second inventory");
    , do you get the message then or not
     
  13. Could everyone overcome ;) nice that it is fixed now