Add support for specifying the option for events in the declaration (where applicable).

This commit is contained in:
Major-
2014-08-08 02:00:01 +01:00
parent a863d8ea71
commit 22f712da23
9 changed files with 89 additions and 96 deletions
+4 -6
View File
@@ -30,19 +30,17 @@ class BankAction < DistancedAction
end end
# Intercepts the object action event # Intercepts the object action event
on :event, :object_action do |ctx, player, event| on :event, :second_object_action do |ctx, player, event|
if (event.option == 2 and event.id == BANK_BOOTH_ID) if event.id == BANK_BOOTH_ID
player.start_action(BankAction.new(player, event.position)) player.start_action(BankAction.new(player, event.position))
ctx.break_handler_chain ctx.break_handler_chain
end end
end end
on :event, :npc_action do |ctx, player, event| on :event, :second_npc_action do |ctx, player, event|
if (event.option == 2)
npc = World.world.npc_repository.get(event.index) npc = World.world.npc_repository.get(event.index)
if (BANKER_NPCS.include?(npc.id)) if BANKER_NPCS.include?(npc.id)
player.start_action(BankAction.new(player, npc.position)) player.start_action(BankAction.new(player, npc.position))
ctx.break_handler_chain ctx.break_handler_chain
end end
end end
end
+21 -8
View File
@@ -40,7 +40,7 @@ end
# and the command. # and the command.
class ProcCommandListener < CommandListener class ProcCommandListener < CommandListener
def initialize(rights, block) def initialize(rights, block)
super rights super(rights)
@block = block @block = block
end end
@@ -80,15 +80,18 @@ end
# An EventHandler which executes a Proc object with three arguments: the chain # An EventHandler which executes a Proc object with three arguments: the chain
# context, the player and the event. # context, the player and the event.
class ProcEventHandler < EventHandler class ProcEventHandler < EventHandler
def initialize(block) def initialize(block, option)
super() # required (with brackets!), see http://jira.codehaus.org/browse/JRUBY-679 super() # required (with brackets!), see http://jira.codehaus.org/browse/JRUBY-679
@block = block @block = block
@option = option
end end
def handle(ctx, player, event) def handle(ctx, player, event)
if (@option == 0 || @option == event.option)
@block.call(ctx, player, event) @block.call(ctx, player, event)
end end
end end
end
# A ScheduledTask which executes a Proc object with one argument (itself). # A ScheduledTask which executes a Proc object with one argument (itself).
class ProcScheduledTask < ScheduledTask class ProcScheduledTask < ScheduledTask
@@ -113,7 +116,7 @@ end
# behaviour is undefined (and most likely it'll be bad). # behaviour is undefined (and most likely it'll be bad).
def schedule(*args, &block) def schedule(*args, &block)
if block_given? 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] delay = args[0]
immediate = args.length == 2 ? args[1] : false immediate = args.length == 2 ? args[1] : false
World.world.schedule(ProcScheduledTask.new(delay, immediate, block)) World.world.schedule(ProcScheduledTask.new(delay, immediate, block))
@@ -165,18 +168,28 @@ def on_button(args, proc)
end end
# Defines an action to be taken upon an event. # Defines an action to be taken upon an event.
# The event can either be a symbol with the lower case, underscored class name # The event can either be a symbol with the lower-case underscored class name, or the class itself.
# or the class itself.
def on_event(args, proc) 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) if event.is_a?(Symbol)
class_name = event.to_s.camelize.concat('Event') class_name = event.to_s.camelize.concat('Event')
event = Java::JavaClass.for_name("org.apollo.game.event.impl.#{class_name}") event = Java::JavaClass.for_name("org.apollo.game.event.impl.#{class_name}")
end end
$ctx.add_last_event_handler(event, ProcEventHandler.new(proc)) $ctx.add_last_event_handler(event, ProcEventHandler.new(proc, option))
end end
# Defines an action to be taken upon a command. # Defines an action to be taken upon a command.
+1 -3
View File
@@ -54,12 +54,10 @@ class ConsumeAction < Action
end end
# Intercepts the first item option event and consumes the consumable, if necessary. # Intercepts the first item option event and consumes the consumable, if necessary.
on :event, :item_option do |ctx, player, event| on :event, :first_item_option do |ctx, player, event|
if (event.option == 1)
consumable = CONSUMABLES[event.id] consumable = CONSUMABLES[event.id]
unless consumable == nil unless consumable == nil
player.start_action(ConsumeAction.new(player, event.slot, consumable)) player.start_action(ConsumeAction.new(player, event.slot, consumable))
ctx.break_handler_chain ctx.break_handler_chain
end end
end end
end
+2 -2
View File
@@ -45,8 +45,8 @@ class DummyAction < DistancedAction
end end
end end
on :event, :object_action do |ctx, player, event| on :event, :second_object_action do |ctx, player, event|
if (event.option == 2 and event.id == DUMMY_ID) if (event.id == DUMMY_ID)
player.start_action(DummyAction.new(player, event.position)) player.start_action(DummyAction.new(player, event.position))
end end
end end
+1 -3
View File
@@ -47,8 +47,7 @@ on :event, :item_on_item do |ctx, player, event|
end end
# The ItemOptionEvent handler for all Herblore-related functions. # The ItemOptionEvent handler for all Herblore-related functions.
on :event, :item_option do |ctx, player, event| on :event, :first_item_option do |ctx, player, event|
if event.option == 1
id = event.id id = event.id
method = HERBLORE_ITEM[id] method = HERBLORE_ITEM[id]
@@ -63,7 +62,6 @@ on :event, :item_option do |ctx, player, event|
ctx.break_handler_chain ctx.break_handler_chain
end end
end end
end
# Utility for adding the various Herblore methods to the handled constant arrays. # Utility for adding the various Herblore methods to the handled constant arrays.
def append_herblore_item(method, key, secondary = -1) def append_herblore_item(method, key, secondary = -1)
+2 -6
View File
@@ -141,17 +141,14 @@ class ProspectingAction < DistancedAction
end end
end end
on :event, :object_action do |ctx, mob, event| on :event, :first_object_action do |ctx, mob, event|
if event.option == 1
ore = ORES[event.id] ore = ORES[event.id]
if ore != nil if ore != nil
mob.start_action(MiningAction.new(mob, event.position, ore)) mob.start_action(MiningAction.new(mob, event.position, ore))
end end
end end
end
on :event, :object_action do |ctx, mob, event| on :event, :second_object_action do |ctx, mob, event|
if event.option == 2
ore = ORES[event.id] ore = ORES[event.id]
if ore != nil if ore != nil
mob.start_action(ProspectingAction.new(mob, event.position, ore)) mob.start_action(ProspectingAction.new(mob, event.position, ore))
@@ -159,4 +156,3 @@ on :event, :object_action do |ctx, mob, event|
mob.start_action(ExpiredProspectingAction.new(mob, event.position)) mob.start_action(ExpiredProspectingAction.new(mob, event.position))
end end
end end
end
+1 -3
View File
@@ -43,15 +43,13 @@ class BuryBoneAction < Action
end end
# Intercepts the first item option event, # Intercepts the first item option event,
on :event, :item_option do |ctx, player, event| on :event, :first_item_option do |ctx, player, event|
if event.option == 1
bone = BONES[event.id] bone = BONES[event.id]
unless bone == nil unless bone == nil
player.start_action(BuryBoneAction.new(player, event.slot, bone)) player.start_action(BuryBoneAction.new(player, event.slot, bone))
ctx.break_handler_chain ctx.break_handler_chain
end end
end end
end
# Appends a bone to the array # Appends a bone to the array
def append_bone(hash) def append_bone(hash)
+1 -3
View File
@@ -30,15 +30,13 @@ def append_talisman(hash)
end end
# Intercepts the item option event. # Intercepts the item option event.
on :event, :item_option do |ctx, player, event| on :event, :fourth_item_option do |ctx, player, event|
if (event.option == 4)
talisman = TALISMANS[event.id] talisman = TALISMANS[event.id]
if (talisman != nil) if (talisman != nil)
player.send_message(talisman.get_message(player.position)) player.send_message(talisman.get_message(player.position))
ctx.break_handler_chain ctx.break_handler_chain
end end
end end
end
# Appends talismans to the list. # Appends talismans to the list.
append_talisman :name => :air_talisman, :id => 1438, :altar => [ 2985, 3292 ] append_talisman :name => :air_talisman, :id => 1438, :altar => [ 2985, 3292 ]
+3 -9
View File
@@ -57,8 +57,7 @@ on :login do |player|
end end
#Accesses the altar with 1 click when wielding the correct tiara. #Accesses the altar with 1 click when wielding the correct tiara.
on :event, :object_action do |ctx, player, event| on :event, :second_object_action do |ctx, player, event|
if (event.option == 1)
object_id = event.id object_id = event.id
tiara = TIARAS_BY_ALTAR[object_id] tiara = TIARAS_BY_ALTAR[object_id]
if (tiara != nil) if (tiara != nil)
@@ -72,29 +71,24 @@ on :event, :object_action do |ctx, player, event|
end end
end end
end end
end
#Equip tiara #Equip tiara
on :event, :item_option do |ctx, player, event| on :event, :second_item_option, item_option do |ctx, player, event|
if (event.option == 2)
tiara = TIARAS_BY_ID[event.id] tiara = TIARAS_BY_ID[event.id]
if (tiara != nil) if (tiara != nil)
tiara.send_config(player) tiara.send_config(player)
ctx.break_handler_chain ctx.break_handler_chain
end end
end end
end
#Unequip tiara #Unequip tiara
on :event, :item_action do |ctx, player, event| on :event, :first_item_action do |ctx, player, event|
if (event.option == 1)
tiara = TIARAS_BY_ID[event.id] tiara = TIARAS_BY_ID[event.id]
if (tiara != nil) if (tiara != nil)
send_empty_config(player) send_empty_config(player)
ctx.break_handler_chain ctx.break_handler_chain
end end
end end
end
#Create tiara #Create tiara
on :event, :item_on_object do |ctx, player, event| on :event, :item_on_object do |ctx, player, event|