Solved Plugin Already Initialized

Discussion in 'Spigot Plugin Development' started by AwesomeFishh, Jul 7, 2018.

  1. SOLUTION:
    In plugin.yml, add the line: depend: [PluginName1, PluginName2, etc].
    In code, change:
    Code (Java):
    private Class class = (Class) getServer().getPluginManager().getPlugin("Pluginname");
    to
    Code (Text):
    private Class class;

    public void onEnable(){
       class = (Class) getServer().getPluginManager().getPlugin("Pluginname");
    }


    Hey everyone,

    I have a Chat plugin which uses my Permissions plugin, to get the prefixes and suffixes of a player, but it doesn't want to load the permissions plugin, and says it has already been initialized. I had the thought that it might be because both classes extend JavaPlugin, but how would you avoid that?

    EDIT: For clarification, those 2 classes are in different plugins

    Error log:
    Code (Text):
    [19:06:29 ERROR]: Could not load 'plugins\BubbleMC_CorePermissions.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:326) ~[craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:248) [craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.loadPlugins(CraftServer.java:304) [craftbukkit.jar:git-Bukkit-809c399]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:188) [craftbukkit.jar:git-Bukkit-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:522) [craftbukkit.jar:git-Bukkit-809c399]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_172]
    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:98) ~[craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:60) ~[craftbukkit.jar:git-Bukkit-809c399]
            at me.AwesomeFishh.CorePermissions.CorePermissions.<init>(CorePermissions.java:16) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_172]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_172]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_172]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_172]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_172]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:127) ~[craftbukkit.jar:git-Bukkit-809c399]
            ... 6 more
    Caused by: java.lang.IllegalStateException: Initial initialization
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:101) ~[craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:60) ~[craftbukkit.jar:git-Bukkit-809c399]
            at me.AwesomeFishh.CoreChat.CoreChat.<init>(CoreChat.java:6) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_172]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_172]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_172]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_172]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_172]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit.jar:git-Bukkit-809c399]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:127) ~[craftbukkit.jar:git-Bukkit-809c399]
            ... 6 more
    My CorePermissions.java class:
    Code (Java):
    package me.AwesomeFishh.CorePermissions;

    import me.AwesomeFishh.CorePermissions.Commands.CommandManager;
    import me.AwesomeFishh.CorePermissions.Configurations.ConfigManager;
    import me.AwesomeFishh.CorePermissions.Listeners.JoinEvent;
    import me.AwesomeFishh.CorePermissions.Utils.Messages;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.PermissionAttachment;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;

    public class CorePermissions extends JavaPlugin {

        public HashMap<UUID, PermissionAttachment> permissionAttachments = new HashMap<>();
        public HashMap<UUID, String> playerPrefixes = new HashMap<>();
        public HashMap<UUID, String> playerSuffixes = new HashMap<>();

        public Messages messages;
        public ConfigManager configManager;
        public CommandManager commandManager;

        public JoinEvent joinEvent;

        public void onEnable() {
            saveDefaultConfig();

            registerClasses();
            registerListeners();

            getLogger().info("Plugin Enabled!");

        }

        public void onDisable() {
            saveConfig();

            getLogger().info("Plugin Disabled!");

        }

        public void registerClasses() {
            messages = new Messages(this);
            configManager = new ConfigManager(this);
            configManager.setupConfigs();

            commandManager = new CommandManager(this);
            commandManager.setup();
        }

        public void registerListeners() {
            getServer().getPluginManager().registerEvents(joinEvent = new JoinEvent(this), this);
        }

        public void setupPermissions(Player player) {
            UUID uuid = player.getUniqueId();

            PermissionAttachment attachment = player.addAttachment(this);
            this.permissionAttachments.put(uuid, attachment);

            List<String> playerGroups = configManager.getUsersConfig().getStringList("users." + uuid + ".groups");
            List<String> playerPermissions = configManager.getUsersConfig().getStringList("users." + uuid + ".permissions");

            String prefix = "";
            String suffix = "";

            for (String group : playerGroups) {
                for (String permission : getConfig().getStringList("groups." + group + ".permissions")) {
                    attachment.setPermission(permission, true);
                }

                if (getConfig().getString("groups." + group + ".prefix") != null)
                    prefix = prefix + getConfig().getString("groups." + group + ".prefix");

                if (getConfig().getString("groups." + group + ".suffix") != null)
                    suffix = suffix + getConfig().getString("groups." + group + ".suffix");

            }

            for (String permission : playerPermissions) {
                attachment.setPermission(permission, true);
            }

            playerPrefixes.put(uuid, prefix);
            playerSuffixes.put(uuid, suffix);

        }
    }
     
    My CoreChat.java class:
    Code (Java):
    package me.AwesomeFishh.CoreChat;

    import me.AwesomeFishh.CorePermissions.CorePermissions;
    import org.bukkit.plugin.java.JavaPlugin;

    public class CoreChat extends JavaPlugin {

        public Format format;

        public CorePermissions corePermissions = (CorePermissions) getServer().getPluginManager().getPlugin("Bubble_CorePermissions");

        public void onEnable() {
            saveDefaultConfig();

            registerClasses();

            getLogger().info("Plugin Enabled!");

        }

        public void onDisable() {
            saveConfig();

            getLogger().info("Plugin Disabled!");

        }

        public void registerClasses() {
            format = new Format(this);
        }

    }
     
     
    #1 AwesomeFishh, Jul 7, 2018
    Last edited: Jul 7, 2018
  2. Don't extend JavaPlugin in classes other than your main class, otherwise it breaks itself as you see. You need to pass an instance of your main class to use JavaPlugin methods.
     
  3. Yeah, but how else can you use methods of another plugin in your plugin? using getPluginManager().getPlugin("123") returns the class which extends JavaPlugin. Is there another way to do this?

    EDIT: For clarification, those 2 classes are in different plugins
     
  4. Didn't notice they were different plugins, my bad, should've taken a better look. Not sure about that, but on another note, as far as I know your package names should be all lowercase.
     
  5. Choco

    Moderator

    Java conventions state that packages must be entirely lowercase (as mentioned by @Rabbitual)

    • Liskov's Substitution Principle. Declare the type as its interfaced type, not as its specific implementation. Map<UUID, PermissionAttachment> blah = new HashMap<>();
    • These maps should not be public. See the following quotation / critique for more information
    • You're using 3 different maps to hold information for a single UUID. Consider creating an object and mapping a UUID to a single object instead
    Having your fields as public is bad practice and breaks one of the four pillars of OOP, encapsulation. You should privatize your variable and use getters / setters where necessary. As for your Maps above, you should have getters and setters that access the Map directly rather than returning the Map as that would leave it mutable and vulnerable. Something such as #getSuffix(Player) would return a String directly from the Map such that the key is present. The values of Messages, ConfigManager and CommandManager can likely have direct getters as there's no reason to keep them immutable. Either way, they should be private. Read up on encapsulation for more in-depth explanation on the topic.

    Have @Override annotations over any methods you override from super classes. It's a compiler hint to ensure that the method you've overriding is correct. Prevents any accidental mistypes

    No reason to log enable / disable messages because Bukkit will do this for you automatically

    This is an unnecessary call to #saveConfig() onDisable(). You really only need to call this when you programmatically make changes to the configuration file (i.e. getConfig().setString("something", "something") requires a call to #saveConfig() to write to file)

    This is the reason you're getting the exception. You're calling #getPlugin() in the initialization context of your plugin, therefore the CorePermissions plugin has not yet finished initialization. Consider calling this method and setting "corePermissions" value instead in the onEnable() method. This should resolve any issues you're having. Also, follow the above tips on encapsulation.
     
    • Winner Winner x 3
    • Useful Useful x 2
    • Like Like x 1
    • Informative Informative x 1
  6. I did what you suggested but it's still not working, gives the same error, or did I just completely misunderstand you?

    Code (Text):
    package me.AwesomeFishh.CoreChat;
    package me.AwesomeFishh.CoreChat;

    import me.AwesomeFishh.CorePermissions.CorePermissions;
    import org.bukkit.plugin.java.JavaPlugin;

    public class CoreChat extends JavaPlugin {

        private Format format;

        private CorePermissions corePermissions;

        @Override
        public void onEnable() {
            corePermissions = (CorePermissions) getServer().getPluginManager().getPlugin("Bubble_CorePermissions");
            saveDefaultConfig();

            registerClasses();
        }

        private void registerClasses() {
            format = new Format(this);
        }

        public Format getFormat() {
            return format;
        }

        public CorePermissions getCorePermissions() {
            return corePermissions;
        }
    }
     
     
  7. Choco

    Moderator

    From there it seems fine. Have you indicated that "CoreChat" depends on "CorePermissions" in your plugin.yml?
     
    • Useful Useful x 1
  8. Yes, that did it! Strangely enough, I have a Skyblock plugin which uses WorldEdit and another one of my plugins, and I just use
    Code (Java):
    private WorldEditPlugin worldEditPlugin = (WorldEditPlugin) getServer().getPluginManager().getPlugin("WorldEdit");
    private CoreCommands coreCommands = (CoreCommands) getServer().getPluginManager().getPlugin("Bubble_CoreCommands");
    without [depend] in plugin.yml, and it worked fine. Anyway, huge thanks for all the help!
     
    • Like Like x 1
  9. Choco

    Moderator

    Load order is seemingly random unless otherwise specified, so it may have just been by chance that WorldEdit was loading first. Glad you got it sorted :)
     
    • Like Like x 1