diff --git a/game/plugin/entity/following/src/following.plugin.kts b/game/plugin/entity/following/src/following.plugin.kts index 3fc65c1f..25e97d82 100644 --- a/game/plugin/entity/following/src/following.plugin.kts +++ b/game/plugin/entity/following/src/following.plugin.kts @@ -1,7 +1,8 @@ +import org.apollo.game.plugin.entity.player_action.PlayerActionEvent import org.apollo.game.plugin.entity.player_action.PlayerActionType import org.apollo.plugin.entity.following.FollowAction -on_player_event { org.apollo.game.plugin.entity.player_action.PlayerActionEvent::class } +on_player_event { PlayerActionEvent::class } .where { action == PlayerActionType.FOLLOW } .then { FollowAction.start(it, target) diff --git a/game/plugin/entity/player-action/src/player_action.kt b/game/plugin/entity/player-action/src/PlayerAction.kt similarity index 67% rename from game/plugin/entity/player-action/src/player_action.kt rename to game/plugin/entity/player-action/src/PlayerAction.kt index 2b5c230d..22026b62 100644 --- a/game/plugin/entity/player-action/src/player_action.kt +++ b/game/plugin/entity/player-action/src/PlayerAction.kt @@ -3,35 +3,29 @@ package org.apollo.game.plugin.entity.player_action import org.apollo.game.message.impl.SetPlayerActionMessage import org.apollo.game.model.entity.Player import org.apollo.game.model.event.PlayerEvent -import java.util.EnumSet - -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) -} +import java.util.* class PlayerActionEvent(player: Player, val target: Player, val action: PlayerActionType) : PlayerEvent(player) -private val playerActionsMap = mutableMapOf>() -private val Player.actions: EnumSet - get() = playerActionsMap.computeIfAbsent(this, { EnumSet.noneOf(PlayerActionType::class.java) }) - fun Player.enableAction(action: PlayerActionType) { send(SetPlayerActionMessage(action.displayName, action.slot, action.primary)) - actions.add(action) + actions += action } fun Player.disableAction(action: PlayerActionType) { send(SetPlayerActionMessage("null", action.slot, action.primary)) - actions.remove(action) + actions -= action } fun Player.actionEnabled(action: PlayerActionType): Boolean { - return actions.contains(action) + return action in actions } fun Player.actionAt(slot: Int): PlayerActionType? { return actions.find { it.slot == slot } -} \ No newline at end of file +} + +private val playerActionsMap = mutableMapOf>() + +private val Player.actions: EnumSet + get() = playerActionsMap.computeIfAbsent(this) { EnumSet.noneOf(PlayerActionType::class.java) } diff --git a/game/plugin/entity/player-action/src/PlayerActionType.kt b/game/plugin/entity/player-action/src/PlayerActionType.kt new file mode 100644 index 00000000..b4ac185f --- /dev/null +++ b/game/plugin/entity/player-action/src/PlayerActionType.kt @@ -0,0 +1,8 @@ +package org.apollo.game.plugin.entity.player_action + +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) +} \ No newline at end of file diff --git a/game/plugin/entity/player-action/src/player_action.plugin.kts b/game/plugin/entity/player-action/src/PlayerActions.plugin.kts similarity index 66% rename from game/plugin/entity/player-action/src/player_action.plugin.kts rename to game/plugin/entity/player-action/src/PlayerActions.plugin.kts index b3859a79..6b4a0fe4 100644 --- a/game/plugin/entity/player-action/src/player_action.plugin.kts +++ b/game/plugin/entity/player-action/src/PlayerActions.plugin.kts @@ -1,9 +1,7 @@ +package org.apollo.game.plugin.entity.player_action + import org.apollo.game.message.impl.PlayerActionMessage import org.apollo.game.model.event.impl.LoginEvent -import org.apollo.game.plugin.entity.player_action.PlayerActionEvent -import org.apollo.game.plugin.entity.player_action.PlayerActionType -import org.apollo.game.plugin.entity.player_action.actionAt -import org.apollo.game.plugin.entity.player_action.enableAction on { PlayerActionMessage::class } .then { diff --git a/game/plugin/entity/player-action/test/PlayerActionTests.kt b/game/plugin/entity/player-action/test/PlayerActionTests.kt new file mode 100644 index 00000000..4c1dc248 --- /dev/null +++ b/game/plugin/entity/player-action/test/PlayerActionTests.kt @@ -0,0 +1,34 @@ +package org.apollo.game.plugin.entity.player_action + +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." } + } + +} \ No newline at end of file diff --git a/game/src/main/java/org/apollo/game/message/impl/SetPlayerActionMessage.java b/game/src/main/java/org/apollo/game/message/impl/SetPlayerActionMessage.java index 4ebae8c3..4db7ceef 100644 --- a/game/src/main/java/org/apollo/game/message/impl/SetPlayerActionMessage.java +++ b/game/src/main/java/org/apollo/game/message/impl/SetPlayerActionMessage.java @@ -2,6 +2,8 @@ package org.apollo.game.message.impl; import org.apollo.net.message.Message; +import java.util.Objects; + /** * A {@link Message} sent by the client to add an action to the menu when a player right-clicks another. * @@ -37,8 +39,8 @@ public final class SetPlayerActionMessage extends Message { /** * Creates the set player action message. * - * @param text The action text. - * @param slot The menu slot. + * @param text The action text. + * @param slot The menu slot. * @param primaryInteraction Whether or not the action is the primary action. */ public SetPlayerActionMessage(String text, int slot, boolean primaryInteraction) { @@ -75,4 +77,20 @@ public final class SetPlayerActionMessage extends Message { return primaryAction; } + @Override + public boolean equals(Object o) { + if (o instanceof SetPlayerActionMessage) { + SetPlayerActionMessage other = (SetPlayerActionMessage) o; + return slot == other.slot && primaryAction == other.primaryAction && Objects.equals(text, other.text); + + } + + return false; + } + + @Override + public int hashCode() { + return Objects.hash(text, slot, primaryAction); + } + } \ No newline at end of file