module Nova::Starbound::DefaultBehavior::Eventable::ClassMethods

Class methods.

Public Instance Methods

handle(type, meth = nil, &block) click to toggle source

Defines how to handle a specific type of packet. The method of handling a packet can be defined in 3 ways: first, if a method name (second argument) is given, that method is used to handle the packet. Second, if a block is given, that block is used. And last, if neither of them are provided, the method name is assumed from the struct type and packet type in the format +handle_<struct>_<packet>+. The given method can and should be private.

@note If a block isn’t used, the method should accept two

arguments (see yield params for the arguments).

@param type [Hash<Symbol, Symbol>] a single key-value pair,

with the key being the struct type and the value being
the packet type.

@param meth [nil, Symbol] the method name to use when

calling a block.

@yieldparam packet [Protocol::Packet] the packet. @yieldparam protocol [Protocol] the protocol. @return [void]

# File lib/nova/starbound/default_behavior/eventable.rb, line 31
def handle(type, meth = nil, &block)
  struct = type.keys.first
  packet = type.values.first

  proc = nil

  if meth
    proc = meth
  elsif block_given?
    proc = block
  else
    proc = :"handle_#{struct}_#{packet}"
  end

  handles[{struct => packet}] = proc
end
handles() click to toggle source

The handles for events that are defined on this class.

@return [Hash]

# File lib/nova/starbound/default_behavior/eventable.rb, line 51
def handles
  @handles ||= {}
end