From 22f712da232327adb4db0ded800114eb814e79d2 Mon Sep 17 00:00:00 2001 From: Major- Date: Fri, 8 Aug 2014 02:00:01 +0100 Subject: [PATCH] Add support for specifying the option for events in the declaration (where applicable). --- data/plugins/bank/bank.rb | 16 ++++---- data/plugins/bootstrap.rb | 31 ++++++++++----- data/plugins/consumables/consumable.rb | 12 +++--- data/plugins/dummy/dummy.rb | 4 +- data/plugins/skill/herblore/herblore.rb | 24 ++++++------ data/plugins/skill/mining/mining.rb | 24 +++++------- data/plugins/skill/prayer/bury.rb | 12 +++--- data/plugins/skill/runecraft/talisman.rb | 12 +++--- data/plugins/skill/runecraft/tiara.rb | 50 +++++++++++------------- 9 files changed, 89 insertions(+), 96 deletions(-) diff --git a/data/plugins/bank/bank.rb b/data/plugins/bank/bank.rb index 0a4a99a6..5c6a2fe9 100644 --- a/data/plugins/bank/bank.rb +++ b/data/plugins/bank/bank.rb @@ -30,19 +30,17 @@ class BankAction < DistancedAction end # Intercepts the object action event -on :event, :object_action do |ctx, player, event| - if (event.option == 2 and event.id == BANK_BOOTH_ID) +on :event, :second_object_action do |ctx, player, event| + if event.id == BANK_BOOTH_ID player.start_action(BankAction.new(player, event.position)) ctx.break_handler_chain end end -on :event, :npc_action do |ctx, player, event| - if (event.option == 2) - npc = World.world.npc_repository.get(event.index) - if (BANKER_NPCS.include?(npc.id)) - player.start_action(BankAction.new(player, npc.position)) - ctx.break_handler_chain - end +on :event, :second_npc_action do |ctx, player, event| + npc = World.world.npc_repository.get(event.index) + if BANKER_NPCS.include?(npc.id) + player.start_action(BankAction.new(player, npc.position)) + ctx.break_handler_chain end end \ No newline at end of file diff --git a/data/plugins/bootstrap.rb b/data/plugins/bootstrap.rb index 26ee1090..8f19da61 100644 --- a/data/plugins/bootstrap.rb +++ b/data/plugins/bootstrap.rb @@ -40,7 +40,7 @@ end # and the command. class ProcCommandListener < CommandListener def initialize(rights, block) - super rights + super(rights) @block = block end @@ -80,13 +80,16 @@ end # An EventHandler which executes a Proc object with three arguments: the chain # context, the player and the event. class ProcEventHandler < EventHandler - def initialize(block) + def initialize(block, option) super() # required (with brackets!), see http://jira.codehaus.org/browse/JRUBY-679 @block = block + @option = option end def handle(ctx, player, event) - @block.call(ctx, player, event) + if (@option == 0 || @option == event.option) + @block.call(ctx, player, event) + end end end @@ -113,7 +116,7 @@ end # behaviour is undefined (and most likely it'll be bad). def schedule(*args, &block) if block_given? - raise 'Invalid combination of arguments.' unless args.length == 1 or args.length == 2 + raise 'Invalid combination of arguments.' unless (1..2).include?(args.length) delay = args[0] immediate = args.length == 2 ? args[1] : false World.world.schedule(ProcScheduledTask.new(delay, immediate, block)) @@ -165,18 +168,28 @@ def on_button(args, proc) end # Defines an action to be taken upon an event. -# The event can either be a symbol with the lower case, underscored class name -# or the class itself. +# The event can either be a symbol with the lower-case underscored class name, or the class itself. def on_event(args, proc) - raise "event must have one argument" unless args.length == 1 + raise 'Event must have one or two arguments.' unless (1..2).include?(args.length) + numbers = [ 'first', 'second', 'third', 'fourth', 'fifth' ] + event = args[0]; option = 0 + + numbers.each_index do |index| + number = numbers[index] + + if event.to_s.start_with?(number) + option = index + 1 + event = event[number.length + 1, event.length].to_sym + break + end + end - event = args[0] if event.is_a?(Symbol) class_name = event.to_s.camelize.concat('Event') event = Java::JavaClass.for_name("org.apollo.game.event.impl.#{class_name}") end - $ctx.add_last_event_handler(event, ProcEventHandler.new(proc)) + $ctx.add_last_event_handler(event, ProcEventHandler.new(proc, option)) end # Defines an action to be taken upon a command. diff --git a/data/plugins/consumables/consumable.rb b/data/plugins/consumables/consumable.rb index 177dc15c..56275417 100644 --- a/data/plugins/consumables/consumable.rb +++ b/data/plugins/consumables/consumable.rb @@ -54,12 +54,10 @@ class ConsumeAction < Action end # Intercepts the first item option event and consumes the consumable, if necessary. -on :event, :item_option do |ctx, player, event| - if (event.option == 1) - consumable = CONSUMABLES[event.id] - unless consumable == nil - player.start_action(ConsumeAction.new(player, event.slot, consumable)) - ctx.break_handler_chain - end +on :event, :first_item_option do |ctx, player, event| + consumable = CONSUMABLES[event.id] + unless consumable == nil + player.start_action(ConsumeAction.new(player, event.slot, consumable)) + ctx.break_handler_chain end end \ No newline at end of file diff --git a/data/plugins/dummy/dummy.rb b/data/plugins/dummy/dummy.rb index 229f94fd..ec91f5c5 100644 --- a/data/plugins/dummy/dummy.rb +++ b/data/plugins/dummy/dummy.rb @@ -45,8 +45,8 @@ class DummyAction < DistancedAction end end -on :event, :object_action do |ctx, player, event| - if (event.option == 2 and event.id == DUMMY_ID) +on :event, :second_object_action do |ctx, player, event| + if (event.id == DUMMY_ID) player.start_action(DummyAction.new(player, event.position)) end end \ No newline at end of file diff --git a/data/plugins/skill/herblore/herblore.rb b/data/plugins/skill/herblore/herblore.rb index aa06335c..6b7d474d 100644 --- a/data/plugins/skill/herblore/herblore.rb +++ b/data/plugins/skill/herblore/herblore.rb @@ -47,21 +47,19 @@ on :event, :item_on_item do |ctx, player, event| end # The ItemOptionEvent handler for all Herblore-related functions. -on :event, :item_option do |ctx, player, event| - if event.option == 1 - id = event.id - method = HERBLORE_ITEM[id] +on :event, :first_item_option do |ctx, player, event| + id = event.id + method = HERBLORE_ITEM[id] - if method != nil - method.invoke(player, id, event.slot) - ctx.break_handler_chain - end - method = DRINK_ITEM[id] + if method != nil + method.invoke(player, id, event.slot) + ctx.break_handler_chain + end + method = DRINK_ITEM[id] - if method != nil - method.invoke(player, id, event.slot) - ctx.break_handler_chain - end + if method != nil + method.invoke(player, id, event.slot) + ctx.break_handler_chain end end diff --git a/data/plugins/skill/mining/mining.rb b/data/plugins/skill/mining/mining.rb index c204966d..425f57c9 100644 --- a/data/plugins/skill/mining/mining.rb +++ b/data/plugins/skill/mining/mining.rb @@ -141,22 +141,18 @@ class ProspectingAction < DistancedAction end end -on :event, :object_action do |ctx, mob, event| - if event.option == 1 - ore = ORES[event.id] - if ore != nil - mob.start_action(MiningAction.new(mob, event.position, ore)) - end +on :event, :first_object_action do |ctx, mob, event| + ore = ORES[event.id] + if ore != nil + mob.start_action(MiningAction.new(mob, event.position, ore)) end end -on :event, :object_action do |ctx, mob, event| - if event.option == 2 - ore = ORES[event.id] - if ore != nil - mob.start_action(ProspectingAction.new(mob, event.position, ore)) - elsif EXPIRED_ORES[event.id] != nil - mob.start_action(ExpiredProspectingAction.new(mob, event.position)) - end +on :event, :second_object_action do |ctx, mob, event| + ore = ORES[event.id] + if ore != nil + mob.start_action(ProspectingAction.new(mob, event.position, ore)) + elsif EXPIRED_ORES[event.id] != nil + mob.start_action(ExpiredProspectingAction.new(mob, event.position)) end end \ No newline at end of file diff --git a/data/plugins/skill/prayer/bury.rb b/data/plugins/skill/prayer/bury.rb index fb923bb1..1f27c1e1 100644 --- a/data/plugins/skill/prayer/bury.rb +++ b/data/plugins/skill/prayer/bury.rb @@ -43,13 +43,11 @@ class BuryBoneAction < Action end # Intercepts the first item option event, -on :event, :item_option do |ctx, player, event| - if event.option == 1 - bone = BONES[event.id] - unless bone == nil - player.start_action(BuryBoneAction.new(player, event.slot, bone)) - ctx.break_handler_chain - end +on :event, :first_item_option do |ctx, player, event| + bone = BONES[event.id] + unless bone == nil + player.start_action(BuryBoneAction.new(player, event.slot, bone)) + ctx.break_handler_chain end end diff --git a/data/plugins/skill/runecraft/talisman.rb b/data/plugins/skill/runecraft/talisman.rb index 7fdf039c..91b4e455 100644 --- a/data/plugins/skill/runecraft/talisman.rb +++ b/data/plugins/skill/runecraft/talisman.rb @@ -30,13 +30,11 @@ def append_talisman(hash) end # Intercepts the item option event. -on :event, :item_option do |ctx, player, event| - if (event.option == 4) - talisman = TALISMANS[event.id] - if (talisman != nil) - player.send_message(talisman.get_message(player.position)) - ctx.break_handler_chain - end +on :event, :fourth_item_option do |ctx, player, event| + talisman = TALISMANS[event.id] + if (talisman != nil) + player.send_message(talisman.get_message(player.position)) + ctx.break_handler_chain end end diff --git a/data/plugins/skill/runecraft/tiara.rb b/data/plugins/skill/runecraft/tiara.rb index 7a3cc46d..a4c9a793 100644 --- a/data/plugins/skill/runecraft/tiara.rb +++ b/data/plugins/skill/runecraft/tiara.rb @@ -48,7 +48,7 @@ on :login do |player| hat = player.equipment.get(EquipmentConstants::HAT) if hat != nil tiara = TIARAS_BY_ID[hat] - if(tiara != nil) + if (tiara != nil) tiara.send_config return end @@ -57,42 +57,36 @@ on :login do |player| end #Accesses the altar with 1 click when wielding the correct tiara. -on :event, :object_action do |ctx, player, event| - if (event.option == 1) - object_id = event.id - tiara = TIARAS_BY_ALTAR[object_id] - if(tiara != nil) - hat = player.equipment.get(EquipmentConstants::HAT) - if(hat != nil && hat.id == tiara.tiara_id) - altar = ENTRANCE_ALTARS[tiara.altar] - if(altar != nil) - player.start_action(TeleportAction.new(player, event.position, 2, altar.entrance_position)) - end - ctx.break_handler_chain - end +on :event, :second_object_action do |ctx, player, event| + object_id = event.id + tiara = TIARAS_BY_ALTAR[object_id] + if (tiara != nil) + hat = player.equipment.get(EquipmentConstants::HAT) + if (hat != nil && hat.id == tiara.tiara_id) + altar = ENTRANCE_ALTARS[tiara.altar] + if (altar != nil) + player.start_action(TeleportAction.new(player, event.position, 2, altar.entrance_position)) + end + ctx.break_handler_chain end end end #Equip tiara -on :event, :item_option do |ctx, player, event| - if (event.option == 2) - tiara = TIARAS_BY_ID[event.id] - if(tiara != nil) - tiara.send_config(player) - ctx.break_handler_chain - end +on :event, :second_item_option, item_option do |ctx, player, event| + tiara = TIARAS_BY_ID[event.id] + if (tiara != nil) + tiara.send_config(player) + ctx.break_handler_chain end end #Unequip tiara -on :event, :item_action do |ctx, player, event| - if (event.option == 1) - tiara = TIARAS_BY_ID[event.id] - if(tiara != nil) - send_empty_config(player) - ctx.break_handler_chain - end +on :event, :first_item_action do |ctx, player, event| + tiara = TIARAS_BY_ID[event.id] + if (tiara != nil) + send_empty_config(player) + ctx.break_handler_chain end end