mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-07 00:38:46 +00:00
Fix nitpicks in Fishing plugin
This commit is contained in:
@@ -6,44 +6,31 @@ import org.apollo.game.message.impl.NpcActionMessage
|
||||
import org.apollo.game.model.Position
|
||||
import org.apollo.game.model.entity.Player
|
||||
import org.apollo.game.plugin.api.fishing
|
||||
import org.apollo.game.plugin.api.rand
|
||||
import org.apollo.game.plugin.skills.fishing.FishingSpot
|
||||
import org.apollo.game.plugin.skills.fishing.FishingTool
|
||||
import java.util.Objects
|
||||
import java.util.Random
|
||||
|
||||
// TODO: moving fishing spots, seaweed and caskets, evil bob
|
||||
|
||||
/**
|
||||
* Intercepts the [NpcActionMessage] and starts a [FishingAction] if the npc
|
||||
*/
|
||||
on { NpcActionMessage::class }
|
||||
.where { option == 1 || option == 3 }
|
||||
.then { player ->
|
||||
val entity = player.world.npcRepository[index]
|
||||
val spot = FishingSpot.lookup(entity.id) ?: return@then
|
||||
|
||||
val option = spot.option(option)
|
||||
player.startAction(FishingAction(player, entity.position, option))
|
||||
|
||||
terminate()
|
||||
}
|
||||
|
||||
class FishingAction(player: Player, position: Position, val option: FishingSpot.Option) :
|
||||
AsyncDistancedAction<Player>(0, true, player, position, SPOT_DISTANCE) {
|
||||
|
||||
companion object {
|
||||
private const val SPOT_DISTANCE = 1
|
||||
private const val FISHING_DELAY = 4
|
||||
|
||||
/**
|
||||
* The random number generator used by the fishing plugin.
|
||||
*/
|
||||
private val random = Random()
|
||||
|
||||
/**
|
||||
* Returns whether or not the catch was successful.
|
||||
* TODO: We need to identify the correct algorithm for this
|
||||
*/
|
||||
private fun successfulCatch(level: Int, req: Int): Boolean = minOf(level - req + 5, 40) > random.nextInt(100)
|
||||
|
||||
/**
|
||||
* Returns whether or not the [Player] has (or does not need) bait.
|
||||
*/
|
||||
private fun hasBait(player: Player, bait: Int): Boolean = bait == -1 || player.inventory.contains(bait)
|
||||
|
||||
/**
|
||||
* @return if the player has the needed tool to fish at the spot.
|
||||
*/
|
||||
private fun hasTool(player: Player, tool: FishingTool): Boolean = player.equipment.contains(tool.id) ||
|
||||
player.inventory.contains(tool.id)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The [FishingTool] used for the fishing spot.
|
||||
*/
|
||||
@@ -75,10 +62,12 @@ class FishingAction(player: Player, position: Position, val option: FishingSpot.
|
||||
|
||||
if (mob.inventory.freeSlots() == 0) {
|
||||
mob.inventory.forceCapacityExceeded()
|
||||
|
||||
mob.stopAnimation()
|
||||
stop()
|
||||
} else if (!hasBait(mob, tool.bait)) {
|
||||
mob.sendMessage("You need more ${tool.baitName} to fish at this spot.")
|
||||
|
||||
mob.stopAnimation()
|
||||
stop()
|
||||
}
|
||||
@@ -90,21 +79,17 @@ class FishingAction(player: Player, position: Position, val option: FishingSpot.
|
||||
* Verifies that the player can gather fish from the [FishingSpot] they clicked.
|
||||
*/
|
||||
private fun verify(): Boolean {
|
||||
if (mob.fishing.current < option.level) {
|
||||
mob.sendMessage("You need a fishing level of ${option.level} to fish at this spot.")
|
||||
return false
|
||||
} else if (!hasTool(mob, tool)) {
|
||||
mob.sendMessage("You need a ${tool.formattedName} to fish at this spot.")
|
||||
return false
|
||||
} else if (!hasBait(mob, tool.bait)) {
|
||||
mob.sendMessage("You need some ${tool.baitName} to fish at this spot.")
|
||||
return false
|
||||
} else if (mob.inventory.freeSlots() == 0) {
|
||||
mob.inventory.forceCapacityExceeded()
|
||||
return false
|
||||
val current = mob.fishing.current
|
||||
|
||||
when {
|
||||
current < option.level -> mob.sendMessage("You need a fishing level of ${option.level} to fish at this spot.")
|
||||
!hasTool(mob, tool) -> mob.sendMessage("You need a ${tool.formattedName} to fish at this spot.")
|
||||
!hasBait(mob, tool.bait) -> mob.sendMessage("You need some ${tool.baitName} to fish at this spot.")
|
||||
mob.inventory.freeSlots() == 0 -> mob.inventory.forceCapacityExceeded()
|
||||
else -> return true
|
||||
}
|
||||
|
||||
return true
|
||||
return false
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
@@ -117,19 +102,27 @@ class FishingAction(player: Player, position: Position, val option: FishingSpot.
|
||||
|
||||
override fun hashCode(): Int = Objects.hash(option, position, mob)
|
||||
|
||||
}
|
||||
private companion object {
|
||||
private const val SPOT_DISTANCE = 1
|
||||
private const val FISHING_DELAY = 4
|
||||
|
||||
/**
|
||||
* Intercepts the [NpcActionMessage] and starts a [FishingAction] if the npc
|
||||
*/
|
||||
on { NpcActionMessage::class }
|
||||
.where { option == 1 || option == 3 }
|
||||
.then {
|
||||
val entity = it.world.npcRepository[index]
|
||||
val spot = FishingSpot.lookup(entity.id) ?: return@then
|
||||
/**
|
||||
* Returns whether or not the catch was successful.
|
||||
* TODO: We need to identify the correct algorithm for this
|
||||
*/
|
||||
private fun successfulCatch(level: Int, req: Int): Boolean = minOf(level - req + 5, 40) > rand(100)
|
||||
|
||||
val option = spot.option(option)
|
||||
it.startAction(FishingAction(it, entity.position, option))
|
||||
/**
|
||||
* Returns whether or not the [Player] has (or does not need) bait.
|
||||
*/
|
||||
private fun hasBait(player: Player, bait: Int): Boolean = bait == -1 || player.inventory.contains(bait)
|
||||
|
||||
/**
|
||||
* Returns whether or not the player has the required tool to fish at the spot.
|
||||
*/
|
||||
private fun hasTool(player: Player, tool: FishingTool): Boolean = player.equipment.contains(tool.id) ||
|
||||
player.inventory.contains(tool.id)
|
||||
|
||||
terminate()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user