mirror of
https://gitlab.com/2009scape/2009scape.git
synced 2025-12-17 12:00:17 -07:00
Added full seedling + sapling functionality
This commit is contained in:
parent
afbfb83423
commit
05ffc93515
6 changed files with 195 additions and 0 deletions
|
|
@ -804,6 +804,13 @@ public class Container {
|
|||
return get(getSlot(item));
|
||||
}
|
||||
|
||||
public Item get(Item item){
|
||||
for(Item i : items){
|
||||
if(item.getId() == i.getId()) return i;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next free slot.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
package core.game.node.entity.skill.farming
|
||||
|
||||
import core.game.interaction.NodeUsageEvent
|
||||
import core.game.interaction.UseWithHandler
|
||||
import core.game.node.item.Item
|
||||
import core.plugin.Initializable
|
||||
import core.plugin.Plugin
|
||||
import core.tools.Items
|
||||
|
||||
@Initializable
|
||||
class SeedOnPlantPot : UseWithHandler(Items.ACORN_5312,Items.WILLOW_SEED_5313,Items.MAPLE_SEED_5314,Items.YEW_SEED_5315,Items.MAGIC_SEED_5316,Items.APPLE_TREE_SEED_5283,Items.BANANA_TREE_SEED_5284,Items.ORANGE_TREE_SEED_5285,Items.CURRY_TREE_SEED_5286,Items.PINEAPPLE_SEED_5287,Items.PAPAYA_TREE_SEED_5288,Items.PALM_TREE_SEED_5289) {
|
||||
override fun newInstance(arg: Any?): Plugin<Any> {
|
||||
addHandler(Items.PLANT_POT_5354, ITEM_TYPE,this)
|
||||
return this
|
||||
}
|
||||
|
||||
override fun handle(event: NodeUsageEvent?): Boolean {
|
||||
val player = event?.player ?: return false
|
||||
val pot = event.usedItem ?: return false
|
||||
val seed = event.usedWith.asItem() ?: return false
|
||||
|
||||
if(!player.inventory.contains(Items.GARDENING_TROWEL_5325,1)){
|
||||
player.dialogueInterpreter.sendDialogue("You need a gardening trowel on you to do this.")
|
||||
return false
|
||||
}
|
||||
|
||||
val seedling = when(seed.id){
|
||||
Items.ACORN_5312 -> Items.OAK_SEEDLING_5358
|
||||
Items.WILLOW_SEED_5313 -> Items.WILLOW_SEEDLING_5359
|
||||
Items.MAPLE_SEED_5314 -> Items.MAPLE_SEEDLING_5360
|
||||
Items.YEW_SEED_5315 -> Items.YEW_SEEDLING_5361
|
||||
Items.MAGIC_SEED_5316 -> Items.MAGIC_SEEDLING_5362
|
||||
else -> return false
|
||||
}
|
||||
|
||||
if(player.inventory.remove(pot) && player.inventory.remove(seed)){
|
||||
player.inventory.add(Item(seedling))
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package core.game.node.entity.skill.farming
|
||||
|
||||
class Seedling(val id: Int, val TTL: Long, val sapling: Int) {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
package core.game.node.entity.skill.farming
|
||||
|
||||
import core.game.interaction.NodeUsageEvent
|
||||
import core.game.interaction.UseWithHandler
|
||||
import core.game.node.entity.state.newsys.states.SeedlingState
|
||||
import core.game.node.item.Item
|
||||
import core.plugin.Initializable
|
||||
import core.plugin.Plugin
|
||||
import core.tools.Items
|
||||
|
||||
private val cans = arrayListOf(Items.WATERING_CAN8_5340,Items.WATERING_CAN7_5339,Items.WATERING_CAN6_5338,Items.WATERING_CAN5_5337,Items.WATERING_CAN4_5336,Items.WATERING_CAN3_5335,Items.WATERING_CAN2_5334,Items.WATERING_CAN1_5333)
|
||||
private val seedlings = arrayListOf(Items.OAK_SEEDLING_5358,Items.WILLOW_SEEDLING_5359,Items.MAPLE_SEEDLING_5360,Items.YEW_SEEDLING_5361,Items.MAGIC_SEEDLING_5362)
|
||||
|
||||
@Initializable
|
||||
class SeedlingWaterer : UseWithHandler(*cans.toIntArray()) {
|
||||
|
||||
override fun newInstance(arg: Any?): Plugin<Any> {
|
||||
for(seed in seedlings) addHandler(seed, ITEM_TYPE,this)
|
||||
return this
|
||||
}
|
||||
|
||||
override fun handle(event: NodeUsageEvent?): Boolean {
|
||||
val player = event?.player ?: return false
|
||||
val seedling = event.used.asItem() ?: return false
|
||||
val can = event.usedWith.asItem() ?: return false
|
||||
|
||||
val nextCan = can.id.getNext()
|
||||
val wateredSeedling = seedling.id + 6
|
||||
|
||||
if(player.inventory.remove(can) && player.inventory.remove(seedling)){
|
||||
player.inventory.add(Item(wateredSeedling))
|
||||
player.inventory.add(Item(nextCan))
|
||||
|
||||
var state = player.states["seedling"] as SeedlingState?
|
||||
if(state == null){
|
||||
state = player.registerState("seedling") as SeedlingState?
|
||||
state?.addSeedling(wateredSeedling)
|
||||
state?.init()
|
||||
} else {
|
||||
state.addSeedling(wateredSeedling)
|
||||
}
|
||||
|
||||
player.sendMessage("You water the seedling.")
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun Int.getNext(): Int{
|
||||
var index = cans.indexOf(this)
|
||||
if(index == -1) return Items.WATERING_CAN_5331
|
||||
if(index != cans.size -1) return cans[index + 1] else return Items.WATERING_CAN_5331
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -168,6 +168,9 @@ object UseWithPatchHandler{
|
|||
p.plant(plantable)
|
||||
player.skills.addExperience(Skills.FARMING, plantable.plantingXP)
|
||||
p.setNewHarvestAmount()
|
||||
if(p.patch.type == PatchType.TREE || p.patch.type == PatchType.FRUIT_TREE){
|
||||
player.inventory.add(Item(Items.PLANT_POT_5356))
|
||||
}
|
||||
return true
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -0,0 +1,84 @@
|
|||
package core.game.node.entity.state.newsys.states
|
||||
|
||||
import core.game.node.entity.player.Player
|
||||
import core.game.node.entity.skill.farming.Seedling
|
||||
import core.game.node.entity.state.newsys.PlayerState
|
||||
import core.game.node.entity.state.newsys.State
|
||||
import core.game.node.item.Item
|
||||
import core.game.system.task.Pulse
|
||||
import core.tools.Items
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.json.simple.JSONArray
|
||||
import org.json.simple.JSONObject
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@PlayerState("seedling")
|
||||
class SeedlingState(player: Player? = null) : State(player) {
|
||||
val seedlings = ArrayList<Seedling>()
|
||||
|
||||
fun addSeedling(seedling: Int){
|
||||
seedlings.add(Seedling(seedling, System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5),seedling + 6))
|
||||
}
|
||||
|
||||
override fun save(root: JSONObject) {
|
||||
val seedArray = JSONArray()
|
||||
for(s in seedlings){
|
||||
val seed = JSONObject()
|
||||
seed.put("id",s.id)
|
||||
seed.put("ttl",s.TTL)
|
||||
seed.put("sapling",s.sapling)
|
||||
seedArray.add(seed)
|
||||
}
|
||||
root.put("seedlings",seedArray)
|
||||
}
|
||||
|
||||
override fun parse(_data: JSONObject) {
|
||||
if(_data.containsKey("seedlings")){
|
||||
(_data["seedlings"] as JSONArray).forEach {
|
||||
val s = it as JSONObject
|
||||
val id = s["id"].toString().toInt()
|
||||
val ttl = s["ttl"].toString().toLong()
|
||||
val sapling = s["sapling"].toString().toInt()
|
||||
seedlings.add(Seedling(id,ttl,sapling))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun newInstance(player: Player?): State {
|
||||
return SeedlingState(player)
|
||||
}
|
||||
|
||||
override fun createPulse() {
|
||||
if(seedlings.isEmpty()) return
|
||||
player ?: return
|
||||
|
||||
pulse = object : Pulse(5){
|
||||
override fun pulse(): Boolean {
|
||||
val removeList = ArrayList<Seedling>()
|
||||
GlobalScope.launch {
|
||||
for (seed in seedlings) {
|
||||
if (System.currentTimeMillis() > seed.TTL) {
|
||||
val inInventory = player.inventory.get(Item(seed.id))
|
||||
if (inInventory != null) {
|
||||
player.inventory.replace(Item(seed.sapling), inInventory.slot)
|
||||
removeList.add(seed)
|
||||
} else {
|
||||
val inBank = player.bank.get(Item(seed.id))
|
||||
if(inBank == null) removeList.add(seed)
|
||||
else {
|
||||
player.bank.replace(Item(seed.sapling), inBank.slot)
|
||||
removeList.add(seed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
seedlings.removeAll(removeList)
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue