mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-06 08:40:03 +00:00
Add extra utility methods to Inventory and utils plugin.
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<plugin>
|
||||||
|
<id>skill-fletching</id>
|
||||||
|
<version>1</version>
|
||||||
|
<name>Fletching</name>
|
||||||
|
<description>Adds the Fletching skill.</description>
|
||||||
|
<authors>
|
||||||
|
<author>Major</author>
|
||||||
|
</authors>
|
||||||
|
<scripts>
|
||||||
|
<script>fletching.rb</script>
|
||||||
|
</scripts>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>util</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</plugin>
|
||||||
@@ -15,5 +15,7 @@
|
|||||||
<script>talisman.rb</script>
|
<script>talisman.rb</script>
|
||||||
<script>tiara.rb</script>
|
<script>tiara.rb</script>
|
||||||
</scripts>
|
</scripts>
|
||||||
<dependencies />
|
<dependencies>
|
||||||
|
<dependency>util</dependency>
|
||||||
|
</dependencies>
|
||||||
</plugin>
|
</plugin>
|
||||||
@@ -7,6 +7,16 @@ def valid_arg_length(args, length, player, message)
|
|||||||
return valid
|
return valid
|
||||||
end
|
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)
|
||||||
|
raise "Invalid type of #{type} specified, must be one of #{types}"
|
||||||
|
end
|
||||||
|
|
||||||
|
return Kernel.const_get("#{type.capitalize}Definition").lookup(id).name.to_s
|
||||||
|
end
|
||||||
|
|
||||||
# Add a has_keys? method to hash
|
# Add a has_keys? method to hash
|
||||||
class Hash
|
class Hash
|
||||||
|
|
||||||
@@ -19,7 +29,7 @@ end
|
|||||||
|
|
||||||
class Player
|
class Player
|
||||||
|
|
||||||
# Returns whether or not the player's
|
# Returns whether or not the player's current level is greater than or equal to the specified level.
|
||||||
def has_level(skill, level)
|
def has_level(skill, level)
|
||||||
return skill_set.get_skill(skill).current_level >= level
|
return skill_set.get_skill(skill).current_level >= level
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -19,17 +19,13 @@ end
|
|||||||
def lookup_entity(type, symbol)
|
def lookup_entity(type, symbol)
|
||||||
symbol = symbol.to_s
|
symbol = symbol.to_s
|
||||||
cached = NAME_CACHE[type + symbol]
|
cached = NAME_CACHE[type + symbol]
|
||||||
return cached unless cached == nil
|
return cached unless cached.nil?
|
||||||
|
|
||||||
name = symbol.to_s.gsub('_', ' ')
|
name = symbol.to_s.gsub('_', ' ')
|
||||||
if name.include?(' ')
|
id = name[name.rindex(' ') + 1, name.length - 1].to_i if name.include?(' ')
|
||||||
id = name[name.rindex(' ') + 1, name.length - 1].to_i
|
id = find_entities(type, name, 1).first if (id .nil? || id.zero?)
|
||||||
end
|
|
||||||
id = find_entities(type, name, 1).first if id == nil || id == 0
|
|
||||||
|
|
||||||
if id == nil
|
raise "The #{type} called #{name} could not be identified." if id.nil?
|
||||||
raise "The #{type} called #{name} could not be identified."
|
|
||||||
end
|
|
||||||
|
|
||||||
NAME_CACHE[type + symbol] = id
|
NAME_CACHE[type + symbol] = id
|
||||||
return id
|
return id
|
||||||
@@ -37,15 +33,16 @@ end
|
|||||||
|
|
||||||
# Finds entities with the specified type (e.g. npc) and name, returning possible ids as an array.
|
# 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!
|
ids = [];
|
||||||
|
name.downcase!
|
||||||
|
|
||||||
Kernel.const_get("#{type.capitalize}Definition").definitions.each do |definition|
|
Kernel.const_get("#{type.capitalize}Definition").definitions.each do |definition|
|
||||||
break if ids.length == limit
|
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
|
end
|
||||||
|
|
||||||
return ids
|
return ids
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
NAME_CACHE = {}
|
NAME_CACHE = {} # Primitive, caching all may not be desirable.
|
||||||
@@ -415,6 +415,24 @@ public final class Inventory {
|
|||||||
return remove(id, 1) == 1;
|
return remove(id, 1) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes one item with each of the specified ids.
|
||||||
|
* <p>
|
||||||
|
* This method will attempt to remove one of each item, and will continue even if a previous item could not be
|
||||||
|
* removed.
|
||||||
|
*
|
||||||
|
* @param ids The ids of the item to remove.
|
||||||
|
* @return {@code true} if one of each item could be removed, otherwise {@code false}.
|
||||||
|
*/
|
||||||
|
public boolean remove(int... ids) {
|
||||||
|
boolean successful = true;
|
||||||
|
for (int id : ids) {
|
||||||
|
successful &= remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return successful;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes {@code amount} of the item with the specified {@code id}. If the item is stackable, it will remove it
|
* Removes {@code amount} of the item with the specified {@code id}. If the item is stackable, it will remove it
|
||||||
* from the stack. If not, it'll remove {@code amount} items.
|
* from the stack. If not, it'll remove {@code amount} items.
|
||||||
@@ -505,7 +523,7 @@ public final class Inventory {
|
|||||||
int removed = Math.min(amount, itemAmount);
|
int removed = Math.min(amount, itemAmount);
|
||||||
int remainder = itemAmount - removed;
|
int remainder = itemAmount - removed;
|
||||||
|
|
||||||
set(slot, remainder > 0 ? new Item(item.getId(), remainder) : null);
|
set(slot, (remainder > 0) ? new Item(item.getId(), remainder) : null);
|
||||||
return removed;
|
return removed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user