Home > OS >  Reference field in the Interface subclass (Kotlin)
Reference field in the Interface subclass (Kotlin)

Time:07-15

Is there a possible way to reference interface's static field in the class, which implements interface without importing, or calling referencing to interface explicitly.

I want to do like this:

interface Globals {
    companion object {
        val mc get() = Bloomware.mc
        val cPlayer get() = mc.player!!
        val cWorld get() = mc.world!!

        fun sendPacket(p: Packet<*>?) {
            mc.networkHandler!!.sendPacket(p)
        }
    }
}

(Class user)

object ResourceCancel : Module(), Globals {
    @Subscribe
    private fun onPacketSend(event: EventPacket.Send) {
        cPlayer.jump() // call this
}

I know it's possible in Java, but i can't repeat the same in Kotlin

CodePudding user response:

I'm pretty sure there's no way to do this in Kotlin.

The closest I think you could get is to define your global stuff at the top level in its own package, and then import that package.* in files where you want to access them.

package com.foo.globals

val mc get() = Bloomware.mc
val cPlayer get() = mc.player!!
val cWorld get() = mc.world!!

fun sendPacket(p: Packet<*>?) {
    mc.networkHandler!!.sendPacket(p)
}
import com.foo.globals.*

object ResourceCancel : Module() {
    @Subscribe
    private fun onPacketSend(event: EventPacket.Send) {
        cPlayer.jump() // call this
}
  • Related