
walkmc.entity.Entities.kt Maven / Gradle / Ivy
package walkmc.entity
import net.minecraft.server.*
import net.minecraft.server.World
import org.bukkit.*
import walkmc.extensions.*
import kotlin.collections.set
import kotlin.reflect.full.*
/**
* A registry used to register custom entities.
*/
object Entities {
/**
* Creates a entity instance without spawning it with the given [world].
*/
inline fun create(
world: World
): T = with(T::class) {
java.getDeclaredConstructor(World::class.java).newInstance(world) ?:
error("Cannot create instance for entity $this, because no constructor with ${World::class} has found")
}
/**
* Creates a entity instance and sets the entity position without spawning it
* in the world of the given [location].
*/
inline fun create(
location: Location
): T = create(location.world.handler).also {
it.setPosition(location.x, location.y, location.z)
}
/**
* Creates a entity instance and sets the entity position and spawns them in the given [location].
*/
inline fun spawn(
location: Location,
force: Boolean = false
): T = create(location).also { it.spawnInWorld(force) }
/**
* Register a new custom entity of the specified class.
*
* Note that the class must be annoted with [Mob].
*/
inline fun register() {
val klass = T::class
val mob = klass.findAnnotation() ?: error("The entity class $klass is not annoted with ${Mob::class}")
val name = mob.name.ifEmpty { klass.simpleName ?: klass.toString() }
val java = klass.java
EntityTypes.c[name] = java
EntityTypes.d[java] = name
EntityTypes.f[java] = mob.type.typeId.toInt()
}
}
/**
* Register a new custom entity of the specified class.
*
* Note that the class must be annoted with [Mob].
*/
inline fun registerEntity() = Entities.register()
© 2015 - 2025 Weber Informatics LLC | Privacy Policy