Housekeeping

This commit is contained in:
KeepBotting
2019-03-26 14:05:40 -04:00
parent a0c78ced90
commit 739c331860
135 changed files with 4 additions and 4 deletions
+44
View File
@@ -0,0 +1,44 @@
require 'java'
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.
def valid_arg_length(args, length, player, message)
valid = length.is_a?(Range) ? length.include?(args.length) : length == args.length
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]
unless types.include?(type)
fail "Invalid type of #{type} specified, must be one of #{types}"
end
Kernel.const_get("#{type.capitalize}Definition").lookup(id).name.to_s
end
# Monkey-patches Hash to add a has_keys? method.
class Hash
def has_keys?(*keys)
keys.all? { |key| self.key?(key) }
end
end
# Monkey-patches Player to add a level? method.
class Player
# 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
+50
View File
@@ -0,0 +1,50 @@
require 'java'
# Looks up the id of the npc with the specified name.
def lookup_npc(name)
lookup_entity(:npc, name)
end
# Looks up the id of the item with the specified name.
def lookup_item(name)
lookup_entity(:item, name)
end
# Looks up the id of the object with the specified name.
def lookup_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?
fail "The #{type} called #{name} could not be identified." if id.nil?
NAME_CACHE[type + name] = 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)
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
end
ids
end
private
NAME_CACHE = {} # Primitive, caching all may not be desirable.
+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<plugin>
<id>util</id>
<version>1</version>
<name>Util</name>
<description>Adds utility methods for plugins.</description>
<authors>
<author>Major</author>
</authors>
<scripts>
<script>command.rb</script>
<script>name_lookup.rb</script>
</scripts>
<dependencies /> <!-- This plugin should _NOT_ depend on anything. -->
</plugin>