wyvern.lib
Interface Bag

All Superinterfaces:
Broadcaster, Container, GameObject, MethodHookable, PropertyList
All Known Subinterfaces:
Inventory
All Known Implementing Classes:
BasicBag, MonsterInventory, Trenchcoat

public interface Bag
extends GameObject, Container

Main container interface in the game, for bags, quivers, scabbards, reagent pouches, backpacks, chests, and so on.

The generic bag type can hold any sort of object, but has a maximum size object that it can contain.

Bags' contents are stored in a sequential list; i.e. each item in the bag has an index, to facilitate removing otherwise identical items.

If you don't want people to take stuff from the bag, set a "no-get-from" property on it whose value is a message to print, such as "You cannot take things from this bag." A supported variation on this is "no-get-from-inv", which prevents the user from taking things from the container while it's in the user's inventory.

Version:
1.0, Jun 12, 1998
Author:
Steve Yegge

Nested Class Summary
static class Bag.FullBagException
          This exception is thrown when someone tries to put something in a bag that can't hold any more items.
static class Bag.InvalidItemException
          Thrown when someone tries to put an item in the bag that isn't appropriate for this bag, as defined by the bag's filter() method.
 
Field Summary
static int BAG_CUT_PERCENT_CAP
          Bags aren't allowed to reduce weight more than this.
static long BAG_DEFAULT_CAPACITY
          If a bag is missing its "capacity" property, it makes more sense to have it hold a default value rather than nothing at all.
 
Fields inherited from interface wyvern.lib.PropertyList
PROPERTY_PACKAGE
 
Method Summary
 GameObject add(GameObject item)
          Adds an item to the bag (at the end).
 void adjust(GameObject item, int index)
          Adjusts the position of an item in the list without removing it and adding it (thereby skipping the encumbrance and other checks).
 boolean canHoldMore()
          Returns true if we're not at our max count for this bag.
 boolean contains(GameObject item)
          Returns true if the container contains the passed item.
 boolean filter(GameObject item)
          Returns true if the container can hold this type of item.
 GameObject find(Predicate p)
          Returns an object satisfying the specified Predicate, without actually removing the object.
 GameObject findByName(java.lang.String name)
          Returns the first object that matches the passed name.
 long getCapacity()
          Returns the carrying capacity of the bag.
 long getItemWeight()
          Returns the (unadjusted) weight of the items in this bag.
 int getMaxCount()
          Returns the maximum number of items that can be stored in this bag.
 int getPosition(GameObject item)
          Returns the position of the specified item in the list.
 long getRemainingCapacity()
          Returns how much more weight we can hold.
 boolean hasRoomFor(GameObject obj)
          Returns true if the specified object could be added to the bag's current contents without exceeding the bag's weight capacity.
 boolean hasRoomFor(long weight)
          Returns true if the specified weight could be added to the bag's current contents without exceeding the bag's weight capacity.
 int indexOf(GameObject item)
          Returns the index of the specified object in the bag.
 void insert(GameObject item, int index)
          Inserts an item into the bag at the specified index.
 void invalidate(GameObject item)
          Visually invalidates the specified object in the container.
 boolean isEmpty()
          Returns true if the bag contains no items.
 java.util.Iterator iterator()
          Returns an Iterator over the items in the bag.
 GameObject objectAt(int index)
          Returns the object at the specified index, or null if the index is out-of-bounds.
 java.lang.String printItemList()
          Prints a list of the items in the container, without any heading.
 boolean remove(GameObject item)
          Removes an item from the bag; does nothing if the item isn't in the bag.
 GameObject remove(int index)
          Removes and returns the object at the specified index.
 java.util.List removeAll()
          Removes all items and returns them as a list.
 java.util.List removeAll(Predicate p)
          Removes all items matching the specified Predicate and returns them as a list.
 GameObject removeFirst()
          Removes and returns the first object from the inventory.
 GameObject removeFirst(Predicate p)
          Removes the first instance of an object satisfying a Predicate.
 void setCapacity(long capacity)
          Sets the max carrying-capacity of the bag.
 void setCapacity(Weight w)
          Sets the max carrying-capacity of the bag.
 GameObject setObjectAt(int index, GameObject item)
          Replaces the object at the specified index with a new object.
 int size()
          Returns the number of objects in the bag.
 void visitObjects(Visitor v)
          Visits the objects in the inventory with the passed Visitor.
 
