Solved Null GUI

Discussion in 'Plugin Development' started by user_91277742, Oct 2, 2018.

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

    user_91277742

    Hi there, so im trying to open a GUI when i do "p.openinventory(blabla(player))", the problem is that i have a null in that line. I have the GUI in another class and i have a constructor in all. Maybe i need to register the GUI in the main Class?

    I tried "p.openinventory(blabla()) and "inv.blabla(player);"
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    user_91277742

    Main
    Code:
    public class testGUI extends JavaPlugin{
     
        Plugin instance;
     
        public GUI inv;
         
        public File testFile = new File(this.getDataFolder() + "/alola.yml");
        public FileConfiguration testConfiguration = YamlConfiguration.loadConfiguration(testFile);
        @Override
        public void onEnable() {
            if(!(getDataFolder().exists())){
                getDataFolder().mkdir();
            }
            try {
                testConfiguration.save(testFile);
                testConfiguration.load(testFile);
            } catch (IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
            instance = this;
            getLogger().info("yoyo");
            registrarComandos();
            registrarEventos();
        }
     
        @Override
        public void onDisable() {
            getLogger().info("yaya");
        }
        public void registrarComandos() {
            getCommand("test").setExecutor(new Comandos(this,inv));
        }
        public void registrarEventos() {
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(new Eventos(this), this);
        }
    }
    
    GUI
    Code:
    public class GUI {
     
        testGUI main;
     
        public GUI(testGUI mainClass) {
            main = mainClass;
        }
     
        public Inventory abrirMenu(Player p) {
            ItemStack item0 = new ItemStack(Material.DIAMOND);
            ItemMeta meta0 = item0.getItemMeta();
            meta0.setDisplayName(ChatColor.AQUA + "Test");
            meta0.setLore(Arrays.asList("Another TEST"));
            item0.setItemMeta(meta0);
         
            Inventory inventario = Bukkit.createInventory(null, 9, ChatColor.AQUA + "yoyo");
            inventario.setItem(1, item0);
         
            return inventario;
         
        }
    }
    
    command
    Code:
    
        testGUI main;
        GUI inv;
        public Comandos(testGUI mainClass,GUI invClass) {
            main = mainClass;
            inv = invClass;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
                Player player = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("test")){
                    if(args.length == 1) {
                        String nombre = args[0];
                        if(nombre != player.getCustomName()) {
                            inv.abrirMenu(player);
                            player.sendMessage(ChatColor.GREEN + "Has abierto el para " + nombre);
                            return true;
                        }else {
                            player.sendMessage("no puedes castigarte a ti mismo aunk seas mazoquista");
                        }
                    }
                }
            }
            return false;
        }
    
    I have the null on "inv.abrirMenu(player);"
     
    Last edited by a moderator: Oct 4, 2018
  4. Offline

    KarimAKL

    @Ahrigumi My guess is that it's because you are not initializing the variable 'inv' in the main class.
     
  5. Offline

    user_91277742

    Yes! I was thinking about that but i dont reaaaally remember how to do that haha. In my last plugin i just create the GUI in the mainClass, but in this case is in another jeje
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    user_91277742

    ups, removed
     
  8. Offline

    timtower Administrator Administrator Moderator

    @Ahrigumi Initialize inv and you are done.
     
Thread Status:
Not open for further replies.

Share This Page