Using config related methods from another class

Discussion in 'Plugin Development' started by deivisxm, Feb 1, 2014.

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

    deivisxm

    Hello im making mmorpg type plugin and i want spells to use mana. Mana of every player is stored in config and i have created methods removeMana(); and getMana(); they work fine in main class but I have class spelllistener and when i used these methods there, it gives me an error:
    Code:
    [19:12:06 ERROR]: Could not pass event InventoryClickEvent to mravian v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :1351) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.a(SourceFile:32)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.handle(SourceFil
    e:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.IllegalArgumentException: File cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbu
    kkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(Yam
    lConfiguration.java:171) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:118) ~
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:112) ~[cr
    aftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at com.gmail.blazer.SpellListener.getMana(SpellListener.java:132) ~[?:?]
     
            at com.gmail.blazer.SpellListener.removeMana(SpellListener.java:138) ~[?
    :?]
            at com.gmail.blazer.SpellListener.onInventoryClick(SpellListener.java:78
    ) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _45]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _45]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_45]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_45]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            ... 13 more
    Here are my methods. not sure anything else can help... (lines in error are in these methods)
    Code:java
    1. public Integer getMana(Player p){
    2. Integer mana= getConfig().getInt(p.getName() +".Mana");
    3.  
    4. return mana;
    5. }
    6. public void removeMana(Player p, Integer mana){
    7.  
    8. int ma =getMana(p)-mana;
    9. getConfig().set(p.getName() +".Mana", ma);
    10. }

    Any tips? I think something is wrong with getConfig().
     
  2. Offline

    Iaccidentally

    You need to call getConfig() from a reference to the main class. Pass the singleton instance to your listener when you register your events and then use it from there.
    Code:java
    1. //onEnable
    2. getServer().getPluginManager().registerEvents(new SpellListener(this), this);
    3. //in the listener
    4. private final <PluginName> plugin;
    5. public SpellListener(Plugin plugin) {
    6. this.plugin = plugin;
    7. }
    8.  

    You can then call plugin.getConfig()
     
Thread Status:
Not open for further replies.

Share This Page