Methods inherited from interface wyvern.lib.GameObject
addContainerChangeListener, addMapChangeListener, addMapMotionListener, addMotionTracker, addObjectTracker, addPrefix, addSuffix, bless, canEnter, canEnterBlockedBy, canMove, canMove, canMoveBlockedBy, canMoveBlockedBy, canMoveTo, canMoveToBlockedBy, checkDrop, curse, cycleFrame, destroy, externalize, getAlpha, getAppearance, getArchetype, getBaseWeight, getBitmap, getBounds, getCanonicalClassName, getCategory, getContainer, getCreator, getDamagedDescription, getDescription, getDirection, getGenderPossessive, getGenderPronoun, getImage, getImageDescriptor, getImageDescriptors, getLayer, getLocations, getMap, getMapLink, getMaterial, getMaterialDescription, getMover, getOwningPlayer, getParentMap, getPrefixes, getQuantity, getReferenceLoc, getRelativeLocs, getShortDesc, getSuffixes, getTile, getTile, getValue, getWeight, inContainer, initialize, inMapEditor, invalidate, invalidateImage, invalidateParent, invalidateText, isAnimated, isAttackable, isBlessed, isCommandable, isCursed, isDamned, isGroupable, isIdentified, isMonster, isMonsterOrPlayer, isPlayer, isTerrain, isUncursed, isUnpaid, isWizard, makeClone, markPaid, markUnpaid, move, occupies, positionAt, positionAtMap, remove, removeContainerChangeListener, removeMapChangeListener, removeMapMotionListener, removeMotionTracker, removeObjectTracker, removePrefix, removeSuffix, setAlpha, setAnimated, setBitmap, setCategory, setContainer, setDirection, setImage, setImage, setLayer, setLocations, setMap, setMapLink, setQuantity, setShape, setSize, setTransientSize, setWeight, setWeight, teleport
 
Methods inherited from interface wyvern.lib.PropertyList
addProperty, addTransientProperty, adjustDoubleProperty, adjustIntProperty, adjustLongProperty, adjustTransientDoubleProperty, adjustTransientIntProperty, adjustTransientLongProperty, countLocalProperties, getDoubleProperty, getInheritedProperty, getIntProperty, getLocalProperties, getLocalProperty, getLongProperty, getParent, getPersistentDoubleProperty, getPersistentIntProperty, getPersistentLocalProperties, getPersistentLongProperty, getPersistentProperty, getProperties, getProperties, getPropertiesIncludingTransients, getPropertiesIncludingTransients, getProperty, getSerializableProperties, getSerializableProperty, getStringProperty, getTransientDoubleProperty, getTransientIntProperty, getTransientLongProperty, getTransientProperties, getTransientProperty, hasLocalProperty, hasPersistentProperty, hasProperty, hasTransientProperty, inheritProperty, isReadOnly, isRemoved, isTransientlyRemoved, printLocalProperties, printProperties, printProperties, printTransientProperties, removeProperty, removeTransientProperty, setDoubleProperty, setIntProperty, setLongProperty, setParent, setProperty, setReadOnly, setTransientDoubleProperty, setTransientIntProperty, setTransientLongProperty, setTransientProperty, toString, transientlyRemoveProperty
 
Methods inherited from interface wyvern.lib.MethodHookable
addMethodHook, removeMethodHook, runMethodHook
 
Methods inherited from interface wyvern.lib.Broadcaster
broadcast, broadcast, broadcast, broadcast, broadcast
 
Methods inherited from interface wyvern.lib.Container
isBag, isInventory, isMap
 

Field Detail

BAG_CUT_PERCENT_CAP

public static final int BAG_CUT_PERCENT_CAP
Bags aren't allowed to reduce weight more than this. Otherwise players wind up with thousands of items in their inventory, which crashes the database.

See Also:
Constant Field Values

BAG_DEFAULT_CAPACITY

public static final long BAG_DEFAULT_CAPACITY
If a bag is missing its "capacity" property, it makes more sense to have it hold a default value rather than nothing at all. This constant specifies the capacity (in grams) for a default bag.

See Also:
Constant Field Values
Method Detail

add

public GameObject add(GameObject item)
               throws Bag.FullBagException
Adds an item to the bag (at the end).

Parameters:
item - a GameObject to add
Returns:
the object that was added, or, if the item was added to a group, returns the group.
Throws:
Bag.FullBagException - bag doesn't have space for the item

contains

public boolean contains(GameObject item)
Returns true if the container contains the passed item.

Returns:
true if the item is in the container

filter

public boolean filter(GameObject item)
Returns true if the container can hold this type of item.

Returns:
true if the item can be placed in the container Default implementation always returns true, but subclasses like reagent pouches, wallets or quivers can implement their own rules.

find

public GameObject find(Predicate p)
Returns an object satisfying the specified Predicate, without actually removing the object.

Parameters:
p - a Predicate returning true or false for a given object
Returns:
the first object that satisfies the predicate
See Also:
Predicate

getCapacity

public long getCapacity()
Returns the carrying capacity of the bag.

Returns:
the capacity (in grams)

getRemainingCapacity

public long getRemainingCapacity()
Returns how much more weight we can hold.

Returns:
the total capacity of the bag, minus the total weight of the items in the bag so far

hasRoomFor

public boolean hasRoomFor(GameObject obj)
Returns true if the specified object could be added to the bag's current contents without exceeding the bag's weight capacity.

Parameters:
obj - the object to check
Returns:
true if the bag has sufficient capacity to hold obj

canHoldMore

public boolean canHoldMore()
Returns true if we're not at our max count for this bag. Some bags have a maximum number of items allowed, even if they can hold more weight.

