Plugin wont load: Already initialized!

Discussion in 'Spigot Plugin Development' started by Skptical, Mar 14, 2020.

  1. Error message:

    Code (Text):
    [16:42:34 ERROR]: Could not load 'plugins\SparkCore.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at me.skptical.spark.main.SparkCore.<init>(SparkCore.java:12) ~[?:?]
            at me.skptical.spark.main.FileManager.<init>(FileManager.java:22) ~[?:?]
            at me.skptical.spark.api.SparkAPI.<init>(SparkAPI.java:13) ~[?:?]
            at me.skptical.spark.main.SparkCore.<init>(SparkCore.java:19) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_231]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_231]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_231]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 6 more
    Caused by: java.lang.IllegalStateException: Initial initialization
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at me.skptical.spark.main.SparkCore.<init>(SparkCore.java:12) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_231]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_231]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_231]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 6 more


    Main Class:


    Code (Java):
    package me.skptical.spark.main;


    import me.skptical.spark.api.SparkAPI;
    import me.skptical.spark.commands.Gamemode;
    import org.bukkit.Bukkit;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class SparkCore extends JavaPlugin implements Listener {


        private static SparkCore instance;
        private String version = this.getDescription().getVersion();
        private ConsoleCommandSender console = this.getServer().getConsoleSender();
        private PluginManager pm;
        private SparkAPI api = new SparkAPI();
        private Gamemode gamemode = new Gamemode();


        public static SparkCore getInstance(){
            return instance;
        }

        public static void setInstance(SparkCore inst){
             instance = inst;
        }

        public SparkCore(){}


        @Override
        public void onEnable(){
            if(Bukkit.getPluginManager().getPlugin("Vault") == null){
                this.getLogger().info("Could not find the plugin Vault shutting down plugin.");
                this.getPluginLoader().disablePlugin(this);
            }else {
                FileManager fileManager = new FileManager();
                fileManager.createMessages(true);
                fileManager.reloadMessages();
                fileManager.createPlayers(false);
                fileManager.reloadPlayers();
                fileManager.createConfig(true);
                fileManager.reloadConfig();

                this.getCommand("gamemode").setExecutor(gamemode);



                this.console.sendMessage(api.getColored("&e[SparkCore] Has successfully been enabled and loaded."));
                this.console.sendMessage(api.getColored("&e[SparkCore] If you encounter any errors contact Skptical on SpigotMC.org"));
                instance = this;
            }
        }


        @Override
        public void onDisable(){

            this.console.sendMessage(api.getColored("&e[SparkCore] Has successfully been disabled and unloaded."));
            this.console.sendMessage(api.getColored("&e[SparkCore] If you encounter any errors contact Skptical on SpigotMC.org"));

        }


    }





     


    API Class:


    Code (Text):
    package me.skptical.spark.api;

    import me.skptical.spark.main.FileManager;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;

    import java.util.ArrayList;
    import java.util.Collection;

    public class SparkAPI {

        public String getColored(String messageToTranslate){
            return ChatColor.translateAlternateColorCodes('&', messageToTranslate);
        }

        public String getMessage(String path){
            FileManager fm = new FileManager();
            String message = fm.getMessages().getString(path);
            return getColored(message);
        }

        public ArrayList<Player> loopAllPlayers(String permission){
            ArrayList<Player> ps = new ArrayList<>();
            for(Player p: Bukkit.getOnlinePlayers()){
                if(p.hasPermission(permission)){
                    ps.add(p);

                }
            }
            return ps;
        }

        public Boolean checkOnlineStatus(String  player){
            Player p = Bukkit.getPlayerExact(player);
            if(p != null){
                return true;
            }
            return false;

        }

        public Player convertToPlayer(String player){
            return Bukkit.getPlayerExact(player);
        }

    }
     

    File manager class:


    Code (Java):
    package me.skptical.spark.main;

    import org.bukkit.ChatColor;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.yaml.snakeyaml.Yaml;

    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;

    public class FileManager {

        private File configFile = null;
        private YamlConfiguration configuration = null;

        private File messagesFile = null;
        private YamlConfiguration messagesConfig = null;

        private File playersFile = null;
        private YamlConfiguration playersConfig = null;

        private SparkCore plugin = SparkCore.getPlugin(SparkCore.class);

        public void reloadMessages(){
            if(messagesFile == null){
                messagesFile = new File(plugin.getDataFolder(), "messages.yml");
            }
            messagesConfig = YamlConfiguration.loadConfiguration(messagesFile);
        }

        public FileConfiguration getMessages(){
            if(messagesConfig == null){
                reloadMessages();
            }
            return messagesConfig;
        }

        public void saveMessages() {
            if (messagesConfig == null || messagesFile == null) {
                return;
            }
            try {
                getMessages().save(messagesFile);
            } catch (IOException ex) {
                plugin.getLogger().log(Level.SEVERE, ChatColor.RED + "(!) Error saving file to " + messagesFile, ex);

            }
        }

        public void createMessages(Boolean copyDefaults){
            if(!plugin.getDataFolder().exists()){
                plugin.getDataFolder().mkdirs();
            }
            if(copyDefaults){
                if(messagesFile == null){
                    messagesFile = new File(plugin.getDataFolder(), "messages.yml");

                }
                if(!messagesFile.exists()){
                    plugin.saveResource("messages.yml", false);
                }
            }else{
                if(messagesFile == null) {
                    messagesFile = new File(plugin.getDataFolder(), "messages.yml");
                }
                if(!messagesFile.exists()){
                    try{
                        messagesFile.createNewFile();

                    }catch (IOException e){
                        plugin.getLogger().log(Level.SEVERE, ChatColor.RED + "(!) Error creating file " + messagesFile, e );

                    }
                }
                messagesConfig = YamlConfiguration.loadConfiguration(messagesFile);
            }

        }



        public void reloadConfig(){
            if(configFile == null){
                configFile = new File(plugin.getDataFolder(), "config.yml");
            }
            configuration = YamlConfiguration.loadConfiguration(configFile);
        }

        public FileConfiguration getConfig(){
            if(configuration == null){
                reloadConfig();
            }
            return configuration;
        }

        public void saveConfig() {
            if (configuration == null || configFile == null) {
                return;
            }
            try {
                getConfig().save(configFile);
            } catch (IOException ex) {
                plugin.getLogger().log(Level.SEVERE, ChatColor.RED + "(!) Error saving file to " + configFile, ex);

            }
        }

        public void createConfig(Boolean copyDefaults){
            if(!plugin.getDataFolder().exists()){
                plugin.getDataFolder().mkdirs();
            }
            if(copyDefaults){
                if(configFile == null){
                    configFile = new File(plugin.getDataFolder(), "config.yml");

                }
                if(!configFile.exists()){
                    plugin.saveResource("config.yml", false);
                }
            }else{
                if(configFile == null) {
                    configFile = new File(plugin.getDataFolder(), "config.yml");
                }
                if(!configFile.exists()){
                    try{
                        configFile.createNewFile();

                    }catch (IOException e){
                        plugin.getLogger().log(Level.SEVERE, ChatColor.RED + "(!) Error creating file " + configFile, e );

                    }
                }
                configuration = YamlConfiguration.loadConfiguration(configFile);
            }

        }

        public void reloadPlayers(){
            if(playersFile == null){
                playersFile = new File(plugin.getDataFolder(), "players.yml");
            }
            playersConfig = YamlConfiguration.loadConfiguration(playersFile);
        }

        public FileConfiguration getPlayers(){
            if(playersConfig == null){
                reloadPlayers();
            }
            return playersConfig;
        }

        public void savePlayers() {
            if (playersConfig == null || playersFile == null) {
                return;
            }
            try {
                getPlayers().save(playersFile);
            } catch (IOException ex) {
                plugin.getLogger().log(Level.SEVERE, ChatColor.RED + "(!) Error saving file to " + playersFile, ex);

            }
        }

        public void createPlayers(Boolean copyDefaults){
            if(!plugin.getDataFolder().exists()){
                plugin.getDataFolder().mkdirs();
            }
            if(copyDefaults){
                if(playersFile == null){
                    playersFile = new File(plugin.getDataFolder(), "players.yml");

                }
                if(!playersFile.exists()){
                    plugin.saveResource("players.yml", false);
                }
            }else{
                if(playersFile == null) {
                    playersFile = new File(plugin.getDataFolder(), "players.yml");
                }
                if(!playersFile.exists()){
                    try{
                        playersFile.createNewFile();

                    }catch (IOException e){
                        plugin.getLogger().log(Level.SEVERE, ChatColor.RED + "(!) Error creating file " + playersFile, e );

                    }
                }
                playersConfig = YamlConfiguration.loadConfiguration(playersFile);
            }

        }

    }
     
     
  2. You have more than likely another version of the plugin within the plugins folder which has a different name.

    Whilst developing ...
    It is easily done when you Export a plugin by clicking ... next, next, next ... without checking the names and versions.

    In Eclipse, the last saved 'jar's file name is remembered and can be over-written with a different plugin's content.