Solved getConfig() from other classes

Discussion in 'Plugin Development' started by stamline, Apr 21, 2014.

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

    stamline

    Hi, i have a problem....
    I will make a tokens plugin and save all information in a config file... I started easy and tried to make when a player join it will check if the player are in the config file, if it´s not it will put the player in it.

    Here´s the code:

    Code:java
    1. package me.stamline.games.Events;
    2.  
    3.  
    4. import me.stamline.games.Main;
    5.  
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerJoinEvent;
    10.  
    11. public class OnJoin implements Listener{
    12. Main plugin;
    13. @EventHandler
    14. public void onJoin(PlayerJoinEvent event){
    15. Player player = event.getPlayer();
    16. if(!plugin.getConfig().contains("Tokens." + player.getName())){
    17. plugin.getConfig().set("Tokens." + player.getName(), 0);
    18. player.sendMessage("TEST");
    19. }
    20. }
    21. }
    22.  


    And here´s the console:

    Code:
    [15:36:31] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to Events v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[spigot.jar:git-Spigot-1377]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-1377]
        at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30) ~[spigot.jar:git-Spigot-1377]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-1377]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.PlayerList.c(PlayerList.java:249) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.PlayerList.a(PlayerList.java:135) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.LoginListener.c(LoginListener.java:97) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.LoginListener.a(LoginListener.java:42) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.NetworkManager.a(NetworkManager.java:151) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.ServerConnection.c(ServerConnection.java:77) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.MinecraftServer.v(MinecraftServer.java:703) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.DedicatedServer.v(DedicatedServer.java:273) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.MinecraftServer.u(MinecraftServer.java:566) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:472) [spigot.jar:git-Spigot-1377]
        at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [spigot.jar:git-Spigot-1377]
    Caused by: java.lang.NullPointerException
        at me.stamline.games.Events.OnJoin.onJoin(OnJoin.java:16) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[spigot.jar:git-Spigot-1377]
        ... 15 more
    [15:36:31] [Server thread/INFO]: stamline[/192.168.1.222:59238] logged in with entity id 6848 at ([world] -19.464196768890766, 15.0, 16.297411649112554)
     
  2. Offline

    2MBKindiegames

    Hi,
    are you sure the 'Main plugin' ever receives a value?
    I normally use
    Code:java
    1. private MainClass plugin;
    2.  
    3. public Class(MainClass plugin) {
    4. this.plugin = plugin;
    5. }
     
  3. Offline

    thisguy128512

    You do not appear to have a constructor, and you never assign a value to (Main)plugin. You should at some point probably add this:

    Code:java
    1. public OnJoin(Main p) {
    2. this.plugin = p;
    3. }


    And when you register the listener, do it something like this:

    Code:java
    1. this.getServer().getPluginManager().registerEvents(this, new OnJoin(this));
     
  4. Offline

    xAstraah

    Code:
    public MainClass plugin;
     
    public NextClass(MainClass plugin)
    {
        this.plugin = plugin;
    }
     
  5. Offline

    stamline

    thanks
     
  6. Offline

    2MBKindiegames

    stamline You're welcome!

    -If you ever need my help again, just PM ME or create a new Topic
     
    stamline likes this.
Thread Status:
Not open for further replies.

Share This Page