Solved Problem to open a custom inventory

Discussion in 'Plugin Development' started by AntonioC94, Dec 8, 2019.

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

    AntonioC94

    Hi there!
    Im working on a teleport plugin with a custom GUI and more things.
    So the problem here is when i want to open the inventory without an static method.

    The problem when i want to open the GUI is a null, so i think the real problem its because the inventory class is not loaded on my onenable BUT im not really sure about how to solve this, because i have constructors on all my classes

    Here my onEnable()

    Code:
        Plugin instance;
    
        Events events;
    
        Inventory inv;
    
        Commands cmd;
    
        public void onEnable() {
            this.events = new Events(this, inv);
            this.inv = new Inventory(cmd, this);
            this.instance = (Plugin) this;
            getLogger().info("asdasdasd");
            registerEvents();
            loadConfiguration();
            registerCommands();
        }
    
     
  2. Offline

    DerDonut

    You're right. You pass inv, which is null, to Events. You have to declare it somewhere. Either in the main or in Events
     
  3. Offline

    KarimAKL

    @AntonioC94 Is that your own "Inventory" class? And as @DerDonut already stated; you are passing 'inv' before it's initialized, when you are initializing 'events'.
     
  4. Offline

    AntonioC94

    @DerDonut @KarimAKL thanks for your comments!
    So first to all, if i understand i need to initialize at first the inventory class?
    And yes, its my own inventory.
     
  5. Offline

    KarimAKL

  6. Offline

    AntonioC94

    @KarimAKL I tried to do that and i have the same error at the same line

    I will post my code

    Event class
    Code:
    public class Eventos implements Listener {
    
        PolvosTeleport main;
       
        Inventarios inv;
    
        public Eventos(PolvosTeleport mainClass, Inventarios invClass) {
            main = mainClass;
            inv = invClass;
        }
    
        @EventHandler
        public void bloque(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Block maceta = event.getClickedBlock();
            if (player != null) {
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    if (maceta.getType() == Material.FLOWER_POT) {
                        Block base = maceta.getRelative(BlockFace.DOWN);
                        if(base.getBlockData().getMaterial().equals(Material.BEDROCK)) {
                            player.sendMessage("Abriendo inventario");
                            inv.estaciones(player);                                                                             <--  ERROR HERE "Null"
                        }
                    }
                }
            }
        }
    }
    

    onEnable()
    Code:
    
        Plugin instance;
    
        Eventos events;
    
        Inventarios inv;
    
        Comandos cmd;
    
        public void onEnable() {
            this.inv = new Inventarios(cmd, this);
            this.events = new Eventos(this, inv);
            this.instance = (Plugin) this;
            getLogger().info("está activado y cargado");
            registrarEventos();
            cargarConfiguracion();
            registrarComandos();
        }
    
    Inventory class
    Code:
    public class Inventarios {
       
        Comandos cmd;
        PolvosTeleport main;
       
        public Inventarios(Comandos cmdClass, PolvosTeleport mainClass) {
            cmd = cmdClass;
            main = mainClass;
        }
       
        public void estaciones (Player player) {
            Inventory inventario = Bukkit.createInventory(null, 9 , ChatColor.DARK_RED + ("Bases"));       
            player.openInventory(inventario);
        }
    }
    
     
  7. Offline

    KarimAKL

    @AntonioC94 That looks correct to me, are you sure you didn't forget to update the jar file used to test it? (The error shouldn't happen on the same line if you swapped the lines)
     
  8. Offline

    DerDonut

    @AntonioC94 Could you show us the
    registrarEventos() method?
     
  9. Offline

    AntonioC94

    @KarimAKL @DerDonut Im sorry for the delay, finally the GUI works! , the problem was another imcompatibility plugin... really thanks!
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page