Returns:
true if we're below our max count yet.

getMaxCount

public int getMaxCount()
Returns the maximum number of items that can be stored in this bag.

Returns:
the max count (Integer.MAX_VALUE, usually).

hasRoomFor

public boolean hasRoomFor(long weight)
Returns true if the specified weight could be added to the bag's current contents without exceeding the bag's weight capacity.


indexOf

public int indexOf(GameObject item)
Returns the index of the specified object in the bag.

Parameters:
item - the object to look for
Returns:
the index (zero-based), or -1 if not found

insert

public void insert(GameObject item,
                   int index)
            throws Bag.FullBagException
Inserts an item into the bag at the specified index.

Parameters:
index - the index to insert at. An index less than zero will be changed to zero (put obj at front), and an index larger than the size of the bag will put the object at the end.
item - the item to insert
Throws:
Bag.FullBagException

adjust

public void adjust(GameObject item,
                   int index)
Adjusts the position of an item in the list without removing it and adding it (thereby skipping the encumbrance and other checks).

Parameters:
index - the index to move it to. An index less than zero will be changed to zero (put obj at front), and an index larger than the size of the bag will put the object at the end.
item - the item to insert

isEmpty

public boolean isEmpty()
Returns true if the bag contains no items.

Returns:
true if the container is empty

invalidate

public void invalidate(GameObject item)
Visually invalidates the specified object in the container.

Parameters:
item - the item whose appearance has changed in some way

printItemList

public java.lang.String printItemList()
Prints a list of the items in the container, without any heading.

Returns:
a list of the items in the bag

iterator

public java.util.Iterator iterator()
Returns an Iterator over the items in the bag.

Warning: You *MUST* place iteration in a "synchronized (bag)" block during the iteration, as others may also be going through the bag's contents.

Returns:
an iterator that can be cast as a ListIterator

objectAt

public GameObject objectAt(int index)
Returns the object at the specified index, or null if the index is out-of-bounds.

Parameters:
index - the index to retrieve
Returns:
the object at that index, or null if not found

remove

public GameObject remove(int index)
Removes and returns the object at the specified index.

Parameters:
index - the 0-based index of the element to remove.
Returns:
the element previously at the specified position
Throws:
java.lang.IndexOutOfBoundsException - if the index is out of bounds

remove

public boolean remove(GameObject item)
Removes an item from the bag; does nothing if the item isn't in the bag.

Parameters:
item - the item to remove, or null if not found
Returns:
true if the item was removed, false if not found

removeAll

public java.util.List removeAll()
Removes all items and returns them as a list.

Returns:
a List containing all the GameObjects from the bag; the bag will be empty after this call.

removeAll

public java.util.List removeAll(Predicate p)
Removes all items matching the specified Predicate and returns them as a list.

Returns:
a List containing all the GameObjects that were removed from the bag. Any object in the bag that passes the passed predicate() function will be removed. An empty list is returned if no objects in the bag matched the predicate.

removeFirst

public GameObject removeFirst()
Removes and returns the first object from the inventory.

Returns:
the first object in the inventory (object is removed)
Throws:
java.lang.IndexOutOfBoundsException - if the list is empty

removeFirst

public GameObject removeFirst(Predicate p)
Removes the first instance of an object satisfying a Predicate.

Parameters:
p - a Predicate that returns true if the object is "the one"
See Also:
Predicate

setCapacity

public void setCapacity(long capacity)
Sets the max carrying-capacity of the bag.

Parameters:
capacity - the new capacity, stored in "capacity" property as a Weight object.

setCapacity

public void setCapacity(Weight w)
Sets the max carrying-capacity of the bag.

Parameters:
w - the weight to use as the new capacity

setObjectAt

public GameObject setObjectAt(int index,
                              GameObject item)
                       throws Bag.FullBagException
Replaces the object at the specified index with a new object.

Parameters:
index - the index to replace; if greater than the end of the list, the object will be appended; if less than zero, the object will be prepended.
item - the object to replace
Returns:
the replaced object, or null if none was replaced
Throws:
Bag.FullBagException

size

public int size()
Returns the number of objects in the bag.

Returns:
the number of distinct objects in the bag. Collection- type objects such as arrows or coins are counted as single objects.

visitObjects

public void visitObjects(Visitor v)
Visits the objects in the inventory with the passed Visitor.


findByName

public GameObject findByName(java.lang.String name)
Returns the first object that matches the passed name. Searches with a standard wyvern.lib.predicates.NameMatchPredicate.

Parameters:
name - the name substring to match
Returns:
the first object in the inventory matching the name, or null

getPosition

public int getPosition(GameObject item)
Returns the position of the specified item in the list.

Parameters:
item - the item to look for
Returns:
the item's 0-indexed list position, or -1 if not found

getItemWeight

public long getItemWeight()
Returns the (unadjusted) weight of the items in this bag. If you call bag.getWeight(), it adjusts the weight of the items in the bag by the "cut-%" property. This returns the unadjusted weight.

Returns:
the weight of the items in the bag, not including the weight of the bag itself