IllegalStateException?

Discussion in 'Plugin Development' started by TheSporech, Mar 20, 2016.

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

    TheSporech

    Hi, I'm making a simple plugin for a friend, and I've got this bug which I can't solve. It's to do with a player listener class, which in the past I have used in the exact same way, and has worked, but now I get this error:
    Code:
    [15:18:59 ERROR]: Error occurred while enabling SuperPlugin v2.0 (Is it up to date?)
    java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        at me.sporech.MainClass.<init>(MainClass.java:20) ~[?:?]
        at me.sporech.Power.<init>(Power.java:22) ~[?:?]
        at me.sporech.MainClass.onEnable(MainClass.java:30) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:356) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:316) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:742) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.Bukkit.reload(Bukkit.java:534) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:628) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:404) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:368) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot_server.jar:git-Spigot-044d928-e8c6403]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45]
    Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        at me.sporech.MainClass.<init>(MainClass.java:20) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_45]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_45]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[?:1.8.0_45]
        at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:291) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:740) ~[spigot_server.jar:git-Spigot-044d928-e8c6403]
        ... 10 more
    
    Here's my code (in 3 classes):
    Code:
    package me.sporech;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.LightningStrike;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class MainClass extends JavaPlugin implements CommandExecutor {
        public static Plugin that;
        public static Map<Player, Integer> powers = new HashMap<>();
        public static Map<Integer,Power> matcher1 = new HashMap<>();
        public static Map<Integer,Power> matcher2 = new HashMap<>();
        public static Map<Integer,Power> matcher3 = new HashMap<>();
       
        public void onEnable() {
            that = this;
            new PlayerListener(this);
            matcher1.put(0, new Power(Heroes.Zephyrstorm,Powers.Lightning));
            matcher1.put(1, new Power(Heroes.Zephyrstorm,Powers.Wind));
            matcher1.put(2, new Power(Heroes.Zephyrstorm,Powers.Blind));
            matcher2.put(0, new Power(Heroes.Cataclysm,Powers.Gravityslam));
            matcher2.put(1, new Power(Heroes.Cataclysm,Powers.Gravityblast));
            matcher3.put(0, new Power(Heroes.Tectus,Powers.Lavabolt));
            matcher3.put(1, new Power(Heroes.Tectus,Powers.Earthblast));
            matcher3.put(2, new Power(Heroes.Tectus,Powers.Shockwave));
        }
       
       
        protected enum Heroes {
            Zephyrstorm,Cataclysm,Tectus
        }
        protected enum Powers {
            Lightning,Wind,Blind,Gravityslam,Gravityblast,Lavabolt,Earthblast,Shockwave
        }
    
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                if (cmd.getName().equalsIgnoreCase("zephyrstorm")) {
                    sender.sendMessage("zephyrstorm");
                    powers.put((Player) sender, 1);
                    return true;
                }
                if (cmd.getName().equalsIgnoreCase("cataclysm")) {
                    sender.sendMessage("cataclysm");
                    powers.put((Player) sender, 2);
                    return true;
                }
                if (cmd.getName().equalsIgnoreCase("tectus")) {
                    sender.sendMessage("tectus");
                    powers.put((Player) sender, 3);
                    return true;
                }
            }
            return false;
        }
    }
    
    (Next class)
    
    package me.sporech;
    import java.util.Map;
    
    import org.bukkit.entity.LightningStrike;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class PlayerListener implements Listener {
        public PlayerListener(MainClass plugin) {
            plugin.getServer().getPluginManager().registerEvents(this,plugin);;
        }
        static Map<Player, Integer> powers = MainClass.powers;
        static Map<Integer,Power> matcher1 = MainClass.matcher1;
        static Map<Integer,Power> matcher2 = MainClass.matcher2;
        static Map<Integer,Power> matcher3 = MainClass.matcher3;
       
        @EventHandler
        public void lightningnullify(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof LightningStrike) {
                event.setCancelled(true);
            }
        }
        @EventHandler
        public void useAbility(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            int set = powers.get(player);
           
            if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
                int slot = player.getInventory().getHeldItemSlot();
                if (set == 1) {
                    Power p = matcher1.get(slot);
                    p.cast(player);
                }
                if (set == 2) {
                    Power p = matcher1.get(slot);
                    p.cast(player);
                }
                if (set == 3) {
                    Power p = matcher1.get(slot);
                    p.cast(player);
                }
            }
        }
    }
    
    (Next class)
    
    package me.sporech;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;
    
    public class Power extends MainClass{
        public Heroes hero;
        public Powers power;
       
        public Power(Heroes h, Powers p) {
            hero = h;
            power = p;
        }
        public void cast(Player caster) {
            switch(hero) {
            case Zephyrstorm:
                switch(power) {
                case Lightning:
                    lightning(caster);
                case Wind:
                    blind(caster);
                case Blind:
                    blind(caster);
                }
            case Cataclysm:
                switch(power) {
                case Gravityslam:
                    gravityslam(caster);
                case Gravityblast:
                    gravityblast(caster);
                }
            case Tectus:
                switch(power) {
                case Lavabolt:
                    lavabolt(caster);
                case Earthblast:
                    earthblast(caster);
                case Shockwave:
                    shockwave(caster);
                }
            }
        }
    
        public void lightning(Player player) {
            Block b = player.getTargetBlock((Set) null, 20);
            b.getWorld().strikeLightning(b.getLocation());
            b.getWorld().playEffect(b.getLocation(), Effect.EXPLOSION_LARGE, 10);
            List<Entity> targets = (List<Entity>) b.getWorld().getNearbyEntities(b.getLocation(), 3, 3, 3);
            for (Entity target : targets) {
                if (target instanceof LivingEntity) {
                    ((LivingEntity) target).damage(4,player);
                }
            }
        }
        public void wind(final Player player) {
            List<Block> locations = player.getLineOfSight((Set) null, 20);
            long pathway = 0;
            for (final Block location : locations) {
                Bukkit.getScheduler().runTaskLater(MainClass.that, new Runnable(){
                    @Override
                    public void run(){
                        for (Entity entity : location.getWorld().getNearbyEntities(location.getLocation(),3,3,3)) {
                            if (entity instanceof LivingEntity) {
                                ((LivingEntity) entity).damage(1);
                                entity.setVelocity(new Vector((entity.getLocation().getX() - player.getLocation().getX())/3,
                                        ((entity.getLocation().getY() - player.getLocation().getY())/3)+0.5,
                                        (entity.getLocation().getZ() - player.getLocation().getZ())/3));
                            }
                        }
                        List<Block> a = getAdjacent(location);
                        for (Block block : a) {
                            block.getWorld().playEffect(block.getLocation(),Effect.CLOUD,0);
                        }
                    }
                },pathway);
                pathway += 0.5;
            }
        }
        public void blind(Player player) {
            List<Location> aoe = getSphere(player.getLocation(),7,7,7,true);
            for (Location loc : aoe) {
                loc.getWorld().playEffect(loc, Effect.CLOUD, 0);
                List<Entity> targets = (List<Entity>) loc.getWorld().getNearbyEntities(loc, 1, 1, 1);
                for (Entity target : targets) {
                    if (target instanceof LivingEntity) {
                        ((LivingEntity) target).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 1), true);
                    }
                }
            }
        }
        public void gravityslam(Player player) {
           
        }
        public void gravityblast(Player player) {
           
        }
        public void lavabolt(Player player) {
           
        }
        public void earthblast(Player player) {
           
        }
        public void shockwave(Player player) {
           
        }
        public List<Block> getAdjacent(Block b) {
            List<Block> blocks = null;
            blocks.add(b.getLocation().clone().add(0,1,0).getBlock());
            blocks.add(b.getLocation().clone().add(0,-1,0).getBlock());
            blocks.add(b.getLocation().clone().add(1,0,0).getBlock());
            blocks.add(b.getLocation().clone().add(-1,0,0).getBlock());
            blocks.add(b.getLocation().clone().add(0,1,1).getBlock());
            blocks.add(b.getLocation().clone().add(0,1,-1).getBlock());
            return blocks;
        }
        public List<Location> getSphere(Location location, double radiusX, double radiusY, double radiusZ, boolean filled){
            Vector pos = location.toVector();
            World world = location.getWorld();
            List<Location> blocks = new ArrayList<Location>();
    
            radiusX += 0.5;
            radiusY += 0.5;
            radiusZ += 0.5;
    
            final double invRadiusX = 1 / radiusX;
            final double invRadiusY = 1 / radiusY;
            final double invRadiusZ = 1 / radiusZ;
    
            final int ceilRadiusX = (int) Math.ceil(radiusX);
            final int ceilRadiusY = (int) Math.ceil(radiusY);
            final int ceilRadiusZ = (int) Math.ceil(radiusZ);
    
            double nextXn = 0;
            forX: for (int x = 0; x <= ceilRadiusX; ++x) {
                final double xn = nextXn;
                nextXn = (x + 1) * invRadiusX;
                double nextYn = 0;
                forY: for (int y = 0; y <= ceilRadiusY; ++y) {
                    final double yn = nextYn;
                    nextYn = (y + 1) * invRadiusY;
                    double nextZn = 0;
                    forZ: for (int z = 0; z <= ceilRadiusZ; ++z) {
                        final double zn = nextZn;
                        nextZn = (z + 1) * invRadiusZ;
    
                        double distanceSq = lengthSq(xn, yn, zn);
                        if (distanceSq > 1) {
                            if (z == 0) {
                                if (y == 0) {
                                    break forX;
                                }
                                break forY;
                            }
                            break forZ;
                        }
    
                        if (!filled) {
                            if (lengthSq(nextXn, yn, zn) <= 1 && lengthSq(xn, nextYn, zn) <= 1 && lengthSq(xn, yn, nextZn) <= 1) {
                                continue;
                            }
                        }
    
                        blocks.add(pos.add(new Vector(x,y,z)).toLocation(world));
                        blocks.add(pos.add(new Vector(-x,y,z)).toLocation(world));
                        blocks.add(pos.add(new Vector(x,-y,z)).toLocation(world));
                        blocks.add(pos.add(new Vector(x,y,-z)).toLocation(world));
                        blocks.add(pos.add(new Vector(-x,-y,z)).toLocation(world));
                        blocks.add(pos.add(new Vector(x,-y,-z)).toLocation(world));
                        blocks.add(pos.add(new Vector(-x,y,-z)).toLocation(world));
                        blocks.add(pos.add(new Vector(-x,-y,-z)).toLocation(world));
                    }
                }
            }
    
            return blocks;
        }
        private static final double lengthSq(double x, double y, double z) {
            return (x * x) + (y * y) + (z * z);
        }
        private static final double lengthSq(double x, double z) {
            return (x * x) + (z * z);
        }
    }
    
     
  2. Offline

    mcdorli

    I'm 1000% certain, you have 2 separate plugins in your plugins folder with the same me.sporech.MainClass path to the mainClass, am I right?
     
  3. Offline

    TheSporech

    I have made plugins in the past which use that path, but they're not on the server
     
  4. Offline

    Zombie_Striker

    Your problem is that there are two files with the same 'path' trying to be enabled. This can be caused by:
    1. Having the same package name for multiple plugins (which it seems like you are doing)
    2. Having mutliple classes extend JavaPlugin
    3. Creating new instances of the main class.
    Always remember to add the "plugin's name" to the package, make sure you only have one class that extends Javaplugin, and never create new instance of the main class.
     
  5. Offline

    TheSporech

    That's odd, because my other plugins which use that classpath aren't there.
     
  6. Offline

    mcdorli

    Then try to set your package to me.sporech.yourpluginsname, and re-export it
     
    Zombie_Striker likes this.
  7. Offline

    TheSporech

    :O if you look, my third class extends the MainClass. That might be the problem!
     
  8. Offline

    mcdorli

    Wait, one of your classes extend the mainClass, that's the same as having 2 classes extending JavaPlugin, remove it.
     
    Zombie_Striker likes this.
  9. Offline

    TheSporech

    Thanks
     
  10. Offline

    Zombie_Striker

    @TheSporech
    Does that mean your problem has been solved? If so, mark this thread as solved.
     
  11. Offline

    TheSporech

    Yep. This error is fixed. Now I've got another one, but I'll mark it as solved.
     
Thread Status:
Not open for further replies.

Share This Page