This commit is contained in:
Gary Tierney
2019-08-04 02:00:48 +01:00
parent 12b4bef1f8
commit 8c50d3e091
20 changed files with 284 additions and 122 deletions
@@ -4,6 +4,8 @@ import java.lang.IllegalArgumentException
import org.apollo.cache.def.ItemDefinition
import org.apollo.cache.def.NpcDefinition
import org.apollo.cache.def.ObjectDefinition
import org.intellij.lang.annotations.Language
import java.util.regex.Pattern
/**
* Provides plugins with access to item, npc, and object definitions
@@ -76,14 +78,18 @@ object Definitions {
return findEntity(NpcDefinition::getDefinitions, NpcDefinition::getName, name)
}
fun npcs(@Language("RegExp") pattern: String): Sequence<NpcDefinition> {
return findEntities(NpcDefinition::getDefinitions, NpcDefinition::getName, pattern)
}
/**
* The [Regex] used to match 'names' that have an id attached to the end.
*/
private val ID_REGEX = Regex(".+_[0-9]+$")
private fun <T : Any> findEntity(
private inline fun <T : Any> findEntity(
definitionsProvider: () -> Array<T>,
nameSupplier: T.() -> String,
crossinline nameSupplier: T.() -> String,
name: String
): T? {
val definitions = definitionsProvider()
@@ -98,7 +104,22 @@ object Definitions {
return definitions[id]
}
val normalizedName = name.replace('_', ' ')
return definitions.firstOrNull { it.nameSupplier().equals(normalizedName, ignoreCase = true) }
return findEntities(definitionsProvider, nameSupplier, name).firstOrNull()
}
private inline fun <T: Any> findEntities(
definitionsProvider: () -> Array<T>,
crossinline nameSupplier: T.() -> String,
regexp: String
) : Sequence<T> {
val definitions = definitionsProvider().asSequence()
val pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE)
return definitions.filter {
val name = it.nameSupplier()
val matcher = pattern.matcher(name)
matcher.matches()
}
}
}
+1
View File
@@ -5,6 +5,7 @@ dependencies {
implementation project(':cache')
implementation project(':net')
implementation project(':util')
implementation project(':game:plugin:api')
testImplementation project(':game:plugin-testing')
}
+16 -24
View File
@@ -1,38 +1,31 @@
import org.apollo.game.action.DistancedAction
import org.apollo.game.message.impl.NpcActionMessage
import org.apollo.game.message.impl.ObjectActionMessage
import org.apollo.game.model.Position
import org.apollo.game.model.entity.Npc
import org.apollo.game.model.entity.Player
import org.apollo.game.model.inter.bank.BankUtils
import org.apollo.game.plugin.api.Definitions.npcs
import org.apollo.game.plugin.kotlin.message.action.npc.NpcAction
import org.apollo.game.plugin.kotlin.message.action.obj.InteractiveObject
import org.apollo.game.plugin.kotlin.message.action.obj.ObjectAction
import org.apollo.net.message.Message
val BANK_BOOTH_ID = 2213
enum class QuickBankObject(override val id: Int) : InteractiveObject {
BankBooth(2213)
}
/**
* Hook into the [ObjectActionMessage] and listen for when a bank booth's second action ("Open Bank") is selected.
*/
on { ObjectActionMessage::class }
.where { option == 2 && id == BANK_BOOTH_ID }
.then { BankAction.start(this, it, position) }
on(ObjectAction, "Open Bank", objects = QuickBankObject.values()) {
BankAction.start(player, target.position)
}
val bankerNpcs = npcs("(Gnome )?Banker")
/**
* Hook into the [NpcActionMessage] and listen for when a banker's second action ("Open Bank") is selected.
*/
on { NpcActionMessage::class }
.where { option == 2 }
.then {
val npc = it.world.npcRepository[index]
if (npc.id in BANKER_NPCS) {
BankAction.start(this, it, npc.position)
}
}
/**
* The ids of all banker [Npcs][Npc].
*/
val BANKER_NPCS = setOf(166, 494, 495, 496, 497, 498, 499, 1036, 1360, 1702, 2163, 2164, 2354, 2355, 2568, 2569, 2570)
on(NpcAction, "Open Bank", bankerNpcs) {
BankAction.start(player, target.position)
}
/**
* A [DistancedAction] that opens a [Player]'s bank when they get close enough to a booth or banker.
@@ -51,9 +44,8 @@ class BankAction(player: Player, position: Position) : DistancedAction<Player>(0
/**
* Starts a [BankAction] for the specified [Player], terminating the [Message] that triggered.
*/
fun start(message: Message, player: Player, position: Position) {
fun start(player: Player, position: Position) {
player.startAction(BankAction(player, position))
message.terminate()
}
}
+21 -23
View File
@@ -1,33 +1,31 @@
gradle.projectsEvaluated {
configure(subprojects.findAll { it.buildFile.exists() }) { subproj ->
apply from: "$rootDir/gradle/kotlin.gradle"
configure(subprojects.findAll { it.buildFile.exists() }) { subproj ->
apply plugin: "kotlin"
sourceSets {
main {
kotlin {
srcDirs += "src"
}
}
test {
kotlin {
srcDirs += "test"
}
sourceSets {
main {
kotlin {
srcDirs += "src"
}
}
test {
useJUnitPlatform()
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.8"
kotlin {
srcDirs += "test"
}
}
}
dependencies {
implementation group: 'com.google.guava', name: 'guava', version: guavaVersion
tasks.withType(Test) {
useJUnitPlatform()
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
dependencies {
kotlinScriptDef(":game")
}
}
+4 -5
View File
@@ -1,10 +1,9 @@
import org.apollo.game.message.impl.ButtonMessage
import org.apollo.game.plugin.kotlin.message.ButtonClick
import org.apollo.game.plugin.kotlin.message.on
val WALK_BUTTON_ID = 152
val RUN_BUTTON_ID = 153
on { ButtonMessage::class }
.where { widgetId == WALK_BUTTON_ID || widgetId == RUN_BUTTON_ID }
.then {
it.toggleRunning()
}
on(ButtonClick, WALK_BUTTON_ID) { player.toggleRunning() }
on(ButtonClick, RUN_BUTTON_ID) { player.toggleRunning() }