mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-06 16:51:37 +00:00
Add attribute delegates to the API plugin
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
import org.apollo.game.model.entity.Mob
|
||||||
|
import org.apollo.game.model.entity.attr.*
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
object MobAttributeDelegators {
|
||||||
|
inline fun <reified T : Any> attribute(name: String, defaultValue: T): MobAttributeDelegate<T> {
|
||||||
|
return MobAttributeDelegate<T>(name, T::class, defaultValue, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <reified T : Any> persistentAttribute(name: String, defaultValue: T): MobAttributeDelegate<T> {
|
||||||
|
return MobAttributeDelegate<T>(name, T::class, defaultValue, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class MobAttributeDelegate<T : Any>(val name: String, val type: KClass<T>, val defaultValue: T, persistent: Boolean = true) {
|
||||||
|
init {
|
||||||
|
val attrType = when {
|
||||||
|
type == Int::class || type == Long::class -> AttributeType.LONG
|
||||||
|
type == Boolean::class -> AttributeType.BOOLEAN
|
||||||
|
type == Double::class -> AttributeType.DOUBLE
|
||||||
|
type == String::class -> AttributeType.STRING
|
||||||
|
else -> throw IllegalArgumentException("Can only store primitive attributes, not: ${type.qualifiedName}")
|
||||||
|
}
|
||||||
|
|
||||||
|
val definition = AttributeDefinition(defaultValue, if (persistent) AttributePersistence.PERSISTENT else AttributePersistence.TRANSIENT, attrType)
|
||||||
|
AttributeMap.define(name, definition)
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun getValue(thisRef: Mob, property: KProperty<*>): T {
|
||||||
|
return thisRef.attributes[name]?.value as T? ?: defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun setValue(thisRef: Mob, property: KProperty<*>, value: T) {
|
||||||
|
val attr = when {
|
||||||
|
type == Double::class || type == Int::class || type == Long::class -> NumericalAttribute(value as Number)
|
||||||
|
type == Boolean::class -> BooleanAttribute(value as Boolean)
|
||||||
|
type == String::class -> StringAttribute(value as String, false)
|
||||||
|
else -> throw IllegalArgumentException("Can only store primitive attributes, not: ${type.qualifiedName}")
|
||||||
|
}
|
||||||
|
|
||||||
|
thisRef.setAttribute(name, attr)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.apollo.game.plugins.api
|
||||||
|
|
||||||
|
import org.apollo.game.model.entity.Mob
|
||||||
|
import org.apollo.game.model.entity.Skill
|
||||||
|
import org.apollo.game.model.entity.SkillSet
|
||||||
|
|
||||||
|
val Mob.skills: SkillSet get() = skillSet
|
||||||
|
val SkillSet.attack: Skill get() = getSkill(Skill.ATTACK)
|
||||||
|
val SkillSet.defence: Skill get() = getSkill(Skill.DEFENCE)
|
||||||
|
val SkillSet.strength: Skill get() = getSkill(Skill.STRENGTH)
|
||||||
|
val SkillSet.hitpoints: Skill get() = getSkill(Skill.HITPOINTS)
|
||||||
|
val SkillSet.ranged: Skill get() = getSkill(Skill.RANGED)
|
||||||
|
val SkillSet.prayer: Skill get() = getSkill(Skill.PRAYER)
|
||||||
|
val SkillSet.magic: Skill get() = getSkill(Skill.MAGIC)
|
||||||
|
val SkillSet.cooking: Skill get() = getSkill(Skill.COOKING)
|
||||||
|
val SkillSet.woodcutting: Skill get() = getSkill(Skill.WOODCUTTING)
|
||||||
|
val SkillSet.fletching: Skill get() = getSkill(Skill.FLETCHING)
|
||||||
|
val SkillSet.fishing: Skill get() = getSkill(Skill.FISHING)
|
||||||
|
val SkillSet.firemaking: Skill get() = getSkill(Skill.FIREMAKING)
|
||||||
|
val SkillSet.crafting: Skill get() = getSkill(Skill.CRAFTING)
|
||||||
|
val SkillSet.smithing: Skill get() = getSkill(Skill.SMITHING)
|
||||||
|
val SkillSet.mining: Skill get() = getSkill(Skill.MINING)
|
||||||
|
val SkillSet.herblore: Skill get() = getSkill(Skill.HERBLORE)
|
||||||
|
val SkillSet.agility: Skill get() = getSkill(Skill.AGILITY)
|
||||||
|
val SkillSet.thieving: Skill get() = getSkill(Skill.THIEVING)
|
||||||
|
val SkillSet.slayer: Skill get() = getSkill(Skill.SLAYER)
|
||||||
|
val SkillSet.farming: Skill get() = getSkill(Skill.FARMING)
|
||||||
|
val SkillSet.runecraft: Skill get() = getSkill(Skill.RUNECRAFT)
|
||||||
@@ -1,9 +1,21 @@
|
|||||||
package org.apollo.game.plugin.api
|
import java.lang.ref.WeakReference
|
||||||
|
import java.util.*
|
||||||
import java.util.Random
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
val RAND = Random()
|
val RAND = Random()
|
||||||
|
|
||||||
fun rand(bounds: Int): Int {
|
fun rand(bounds: Int): Int {
|
||||||
return RAND.nextInt(bounds)
|
return RAND.nextInt(bounds)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class WeakRefHolder<T>(private var _value: WeakReference<T?> = WeakReference(null)) {
|
||||||
|
operator fun getValue(thisRef: Any?, property: KProperty<*>): T? {
|
||||||
|
return _value.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun setValue(
|
||||||
|
thisRef: Any?, property: KProperty<*>, value: T?
|
||||||
|
) {
|
||||||
|
_value = WeakReference(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user