Multiple Listeners in Kotlin

Discussion in 'Plugin Development' started by Cruxial, Oct 14, 2021.

  1. Offline

    Cruxial

    Hey, I have recently started getting into Minecraft Plugin Development, and I am currently trying to implement a handful of listeners in my project. Though, I can't seem to make this work in Kotlin. I stayed up until like 2am last night trying to fix this lol.

    I keep getting a null reference error, and I'm really not sure what's causing it. The EventHandler in my main class seems to work fine, but whenever I try to extend it to external classes, I get the null reference error I was talking about.

    Main class:
    Code:
    class MinecraftStatsListener : JavaPlugin(), Listener {
    
        override fun onEnable() {
            // Plugin startup logic
            send("Plugin Enabled.");
    
            val pm = Bukkit.getServer().pluginManager
            val plugin = this;
    
            val join = Join(this)
    
            pm.registerEvents(this, this)
            //pm.registerEvents(join, plugin)
        }
    
        @EventHandler
        public fun onBlockBreak(e: BlockBreakEvent) {
            val p = e.player
            val b = e.block
    
            send("${b.type} broken by ${p.displayName}!")
            send("${p.displayName} joined the game.")
        }
    
        override fun onDisable() {
            // Plugin shutdown logic
            send("Plugin Disabled.")
        }
    }
    Listener Class:
    Code:
    public class Join constructor(i: MinecraftStatsListener) : Listener {
    
        init {
            i.server.pluginManager.registerEvents(this, i)
        }
    
        @EventHandler
        public fun onLogin(e: PlayerJoinEvent) {
    
            val user: PluginUser = PluginUser()
            user.uuid = e.player.uniqueId
            user.name = e.player.displayName
            user.playerTime = e.player.playerTime
    
            val apiRequest: PluginUserRequest = PluginUserRequest()
            apiRequest.sendPostRequest(user)
    
            send("${user.name} joined the server.")
    
        }
    }
     
  2. Offline

    Kars

    Show the error.

    Edit:
    It is probably either:
    • Your listener class does not have a default constructor
    • The way you pass in and interact with "i" in the second class
     
    Last edited: Oct 14, 2021
  3. Offline

    Cruxial

    Code:
    [18:20:55] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to MinecraftStatsListener v1.0-SNAPSHOT
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.players.PlayerList.a(PlayerList.java:276) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.network.LoginListener.a(LoginListener.java:192) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.network.LoginListener.c(LoginListener.java:179) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.network.LoginListener.tick(LoginListener.java:74) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.network.NetworkManager.a(NetworkManager.java:246) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.network.ServerConnection.c(ServerConnection.java:172) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:1336) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.dedicated.DedicatedServer.b(DedicatedServer.java:438) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:1217) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1050) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:305) ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at java.lang.Thread.run(Thread.java:833) [?:?]
    
     
  4. Online

    timtower Administrator Administrator Moderator

    @Cruxial Any reason why you are using Kotlin? Doesn't make debugging easier...
     
  5. Offline

    Kars

    @Cruxial we're gonna need line numbers of your code also.
    Did you happen to see the edits to my last post?
     
  6. Offline

    Cruxial

    Yeah I saw it now. I posted my reply a few minutes after you made your's. Moderator approvement just took a while.
    I think I might have found my issue by looking at a refrence plugin I found on github.
    I'll update the thread when I get to test my supposed solution.

    I use Kotlin for the features and its simplified language structure.
    I understand that it can come with complications with Bukkit though, and I'm willing to suffer through that lol

    Alright, so I have tried the stuff I found from the reference plugin, but to no avail.

    Regarding this, I have tried registering the plugins in my main class, also didn't work.
    I am still getting the same error posted earlier, but yeah, Kotlin stacktraces aren't exactly helpful...

    I'll be posting my current code in 2 seperate pastebin links so you can see the line numbers.

    Main class
    Listener class

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 15, 2021
  7. Offline

    Kars

    @Cruxial it could be this line right here.
    PHP:
    Bukkit.getPluginManager().registerEvents(Jointhis)
    I'm sure Join is not instantiated correctly there but i could be wrong.
     
  8. Offline

    Cruxial

    Yeah, Kotlin does not have the parentheses as class instantiators.
     

Share This Page