From c6498ba9207e26e550c2e828caa73fdd491dbc0b Mon Sep 17 00:00:00 2001 From: Ceikry Date: Fri, 12 Mar 2021 23:20:47 -0600 Subject: [PATCH] Added UseWithListener.kt --- .../core/net/packet/in/ItemActionPacket.java | 12 ++++- .../allfiredup/AFURepairClimbHandler.kt | 2 - .../FishingTrawlerOptionHandler.kt | 1 - .../kotlin/rs09/game/interaction/Listener.kt | 5 ++ .../kotlin/rs09/game/interaction/Listeners.kt | 48 ++++++++++++++++- .../rs09/game/interaction/OptionListener.kt | 3 +- .../rs09/game/interaction/UseWithListener.kt | 16 ++++++ .../game/interaction/city/IsafdarListeners.kt | 2 - .../interaction/city/MorytaniaListeners.kt | 2 - .../item/withobject/CoalTrucksHandler.kt | 51 ++++++++----------- .../interaction/object/MuddyChestHandler.kt | 2 - .../main/kotlin/rs09/plugin/PluginManager.kt | 4 ++ 12 files changed, 106 insertions(+), 42 deletions(-) create mode 100644 Server/src/main/kotlin/rs09/game/interaction/Listener.kt create mode 100644 Server/src/main/kotlin/rs09/game/interaction/UseWithListener.kt diff --git a/Server/src/main/java/core/net/packet/in/ItemActionPacket.java b/Server/src/main/java/core/net/packet/in/ItemActionPacket.java index 4391210d0..5feba6f8d 100644 --- a/Server/src/main/java/core/net/packet/in/ItemActionPacket.java +++ b/Server/src/main/java/core/net/packet/in/ItemActionPacket.java @@ -15,7 +15,9 @@ import core.net.packet.IoBuffer; import core.net.packet.PacketRepository; import core.net.packet.context.PlayerContext; import core.net.packet.out.ClearMinimapFlag; +import org.rs09.consts.Items; 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.FarmingPatch; 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.command.rottenpotato.RottenPotatoUseWithHandler; import rs09.game.world.repository.Repository; -import org.rs09.consts.Items; /** * The incoming item reward packet. @@ -68,6 +69,9 @@ public class ItemActionPacket implements IncomingPacket { if(PluginInteractionManager.handle(player,event)){ return; } + if(Listeners.run(item,npc,2,player)){ + return; + } event = new NodeUsageEvent(player, interfaceId, item, npc); try { UseWithHandler.run(event); @@ -124,6 +128,9 @@ public class ItemActionPacket implements IncomingPacket { RottenPotatoUseWithHandler.handle(used,player); return; } + if(Listeners.run(used,with,0,player)){ + return; + } if (usedItemId < usedWithItemId) { item = used; used = with; @@ -172,6 +179,9 @@ public class ItemActionPacket implements IncomingPacket { RottenPotatoUseWithHandler.handle(object,player); return; } + if(Listeners.run(used,object,1,player)){ + return; + } event = new NodeUsageEvent(player, 0, used, object); /** diff --git a/Server/src/main/kotlin/rs09/game/content/activity/allfiredup/AFURepairClimbHandler.kt b/Server/src/main/kotlin/rs09/game/content/activity/allfiredup/AFURepairClimbHandler.kt index db7638f7d..32f53a530 100644 --- a/Server/src/main/kotlin/rs09/game/content/activity/allfiredup/AFURepairClimbHandler.kt +++ b/Server/src/main/kotlin/rs09/game/content/activity/allfiredup/AFURepairClimbHandler.kt @@ -8,7 +8,6 @@ import core.game.node.item.Item import core.game.world.map.Direction import core.game.world.map.Location import core.game.world.update.flag.context.Animation -import core.plugin.Initializable import org.rs09.consts.Items import rs09.game.interaction.OptionListener import java.util.* @@ -17,7 +16,6 @@ import java.util.* * Handles repairing and climbing of the 3 beacon shortcuts needed to access them * @author Ceikry */ -@Initializable class AFURepairClimbHandler : OptionListener() { val repairIDs = intArrayOf(38480,38470,38494) diff --git a/Server/src/main/kotlin/rs09/game/content/activity/fishingtrawler/FishingTrawlerOptionHandler.kt b/Server/src/main/kotlin/rs09/game/content/activity/fishingtrawler/FishingTrawlerOptionHandler.kt index abd553dd3..da5575bf2 100644 --- a/Server/src/main/kotlin/rs09/game/content/activity/fishingtrawler/FishingTrawlerOptionHandler.kt +++ b/Server/src/main/kotlin/rs09/game/content/activity/fishingtrawler/FishingTrawlerOptionHandler.kt @@ -22,7 +22,6 @@ import kotlin.math.ceil * Option handler for fishing trawler * @author Ceikry */ -@Initializable class FishingTrawlerOptionHandler : OptionListener() { val ENTRANCE_PLANK = 2178 val EXIT_PLANK = 2179 diff --git a/Server/src/main/kotlin/rs09/game/interaction/Listener.kt b/Server/src/main/kotlin/rs09/game/interaction/Listener.kt new file mode 100644 index 000000000..9d6f51fd4 --- /dev/null +++ b/Server/src/main/kotlin/rs09/game/interaction/Listener.kt @@ -0,0 +1,5 @@ +package rs09.game.interaction + +interface Listener { + fun defineListeners() +} \ No newline at end of file diff --git a/Server/src/main/kotlin/rs09/game/interaction/Listeners.kt b/Server/src/main/kotlin/rs09/game/interaction/Listeners.kt index 6d78e3b62..dad3cd7b4 100644 --- a/Server/src/main/kotlin/rs09/game/interaction/Listeners.kt +++ b/Server/src/main/kotlin/rs09/game/interaction/Listeners.kt @@ -6,7 +6,8 @@ import core.game.node.Node import core.game.node.entity.player.Player object Listeners { - private val listeners = HashMap Boolean>() + private val listeners = HashMap Boolean>(1000) + private val useWithListeners = HashMap Boolean>(1000) @JvmStatic fun add(id: Int, type: Int, option: Array, 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 fun get(id: Int, type: Int, option: String): ((Player,Node) -> Boolean)?{ return listeners["$id:$type:${option.toLowerCase()}"] @@ -46,6 +64,34 @@ object Listeners { 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 fun run(id: Int, type: Int, option: String, player: Player, node: Node): Boolean{ val flag = when(type){ diff --git a/Server/src/main/kotlin/rs09/game/interaction/OptionListener.kt b/Server/src/main/kotlin/rs09/game/interaction/OptionListener.kt index 514b80fb8..e122bae63 100644 --- a/Server/src/main/kotlin/rs09/game/interaction/OptionListener.kt +++ b/Server/src/main/kotlin/rs09/game/interaction/OptionListener.kt @@ -3,11 +3,10 @@ package rs09.game.interaction import core.game.node.Node import core.game.node.entity.player.Player -abstract class OptionListener { +abstract class OptionListener : Listener{ val ITEM = 0 val OBJECT = 1 val NPC = 2 - abstract fun defineListeners() fun on(id: Int, type: Int, vararg option: String,handler: (Player, Node) -> Boolean){ Listeners.add(id,type,option,handler) } diff --git a/Server/src/main/kotlin/rs09/game/interaction/UseWithListener.kt b/Server/src/main/kotlin/rs09/game/interaction/UseWithListener.kt new file mode 100644 index 000000000..bdf14f9c9 --- /dev/null +++ b/Server/src/main/kotlin/rs09/game/interaction/UseWithListener.kt @@ -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) + } +} \ No newline at end of file diff --git a/Server/src/main/kotlin/rs09/game/interaction/city/IsafdarListeners.kt b/Server/src/main/kotlin/rs09/game/interaction/city/IsafdarListeners.kt index bac731042..d9d7d2cd4 100644 --- a/Server/src/main/kotlin/rs09/game/interaction/city/IsafdarListeners.kt +++ b/Server/src/main/kotlin/rs09/game/interaction/city/IsafdarListeners.kt @@ -1,7 +1,6 @@ package rs09.game.interaction.city import core.game.world.map.Location -import core.plugin.Initializable import rs09.game.interaction.OptionListener /** @@ -10,7 +9,6 @@ import rs09.game.interaction.OptionListener * @author Sir Kermit */ -@Initializable class IsafdarListeners : OptionListener() { val CAVE_ENTRANCE = 4006 diff --git a/Server/src/main/kotlin/rs09/game/interaction/city/MorytaniaListeners.kt b/Server/src/main/kotlin/rs09/game/interaction/city/MorytaniaListeners.kt index 16e0ee273..7bd03c1bb 100644 --- a/Server/src/main/kotlin/rs09/game/interaction/city/MorytaniaListeners.kt +++ b/Server/src/main/kotlin/rs09/game/interaction/city/MorytaniaListeners.kt @@ -4,7 +4,6 @@ import core.game.node.entity.impl.ForceMovement import core.game.world.map.Direction import core.game.world.map.Location import core.game.world.update.flag.context.Animation -import core.plugin.Initializable import rs09.game.interaction.OptionListener /** @@ -13,7 +12,6 @@ import rs09.game.interaction.OptionListener * @author Sir Kermit */ -@Initializable class MorytaniaListeners : OptionListener() { val GROTTO_ENTRANCE = 3516 diff --git a/Server/src/main/kotlin/rs09/game/interaction/item/withobject/CoalTrucksHandler.kt b/Server/src/main/kotlin/rs09/game/interaction/item/withobject/CoalTrucksHandler.kt index e34430c6e..4dc5f6404 100644 --- a/Server/src/main/kotlin/rs09/game/interaction/item/withobject/CoalTrucksHandler.kt +++ b/Server/src/main/kotlin/rs09/game/interaction/item/withobject/CoalTrucksHandler.kt @@ -1,19 +1,11 @@ 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.item.Item import core.game.world.map.zone.ZoneBorders -import core.plugin.Initializable -import core.plugin.Plugin import org.rs09.consts.Items import rs09.game.interaction.OptionListener -import rs09.plugin.PluginManager.definePlugin +import rs09.game.interaction.UseWithListener import java.util.* /** @@ -63,34 +55,35 @@ class CoalTrucksHandler : OptionListener() { } //TODO: - inner class useCoalWithTruck : UseWithHandler(Items.COAL_453) { - override fun newInstance(arg: Any?): Plugin? { - addHandler(2114, OBJECT_TYPE, this) - return this - } + class CoalTruckListener : UseWithListener() { + val COAL_TRUCK_2114 = 2114 + val COAL = Items.COAL_453 - override fun handle(event: NodeUsageEvent): Boolean { - val player = event.player - var coalInTruck = player.getAttribute("coal-truck-inventory", 0) + override fun defineListeners() { - 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){ - coalInInventory = 120 - coalInTruck - event.player.packetDispatch.sendMessage("You have filled up the coal truck.") + var coalInInventory = player.inventory.getAmount(Items.COAL_453) - //handle coal truck task for seer's village - if (!player.achievementDiaryManager.getDiary(DiaryType.SEERS_VILLAGE).isComplete(1, 2) + if(coalInInventory + coalInTruck >= 120){ + 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.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 } } } \ No newline at end of file diff --git a/Server/src/main/kotlin/rs09/game/interaction/object/MuddyChestHandler.kt b/Server/src/main/kotlin/rs09/game/interaction/object/MuddyChestHandler.kt index d00d2de0e..7d57fbbd7 100644 --- a/Server/src/main/kotlin/rs09/game/interaction/object/MuddyChestHandler.kt +++ b/Server/src/main/kotlin/rs09/game/interaction/object/MuddyChestHandler.kt @@ -5,11 +5,9 @@ import core.game.node.`object`.ObjectBuilder import core.game.node.item.GroundItemManager import core.game.node.item.Item import core.game.world.update.flag.context.Animation -import core.plugin.Initializable import org.rs09.consts.Items import rs09.game.interaction.OptionListener -@Initializable /** * Handles the muddy chest * @author Ceikry diff --git a/Server/src/main/kotlin/rs09/plugin/PluginManager.kt b/Server/src/main/kotlin/rs09/plugin/PluginManager.kt index 842953dc2..6fd367112 100644 --- a/Server/src/main/kotlin/rs09/plugin/PluginManager.kt +++ b/Server/src/main/kotlin/rs09/plugin/PluginManager.kt @@ -12,6 +12,7 @@ import core.plugin.PluginType import io.github.classgraph.ClassGraph import io.github.classgraph.ClassInfo import rs09.game.interaction.OptionListener +import rs09.game.interaction.UseWithListener import rs09.game.system.SystemLogger import rs09.game.system.command.Command import java.util.* @@ -75,6 +76,9 @@ object PluginManager { result.getSubclasses("rs09.game.interaction.OptionListener").forEach { (it.loadClass().newInstance() as OptionListener).defineListeners() } + result.getSubclasses("rs09.game.interaction.UseWithListener").forEach { + (it.loadClass().newInstance() as UseWithListener).defineListeners() + } } /**