Update all plugins to conform to Rubocop.

This commit is contained in:
Major-
2015-08-27 18:17:58 +01:00
parent 424d2bda29
commit 8f3fd75b33
75 changed files with 1625 additions and 1537 deletions
+21 -18
View File
@@ -12,47 +12,48 @@ java_import 'org.apollo.game.model.entity.attr.BooleanAttribute'
java_import 'org.apollo.game.model.entity.attr.NumericalAttribute'
java_import 'org.apollo.game.model.entity.attr.StringAttribute'
# Declares an attribute and adds its definition.
def declare_attribute(name, default, persistence=:transient)
raise "Attribute #{name} clashes with an existing variable." if (Player.method_defined?(name) || Mob.method_defined?(name) || Npc.method_defined?(name))
AttributeMap::define(name.to_s, AttributeDefinition.new(default, get_persistence(persistence), get_type(default)))
end
def declare_attribute(name, default, persistence = :transient)
if Player.method_defined?(name) || Mob.method_defined?(name) || Npc.method_defined?(name)
fail "Attribute #{name} clashes with an existing variable."
end
definition = AttributeDefinition.new(default, get_persistence(persistence), get_type(default))
AttributeMap::define(name.to_s, definition)
end
private
# The existing Mob class.
class Mob
# Overrides method_missing to implement the functionality.
def method_missing(symbol, *args)
name = symbol.to_s.strip
if name[-1] == "="
raise "Expected argument count of 1, received #{args.length}" unless args.length == 1
if name[-1] == '='
fail "Expected argument count of 1, received #{args.length}" unless args.length == 1
name = name[0...-1].strip # Drop the equals and trim whitespace.
set_attribute(name, to_attribute(args[0]))
elsif AttributeMap::get_definition(name).nil?
super(symbol, *args)
else
attribute = get_attribute(name)
value = attribute.value
return (attribute.type == AttributeType::SYMBOL) ? value.to_sym : value
value = attribute.value
(attribute.type == AttributeType::SYMBOL) ? value.to_sym : value
end
end
end
# Gets the appropriate attribute for the specified value.
# Gets the appropriate attribute for the specified value.
def to_attribute(value)
case value
when String, Symbol then return StringAttribute.new(value.to_s, value.is_a?(Symbol))
when Integer, Float then return NumericalAttribute.new(value)
when TrueClass, FalseClass then return BooleanAttribute.new(value)
else raise "Undefined attribute type #{value.class}."
else fail "Undefined attribute type #{value.class}."
end
end
@@ -64,13 +65,15 @@ def get_type(value)
when Integer then return AttributeType::LONG
when Float then return AttributeType::DOUBLE
when TrueClass, FalseClass then return AttributeType::BOOLEAN
else raise "Undefined attribute type #{value.class}."
else fail "Undefined attribute type #{value.class}."
end
end
# Gets the Persistence type of the specified value.
def get_persistence(persistence)
raise "Undefined persistence type #{persistence}." unless [ :persistent, :transient ].include?(persistence)
unless [:persistent, :transient].include?(persistence)
fail "Undefined persistence type #{persistence}."
end
return (persistence == :persistent) ? AttributePersistence::PERSISTENT : AttributePersistence::TRANSIENT
end
(persistence == :persistent) ? AttributePersistence::PERSISTENT : AttributePersistence::TRANSIENT
end
+37 -27
View File
@@ -10,22 +10,29 @@ java_import 'org.apollo.game.model.entity.Npc'
# Information about npc spawning
#
# Npcs are passed to spawn npc as a hash. Every key and every non-integer value must be a Symbol. Every hash must implement the following:
# :name - the name of the npc. If this npc shares its name with another, append the specific id after the name (e.g. :woman_4)
# Npcs are passed to spawn npc as a hash. Every key and every non-integer value must be a Symbol.
# Every hash must implement the following:
# :name - the name of the npc. If this npc shares its name with another, append the specific id
# after the name (e.g. :woman_4)
# :x - the x coordinate where the npc will spawn.
# :y - the y coordinate where the npc will spawn.
# Optional arguments are as follows:
# :face - the direction the npc should face when it spawns. Supported options are :north, :north_east, :east, :south_east, :south, :south_west, :west, and :north_west
# :bounds - the rectangular bound that the npc can wander about in. Order is [bottom-left x-coordinate, bottom-left y-coordinate, top-right x-coordinate, top-right y-coordinate]
# :delta_bounds - the rectangular bound that the npc can wander about in, as a difference from the spawn point. Order is [x-delta, y-delta]. Should not be used with :bounds.
# :face - the direction the npc should face when it spawns. Supported options are :north,
# :north_east, :east, :south_east, :south, :south_west, :west, and :north_west
# :bounds - the rectangular bound that the npc can wander about in. Order is
# [bottom-left x-coordinate, bottom-left y-coordinate, top-right x-coordinate,
# top-right y-coordinate]
# :delta_bounds - the rectangular bound that the npc can wander about in, as a difference from
# the spawn point. Order is [x-delta, y-delta]. Should not be used with :bounds.
# :spawn_animation - the animation that will be played when the npc spawns.
# :spawn_graphic - the graphic that will be played when the npc spawns.
# Spawns an npc with the properties specified in the hash.
def spawn_npc(hash)
raise 'A name (or id), x coordinate, and y coordinate must be specified to spawn an npc.' unless (hash.has_key?(:name) || hash.has_key?(:id)) && hash.has_keys?(:x, :y)
unless (hash.key?(:name) || hash.key?(:id)) && hash.has_keys?(:x, :y)
fail 'A name (or id), x coordinate, and y coordinate must be specified to spawn an npc.'
end
npc = get_npc(hash)
spawn(npc, hash)
npc
@@ -46,8 +53,8 @@ def get_npc(hash)
id = lookup_npc(hash.delete(:name))
z = hash.delete(:z)
position = Position.new(hash.delete(:x), hash.delete(:y), z == nil ? 0 : z)
return Npc.new($world, id, position)
position = Position.new(hash.delete(:x), hash.delete(:y), z.nil? ? 0 : z)
Npc.new($world, id, position)
end
# Applies a decoded hash (one aquired using parse_hash) to the specified npc.
@@ -58,7 +65,7 @@ def apply_decoded_hash(npc, hash)
when :boundary then npc.boundaries = value
when :spawn_animation then npc.play_animation(Animation.new(value))
when :spawn_graphic then npc.play_graphic(Graphic.new(value))
else raise "Unrecognised key #{key} - value #{value}."
else fail "Unrecognised key #{key} - value #{value}."
end
end
end
@@ -72,27 +79,27 @@ def decode_hash(position, hash)
when :face
decoded[:face] = direction_to_position(value, position)
when :delta_bounds
raise ':delta_bounds must have two values.' unless value.length == 2
fail ':delta_bounds must have two values.' unless value.length == 2
dx, dy, x, y, z = value[0], value[1], position.x, position.y, position.height
raise 'Delta values cannot be less than 0.' if (dx < 0 || dy < 0)
fail 'Delta values cannot be less than 0.' if dx < 0 || dy < 0
decoded[:boundary] = [ Position.new(x - dx, y - dy, z), Position.new(x + dx, y + dy, z) ]
decoded[:boundary] = [Position.new(x - dx, y - dy, z), Position.new(x + dx, y + dy, z)]
when :bounds
raise ':bounds must have four values.' unless value.length == 4
fail ':bounds must have four values.' unless value.length == 4
min_x, min_y, max_x, max_y = value[0], value[1], value[2], value[3]
decoded[:boundary] = [ Position.new(min_x, min_y), Position.new(max_x, max_y) ]
decoded[:boundary] = [Position.new(min_x, min_y), Position.new(max_x, max_y)]
when :spawn_animation then decoded[:spawn_animation] = Animation.new(value)
when :spawn_graphic then decoded[:spawn_graphic ] = Graphic.new(value)
else raise "Unrecognised key #{key} - value #{value}."
when :spawn_graphic then decoded[:spawn_graphic] = Graphic.new(value)
else fail "Unrecognised key #{key} - value #{value}."
end
end
return decoded
decoded
end
# Returns a position that an entity at the specified position should be facing towards if they are looking in the specified direction.
# Returns a position that an entity at the specified position should be facing towards if they are
# looking in the specified direction.
def direction_to_position(direction, position)
x, y, z = position.x, position.y, position.height
@@ -121,13 +128,14 @@ class TemporaryNpcAction < Action
end
def execute
if executions == 0
if @executions == 0
spawn(mob, @hash)
execute_spawn_action
else
execute_action
end
executions += 1
@executions += 1
end
def execute_action
@@ -168,20 +176,22 @@ RANDOM_EVENTS = []
# Spawns a random event for the specified player.
def send_random_event(player)
position = player.position
npc_position = Position.new(position.x + 1, position.y, position.height) # TODO Find an unoccupied tile instead of the assumption that (x + 1) is traversable!!
npc_position = Position.new(position.x + 1, position.y, position.height)
# TODO: Find an unoccupied tile instead of the assumption that (x + 1) is traversable!!
spawn_random_event(npc_position, false)
end
# Spawns a random event in the specified position.
# If 'combat' is false, only non-combat events will be spawned.
def spawn_random_event(position, combat)
def spawn_random_event(_position, _combat)
event = RANDOM_EVENTS[rand(RANDOM_EVENTS.size)]
attempts = 0
while (event.combative && attempts < 5)
while event.combative && attempts < 5
event = RANDOM_EVENTS[rand(RANDOM_EVENTS.size)]
attempts += 1
end
event.execute unless attempts == 5 # 5 iterations is plenty, just give up at this point
end
end