Add API plugin with common functionality

This commit is contained in:
Gary Tierney
2017-09-17 03:09:47 +01:00
parent 3a9e435189
commit cf0e3331e0
7 changed files with 99 additions and 9 deletions
@@ -1,8 +1,10 @@
package org.apollo.game.plugins.api
import org.apollo.game.model.entity.Player
import org.apollo.game.model.entity.Skill
import org.apollo.game.model.entity.SkillSet
val Player.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)
@@ -0,0 +1,7 @@
import java.util.*
val RAND = Random()
fun rand(bounds: Int): Int {
return RAND.nextInt(bounds)
}
@@ -0,0 +1,61 @@
import org.apollo.game.model.Position
import org.apollo.game.model.World
import org.apollo.game.model.area.Region
import org.apollo.game.model.entity.Entity
import org.apollo.game.model.entity.EntityType
import org.apollo.game.model.entity.EntityType.DYNAMIC_OBJECT
import org.apollo.game.model.entity.EntityType.STATIC_OBJECT
import org.apollo.game.model.entity.obj.DynamicGameObject
import org.apollo.game.model.entity.obj.GameObject
import org.apollo.game.model.entity.obj.StaticGameObject
import org.apollo.game.scheduling.ScheduledTask
import java.lang.IllegalArgumentException
import java.util.*
import java.util.stream.Stream
fun <T : Entity> Region.find(position: Position, pred: (T) -> Boolean, vararg types: EntityType): Stream<T> {
val result = this.getEntities<T>(position, *types)
return result.stream()
.filter(pred::invoke)
}
fun Region.findObjects(position: Position, id: Int): Stream<GameObject> {
return find<GameObject>(position, { it.id == id }, DYNAMIC_OBJECT, STATIC_OBJECT)
}
fun Region.findObject(position: Position, id: Int): Optional<GameObject> {
return find<GameObject>(position, { it.id == id }, DYNAMIC_OBJECT, STATIC_OBJECT)
.findFirst()
}
class ExpireObjectTask(
val world: World,
val obj: GameObject,
val replacement: GameObject,
val respawnDelay: Int
) : ScheduledTask(0, true) {
private var respawning: Boolean = false
override fun execute() {
val region = world.regionRepository.fromPosition(obj.position)
if (!respawning) {
world.spawn(replacement)
respawning = true
setDelay(respawnDelay)
} else {
region.removeEntity(replacement, false)
world.spawn(obj)
stop()
}
}
}
fun World.expireObject(obj: GameObject, replacement: Int, respawnDelay: Int) {
val replacementObj = DynamicGameObject.createPublic(this, replacement, obj.position, obj.type, obj.orientation)
val respawnedObj = DynamicGameObject.createPublic(this, obj.id, obj.position, obj.type, obj.orientation)
schedule(ExpireObjectTask(this, obj, replacementObj, respawnDelay))
}