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
+18 -17
View File
@@ -4,40 +4,41 @@ java_import 'org.apollo.cache.def.ItemDefinition'
java_import 'org.apollo.cache.def.NpcDefinition'
java_import 'org.apollo.cache.def.ObjectDefinition'
# Checks whether the amount of arguments provided is correct, sending the player the specified message if not.
# Checks whether the amount of arguments provided is correct, sending the player the specified
# message if not.
def valid_arg_length(args, length, player, message)
valid = length.kind_of?(Range) ? length.include?(args.length) : length == args.length
valid = length.is_a?(Range) ? length.include?(args.length) : length == args.length
player.send_message(message) if !valid
return valid
player.send_message(message) unless valid
valid
end
# Returns the name of the Object, Npc, or Item with the specified id.
def name_of(type, id)
types = [ :object, :item, :npc ]
types = [:object, :item, :npc]
unless types.include?(type)
raise "Invalid type of #{type} specified, must be one of #{types}"
fail "Invalid type of #{type} specified, must be one of #{types}"
end
return Kernel.const_get("#{type.capitalize}Definition").lookup(id).name.to_s
Kernel.const_get("#{type.capitalize}Definition").lookup(id).name.to_s
end
# Add a has_keys? method to hash
# Monkey-patches Hash to add a has_keys? method.
class Hash
def has_keys?(*keys)
keys.each { |key| return false unless has_key?(key) }
return true
keys.all? { |key| self.key?(key) }
end
end
# Monkey-patches Player to add a hash_level? method.
class Player
# Returns whether or not the player's current level is greater than or equal to the specified level.
def has_level(skill, level)
return skill_set.get_skill(skill).current_level >= level
# Returns whether or not the player's current level is greater than or equal to the specified
# level.
def level?(skill, level)
skill_set.get_skill(skill).current_level >= level
end
end
end
-27
View File
@@ -1,27 +0,0 @@
# Essentially a wrapper for specific message types to make them easier to use. Only supports the interception of a message type once (for good reason - this is supposed to
# be a utility, not a chain of interceptors inside the existing chain).
#
# Plugins that wish to expand the list of available message types (you probably don't) should use the add_interception method - see the item-on-item script for example usage.
#
# If you only wish to intercept a message, use the intercept method, e.g.
#
# intercept :item_on_item, used_id, target_id, :irreversible do |player, message|
# # code here
# end
# Calls the registered interception(s), if applicable.
def intercept(message, *args, &block)
raise 'Error - interceptions must provide a block.' unless block_given?
interception = INTERCEPTIONS[message]
if interception == nil then raise "No interception for message #{message}" else interception.call(*args, block) end
end
# Adds an interception.
def add_interception(message, &block)
INTERCEPTIONS[message] = block
end
private
INTERCEPTIONS = {}
@@ -1,46 +0,0 @@
# Adds an interception for the ItemOnItem message
add_interception :item_on_item do |used, target, reversible, block|
interception = ItemOnItemPair.new(used, target)
ITEM_PAIRS[interception] = block
ITEM_PAIRS[interception.reverse] = block if reversible == :reversible
end
private
# A hash of ItemOnItemPairs to blocks.
ITEM_PAIRS = {}
# A pair of items that will cause a block to be executed if one (the 'used' item) is used on the other (the 'target' item).
class ItemOnItemPair
attr_reader :used, :target
def initialize(used, target)
@used = used
@target = target
end
# Returns a new ItemOnItemPair that is the reverse of this.
def reverse
return ItemOnItemPair.new(@target, @used)
end
def eql?(other)
return (other.kind_of?(ItemOnItemPair) && @used == other.used && @target == other.target)
end
def hash
return @used << 16 | @target
end
end
# Adds a message listener to the item on item message.
on :message, :item_on_item do |player, message|
used, target = message.id, message.target_id
pair = ItemOnItemPair.new(used, target)
block = ITEM_PAIRS[pair]
block.call(player, message) unless block == nil
end
@@ -2,48 +2,49 @@ require 'java'
# Looks up the id of the npc with the specified name.
def lookup_npc(name)
return lookup_entity(:npc, name)
lookup_entity(:npc, name)
end
# Looks up the id of the item with the specified name.
def lookup_item(name)
return lookup_entity(:item, name)
lookup_entity(:item, name)
end
# Looks up the id of the object with the specified name.
def lookup_object(name)
return lookup_entity(:object, name)
lookup_entity(:object, name)
end
# Looks up the id of an entity of the specified type (either :npc, :item, or :object)
def lookup_entity(type, name)
type = type.to_s
name = name.to_s.gsub('_', ' ')
cached = NAME_CACHE[type + name]
return cached unless cached.nil?
id = name[name.rindex(' ') + 1, name.length - 1].to_i if name.include?(' ')
id = find_entities(type, name, 1).first if (id.nil? || id.zero?)
raise "The #{type} called #{name} could not be identified." if id.nil?
id = find_entities(type, name, 1).first if id.nil? || id.zero?
fail "The #{type} called #{name} could not be identified." if id.nil?
NAME_CACHE[type + name] = id
return id
id
end
# Finds entities with the specified type (e.g. npc) and name, returning possible ids as an array.
def find_entities(type, name, limit=5)
def find_entities(type, name, limit = 5)
ids = []
name.downcase!
Kernel.const_get("#{type.capitalize}Definition").definitions.each do |definition|
break if (ids.length == limit)
ids << definition.id.to_i if (definition.name.to_s.downcase == name)
ids << definition.id.to_i if definition.name.to_s.downcase == name
end
return ids
ids
end
private
NAME_CACHE = {} # Primitive, caching all may not be desirable.
NAME_CACHE = {} # Primitive, caching all may not be desirable.
+2 -4
View File
@@ -9,9 +9,7 @@
</authors>
<scripts>
<script>command.rb</script>
<script>intercept.rb</script>
<script>item-on-item-intercept.rb</script>
<script>name-lookup.rb</script>
<script>name_lookup.rb</script>
</scripts>
<dependencies /> <!-- This plugin should _NOT_ depend on anything. -->
</plugin>
</plugin>