Encapsulation from main class

Discussion in 'Plugin Development' started by mkuilen, Jul 10, 2020.

  1. Offline

    mkuilen

    Hello,
    I want to use data from the Plugin.YML file outside of the main class. To achieve this, I'm using encapsulation.
    Main class:
    Code:
        private PluginDescriptionFile pdf;
    
        public PluginDescriptionFile getPDF(){
            return this.getDescription();
        }
    Command class:
    Code:
    public class Version implements CommandExecutor{
      
        EndWars pdf = new EndWars();
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("ewversion")){
                sender.sendMessage("You're running version: " + pdf.getPDF().getVersion());
            }
            return true;
        }
    
    }
    When the plugin loads, the server throws the error:
    Code:
    [14:43:32 ERROR]: Error occurred while enabling EndWars v0.1-DEV (Is it up to date?)
    java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            at nl.[name].endwars.EndWars.<init>(EndWars.java:41) ~[?:?]
            at nl.[name].endwars.commands.Version.<init>(Version.java:12) ~[?:?]
            at nl.[name].endwars.EndWars.onEnable(EndWars.java:56) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_231]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_231]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot.jar:git-Spigot-76236cb-9cd1111]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.IllegalStateException: Initial initialization
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            at nl.[name].endwars.EndWars.<init>(EndWars.java:41) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_231]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_231]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_231]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:739) ~[spigot.jar:git-Spigot-76236cb-9cd1111]
            ... 17 more
    
    
    Line 12 in Version.class is causing the error, which is:
    Code:
    EndWars pdf = new EndWars();
    To my understanding, this line is initializing the main class a second time which is probably confusing the server about which class is the main class.

    Does anyone know how to access a variable from the main class?
    AFAIK the PluginDescriptionFile method file is only available in the main class.

    Sorry for any grammar mistakes, English is not my native language.
     
  2. Offline

    timtower Moderator Moderator

    @mkuilen Pass the main class along using a constructor instead.
     

Share This Page