mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-05 00:38:14 +00:00
Update plugin test framework to junit5
Updates the testing infrastructure to use the latest relesae of junit and leverages the new extension mechanism to create an easy to use testing framework. Also adds additional test coverage for several plugins.
This commit is contained in:
@@ -8,6 +8,6 @@ enum class Gem(val id: Int) { // TODO add gem drop chances
|
||||
|
||||
companion object {
|
||||
private val GEMS = Gem.values().associateBy({ it.id }, { it })
|
||||
fun lookup(id: Int): Gem? = GEMS[id]
|
||||
operator fun get(id: Int): Gem? = GEMS[id]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
import org.apollo.game.action.ActionBlock
|
||||
import org.apollo.game.action.AsyncDistancedAction
|
||||
import org.apollo.game.message.impl.ObjectActionMessage
|
||||
import org.apollo.game.model.Position
|
||||
import org.apollo.game.model.World
|
||||
import org.apollo.game.model.entity.Player
|
||||
import org.apollo.game.model.entity.obj.GameObject
|
||||
import org.apollo.game.plugin.api.*
|
||||
import org.apollo.game.plugin.skills.mining.Ore
|
||||
import org.apollo.game.plugin.skills.mining.Pickaxe
|
||||
import org.apollo.net.message.Message
|
||||
import java.util.*
|
||||
|
||||
class MiningAction(
|
||||
player: Player,
|
||||
private val tool: Pickaxe,
|
||||
private val target: MiningTarget
|
||||
) : AsyncDistancedAction<Player>(PULSES, true, player, target.position, ORE_SIZE) {
|
||||
|
||||
companion object {
|
||||
private const val PULSES = 0
|
||||
private const val ORE_SIZE = 1
|
||||
|
||||
/**
|
||||
* Starts a [MiningAction] for the specified [Player], terminating the [Message] that triggered it.
|
||||
*/
|
||||
fun start(message: ObjectActionMessage, player: Player, ore: Ore) {
|
||||
val pickaxe = Pickaxe.bestFor(player)
|
||||
|
||||
if (pickaxe == null) {
|
||||
player.sendMessage("You do not have a pickaxe for which you have the level to use.")
|
||||
} else {
|
||||
val target = MiningTarget(message.id, message.position, ore)
|
||||
val action = MiningAction(player, pickaxe, target)
|
||||
|
||||
player.startAction(action)
|
||||
}
|
||||
|
||||
message.terminate()
|
||||
}
|
||||
}
|
||||
|
||||
override fun action(): ActionBlock = {
|
||||
mob.turnTo(position)
|
||||
|
||||
if (!target.skillRequirementsMet(mob)) {
|
||||
mob.sendMessage("You do not have the required level to mine this rock.")
|
||||
stop()
|
||||
}
|
||||
|
||||
mob.sendMessage("You swing your pick at the rock.")
|
||||
mob.playAnimation(tool.animation)
|
||||
|
||||
wait(tool.pulses)
|
||||
|
||||
if (!target.isValid(mob.world)) {
|
||||
stop()
|
||||
}
|
||||
|
||||
val successChance = rand(100)
|
||||
|
||||
if (target.isSuccessful(mob, successChance)) {
|
||||
if (mob.inventory.freeSlots() == 0) {
|
||||
mob.inventory.forceCapacityExceeded()
|
||||
stop()
|
||||
}
|
||||
|
||||
if (target.reward(mob)) {
|
||||
mob.sendMessage("You manage to mine some ${target.oreName()}")
|
||||
target.deplete(mob.world)
|
||||
|
||||
stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as MiningAction
|
||||
return mob == other.mob && target == other.target
|
||||
}
|
||||
|
||||
override fun hashCode(): Int = Objects.hash(mob, target)
|
||||
|
||||
}
|
||||
|
||||
data class MiningTarget(val objectId: Int, val position: Position, val ore: Ore) {
|
||||
|
||||
/**
|
||||
* Get the [GameObject] represented by this target.
|
||||
*
|
||||
* @todo: api: shouldn't be as verbose
|
||||
*/
|
||||
private fun getObject(world: World): GameObject? {
|
||||
val region = world.regionRepository.fromPosition(position)
|
||||
return region.findObject(position, objectId).orElse(null)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deplete this mining resource from the [World], and schedule it to be respawned
|
||||
* in a number of ticks specified by the [Ore].
|
||||
*/
|
||||
fun deplete(world: World) {
|
||||
world.expireObject(getObject(world)!!, ore.objects[objectId]!!, ore.respawn)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the [Player] was successful in mining this ore with a random success [chance] value between 0 and 100.
|
||||
*/
|
||||
fun isSuccessful(mob: Player, chance: Int): Boolean {
|
||||
val percent = (ore.chance * mob.mining.current + ore.chanceOffset) * 100
|
||||
return chance < percent
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this target is still valid in the [World] (i.e. has not been [deplete]d).
|
||||
*/
|
||||
fun isValid(world: World) = getObject(world) != null
|
||||
|
||||
/**
|
||||
* Get the normalized name of the [Ore] represented by this target.
|
||||
*/
|
||||
fun oreName() = Definitions.item(ore.id)!!.name.toLowerCase()
|
||||
|
||||
/**
|
||||
* Reward a [player] with experience and ore if they have the inventory capacity to take a new ore.
|
||||
*/
|
||||
fun reward(player: Player): Boolean {
|
||||
val hasInventorySpace = player.inventory.add(ore.id)
|
||||
|
||||
if (hasInventorySpace) {
|
||||
player.mining.experience += ore.exp
|
||||
}
|
||||
|
||||
return hasInventorySpace
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the [mob] has met the skill requirements to mine te [Ore] represented by
|
||||
* this [MiningTarget].
|
||||
*/
|
||||
fun skillRequirementsMet(mob: Player) = mob.mining.current < ore.level
|
||||
}
|
||||
|
||||
@@ -1,19 +1,5 @@
|
||||
import org.apollo.game.action.ActionBlock
|
||||
import org.apollo.game.action.AsyncDistancedAction
|
||||
import org.apollo.game.message.impl.ObjectActionMessage
|
||||
import org.apollo.game.model.Position
|
||||
import org.apollo.game.model.World
|
||||
import org.apollo.game.model.entity.Player
|
||||
import org.apollo.game.model.entity.obj.GameObject
|
||||
import org.apollo.game.plugin.api.Definitions
|
||||
import org.apollo.game.plugin.api.expireObject
|
||||
import org.apollo.game.plugin.api.findObject
|
||||
import org.apollo.game.plugin.api.mining
|
||||
import org.apollo.game.plugin.api.rand
|
||||
import org.apollo.game.plugin.skills.mining.Ore
|
||||
import org.apollo.game.plugin.skills.mining.Pickaxe
|
||||
import org.apollo.net.message.Message
|
||||
import java.util.Objects
|
||||
|
||||
on { ObjectActionMessage::class }
|
||||
.where { option == Actions.MINING }
|
||||
@@ -35,99 +21,6 @@ on { ObjectActionMessage::class }
|
||||
}
|
||||
}
|
||||
|
||||
class MiningAction(
|
||||
player: Player,
|
||||
private val tool: Pickaxe,
|
||||
private val target: MiningTarget
|
||||
) : AsyncDistancedAction<Player>(PULSES, true, player, target.position, ORE_SIZE) {
|
||||
|
||||
companion object {
|
||||
private const val PULSES = 0
|
||||
private const val ORE_SIZE = 1
|
||||
|
||||
/**
|
||||
* Starts a [MiningAction] for the specified [Player], terminating the [Message] that triggered it.
|
||||
*/
|
||||
fun start(message: ObjectActionMessage, player: Player, ore: Ore) {
|
||||
val pickaxe = Pickaxe.bestFor(player)
|
||||
|
||||
if (pickaxe == null) {
|
||||
player.sendMessage("You do not have a pickaxe for which you have the level to use.")
|
||||
} else {
|
||||
val target = MiningTarget(message.id, message.position, ore)
|
||||
val action = MiningAction(player, pickaxe, target)
|
||||
|
||||
player.startAction(action)
|
||||
}
|
||||
|
||||
message.terminate()
|
||||
}
|
||||
}
|
||||
|
||||
override fun action(): ActionBlock = {
|
||||
mob.turnTo(position)
|
||||
|
||||
val level = mob.mining.current
|
||||
if (level < target.ore.level) {
|
||||
mob.sendMessage("You do not have the required level to mine this rock.")
|
||||
stop()
|
||||
}
|
||||
|
||||
mob.sendMessage("You swing your pick at the rock.")
|
||||
mob.playAnimation(tool.animation)
|
||||
|
||||
wait(tool.pulses)
|
||||
|
||||
val obj = target.getObject(mob.world)
|
||||
if (obj == null) {
|
||||
stop()
|
||||
}
|
||||
|
||||
if (target.isSuccessful(mob)) {
|
||||
if (mob.inventory.freeSlots() == 0) {
|
||||
mob.inventory.forceCapacityExceeded()
|
||||
stop()
|
||||
}
|
||||
|
||||
if (mob.inventory.add(target.ore.id)) {
|
||||
val oreName = Definitions.item(target.ore.id)!!.name.toLowerCase()
|
||||
mob.sendMessage("You manage to mine some $oreName")
|
||||
|
||||
mob.mining.experience += target.ore.exp
|
||||
mob.world.expireObject(obj!!, target.ore.objects[target.objectId]!!, target.ore.respawn)
|
||||
stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as MiningAction
|
||||
return mob == other.mob && target == other.target
|
||||
}
|
||||
|
||||
override fun hashCode(): Int = Objects.hash(mob, target)
|
||||
|
||||
}
|
||||
|
||||
data class MiningTarget(val objectId: Int, val position: Position, val ore: Ore) {
|
||||
|
||||
fun getObject(world: World): GameObject? {
|
||||
val region = world.regionRepository.fromPosition(position)
|
||||
return region.findObject(position, objectId).orElse(null)
|
||||
}
|
||||
|
||||
fun isSuccessful(mob: Player): Boolean {
|
||||
val offset = if (ore.chanceOffset) 1 else 0
|
||||
val percent = (ore.chance * mob.mining.current + offset) * 100
|
||||
|
||||
return rand(100) < percent
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private object Actions {
|
||||
const val MINING = 1
|
||||
const val PROSPECTING = 2
|
||||
|
||||
@@ -17,12 +17,12 @@ enum class Ore(
|
||||
val exp: Double,
|
||||
val respawn: Int,
|
||||
val chance: Double,
|
||||
val chanceOffset: Boolean = false
|
||||
val chanceOffset: Double = 0.0
|
||||
) {
|
||||
CLAY(CLAY_OBJECTS, id = 434, level = 1, exp = 5.0, respawn = 1, chance = 0.0085, chanceOffset = true),
|
||||
COPPER(COPPER_OBJECTS, id = 436, level = 1, exp = 17.5, respawn = 4, chance = 0.0085, chanceOffset = true),
|
||||
TIN(TIN_OBJECTS, id = 438, level = 1, exp = 17.5, respawn = 4, chance = 0.0085, chanceOffset = true),
|
||||
IRON(IRON_OBJECTS, id = 440, level = 15, exp = 35.0, respawn = 9, chance = 0.0085, chanceOffset = true),
|
||||
CLAY(CLAY_OBJECTS, id = 434, level = 1, exp = 5.0, respawn = 1, chance = 0.0085, chanceOffset = 0.45),
|
||||
COPPER(COPPER_OBJECTS, id = 436, level = 1, exp = 17.5, respawn = 4, chance = 0.0085, chanceOffset = 0.45),
|
||||
TIN(TIN_OBJECTS, id = 438, level = 1, exp = 17.5, respawn = 4, chance = 0.0085, chanceOffset = 0.45),
|
||||
IRON(IRON_OBJECTS, id = 440, level = 15, exp = 35.0, respawn = 9, chance = 0.0085, chanceOffset = 0.45),
|
||||
COAL(COAL_OBJECTS, id = 453, level = 30, exp = 50.0, respawn = 50, chance = 0.004),
|
||||
GOLD(GOLD_OBJECTS, id = 444, level = 40, exp = 65.0, respawn = 100, chance = 0.003),
|
||||
SILVER(SILVER_OBJECTS, id = 442, level = 20, exp = 40.0, respawn = 100, chance = 0.0085),
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.apollo.game.plugin.api.mining
|
||||
enum class Pickaxe(val id: Int, val level: Int, animation: Int, val pulses: Int) {
|
||||
BRONZE(id = 1265, level = 1, animation = 625, pulses = 8),
|
||||
ITRON(id = 1267, level = 1, animation = 626, pulses = 7),
|
||||
STEEL(id = 1269, level = 1, animation = 627, pulses = 6),
|
||||
STEEL(id = 1269, level = 6, animation = 627, pulses = 6),
|
||||
MITHRIL(id = 1273, level = 21, animation = 629, pulses = 5),
|
||||
ADAMANT(id = 1271, level = 31, animation = 628, pulses = 4),
|
||||
RUNE(id = 1275, level = 41, animation = 624, pulses = 3);
|
||||
|
||||
Reference in New Issue
Block a user