From 106fbfb8f9b88b3baff23c658b43745ae36d5e53 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 31 Dec 2017 07:31:22 +0000 Subject: [PATCH] Add attribute delegates to the API plugin --- .../org/apollo/game/plugins/api/attributes.kt | 45 +++++++++++++++++++ .../src/org/apollo/game/plugins/api/player.kt | 28 ++++++++++++ game/plugin/api/src/util.kt | 20 +++++++-- 3 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 game/plugin/api/src/org/apollo/game/plugins/api/attributes.kt create mode 100644 game/plugin/api/src/org/apollo/game/plugins/api/player.kt diff --git a/game/plugin/api/src/org/apollo/game/plugins/api/attributes.kt b/game/plugin/api/src/org/apollo/game/plugins/api/attributes.kt new file mode 100644 index 00000000..ee1dfd78 --- /dev/null +++ b/game/plugin/api/src/org/apollo/game/plugins/api/attributes.kt @@ -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 attribute(name: String, defaultValue: T): MobAttributeDelegate { + return MobAttributeDelegate(name, T::class, defaultValue, false) + } + + inline fun persistentAttribute(name: String, defaultValue: T): MobAttributeDelegate { + return MobAttributeDelegate(name, T::class, defaultValue, true) + } + +} + +class MobAttributeDelegate(val name: String, val type: KClass, 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) + } +} diff --git a/game/plugin/api/src/org/apollo/game/plugins/api/player.kt b/game/plugin/api/src/org/apollo/game/plugins/api/player.kt new file mode 100644 index 00000000..98e25c92 --- /dev/null +++ b/game/plugin/api/src/org/apollo/game/plugins/api/player.kt @@ -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) diff --git a/game/plugin/api/src/util.kt b/game/plugin/api/src/util.kt index 01264cbf..dd80ef59 100644 --- a/game/plugin/api/src/util.kt +++ b/game/plugin/api/src/util.kt @@ -1,9 +1,21 @@ -package org.apollo.game.plugin.api - -import java.util.Random +import java.lang.ref.WeakReference +import java.util.* +import kotlin.reflect.KProperty val RAND = Random() fun rand(bounds: Int): Int { return RAND.nextInt(bounds) -} \ No newline at end of file +} + +class WeakRefHolder(private var _value: WeakReference = 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) + } +}