Solved Plugin Already Initialized

Discussion in 'Plugin Development' started by TheNewTao, Dec 1, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    TheNewTao

    Hi,

    I am having a problem with my plugin. It gives out an error that the plugin has already been initialized. I was reading another post and it said that it was due because either another class extended JavaPlugin or there was another plugin with the same package for main. None of these apply to me. I think my problem is because I am creating a new instance of my Main class on my Events, but I am not exactly sure why. This line:

    Code:
    Main main = new Main();
    I'll leave my code an error here

    Error:
    Code:
    [14:55:16] [Server thread/ERROR]: Could not load 'plugins/DevelopmentTools.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133) ~[craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:289) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:718) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.Bukkit.reload(Bukkit.java:544) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_60]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_60]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]
    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:98) ~[craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at me.taopablot.development.Main.<init>(Main.java:13) ~[?:?]
        at me.taopablot.development.Events.<init>(Events.java:23) ~[?:?]
        at me.taopablot.development.Main.<init>(Main.java:14) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_60]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_60]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_60]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[?:1.8.0_60]
        at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_60]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        ... 21 more
    Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:101) ~[craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at me.taopablot.development.Main.<init>(Main.java:13) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_60]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_60]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_60]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[?:1.8.0_60]
        at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_60]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[craftbukkit-BT45_1.8.8_10.21.jar:git-Bukkit-18fbb24]
        ... 21 more
    
    Main:
    Code:
    package me.taopablot.development;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    
    public class Main extends JavaPlugin{
        Events event = new Events();
    
        ScoreboardManager manager = Bukkit.getScoreboardManager();
        Scoreboard creepersSpawned = manager.getNewScoreboard();
        Objective amount = creepersSpawned.registerNewObjective("Creepers", "dummy");
    
        @Override
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(event, this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(sender instanceof Player && sender != null){
                Player p = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("freeze")){
                    if (args.length == 0) {
                        if (event.freezeAll) {
                            event.freezeAll = false;
                            Bukkit.broadcastMessage(ChatColor.GOLD + ChatColor.BOLD.toString() +"Players unfrozen");
                        } else {
                            event.freezeAll = true;
                            Bukkit.broadcastMessage(ChatColor.GOLD + ChatColor.BOLD.toString() + "Players frozen");
                        }
                    } else if (args.length == 1) {
                        Player target = Bukkit.getPlayerExact(args[0]);
                        if (target != null) {
                            if(event.freeze.contains(target.getUniqueId())){
                                event.freeze.remove(target.getUniqueId());
                                Bukkit.broadcastMessage(ChatColor.GOLD + ChatColor.BOLD.toString() + args[0] +" is now unfrozen");
                            }
                            else{
                                Bukkit.broadcastMessage(ChatColor.GOLD + ChatColor.BOLD.toString() + args[0] +" is now frozen");
                                event.freeze.add(target.getUniqueId());
                            }
                        } else {
                            p.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "This player is not online!");
                        }
    
                    }
                    else if(args.length >1){
                        p.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "Please use the correct /freeze format!");
                    }
                }
                else if(cmd.getName().equalsIgnoreCase("spacer")){
                    String spacer = ChatColor.WHITE + ChatColor.BOLD.toString() + ChatColor.ITALIC.toString() + "Spacer";
                    Bukkit.broadcastMessage(ChatColor.GRAY + ChatColor.BOLD.toString() + "######################################################");
                    Bukkit.broadcastMessage(ChatColor.GRAY + ChatColor.BOLD.toString() + "#####################  " + spacer + ChatColor.GRAY + ChatColor.BOLD.toString()+ "  ########################");
                    Bukkit.broadcastMessage(ChatColor.GRAY + ChatColor.BOLD.toString() + "#####################  " + spacer + ChatColor.GRAY + ChatColor.BOLD.toString() +"  ########################");
                    Bukkit.broadcastMessage(ChatColor.GRAY + ChatColor.BOLD.toString() + "######################################################");
    
                
                }
            }
            return true;
        }
    
    }
    
    Events
    Code:
    package me.taopablot.development;
    
    import java.util.ArrayList;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Creeper;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Score;
    
    public class Events implements Listener {
    
        int score = 0;
    
        Main main = new Main();
        ArrayList<UUID> freeze = new ArrayList<UUID>();
        public static boolean freezeAll = false;
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
            main.amount.setDisplayName("Creepers Spawned");
            main.amount.setDisplaySlot(DisplaySlot.SIDEBAR);
            Score score = main.amount.getScore(p.getName());
            score.setScore(this.score);
        }
    
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            String snip = ChatColor.WHITE   + "ยป";
            String oldMessage = e.getMessage();
            if (!e.getPlayer().getName().equals("TaoPablot")) {
                e.setFormat(ChatColor.GRAY + e.getPlayer().getDisplayName() +" "+ snip + " " + ChatColor.WHITE.toString()
                        + oldMessage);
            } else {
                String prefix = ChatColor.AQUA + ChatColor.BOLD.toString() + "DEVELOPER ";
                e.setFormat(prefix + ChatColor.WHITE + ChatColor.BOLD.toString() + e.getPlayer().getDisplayName() + " "+ snip
                        + " " + ChatColor.WHITE.toString() + oldMessage);
            }
        }
    
        @EventHandler
        public void onEntityDeath(CreatureSpawnEvent e) {
            Bukkit.broadcastMessage("TEST");
            if(e.getEntity() instanceof Creeper){
                Creeper creeper = (Creeper)e.getEntity();
                creeper.setCustomName(ChatColor.GREEN + ChatColor.BOLD.toString() + ChatColor.UNDERLINE.toString() + "Mini-Creepers");
                creeper.setPowered(true);
                score++;
            }
        }
    
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            if (freezeAll) {
                e.setCancelled(true);
            }
            if (freeze.contains(e.getPlayer().getUniqueId())) {
                e.setCancelled(true);
            }
        }
    }
    
    By the way, it is my first time playing with scoreboards, so if you find any errors please don't hesitate to tell me.

    Thank you
     
  2. Offline

    teej107

    You just gave the answer to your own problem.
     
    oceantheskatr likes this.
  3. Offline

    timtower Administrator Administrator Moderator

    @TheNewTao Don't make a new instance of your main class, you just constructors.
     
  4. Offline

    TheNewTao

    @timtower

    What do you mean with "you just constructors"? And I know creating a new instance of Main is my problem but why does that initialize the plugin again?
     
  5. Offline

    Creeper674

    Main represents the core of your plugin. If you create a new instance every time you want to reference the class, it's like creating a new plugin, in a way. You want to intialize a single instance only once, and reference that instance when you want to use a method in your main class.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @TheNewTao That should be "you just use constructors"
    Use constructors to pass the main around.
     
  7. Offline

    TheNewTao

    @timtower

    Sorry I am a bit confused, use constructors for what?
     
  8. Offline

    timtower Administrator Administrator Moderator

    @TheNewTao To pass the main around.
    Give all classes that need the main plugin a constructor that takes the main as argument.
     
  9. Offline

    TheNewTao

    @timtower

    So something like:

    Code:
        public Events(Main main){
            this.amount = main.amount;
        }
     
  10. Offline

    timtower Administrator Administrator Moderator

    @TheNewTao No, just pass the entire main, then you can use it more times than just once
     
  11. Offline

    Scimiguy

    Main is a singleton, there is only 1
     
  12. Offline

    TheNewTao

    @timtower

    I am not sure if this is what you mean, but I passed the main in the constructor and then set that main to an instance Main variable in the Events class.

    Code:
        Main myMain;
        public Events(Main main){
            myMain = main;
        }
     
  13. Offline

    Mrs. bwfctower

    @TheNewTao Yeah. And while you're at it, make it private.
     
  14. Offline

    TheNewTao

    @Mrs. bwfctower

    Question, what is the difference between using
    List <String> list = new ArrayList<String>();
    and
    ArrayList<String> list = new ArrayList<String>();
     
  15. Offline

    Mrs. bwfctower

    @TheNewTao First of all, that's offtopic. You should really be posting that on a Java forums, as it's only pertaining to Java.

    The first one is an example of decoupling, where you separate the implementation from the interface. A List is an Abstract Data Type (ADT). An ArrayList (among others) is a Data Structure, and is the implementation of the ADT. It's the 'real' thing, which can be implemented.
     
Thread Status:
Not open for further replies.

Share This Page