Need help with my own API

Discussion in 'Spigot Plugin Help' started by MarkIsCool, Jun 15, 2018.

  1. This plugin isn't the greatest. I was just using it for testing. I believe I am getting an error everytime my API tries to access its own config. It sends an error once another plugin tries to call the method using my own API.

    Here is a picture of the error in the console:
    This is the error that I get when I join the server.
    http://prntscr.com/jvfhm0

    The error involves two plugins:

    Joiner Plugin
    Simply sends a message to the player once they join.
    Code (Text):

    package me.markiscool.Joiner;

    import org.bukkit.Bukkit;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.markiscool.MyAPI.JoinerAPI;

    public class JoinerMain extends JavaPlugin implements Listener {

        JoinerAPI core = (JoinerAPI) Bukkit.getServer().getPluginManager().getPlugin("MyAPI");
     
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this,this);
        }
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            core.welcomeMessage(e.getPlayer());
        }
     
    }
     
    MyAPI
    MyAPI plugin consists of two classes; the main class and the class that holds the method.

    MyAPIMain
    I don't believe the error is in this class as it is fairly simple, but I'm putting it here anyway.
    Code (Text):

    package me.markiscool.MyAPI;

    import org.bukkit.plugin.java.JavaPlugin;

    public class MyAPIMain extends JavaPlugin {
     
        @Override
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
     
    JoinerAPI
    The class in the "MyAPI" plugin which contains the methods meant to be used by my "Joiner" plugin.
    Code (Text):

    package me.markiscool.MyAPI;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;


    public class JoinerAPI {

        MyAPIMain plugin = MyAPIMain.getPlugin(MyAPIMain.class);
     
        public void welcomeMessage(Player player) {

            String configMessage = plugin.getConfig().getString("message");
            String splitConfigMessage[] = configMessage.split("*");
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', splitConfigMessage[0] + player.getName() + splitConfigMessage[1]));
         
        }
     
    }
     
    And lastly, just in case the error is something completely different, here is the default config.
    Code (Text):

    message: '&bWelcome &e* &bto the server!'
     
    This probably has a super easy fix. I'd appreciate any help :D
     
  2. You should probably set the instance of JoinerAPI (core) during the onEnable instead of directly on declaration. Chances are it's trying to initiate your API (through Bukkit) before the plugin even started up. Also be sure to add a dependency on the API in your plugin.yml to make sure the API plugin starts up before your plugin.

    Edit: Wait, JoinerAPI is not even a plugin? Why would you try loading it through the Bukkit plugin instance thingy then? :confused:
    Just use
    Code (Java):
    new JoinerAPI();
    to get an instance. Also, pass the plugin instance as a parameter to the API instead of telling the API to load a specific plugin, since that's not how an API works.
     
  3. I changed it up a bit. Like this?

    Code (Text):

    public class MyAPIMain extends JavaPlugin {
       
        public GameManager m;
       
        @Override
        public void onEnable() {
            m = new GameManager();
        }
    }
     
    Code (Text):

    package me.markiscool.MyAPI;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;

    public class GameManager {
        public void sendWelcomeMessage(Player player) {
            player.sendMessage("Welcome to the server, " + ChatColor.YELLOW + player.getName());
        }
    }
     
    Code (Text):

    package me.markiscool.Joiner;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.markiscool.MyAPI.MyAPIMain;

    public class JoinerMain extends JavaPlugin implements Listener {

        private MyAPIMain api = (MyAPIMain) Bukkit.getServer().getPluginManager().getPlugin("MyAPI");

        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            api.m.sendWelcomeMessage(player);
        }

    }