Plugin already initialized?

Discussion in 'Plugin Development' started by Profaann, Dec 8, 2015.

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

    Profaann

    Hi, when i register another class on main class, the plugin just stops working, i have no idea what is wrong.
    OBS: the class i want to register is in another package.

    Main.java:
    Code:
    package me.profan.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.event.player.PlayerQuitEvent;
    import org.bukkit.event.server.ServerListPingEvent;
    import org.bukkit.event.weather.WeatherChangeEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    import org.fusesource.jansi.Ansi;
    
    import me.profan.instance.Instance;
    import me.profan.monsters.MonstersMechanics;
    
    
    public class Main extends JavaPlugin implements Listener{
        // Color codes are replaced after.
        public String motd = "&b| BR-0 |&3 Development Server";
     
        // Check if server is frozen, if is true, server will be shutdown.
        public boolean server_frozen = false;
     
        public void onEnable() {
            getServer().getWorld("world").setGameRuleValue("doMobLoot", "false");
            getServer().getWorld("world").setGameRuleValue("mobGriefing", "false");
            getCommand("instance").setExecutor(new Instance());
            Bukkit.getPluginManager().registerEvents(this, this);
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new MonstersMechanics(this), this);
            Bukkit.getLogger().info(Ansi.ansi().fg(Ansi.Color.CYAN).boldOff().toString() +"*********************************");
            Bukkit.getLogger().info(Ansi.ansi().fg(Ansi.Color.CYAN).boldOff().toString() +"ETHERIA KINGDOM:  Plugin enabled.");
            Bukkit.getLogger().info(Ansi.ansi().fg(Ansi.Color.CYAN).boldOff().toString() +"*********************************" + Ansi.ansi().fg(Ansi.Color.WHITE).boldOff().toString());
            Bukkit.getLogger().info("[Etheria Kingdom] This plugin logs everything, they are saved in the normal log file.");
            if (Bukkit.getWorld("dungeon") != null) {
                Bukkit.getLogger().info("[Etheria Kingdom] Over 1 dungeon(s) LOADED.");
            }
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    if(Bukkit.getMotd().contains("BR-0")) {
                        Bukkit.getLogger().info(Ansi.ansi().fg(Ansi.Color.CYAN).boldOff().toString() +"[Etheria Kingdom] clearAllEntities failed (SERVER BR-0)" + Ansi.ansi().fg(Ansi.Color.WHITE).boldOff().toString());
                    }
                    else {
                        Bukkit.getLogger().info(Ansi.ansi().fg(Ansi.Color.CYAN).boldOff().toString() +"[Etheria Kingdom] Killing all entities alive." + Ansi.ansi().fg(Ansi.Color.WHITE).boldOff().toString());
                        MonstersMechanics.clearAllEntities();
                    }
                     }
        }, 72000, 72000);
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                        Bukkit.getLogger().info(Ansi.ansi().fg(Ansi.Color.CYAN).boldOff().toString() +"[Etheria Kingdom] Saving players data." + Ansi.ansi().fg(Ansi.Color.WHITE).boldOff().toString());
                        for(Player p : Bukkit.getOnlinePlayers()) {
                            savePlayerData(p);
                        }
                }
        }, 72000, 72000);
            // Is this really necessary?
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    for(Player p : Bukkit.getOnlinePlayers()){
                        p.setLevel((int) p.getHealth());
                        p.setExp(0);
                    }
                }
            }, 0L, 10L);
    }
    
        public void onDisable() {
            Bukkit.getLogger().info(Ansi.ansi().fg(Ansi.Color.RED).boldOff().toString() +"*********************************");
            Bukkit.getLogger().info(Ansi.ansi().fg(Ansi.Color.RED).boldOff().toString() +"ETHERIA KINGDOM:  Plugin disabled.");
            Bukkit.getLogger().info(Ansi.ansi().fg(Ansi.Color.RED).boldOff().toString() +"*********************************" + Ansi.ansi().fg(Ansi.Color.WHITE).boldOff().toString());
            for(Player p : Bukkit.getOnlinePlayers()){
                savePlayerData(p);
                p.kickPlayer(ChatColor.AQUA + "O Etheria Kingdom esta sendo reiniciado.");
            }
            Bukkit.getServer().shutdown();
           }
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            e.setJoinMessage(null);
         
            Bukkit.getLogger().info("[Etheria Kingdom] Loading data for " + p.getName());
            p.loadData();
            Bukkit.getLogger().info("[Etheria Kingdom] " + p.getName() + " data loaded.");
            p.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "ETHERIA KINGDOM: Bem-Vindo!");
            p.setMaxHealth(50);
            p.setHealthScaled(true);
            p.setLevel((int) p.getHealth());
        }
        @EventHandler
        public void onServerPing(ServerListPingEvent e) {
                motd = motd.replaceAll("&", "\u00A7");
                e.setMotd(motd);
        }
        @EventHandler
        public void onLeave(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            e.setQuitMessage(null);
         
            Bukkit.getLogger().info("[Etheria Kingdom] Saving data for " + p.getName());
            savePlayerData(p);
            Bukkit.getLogger().info("[Etheria Kingdom] " + p.getName() + " data saved.");
        }
        @EventHandler
        public void onBreakBlock(BlockBreakEvent e) {
            Player p = e.getPlayer();
            if (!p.isOp()){
                e.setCancelled(true);
                Bukkit.getLogger().info("[Etheria Kingdom] " + p.getName() + " tried to break a block at X:" + p.getLocation().getX() + " Y:" + p.getLocation().getY() + " Z:" + p.getLocation().getZ());
            }
        }
        @EventHandler
        public void onWeather(WeatherChangeEvent e) {
            e.setCancelled(true);
            Bukkit.getLogger().info("[Etheria Kingdom] Blocked rain in world" + e.getWorld().toString());
        }
        public void savePlayerData(Player p) {
            p.saveData();
            p.setHealth(p.getHealth());
            p.teleport(p);
        }
    }
    
    error:
    Error (open)
    [01:53:30] [Server thread/ERROR]: Error occurred while enabling EtheriaKingdom v0.1 (Is it up to date?)
    java.lang.IllegalArgumentException: Plugin already initialized!
    at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:98) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at me.profan.monsters.MonstersMechanics.<init>(MonstersMechanics.java:27) ~[?:?]
    at me.profan.main.Main.onEnable(Main.java:34) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:342) [craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:314) [craftbukkit.jar:git-Bukkit-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:406) [craftbukkit.jar:git-Bukkit-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:370) [craftbukkit.jar:git-Bukkit-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:325) [craftbukkit.jar:git-Bukkit-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:235) [craftbukkit.jar:git-Bukkit-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:504) [craftbukkit.jar:git-Bukkit-18fbb24]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    Caused by: java.lang.IllegalStateException: Initial initialization
    at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:101) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at me.profan.main.Main.<init>(Main.java:22) ~[?:?]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_65]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_65]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_65]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_65]
    at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_65]
    at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:289) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.<init>(CraftServer.java:251) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerList.<init>(PlayerList.java:69) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedPlayerList.<init>(SourceFile:14) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:179) ~[craftbukkit.jar:git-Bukkit-18fbb24]
    ... 2 more
     
    Last edited: Dec 8, 2015
  2. Offline

    timtower Administrator Administrator Moderator

    @Profaann Could you please post the Instance class?
     
  3. Offline

    mythbusterma

    @Profaann

    His point is not that it doesn't work, but that you're being lazy. Which you are. You should fix those issues with your plugin, even if it "works."

    There's NO reason whatsoever to call Server#shutdown() in your onDisable() method. It's just a waste of bytecode and might cause some server implementations to shutdown poorly.
     
  4. Offline

    Profaann

    So call server#shutdown on disable is being a lazy? please, if you dont want to help dont come here.

    Instance? the main class? i will post the class Instance. but the one i posted is the main class.
    There is the instance class:
    Code:
    package me.profan.instance;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.WorldType;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Instance implements CommandExecutor {
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = null;
           
            if(sender instanceof Player) {
                p = (Player) sender;
            }
           
            if(p != null && !(p.isOp())) { return true; }
           
            if(args.length == 0) {
                p.sendMessage(ChatColor.RED + "ETHERIA KINGDOM: /instance <load/unload/create>");
               
                return true;
            }
           
            String sub_cmd = args[0];
            if(sub_cmd.equalsIgnoreCase("load")) {
                String instance_name = args[1];
                World world = Bukkit.getWorld(instance_name);
                if(world == null){
                   p.sendMessage(ChatColor.RED + "ETHERIA KINGDOM: Esta instance não existe.");
                }
             else {
                p.teleport(world.getSpawnLocation());
                p.sendMessage(ChatColor.YELLOW + "ETHERIA KINGDOM: Teleportado.");
            }
    }
            if(sub_cmd.equalsIgnoreCase("unload")) {
                World world = Bukkit.getWorld("world");
                Bukkit.unloadWorld(world, false);
                p.teleport(world.getSpawnLocation());
                p.sendMessage(ChatColor.YELLOW + "ETHERIA KINGDOM: Teleportado.");
            }
            if(sub_cmd.equalsIgnoreCase("create")) {
                String instance_name = args[1];
                World world = Bukkit.getWorld(instance_name);
                if(world == null){
                WorldCreator creator = new WorldCreator(instance_name);
                creator.environment(World.Environment.NORMAL);
                creator.generateStructures(false);
                creator.type(WorldType.FLAT);
                world = creator.createWorld();
                p.sendMessage(ChatColor.YELLOW + "ETHERIA KINGDOM: Instance criada com sucesso.");
                }
                else {
                    p.sendMessage(ChatColor.RED + "ETHERIA KINGDOM: Esta instance já existe, carregue-a com /instance load.");
                }
            }
            return true;
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  5. Offline

    timtower Administrator Administrator Moderator

    @Profaann I know that the class posted was the main, class names tell a lot.
    Do you have 2 classes extending JavaPlugin? If so: stop doing that, it won't work.
     
    Profaann likes this.
  6. Offline

    Profaann

    Thank you! that worked!
     
  7. @Profaann I suggest you remove server.shutdown(), especially if you plan on publishing this plugin. Some people use PlugMan to disable/reload... This will make the server turn off whenever the plugin is reloaded, unloaded, or disabled.
     
  8. Offline

    mcdorli

    Or just some people doesn't like, when a plugin takes control about everything.
     
  9. Offline

    Profaann

    I already said: ITS FOR MY SERVER I WONT PUBLISH
     
  10. @Profaann I never saw you state that anywhere on this page. Please quote it... I did command+f and didn't find a single instance of you saying it was for your server, so, politely as possible, I think you might be mistaken.

    Besides that, please try and be a little more kind/patient.
     
  11. Offline

    Profaann

    The post was removed.
     
  12. @Profaann then why are you flipping out about it? I never saw the post before.
     
  13. Offline

    TheNewTao

    @Profaann

    He was just giving you a suggestion. In fact, I would thank him, he just made you a little bit smarter :) It is always good to know new stuff.
     
  14. Offline

    Profaann

    Who?
     
  15. Offline

    eyamaz

    This thread has gone off topic and the original issue has been fixed, therefore it is now locked.
     
Thread Status:
Not open for further replies.

Share This Page