Solved Cannot get plugin for class main.Main from a static initializer

Discussion in 'Plugin Development' started by TheBrightnessMan, Feb 26, 2020.

  1. Im coding a custom token plugin for a server but I keep getting this error someone please help


    Code:
    [ERROR] Could not load 'plugins/Motion Prison.jar' in folder 'plugins'
    
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalStateException: Cannot get plugin for class main.Main from a static initializer
    
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:327) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:248) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.loadPlugins(CraftServer.java:305) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    
    at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:205) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    
    at java.lang.Thread.run(Thread.java:813) [?:1.8.0_212]
    
    Caused by: java.lang.IllegalStateException: Cannot get plugin for class main.Main from a static initializer
    
    at org.bukkit.plugin.java.JavaPlugin.getPlugin(JavaPlugin.java:390) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    
    at main.Commands.<init>(Commands.java:14) ~[?:?]
    
    at main.Main.<init>(Main.java:8) ~[?:?]
    
    at java.lang.J9VMInternals.newInstanceImpl(Native Method) ~[?:1.8.0_212]
    
    at java.lang.Class.newInstance(Class.java:1848) ~[?:1.8.0_212]
    
    at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:90) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
    
    ... 6 more

    Main class
    Code:
    package main;
    
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        private Commands commands = new Commands();
        public void onEnable() {
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Custom Plugin has been enabled");
            getServer().getPluginManager().registerEvents(new Events(), this);
            getCommand(commands.command1).setExecutor(commands);
            getConfig().options().copyDefaults(false);
            saveConfig();
        }
    }

    Event Class
    Code:
    package main;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.Plugin;
    
    public class Events implements Listener {
    
        private Plugin plugin = Main.getPlugin(Main.class);
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = (Player)event.getPlayer();
            if(!plugin.getConfig().contains("Tokens" + player.getName())) {
                plugin.getConfig().set("Tokens" + player.getName(), 0);
            }
    
            if(!plugin.getConfig().contains("Blocks" + player.getName())){
                plugin.getConfig().set("Blocks" + player.getName(), 0);
            }
        }
    
        @EventHandler
        public void onBreak(BlockBreakEvent event){
            Player player = event.getPlayer();
            int block = plugin.getConfig().getInt("Blocks" + player.getName());
            int token = plugin.getConfig().getInt("Tokens" + player.getName());
    
            block = block + 1;
            plugin.getConfig().set("Blocks" + player.getName(), block);
    
            if(block == 5){
                token = token + 1;
                plugin.getConfig().set("Tokens" + player.getName(), token);
            }
        }
    
    }

    Commands Class
    Code:
    package main;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    
    public class Commands implements Listener, CommandExecutor {
    
        public String command1 = "tokens";
        private Plugin plugin = Main.getPlugin(Main.class);
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String s, String[] strings) {
            if(sender instanceof Player){
                if(cmd.getName().equalsIgnoreCase(command1)){
                    Player player = (Player) sender;
                    int block = plugin.getConfig().getInt("Blocks" + player.getName());
                    int token = plugin.getConfig().getInt("Tokens" + player.getName());
                    int diff = 100 - block;
    
                    player.sendMessage("====================");
                    player.sendMessage("You have " + ChatColor.GOLD + token + " token(s)");
                    player.sendMessage("You have mined " + ChatColor.AQUA + block + " blocks");
                    player.sendMessage("====================");
                    player.sendMessage("You need to mine " + ChatColor.LIGHT_PURPLE + diff + " to get another token");
                    player.sendMessage("====================");
                }
            } else {
                sender.sendMessage("You are not a player lol");
            }
            return false;
        }
    }
    plugin.yml
    Code:
    name: CustomPlugin
    main: main.Main
    version: 1.0
    commands:
      tokens:
        usage: /<command>
        description: Shows how much tokens you have

    config.yml is empty
     
    Last edited: Feb 26, 2020
  2. Online

    timtower Moderator Moderator

  3. which part should I use constructor?
     
  4. Online

    timtower Moderator Moderator

  5. I did
    I called for
    Code:
    private Plugin plugin = Main.getPlugin(Main.class);
    in both classes
     
  6. Online

    timtower Moderator Moderator

    That is not a constructor, that is a static getting that gets called when the main class is not fully initialized yet.
    That is why you should not make instances of classes that use the main class outside of the onEnable.
     
  7. whats the correct way to do it?
     
  8. Online

    timtower Moderator Moderator

  9. like this?
    Code:
    package main;
    
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        private Commands commands = new Commands();
        public void onEnable() {
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Custom Plugin has been enabled");
            getServer().getPluginManager().registerEvents(new Events(), this);
            registerCommands();
            getConfig().options().copyDefaults(false);
            saveConfig();
        }
       
        public void registerCommands(){
            getCommand(commands.command1).setExecutor(commands);
        }
    }
    but would it make a difference?
     
  10. Online

    timtower Moderator Moderator

    @TheBrightnessMan That is not a constructor... Did you read the link?
    Code:
    package main;
    
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        private Commands commands;
        public void onEnable() {
            commands = new Commands(this);
            getServer().getPluginManager().registerEvents(new Events(this), this);
            getCommand(commands.command1).setExecutor(commands);
            getConfig().options().copyDefaults(false);
            saveConfig();
        }
    }
    Get that working instead.
     
  11. oh I have to run it inside of onEnable()
    im so dumb
    thank you so much

    EDIT: marked as solved
     

Share This Page