Get GUI from other class

Discussion in 'Spigot Plugin Development' started by ChikenGamer, Jun 22, 2015.

  1. Hi i try to get a GUI from another class of my plugin.
    Error:
    PHP:
    [16:07:38 ERROR]: Could not pass event InventoryClickEvent to AdvancedHubPlus v1
    .0.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :1603) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31)
    [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9)
    [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_31]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spig
    ot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    18) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    67) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    57) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :560) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.NullPointerException
            at com.jimdo.choscar.Principal.GADGETS.GUI.InventoryClickEvent(GUI.java:
    135) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _31]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _31]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_31]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot1_8.jar:git-Spigot-044d928-e8c6403]
            ... 15 more
    Main:
    PHP:
    public class AdvancedHubPlusMain extends JavaPlugin implements Listener{
     
        public static Logger l = Logger.getLogger("Minecraft");
        PluginManager pm = Bukkit.getServer().getPluginManager();
     
         private GUI GUI;
         private Hats Hats;
     
        public void onEnable(){
            saveDefaultConfig();
         
            //pm.registerEvents(this, this);
            //pm.registerEvents(new Hats(this), this);
            pm.registerEvents(new Listeners(this), this);
         
            GUI = new GUI(this);
            Hats = new Hats(this);
         
         /////////////////////////////////////////////////////////////////////////////  
         l.log(Level.INFO, "=========[AdvancedHubPlus]=========");
         l.log(Level.INFO, " > Status: Enabled");
         l.log(Level.INFO, " > Version: " + getDescription().getVersion());
         l.log(Level.INFO, " > Author: " + getDescription().getAuthors());
         l.log(Level.INFO, " > Name: " + getDescription().getName());
         l.log(Level.INFO, "=======================");
         /////////////////////////////////////////////////////////////////////////////  
    }
        public void onDisable(){
        l.log(Level.INFO, "=========[AdvancedHubPlus]=========");
        l.log(Level.INFO, " > Status: Disabled");
        l.log(Level.INFO, " > Version: " + getDescription().getVersion());
        l.log(Level.INFO, " > Author: " + getDescription().getAuthors());
        l.log(Level.INFO, " > Name: " + getDescription().getName());
        l.log(Level.INFO, "=======================");
        saveConfig();
      }
    }
    ClassGUI1:
    PHP:
    public class GUI implements Listener{
     
        private Hats hats;
        private Inventory invGUI;
        private ItemStack a, b, c, d, e, f, g;
     
        public GUI(Plugin p) {//Code, events registered}
        private ItemStack createItem(Material material, int amount, short shrt, String displayname, String lore) {//Code }
            private ItemStack Skull(String skullOwner, String displayName, int quantity) {//Code }
           
            public void GUIM(Player p) {
                    p.openInventory(invGUI);
            }
            @EventHandler (priority = EventPriority.HIGH)
            public void InventoryClickEvent(InventoryClickEvent ev) {
                Player player = (Player)ev.getWhoClicked();
                PlayerInventory i = player.getInventory();
                    if (!ev.getInventory().getName().contains(invGUI.getName())) return;
                    if (ev.getCurrentItem() == null) return;
                    if (ev.getCurrentItem().getItemMeta() == null) return;
               
            if (ev.getCurrentItem().getItemMeta().getDisplayName().contains("Hats")) {
                    ev.setCancelled(true);
                    /////////////////////To-Do1////////////////////
                    player.playSound(player.getLocation(), Sound.LEVEL_UP, 1.0F, 0.0F);
                    //////////////////////////////////////////////
                    ev.getWhoClicked().closeInventory();
                    hats.hats1(player); //Line of the mistake
                    }
            }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {//Code }
        @EventHandler
            public void onPlayerJoin(final PlayerJoinEvent pje) {//Code }
            }
     
    }
    ClassGUI2:
    PHP:

    public class Hats implements Listener{
       
    private GUI GUI;
        private final Inventory invH, invH2, invH3, invH4, invH5;
        private final ItemStack a, b, c, d, e, f, g, h, ii, j, k, l, m, n, o, pp, q, r, s, t, u, al, ar, ad, ex,
                                a1, b1, c1, d1, e1, f1, g1, h1, ii1, j1, k1, l1, m1, n1, o1, pp1, q1, r1, s1, t1, u1,
                                a2, b2, c2, d2, e2, f2, g2, h2, ii2, j2, k2, l2, m2, n2, o2, pp2, q2, r2, s2, t2, u2,
                                a3, b3, c3, d3, e3, f3, g3, h3, ii3, j3, k3, l3, m3, n3, o3, pp3, q3, r3, s3, t3, u3,
                                a4, b4, c4, d4, e4, f4, g4, h4, ii4, j4, k4, l4, m4, n4, o4, pp4, q4, r4, s4, t4, u4;

     
        public Hats(Plugin p) {//Code, events registered}
            private ItemStack createItem(Material material, int amount, short shrt, String displayname, String lore) {//Code}
            private ItemStack Skull(String skullOwner, String displayName, int quantity) {//Code}
         
            public void hats1(Player p) {
                    p.openInventory(invH);
            }
            public void hats2(Player p) {
                    p.openInventory(invH2);
            }
            public void hats3(Player p) {
                    p.openInventory(invH3);
            }
            public void hats4(Player p) {
                    p.openInventory(invH4);
            }
            public void hats5(Player p) {
                    p.openInventory(invH5);
            }
     

    Note: All the "GUI" work well separately

    Thanks.
     
  2. what is on this line com.jimdo.choscar.Principal.GADGETS.GUI.InventoryClickEvent(GUI.java:
    135)

    also you really shouldn't have your method called InventoryClickEvent it should be more something like onInventoryClickEvent
     
  3. line 135: hats.hats1(player);//Line of the mistake
     
  4. I calle the method:
    public void hats1(Player p){
    p.openInventory(invH);
    }
    From the GUI1, and the error is it
     
  5. InventoryClickEvent or onInventoryClickEvent
    No matter
     
  6. from what i can see Hats never was initialized
     
  7. just the matter of staying in line with what is considered the standard for writing OOP
     
  8. How I can initialized?
    I use "private Hats hats;" to get the method
     
  9. the same way you did it in Main. And if your not sure what i mean by that then I would suggest you head to the Java Basics Tutorials because if you don't know what I mean then you don't really know OOP basics
     
  10. @ChikenGamer also add a null check to you InventoryClickEvent, if they click outside the inventory you will get a NullPointerException.
     
  11. But where in the code I do? if I use "GUI = new GUI (this);" I get an error
     
  12. I have the null checks
     
  13. I haven't looked at the code, but try and make it a
    Code (Text):
    void
    instead of a
    Code (Text):
    public void
    and then do like
    Code (Text):
    main.{Your GUI Name}(p);
     
  14. This goes back to my previous post, you need to learn more about what your actually doing before taking this on. At this point you are not even showing a clear understanding how constructors or objects work.
     
    • Agree Agree x 1
  15. I have no idea how to do it, but thank you, keep trying
     
  16. Like fearmig said,
    I'd suggest looking up some basic tutorials about Java OOP
     
  17. player.openInventory(class.Namegui)
     
  18. Just make a method returning your GUI Inventory:

    Code (Text):
    public Inventory getGUI(){
    return (guiname) (inventory);
    }