1.16.1 Why Am I Getting An IllegalPluginAccessException?

Discussion in 'Spigot Plugin Development' started by SmushyTaco, Jul 13, 2020.

  1. IllegalPluginAccessException:
    Code (Text):
    [02:25:11 ERROR]: Error occurred while enabling FirstPlugin v1.0-SNAPSHOT (Is it up to date?)
    org.bukkit.plugin.IllegalPluginAccessException: java.lang.NullPointerException
            at org.bukkit.plugin.SimplePluginManager.getEventListeners(SimplePluginManager.java:688) ~[patched_1.16.1.jar:git-Paper-90]
            at org.bukkit.plugin.SimplePluginManager.registerEvents(SimplePluginManager.java:640) ~[patched_1.16.1.jar:git-Paper-90]
            at com.smushytaco.plugin.Plugin.onEnable(Plugin.kt:10) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[patched_1.16.1.jar:git-Paper-90]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:380) ~[patched_1.16.1.jar:git-Paper-90]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:483) ~[patched_1.16.1.jar:git-Paper-90]
            at org.bukkit.craftbukkit.v1_16_R1.CraftServer.enablePlugin(CraftServer.java:500) ~[patched_1.16.1.jar:git-Paper-90]
            at org.bukkit.craftbukkit.v1_16_R1.CraftServer.enablePlugins(CraftServer.java:414) ~[patched_1.16.1.jar:git-Paper-90]
            at net.minecraft.server.v1_16_R1.MinecraftServer.loadWorld(MinecraftServer.java:471) ~[patched_1.16.1.jar:git-Paper-90]
            at net.minecraft.server.v1_16_R1.DedicatedServer.init(DedicatedServer.java:241) ~[patched_1.16.1.jar:git-Paper-90]
            at net.minecraft.server.v1_16_R1.MinecraftServer.v(MinecraftServer.java:940) ~[patched_1.16.1.jar:git-Paper-90]
            at net.minecraft.server.v1_16_R1.MinecraftServer.lambda$a$0(MinecraftServer.java:177) ~[patched_1.16.1.jar:git-Paper-90]
            at java.lang.Thread.run(Thread.java:832) [?:?]
    ServerBroadCastEvent (My custom event):
    Code (Kotlin):
    package com.smushytaco.plugin.custom.events
    import org.bukkit.entity.Player
    import org.bukkit.event.Cancellable
    import org.bukkit.event.Event
    import org.bukkit.event.HandlerList
    class ServerBroadcastEvent(val player: Player, val message: String) : Event(), Cancellable {
        private var isCancelled = false
        companion object {
            private val HANDLERS = HandlerList()
        }
        private fun getHandlerList(): HandlerList = HANDLERS
        override fun getHandlers(): HandlerList = HANDLERS
        override fun setCancelled(cancel: Boolean) {
            isCancelled = cancel
        }
        override fun isCancelled(): Boolean = isCancelled
    }
    ServerBroadcastCommand:
    Code (Kotlin):
    package com.smushytaco.plugin.commands
    import com.smushytaco.plugin.commands.library.StandardMessages
    import com.smushytaco.plugin.custom.events.ServerBroadcastEvent
    import org.bukkit.Bukkit
    import org.bukkit.command.Command
    import org.bukkit.command.CommandExecutor
    import org.bukkit.command.CommandSender
    import org.bukkit.entity.Player
    object ServerBroadcastCommand : CommandExecutor {
        const val COMMAND = "broadcast"
        override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
            if (sender !is Player) {
                sender.sendMessage(StandardMessages.onlyPlayersCanUseThisCommand(COMMAND))
                return true
            }
            val stringBuilder = StringBuilder()
            for (index in args.indices) {
                stringBuilder.append(args[index])
                if (index != args.indices.last) stringBuilder.append(' ')
            }
            val message = stringBuilder.toString()

            val serverBroadcastEvent = ServerBroadcastEvent(sender, message)
            Bukkit.getPluginManager().callEvent(serverBroadcastEvent)
            if (!serverBroadcastEvent.isCancelled) Bukkit.broadcastMessage(message)
            return true
        }
    }
     
    Main Plugin Class:
    Code (Kotlin):
    package com.smushytaco.plugin
    import com.smushytaco.plugin.commands.ServerBroadcastCommand
    import org.bukkit.Bukkit
    import org.bukkit.plugin.java.JavaPlugin
    class Plugin : JavaPlugin() {
        override fun onEnable() {
            getCommand(ServerBroadcastCommand.COMMAND)?.setExecutor(ServerBroadcastCommand)
            Bukkit.getPluginManager().registerEvents(CustomServerBroadcastEventListener, this)
        }
    }
    Where exactly did I go wrong?
     
  2. That's not how you register custom events, you're trying to register a listener which normally would be a class that implements Listener
     
  3. I wasn't registering a custom event in onEnable(). I was registering a listener. Give the code another read.
     
  4. Code (Kotlin):
    package com.smushytaco.plugin.custom.events
    import org.bukkit.entity.Player
    import org.bukkit.event.Cancellable
    import org.bukkit.event.Event
    import org.bukkit.event.HandlerList
    class ServerBroadcastEvent(val player: Player, val message: String) : Event(), Cancellable {
        private var isCancelled = false
        companion object {
            private val HANDLERS = HandlerList()
            @JvmStatic private fun getHandlerList(): HandlerList = HANDLERS
        }
        override fun getHandlers(): HandlerList = HANDLERS
        override fun setCancelled(cancel: Boolean) {
            isCancelled = cancel
        }
        override fun isCancelled(): Boolean = isCancelled
    }
    Had to move getHandlerList() into the companion object and put the @JvmStatic annotation on it!
     
    • Informative Informative x 1