From 3a9e4351898fc1f4f9093a0887aa4f07d57cc732 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sat, 16 Sep 2017 22:34:24 +0100 Subject: [PATCH] Add an await function to the AsyncActionRunner trait --- game/plugin/api/build.gradle | 4 +++ .../apollo/game/plugins/api/definitions.kt | 19 ++++++++++++++ .../src/org/apollo/game/plugins/api/player.kt | 26 +++++++++++++++++++ .../apollo/game/action/AsyncActionRunner.kt | 17 +++++++++--- 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 game/plugin/api/build.gradle create mode 100644 game/plugin/api/src/org/apollo/game/plugins/api/definitions.kt create mode 100644 game/plugin/api/src/org/apollo/game/plugins/api/player.kt diff --git a/game/plugin/api/build.gradle b/game/plugin/api/build.gradle new file mode 100644 index 00000000..fb25c053 --- /dev/null +++ b/game/plugin/api/build.gradle @@ -0,0 +1,4 @@ +plugin { + name = "Apollo Plugin API" + description = "Helpers and API for common plugin usecases" +} \ No newline at end of file diff --git a/game/plugin/api/src/org/apollo/game/plugins/api/definitions.kt b/game/plugin/api/src/org/apollo/game/plugins/api/definitions.kt new file mode 100644 index 00000000..3de58a11 --- /dev/null +++ b/game/plugin/api/src/org/apollo/game/plugins/api/definitions.kt @@ -0,0 +1,19 @@ +package org.apollo.game.plugins.api + +import org.apollo.cache.def.ItemDefinition +import org.apollo.cache.def.NpcDefinition +import org.apollo.cache.def.ObjectDefinition + +object Definitions { + fun item(id: Int): ItemDefinition? { + return ItemDefinition.lookup(id) + } + + fun obj(id: Int): ObjectDefinition? { + return ObjectDefinition.lookup(id) + } + + fun npc(id: Int): NpcDefinition? { + return NpcDefinition.lookup(id) + } +} \ No newline at end of file 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..d6b46151 --- /dev/null +++ b/game/plugin/api/src/org/apollo/game/plugins/api/player.kt @@ -0,0 +1,26 @@ +package org.apollo.game.plugins.api + +import org.apollo.game.model.entity.Skill +import org.apollo.game.model.entity.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) \ No newline at end of file diff --git a/game/src/main/kotlin/org/apollo/game/action/AsyncActionRunner.kt b/game/src/main/kotlin/org/apollo/game/action/AsyncActionRunner.kt index 366af4e2..e4d3ab5f 100644 --- a/game/src/main/kotlin/org/apollo/game/action/AsyncActionRunner.kt +++ b/game/src/main/kotlin/org/apollo/game/action/AsyncActionRunner.kt @@ -1,10 +1,10 @@ package org.apollo.game.action -import kotlinx.coroutines.experimental.* +import kotlinx.coroutines.experimental.CommonPool +import kotlinx.coroutines.experimental.Job import kotlinx.coroutines.experimental.channels.Channel +import kotlinx.coroutines.experimental.launch import kotlinx.coroutines.experimental.selects.select -import org.apollo.game.model.entity.Mob -import java.util.function.Supplier class AsyncActionRunner(val actionSupplier: () -> Action<*>, val callback: suspend () -> Unit) { var job: Job? = null @@ -53,4 +53,15 @@ class AsyncActionRunner(val actionSupplier: () -> Action<*>, val callback: suspe remainingPulses -= numPulses } } + + suspend fun await(condition: () -> Boolean, timeout: Int = 15) { + var remainingPulsesBeforeTimeout = timeout + + while (!condition.invoke()) { + remainingPulsesBeforeTimeout -= pulseChannel.receive() + if (remainingPulsesBeforeTimeout <= 0) { + break + } + } + } } \ No newline at end of file