Development Assistance Crash when using multiple classes.

Discussion in 'Plugin Help/Development/Requests' started by hopstar, Feb 19, 2015.

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

    hopstar

    Hi, I tryed using multiple classes because it makes it a al lot better for bug fixes and stuff, you guys prob already know al that stuff. But when i try to make a Listener class i woun't get it to work.
    I searched for a answer and found this page:
    https://bukkit.org/threads/tutorial-using-multiple-classes.179833/
    But when i used the folowing line i get a error: registerEvents(this, new Eventlistener());
    Code:
    0 ERROR]: Error occurred while enabling TreeCap v0.1 (Is it up to date?)
    g.IllegalArgumentException: Plugin already initialized!
    at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at treecapitator.Eventlistener.<init>(Eventlistener.java:12) ~[?:?]
    at treecapitator.TreeCapitator.onEnable(TreeCapitator.java:21) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:335) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugin(CraftServer.java:356) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.enablePlugins(CraftServer.java:316) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.reload(CraftServer.java:746) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.Bukkit.reload(Bukkit.java:534) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchServerCommand(CraftServer.java:632) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.DedicatedServer.aM(DedicatedServer.java:353) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:317) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    y: java.lang.IllegalStateException: Initial initialization
    at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at treecapitator.TreeCapitator.<init>(TreeCapitator.java:13) ~[?:?]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_31]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_31]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_31]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_31]
    at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_31]
    at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugins(CraftServer.java:291) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.reload(CraftServer.java:744) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    ... 10 more
    I use the following classes:

    Main:
    Code:
    package treecapitator;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class TreeCapitator extends JavaPlugin implements Listener {
    
        private static Plugin plugin;
    
        @Override
        public void onEnable() {
    //        plugin = this;
    //        Bukkit.getPluginManager().registerEvents(this, this);
            registerEvents(this, new Eventlistener());
            getConfig().options().copyDefaults(true);
            saveConfig();
    
        }
        //Much eaisier then registering events in 10 diffirent methods
    
        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for (Listener listener : listeners) {
                Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
        }
    
    //To access the plugin variable from other classes
        public static Plugin getPlugin() {
            return plugin;
        }
    
    }
    
    Sub class: (lisener)
    Code:
    package treecapitator;
    
    import java.util.Set;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Eventlistener extends JavaPlugin implements Listener {
    
        @EventHandler
        public boolean blockBreak(BlockBreakEvent event) {
            event.getPlayer().sendMessage("Yello");
            // Getting some standard inputs.
            Player player = event.getPlayer();
            Block block = event.getBlock();
    
            // Tree capitator
            // Get the list of tree config lists.
            Set<String> lists = getConfig().getConfigurationSection("list").getKeys(true);
            for (String list : lists) {
                String world = getConfig().getConfigurationSection("main." + list).getString("world");
                if (block.getWorld().toString().equals(world)) {
                    int configblock = getConfig().getConfigurationSection("main." + list).getInt("startblock");
                    if (block.getTypeId() == configblock) {
                        int log = configblock;
                        int leave = getConfig().getConfigurationSection("main." + list).getInt("leave");
                        int sapling = getConfig().getConfigurationSection("main." + list).getInt("sapling");
                        treeCap(block, log, leave, sapling);
    
                    }
                }
            }
            return true;
        }
    
        public void treeCap(Block block, int log, int leave, int sapling) {
            int x = block.getX();
            int y = block.getY();
            int z = block.getZ();
    
            // Break the block
            block.getWorld().getBlockAt(x, y, z).setType(Material.AIR);
    
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    for (int k = 0; k < 3; k++) {
                        Block tempBlock = block.getWorld().getBlockAt((x - 1 + k), (x - 2 + k), (z - 1 + i));
                        if (tempBlock.getTypeId() == (log) || tempBlock.getTypeId() == (leave)) {
                            treeCap(tempBlock, log, leave, sapling);
                        } else if (tempBlock.getTypeId() == (3)) {
                            tempBlock.setTypeId(sapling);
                        }
                    }
                }
    
            }
    
        }
    }
    Thanks :)
     
  2. Offline

    timtower Administrator Administrator Moderator

    Moved to Bukkit alternatives.
    @hopstar Do you have more plugins with the same main file installed?
     
  3. Offline

    hopstar

    No, this is the only plugin i use on the testserver
     
  4. Offline

    timtower Administrator Administrator Moderator

    @hopstar Why do you have 2 classes extending JavaPlugin?
     
  5. Offline

    hopstar

    If i don't do that the getConfig gives erros.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @hopstar And if you do it then it won't run. And if you would solve this error somehow then your getConfig would return null.
    Use a constructor
     
  7. Offline

    hopstar

    Uhmmm. So i make new class for the config and save the config on enable with a constructor?
     
  8. Offline

    timtower Administrator Administrator Moderator

    @hopstar No, you remove the extends Javaplugin on the listener class, then you can add a method there that returns a FileConfiguration and is called getConfig.
    And you add a constructor that takes the main plugin as argument. Save that plugin instance in the class
     
  9. Offline

    hopstar

    Sorry can you give me an example?
     
  10. Offline

    timtower Administrator Administrator Moderator

    @hopstar I will write it down step by step.
    1. Make a constructor with a JavaPlugin argument/parameter/what else you want to call it
    2. Make a field in the class.
    3. Give the field the value of the parameter.
    4. Constructor done.
    5. Make a function that returns a FileConfiguration and is called "getConfig"
    6. Fill in the function
     
  11. Offline

    hopstar

    Thanks for the help but i found a easier way i think. when i was writing yours i found the following commmand
    FileConfiguration config = TreeCapitator.getPlugin().getConfig();

    Only the eventLisiner is still not getting any events.
     
  12. Offline

    timtower Administrator Administrator Moderator

    @hopstar Besides that should give you a nullpointerexception with the current code. And that is using static which I dislike to use to get instances of plugins.
     
  13. Offline

    hopstar

    Oke if you say your way is better, then il use yours :).

    Just to verify
    step 1/4
    I create the constructor in the lisiner class.
    And where do i make that function from step 5?

    Sorry if im stupid. Im learing java on school atm and i wanted to bring that to bukkit plugin so i get more interest in it.
     
  14. Offline

    timtower Administrator Administrator Moderator

    @hopstar 1. In the listener class.
    2. Make sure to have a steady base of Java first, expand on that.
     
Thread Status:
Not open for further replies.

Share This Page