TokenEnchant null when returning tokens

Discussion in 'Spigot Plugin Development' started by Deagan, Sep 11, 2019 at 7:55 PM.

  1. So.. i am trying to check the players balance to see if they have enough for the enchant. but in the stacktrace its returning null on line 98 double balance = te.getTokens(player);

    I thought maybe it was because of this: Player player = (Player) event.getWhoClicked();
    but im not entirely sure.

    Here is the code that isn't working:
    Code (Java):

    @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
       
            ItemStack clicked = event.getCurrentItem();
            Inventory inventory = event.getInventory();
           
            double balance = te.getTokens(player);
             
            if (inventory.getName().equals(gui.getName())) {
                if(clicked.getType().equals(Material.DIAMOND)) {
                    event.setCancelled(true);
                   
                   
                    if(balance >= 500) {
                    te.enchant(player, player.getItemInHand() , "Fortune", +1, isEnabled(), 500, true);
                   
                   
                    }
                }
            }
     
     
  2. SteelPhoenix

    Junior Mod

    Presumably te is null
     
  3. Hm?

    Code (Java):

     public TokenEnchant getTokenEnchant() {
            Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("TokenEnchant");
            if ((plugin == null) || (!(plugin instanceof TokenEnchant))) {
                return null;
               }
            return (TokenEnchant)plugin;
        }
       
       
        TokenEnchant te = getTokenEnchant();
     
    It isn't i have this at the top of the class.
     
  4. How can you be sure it is not null? It literally has return null; in there if the plugin wasn't found or is not an instance of TokenEnchant. Has your plugin declared TokenEnchant an dependency in your plugin.yml? Only then it is guaranteed that it is loaded after TokenEnchant, so it may use that as dependency.

    Edit: Maybe an entire Stacktrace as well as your entire source (with correct line numbers, so please include imports in pastebin) would be useful