Solved Registar event and Get config in same class

Discussion in 'Spigot Plugin Development' started by KingTux, May 25, 2016.

  1. My code
    Main class
    new FirstJoin(this);

    Code (Text):

    package me.KingLinux01;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    public class FirstJoin implements Listener {
       private ServerInfo plugin;

       public FirstJoin(ServerInfo pl) {
         plugin = pl;
       }
       public void FirstJoin(ServerInfo plugin) {
         plugin.getServer().getPluginManager().registerEvents(this, plugin);
       }
       @EventHandler
       public void FirstJoin(PlayerJoinEvent e){
         Player player = e.getPlayer();
         if(player.hasPlayedBefore() == false){
           player.sendMessage("Do /serverinfo to get some info about the server.");
           
           
         }
         
       }

    }

     
     
  2. sothatsit

    Patron

    You need to be more descriptive. You give like no information.

    But from guessing, i am assuming you want to access the configuration in the command, which you can do by using plugin.getConfiguration(). But what i'm wondering is why you have a constructor and a method with the same name, and the method takes the plugin again... Its just not necessary.

    You can remove the method and just register your events in the constructor of the class.

    Like this:
    Code (Text):
    public FirstJoin(ServerInfo pl) {
         plugin = pl;
       }
       public void FirstJoin(ServerInfo plugin) {
         plugin.getServer().getPluginManager().registerEvents(this, plugin);
       }
    to this:
    Code (Text):
    public FirstJoin(ServerInfo pl) {
         plugin = pl;
         Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
       }
     
    • Agree Agree x 1
  3. Choco

    Moderator

    public void FirstJoin(ServerInfo plugin) is not a constructor. That is a method with the name "FirstJoin". Remember that constructors do not contain returnable modifiers (void, any return object, etc.). Take the body of the FirstJoin method, and move it to your FirstJoin constructor

    EDIT: -,- Sniped again. :p
     
    • Agree Agree x 1
  4. Code (Text):

    Error occurred while enabling ServerInfo v1.2 (Is it up to date?)
    java.lang.NullPointerException
       at org.bukkit.plugin.SimplePluginManager.registerEvents(SimplePluginManager.java:523) ~[SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at me.KingLinux01.FirstJoin.<init>(FirstJoin.java:14) ~[?:?]
       at me.KingLinux01.ServerInfo.onEnable(ServerInfo.java:44) ~[?:?]
       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.craftbukkit.v1_9_R2.CraftServer.loadPlugin(CraftServer.java:362) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.craftbukkit.v1_9_R2.CraftServer.enablePlugins(CraftServer.java:322) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.craftbukkit.v1_9_R2.CraftServer.reload(CraftServer.java:746) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.Bukkit.reload(Bukkit.java:539) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:646) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1349) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1184) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_91]
       at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_91]
       at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]

     
     
  5. sothatsit

    Patron

    WE NEED MORE INFORMATION

    We are not genies, what is at line 14 of your class?
     
  6. Sorry
    Code (Text):

        Bukkit.getServer().getPluginManager().registerEvents(this, plugin);

     
     
  7. sothatsit

    Patron

    You are passing in null to the constructor, you need to pass in an instance of your plugin, or 'this' if you are creating FirstJoin from your plugins class.
     
  8. Code (Text):

      public FirstJoin(ServerInfo pl) {
        Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
        plugin = pl;

       }
     
     
  9. Choco

    Moderator

    Put "plugin = pl" one line higher. "plugin" is null if you're only initializing it after you register the event. This is basic Java syntax. Sequence
     
  10. Thank you .