Repackage api plugin

This commit is contained in:
Major-
2018-08-23 18:29:36 +01:00
parent 4689ba9db6
commit cfddd05499
10 changed files with 83 additions and 94 deletions
@@ -3,6 +3,7 @@ package org.apollo.game.plugin.api
import org.apollo.cache.def.ItemDefinition
import org.apollo.cache.def.NpcDefinition
import org.apollo.cache.def.ObjectDefinition
import java.lang.IllegalArgumentException
object Definitions {
fun item(id: Int): ItemDefinition? {
@@ -22,11 +23,7 @@ object Definitions {
}
fun npc(id: Int): NpcDefinition? {
try {
return NpcDefinition.lookup(id)
} catch (e: NullPointerException) {
throw RuntimeException("Failed to find npc $id: count=${NpcDefinition.count()}")
}
return NpcDefinition.lookup(id)
}
fun npc(name: String): NpcDefinition? {
@@ -43,11 +40,16 @@ object Definitions {
nameSupplier: T.() -> String,
name: String
): T? {
val definitions = definitionsProvider.invoke()
val definitions = definitionsProvider()
if (ID_REGEX matches name) {
val id = name.substring(name.lastIndexOf('_') + 1, name.length).toInt()
return definitions.getOrNull(id)
val id = name.substring(name.lastIndexOf('_') + 1, name.length).toIntOrNull()
if (id == null || id >= definitions.size) {
throw IllegalArgumentException("Error while searching for definition: invalid id suffix in $name.")
}
return definitions[id]
}
val normalizedName = name.replace('_', ' ')
@@ -1,4 +1,4 @@
package org.apollo.game.plugins.api
package org.apollo.game.plugin.api
import org.apollo.game.model.Position
@@ -2,7 +2,7 @@ package org.apollo.game.plugin.api
import java.util.*
public val RAND = Random()
val RAND = Random()
fun rand(bounds: Int): Int {
return RAND.nextInt(bounds)
@@ -10,23 +10,34 @@ 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.scheduling.ScheduledTask
import java.util.Optional
import java.util.stream.Stream
fun <T : Entity> Region.find(position: Position, predicate: (T) -> Boolean, vararg types: EntityType): Stream<T> {
val result = getEntities<T>(position, *types)
return result.stream().filter(predicate::invoke)
fun <T : Entity> Region.find(position: Position, predicate: (T) -> Boolean, vararg types: EntityType): Sequence<T> {
return getEntities<T>(position, *types).asSequence().filter(predicate)
}
fun Region.findObjects(position: Position, id: Int): Stream<GameObject> {
fun Region.findObjects(position: Position, id: Int): Sequence<GameObject> {
return find(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()
fun Region.findObject(position: Position, id: Int): GameObject? {
return find<GameObject>(position, { it.id == id }, DYNAMIC_OBJECT, STATIC_OBJECT).firstOrNull()
}
fun World.findObject(position: Position, objectId: Int): GameObject? {
return regionRepository.fromPosition(position).findObject(position, objectId)
}
fun World.findObjects(position: Position, id: Int): Sequence<GameObject> {
return regionRepository.fromPosition(position).findObjects(position, id)
}
fun World.expireObject(obj: GameObject, replacement: Int, respawnDelay: Int) {
val replacementObj = DynamicGameObject.createPublic(this, replacement, obj.position, obj.type, obj.orientation)
schedule(ExpireObjectTask(this, obj, replacementObj, respawnDelay))
}
class ExpireObjectTask(
private val world: World,
private val existing: GameObject,
@@ -49,10 +60,4 @@ class ExpireObjectTask(
stop()
}
}
}
fun World.expireObject(obj: GameObject, replacement: Int, respawnDelay: Int) {
val replacementObj = DynamicGameObject.createPublic(this, replacement, obj.position, obj.type, obj.orientation)
schedule(ExpireObjectTask(this, obj, replacementObj, respawnDelay))
}
}