Added UseWithListener.kt

This commit is contained in:
Ceikry 2021-03-12 23:20:47 -06:00
parent ffe7888ace
commit c6498ba920
12 changed files with 106 additions and 42 deletions

View file

@ -15,7 +15,9 @@ import core.net.packet.IoBuffer;
import core.net.packet.PacketRepository; import core.net.packet.PacketRepository;
import core.net.packet.context.PlayerContext; import core.net.packet.context.PlayerContext;
import core.net.packet.out.ClearMinimapFlag; import core.net.packet.out.ClearMinimapFlag;
import org.rs09.consts.Items;
import rs09.game.interaction.ItemOnBankBooth; import rs09.game.interaction.ItemOnBankBooth;
import rs09.game.interaction.Listeners;
import rs09.game.node.entity.skill.farming.CompostBins; import rs09.game.node.entity.skill.farming.CompostBins;
import rs09.game.node.entity.skill.farming.FarmingPatch; import rs09.game.node.entity.skill.farming.FarmingPatch;
import rs09.game.node.entity.skill.farming.UseWithBinHandler; import rs09.game.node.entity.skill.farming.UseWithBinHandler;
@ -23,7 +25,6 @@ import rs09.game.node.entity.skill.farming.UseWithPatchHandler;
import rs09.game.system.SystemLogger; import rs09.game.system.SystemLogger;
import rs09.game.system.command.rottenpotato.RottenPotatoUseWithHandler; import rs09.game.system.command.rottenpotato.RottenPotatoUseWithHandler;
import rs09.game.world.repository.Repository; import rs09.game.world.repository.Repository;
import org.rs09.consts.Items;
/** /**
* The incoming item reward packet. * The incoming item reward packet.
@ -68,6 +69,9 @@ public class ItemActionPacket implements IncomingPacket {
if(PluginInteractionManager.handle(player,event)){ if(PluginInteractionManager.handle(player,event)){
return; return;
} }
if(Listeners.run(item,npc,2,player)){
return;
}
event = new NodeUsageEvent(player, interfaceId, item, npc); event = new NodeUsageEvent(player, interfaceId, item, npc);
try { try {
UseWithHandler.run(event); UseWithHandler.run(event);
@ -124,6 +128,9 @@ public class ItemActionPacket implements IncomingPacket {
RottenPotatoUseWithHandler.handle(used,player); RottenPotatoUseWithHandler.handle(used,player);
return; return;
} }
if(Listeners.run(used,with,0,player)){
return;
}
if (usedItemId < usedWithItemId) { if (usedItemId < usedWithItemId) {
item = used; item = used;
used = with; used = with;
@ -172,6 +179,9 @@ public class ItemActionPacket implements IncomingPacket {
RottenPotatoUseWithHandler.handle(object,player); RottenPotatoUseWithHandler.handle(object,player);
return; return;
} }
if(Listeners.run(used,object,1,player)){
return;
}
event = new NodeUsageEvent(player, 0, used, object); event = new NodeUsageEvent(player, 0, used, object);
/** /**

View file

@ -8,7 +8,6 @@ import core.game.node.item.Item
import core.game.world.map.Direction import core.game.world.map.Direction
import core.game.world.map.Location import core.game.world.map.Location
import core.game.world.update.flag.context.Animation import core.game.world.update.flag.context.Animation
import core.plugin.Initializable
import org.rs09.consts.Items import org.rs09.consts.Items
import rs09.game.interaction.OptionListener import rs09.game.interaction.OptionListener
import java.util.* import java.util.*
@ -17,7 +16,6 @@ import java.util.*
* Handles repairing and climbing of the 3 beacon shortcuts needed to access them * Handles repairing and climbing of the 3 beacon shortcuts needed to access them
* @author Ceikry * @author Ceikry
*/ */
@Initializable
class AFURepairClimbHandler : OptionListener() { class AFURepairClimbHandler : OptionListener() {
val repairIDs = intArrayOf(38480,38470,38494) val repairIDs = intArrayOf(38480,38470,38494)

View file

@ -22,7 +22,6 @@ import kotlin.math.ceil
* Option handler for fishing trawler * Option handler for fishing trawler
* @author Ceikry * @author Ceikry
*/ */
@Initializable
class FishingTrawlerOptionHandler : OptionListener() { class FishingTrawlerOptionHandler : OptionListener() {
val ENTRANCE_PLANK = 2178 val ENTRANCE_PLANK = 2178
val EXIT_PLANK = 2179 val EXIT_PLANK = 2179

View file

@ -0,0 +1,5 @@
package rs09.game.interaction
interface Listener {
fun defineListeners()
}

View file

@ -6,7 +6,8 @@ import core.game.node.Node
import core.game.node.entity.player.Player import core.game.node.entity.player.Player
object Listeners { object Listeners {
private val listeners = HashMap<String,(Player, Node) -> Boolean>() private val listeners = HashMap<String,(Player, Node) -> Boolean>(1000)
private val useWithListeners = HashMap<String,(Player,Node,Node) -> Boolean>(1000)
@JvmStatic @JvmStatic
fun add(id: Int, type: Int, option: Array<out String>, method: (Player,Node) -> Boolean){ fun add(id: Int, type: Int, option: Array<out String>, method: (Player,Node) -> Boolean){
@ -36,6 +37,23 @@ object Listeners {
} }
} }
@JvmStatic
fun add(used: Int, with: Int, type: Int, method: (Player,Node,Node) -> Boolean){
useWithListeners["$used:$with:$type"] = method
}
@JvmStatic
fun add(type: Int, used: Int, with: IntArray, method: (Player, Node, Node) -> Boolean){
for(id in with){
useWithListeners["$used:$with:$type"] = method
}
}
@JvmStatic
fun get(used: Int, with: Int, type: Int): ((Player,Node,Node) -> Boolean)?{
return useWithListeners["$used:$with:$type"]
}
@JvmStatic @JvmStatic
fun get(id: Int, type: Int, option: String): ((Player,Node) -> Boolean)?{ fun get(id: Int, type: Int, option: String): ((Player,Node) -> Boolean)?{
return listeners["$id:$type:${option.toLowerCase()}"] return listeners["$id:$type:${option.toLowerCase()}"]
@ -46,6 +64,34 @@ object Listeners {
return listeners["$type:${option.toLowerCase()}"] return listeners["$type:${option.toLowerCase()}"]
} }
@JvmStatic
fun run(used: Node, with: Node, type: Int,player: Player): Boolean{
val flag = when(type){
2 -> DestinationFlag.ENTITY
1 -> DestinationFlag.OBJECT
else -> DestinationFlag.OBJECT
}
var flipped = false
val method = get(used.id,with.id,type) ?: get(with.id,used.id,type).also { flipped = true } ?: return false
if(type != 0) {
player.pulseManager.run(object : MovementPulse(player, with, flag) {
override fun pulse(): Boolean {
player.faceLocation(with.location)
if(flipped) method.invoke(player,with,used)
else method.invoke(player,used,with)
return true
}
})
} else {
if(flipped) method.invoke(player,with,used)
else method.invoke(player,used,with)
}
return true
}
@JvmStatic @JvmStatic
fun run(id: Int, type: Int, option: String, player: Player, node: Node): Boolean{ fun run(id: Int, type: Int, option: String, player: Player, node: Node): Boolean{
val flag = when(type){ val flag = when(type){

View file

@ -3,11 +3,10 @@ package rs09.game.interaction
import core.game.node.Node import core.game.node.Node
import core.game.node.entity.player.Player import core.game.node.entity.player.Player
abstract class OptionListener { abstract class OptionListener : Listener{
val ITEM = 0 val ITEM = 0
val OBJECT = 1 val OBJECT = 1
val NPC = 2 val NPC = 2
abstract fun defineListeners()
fun on(id: Int, type: Int, vararg option: String,handler: (Player, Node) -> Boolean){ fun on(id: Int, type: Int, vararg option: String,handler: (Player, Node) -> Boolean){
Listeners.add(id,type,option,handler) Listeners.add(id,type,option,handler)
} }

View file

@ -0,0 +1,16 @@
package rs09.game.interaction
import core.game.node.Node
import core.game.node.entity.player.Player
abstract class UseWithListener : Listener {
val ITEM = 0
val OBJECT = 1
val NPC = 2
fun on(used: Int, with: Int, type: Int, handler: (Player, Node, Node) -> Boolean){
Listeners.add(used,with,type,handler)
}
fun on(type: Int,used: Int,vararg with: Int, handler: (Player, Node, Node) -> Boolean){
Listeners.add(type,used,with,handler)
}
}

View file

@ -1,7 +1,6 @@
package rs09.game.interaction.city package rs09.game.interaction.city
import core.game.world.map.Location import core.game.world.map.Location
import core.plugin.Initializable
import rs09.game.interaction.OptionListener import rs09.game.interaction.OptionListener
/** /**
@ -10,7 +9,6 @@ import rs09.game.interaction.OptionListener
* @author Sir Kermit * @author Sir Kermit
*/ */
@Initializable
class IsafdarListeners : OptionListener() { class IsafdarListeners : OptionListener() {
val CAVE_ENTRANCE = 4006 val CAVE_ENTRANCE = 4006

View file

@ -4,7 +4,6 @@ import core.game.node.entity.impl.ForceMovement
import core.game.world.map.Direction import core.game.world.map.Direction
import core.game.world.map.Location import core.game.world.map.Location
import core.game.world.update.flag.context.Animation import core.game.world.update.flag.context.Animation
import core.plugin.Initializable
import rs09.game.interaction.OptionListener import rs09.game.interaction.OptionListener
/** /**
@ -13,7 +12,6 @@ import rs09.game.interaction.OptionListener
* @author Sir Kermit * @author Sir Kermit
*/ */
@Initializable
class MorytaniaListeners : OptionListener() { class MorytaniaListeners : OptionListener() {
val GROTTO_ENTRANCE = 3516 val GROTTO_ENTRANCE = 3516

View file

@ -1,19 +1,11 @@
package rs09.game.interaction.item.withobject package rs09.game.interaction.item.withobject
import core.cache.def.impl.ObjectDefinition
import core.game.interaction.NodeUsageEvent
import core.game.interaction.OptionHandler
import core.game.interaction.UseWithHandler
import core.game.node.Node
import core.game.node.entity.player.Player
import core.game.node.entity.player.link.diary.DiaryType import core.game.node.entity.player.link.diary.DiaryType
import core.game.node.item.Item import core.game.node.item.Item
import core.game.world.map.zone.ZoneBorders import core.game.world.map.zone.ZoneBorders
import core.plugin.Initializable
import core.plugin.Plugin
import org.rs09.consts.Items import org.rs09.consts.Items
import rs09.game.interaction.OptionListener import rs09.game.interaction.OptionListener
import rs09.plugin.PluginManager.definePlugin import rs09.game.interaction.UseWithListener
import java.util.* import java.util.*
/** /**
@ -63,34 +55,35 @@ class CoalTrucksHandler : OptionListener() {
} }
//TODO: //TODO:
inner class useCoalWithTruck : UseWithHandler(Items.COAL_453) { class CoalTruckListener : UseWithListener() {
override fun newInstance(arg: Any?): Plugin<Any>? { val COAL_TRUCK_2114 = 2114
addHandler(2114, OBJECT_TYPE, this) val COAL = Items.COAL_453
return this
}
override fun handle(event: NodeUsageEvent): Boolean { override fun defineListeners() {
val player = event.player
var coalInTruck = player.getAttribute("coal-truck-inventory", 0)
var coalInInventory = player.inventory.getAmount(Items.COAL_453) on(COAL,COAL_TRUCK_2114,OBJECT){player,_,_ ->
var coalInTruck = player.getAttribute("coal-truck-inventory", 0)
if(coalInInventory + coalInTruck >= 120){ var coalInInventory = player.inventory.getAmount(Items.COAL_453)
coalInInventory = 120 - coalInTruck
event.player.packetDispatch.sendMessage("You have filled up the coal truck.")
//handle coal truck task for seer's village if(coalInInventory + coalInTruck >= 120){
if (!player.achievementDiaryManager.getDiary(DiaryType.SEERS_VILLAGE).isComplete(1, 2) coalInInventory = 120 - coalInTruck
player.packetDispatch.sendMessage("You have filled up the coal truck.")
//handle coal truck task for seer's village
if (!player.achievementDiaryManager.getDiary(DiaryType.SEERS_VILLAGE).isComplete(1, 2)
&& player.viewport.region.id == 10294) { // region 10294 is at coal truck mine, region 10806 is in seers village && player.viewport.region.id == 10294) { // region 10294 is at coal truck mine, region 10806 is in seers village
player.setAttribute("/save:diary:seers:coal-truck-full", true) player.setAttribute("/save:diary:seers:coal-truck-full", true)
}
} }
player.inventory.remove(Item(Items.COAL_453,coalInInventory))
coalInTruck += coalInInventory
player.setAttribute("/save:coal-truck-inventory",coalInTruck)
return@on true
} }
player.inventory.remove(Item(Items.COAL_453,coalInInventory))
coalInTruck += coalInInventory
player.setAttribute("/save:coal-truck-inventory",coalInTruck)
return true
} }
} }
} }

View file

@ -5,11 +5,9 @@ import core.game.node.`object`.ObjectBuilder
import core.game.node.item.GroundItemManager import core.game.node.item.GroundItemManager
import core.game.node.item.Item import core.game.node.item.Item
import core.game.world.update.flag.context.Animation import core.game.world.update.flag.context.Animation
import core.plugin.Initializable
import org.rs09.consts.Items import org.rs09.consts.Items
import rs09.game.interaction.OptionListener import rs09.game.interaction.OptionListener
@Initializable
/** /**
* Handles the muddy chest * Handles the muddy chest
* @author Ceikry * @author Ceikry

View file

@ -12,6 +12,7 @@ import core.plugin.PluginType
import io.github.classgraph.ClassGraph import io.github.classgraph.ClassGraph
import io.github.classgraph.ClassInfo import io.github.classgraph.ClassInfo
import rs09.game.interaction.OptionListener import rs09.game.interaction.OptionListener
import rs09.game.interaction.UseWithListener
import rs09.game.system.SystemLogger import rs09.game.system.SystemLogger
import rs09.game.system.command.Command import rs09.game.system.command.Command
import java.util.* import java.util.*
@ -75,6 +76,9 @@ object PluginManager {
result.getSubclasses("rs09.game.interaction.OptionListener").forEach { result.getSubclasses("rs09.game.interaction.OptionListener").forEach {
(it.loadClass().newInstance() as OptionListener).defineListeners() (it.loadClass().newInstance() as OptionListener).defineListeners()
} }
result.getSubclasses("rs09.game.interaction.UseWithListener").forEach {
(it.loadClass().newInstance() as UseWithListener).defineListeners()
}
} }
/** /**