Repackage player actions plugin

This commit is contained in:
Major-
2018-08-28 18:50:54 +01:00
parent c89179825c
commit 1a4724dcce
7 changed files with 7 additions and 9 deletions
+10
View File
@@ -0,0 +1,10 @@
apply plugin: 'kotlin'
dependencies {
implementation project(':game')
implementation project(':cache')
implementation project(':net')
implementation project(':util')
testImplementation project(':game:plugin-testing')
}
@@ -0,0 +1,31 @@
package org.apollo.game.plugin.entity.actions
import org.apollo.game.message.impl.SetPlayerActionMessage
import org.apollo.game.model.entity.Player
import org.apollo.game.model.event.PlayerEvent
import java.util.*
class PlayerActionEvent(player: Player, val target: Player, val action: PlayerActionType) : PlayerEvent(player)
fun Player.enableAction(action: PlayerActionType) {
send(SetPlayerActionMessage(action.displayName, action.slot, action.primary))
actions += action
}
fun Player.disableAction(action: PlayerActionType) {
send(SetPlayerActionMessage("null", action.slot, action.primary))
actions -= action
}
fun Player.actionEnabled(action: PlayerActionType): Boolean {
return action in actions
}
fun Player.actionAt(slot: Int): PlayerActionType? {
return actions.find { it.slot == slot }
}
private val playerActionsMap = mutableMapOf<Player, EnumSet<PlayerActionType>>()
private val Player.actions: EnumSet<PlayerActionType>
get() = playerActionsMap.computeIfAbsent(this) { EnumSet.noneOf(PlayerActionType::class.java) }
@@ -0,0 +1,8 @@
package org.apollo.game.plugin.entity.actions
enum class PlayerActionType(val displayName: String, val slot: Int, val primary: Boolean = true) {
ATTACK("Attack", 2),
CHALLENGE("Challenge", 2),
FOLLOW("Follow", 4),
TRADE("Trade with", 5)
}
@@ -0,0 +1,20 @@
package org.apollo.game.plugin.entity.actions
import org.apollo.game.message.impl.PlayerActionMessage
import org.apollo.game.model.event.impl.LoginEvent
on { PlayerActionMessage::class }
.then {
val action = it.actionAt(option)
if (action != null) {
it.world.submit(PlayerActionEvent(it, it.world.playerRepository[index], action))
}
terminate()
}
on_player_event { LoginEvent::class }
.then {
it.enableAction(PlayerActionType.FOLLOW)
it.enableAction(PlayerActionType.TRADE)
}
@@ -0,0 +1,34 @@
package org.apollo.game.plugin.entity.actions
import io.mockk.verify
import org.apollo.game.message.impl.SetPlayerActionMessage
import org.apollo.game.model.entity.Player
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.EnumSource
@ExtendWith(ApolloTestingExtension::class)
class PlayerActionTests {
@TestMock
lateinit var player: Player
@ParameterizedTest
@EnumSource(PlayerActionType::class)
fun `enabling and disabling PlayerActions sends SetPlayerActionMessages`(type: PlayerActionType) {
player.enableAction(type)
verify { player.send(eq(SetPlayerActionMessage(type.displayName, type.slot, type.primary))) }
assertTrue(player.actionEnabled(type)) { "Action $type should have been enabled, but was not." }
player.disableAction(type)
verify { player.send(eq(SetPlayerActionMessage("null", type.slot, type.primary))) }
assertFalse(player.actionEnabled(type)) { "Action $type should not have been enabled, but was." }
}
}