Solved getConfig() in seperate classes

Discussion in 'Plugin Development' started by MrSparkzz, Mar 20, 2013.

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

    MrSparkzz

    Code:java
    1.  
    2. public class PlayerListener implements Listener {
    3.  
    4. @EventHandler
    5. public void onPlayerJoin(PlayerJoinEvent e) {
    6. Player p = e.getPlayer();
    7. p.sendMessage(getConfig().getString("motd.ingame"));
    8. }
    9. }
    10.  


    I've been stuck on this problem for a few hours now. The problem is getting getConfig().getString("") to work.

    The first thing I tried was extending JavaPlugin. That got rid of the error, but when I test it out, I get an error in the console. I've narrowed it down to the line that contains getConfig().

    It works perfectly fine when I put the events in the Main class. But not when it's in the PlayerListener class. And I want to have separate classes because this is going to be a very large plugin.

    If you know how to use getConfig().getString("") in multiple classes that would be a great help!
     
  2. Offline

    HeyShibby

    possible show us the log of the error shown in console?
     
  3. Offline

    Tirelessly

    You can't have two classes extending JavaPlugin.

    MrSparkzz pass an instance of your main class into your listener through it's constructor, assign that instance to a class variable, then use variable.getConfig()
     
  4. Offline

    chasechocolate

    MrSparkzz Here is what Tirelessly said, but in code:
    Code:java
    1. private <main class> plugin;
    2. public PlayerListener(<main class> plugin){
    3. this.plugin = plugin;
    4. }
     
    MrSparkzz likes this.
  5. Offline

    Tirelessly

    MrSparkzz This is the usual way, but if you're going to use more than two classes, it's easier to make a utility class with this constructor which makes the plugin reference static. Then have static methods which mirror those of the JavaPlugin class. Example:
    Code:java
    1.  
    2. public class BukkitUtils {
    3.  
    4. private static ServerProtect plugin;
    5.  
    6. public static void setPlugin(ServerProtect plugin){
    7. BukkitUtils.plugin = plugin;
    8. }
    9.  
    10. public static Server getServer(){
    11. return plugin.getServer();
    12. }
    13.  
    14. public static FileConfiguration getConfig(){
    15. return plugin.getConfig();
    16. }
    17.  
    18. public static void saveConfig(){
    19. plugin.saveConfig();
    20. }
    21.  


    I got this idea from Towny, so credit to elgarL or whoever was developing that at the time. It makes it easier to access everything without needing to use that constructor every time.
     
    MrSparkzz likes this.
  6. Offline

    MrSparkzz

    chasechocolate & Tirelessly I made the BukkitUtils class and set it up with no errors. But now in the console I'm having an error getting the Config.

    Code:cmd
    1.  
    2. 22:07:44 [SEVERE] Could not pass event PlayerJoinEvent to ServerControl v1.0.0 org.bukkit.event.EventException
    3. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    4. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    5. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    6. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    7. at net.minecraft.server.v1_5_R1.PlayerList.c(PlayerList.java:204)
    8. at net.minecraft.server.v1_5_R1.PlayerList.a(PlayerList.java:100)
    9. at net.minecraft.server.v1_5_R1.PendingConnection.d(PendingConnection.java:129)
    10. at net.minecraft.server.v1_5_R1.PendingConnection.c(PendingConnection.java:44)
    11. at net.minecraft.server.v1_5_R1.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:41)
    12. at net.minecraft.server.v1_5_R1.DedicatedServerConnection.b(SourceFile:29)
    13. at net.minecraft.server.v1_5_R1.MinecraftServer.r(MinecraftServer.java:580)
    14. at net.minecraft.server.v1_5_R1.DedicatedServer.r(DedicatedServer.java:225)
    15. at net.minecraft.server.v1_5_R1.MinecraftServer.q(MinecraftServer.java:476)
    16. at net.minecraft.server.v1_5_R1.MinecraftServer.run(MinecraftServer.java:409)
    17. at net.minecraft.server.v1_5_R1.ThreadServerApplication.run(SourceFile:573)
    18.  
    19. Caused by: java.lang.NullPointerException
    20. at com.sparkzz.util.BukkitUtils.getConfig(BukkitUtils.java:20)
    21. at com.sparkzz.util.PlayerListener.onPlayerJoin(PlayerListener.java:15)
    22. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    23. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    24. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    25. at java.lang.reflect.Method.invoke(Unknown Source)
    26. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    27. ... 14 more
    28.  


    I will try some things, but you guys were a lot of help so far, and I'm sure you know how to fix this.

    Edit: getConfig() only works in the Main class. I've solved that so far.
     
  7. Offline

    Tirelessly

    Did you ever call setPlugin(JavaPlugin plugin)? Most likely not.
     
  8. Offline

    MrSparkzz

    Nevermind, figured it out, sorry for the trouble. Thanks for the help though!
     
Thread Status:
Not open for further replies.

Share This Page