Class RPSlot

java.lang.Object
marauroa.common.game.RPSlot
All Implemented Interfaces:
Cloneable, Iterable<RPObject>, Serializable
Direct Known Subclasses:
EntitySlot

public class RPSlot extends Object implements Serializable, Iterable<RPObject>, Cloneable
This class represent a slot in an object
  • Constructor Details

    • RPSlot

      public RPSlot()
      Constructor for deserialization. Please use RPSlot(String).
    • RPSlot

      public RPSlot(String name)
      Constructor
      Parameters:
      name - name of the slot
  • Method Details

    • getOwner

      protected SlotOwner getOwner()
      This method returns the owner of the object
      Returns:
      the owner of the slot
    • setName

      public void setName(String name)
      Sets the name of the slot
      Parameters:
      name - the name of the slot.
    • getName

      public String getName()
      Get the name of the slot
      Returns:
      the name of the object.
    • add

      public int add(RPObject object)
      Add an object to the slot. It calls assignSlotID for the object to assign it a valid unique id for the object inside the container.
      Parameters:
      object - the object to add to this slot.
      Returns:
      the id assigned to the object
      Throws:
      SlotIsFullException - if there is no more room at the slot.
      NullPointerException - if Owner is null
    • addPreservingId

      public int addPreservingId(RPObject object)
      adds an object to a slot preserving its it. Note: In most cases you want to assign a new id.
      Parameters:
      object - RPObject to add
      Returns:
      the id of the object
    • add

      protected int add(RPObject object, boolean assignId)
      adds an object to a slot preserving its it. Note: In most cases you want to assign a new id.
      Parameters:
      object - RPObject to add
      assignId - true to assign a new, conflict free ID.
      Returns:
      the id of the object
    • get

      public RPObject get(RPObject.ID id)
      Gets the object from the slot
      Parameters:
      id - the object id. Note that only object_id field is relevant.
      Returns:
      the object or null if it is not found.
    • getFirst

      public RPObject getFirst()
      Gets the first object from the slot.
      Returns:
      the first object of the slot or null if it is empty.
    • remove

      public RPObject remove(RPObject.ID id)
      This method removes the object from the slot. When an object is removed from the slot, its contained information is set to null.
      Parameters:
      id - the object id. Note that only object_id field is relevant.
      Returns:
      the object or null if it is not found.
    • clear

      public void clear()
      This method empty the slot by removing all the objects inside.
    • has

      public boolean has(RPObject.ID id)
      This method returns true if the slot has the object whose id is id
      Parameters:
      id - the object id. Note that only object_id field is relevant.
      Returns:
      true if it is found or false otherwise.
    • hasAsAncestor

      public boolean hasAsAncestor(RPObject object)
      Traverses up the container tree to see if the slot is owned by object or by one of its parents
      Parameters:
      object - the object id. Note that only object_id field is relevant.
      Returns:
      true if this slot is owned (at any depth) by id or false otherwise.
    • hasAsAncestor

      public boolean hasAsAncestor(SlotOwner object)
      Traverses up the container tree to see if the slot is owned by object or by one of its parents
      Parameters:
      object - the object id. Note that only object_id field is relevant.
      Returns:
      true if this slot is owned (at any depth) by id or false otherwise.
    • size

      public int size()
      Return the number of elements in the slot
      Returns:
      the number of elements in the slot
    • getCapacity

      public int getCapacity()
      Returns the maximum amount of objects that can be stored at the slot. When there is no limit we use the -1 value.
      Returns:
      the maximum amount of objects that can be stored at the slot.
    • setCapacity

      public void setCapacity(int capacity)
      Set the capacity of the slot. By default the value from the RPClass definition is used. It is the responsibility of the caller to ensure sane behavior if the capacity of a non-empty slot is modified. Also, since the slot size is not serialized, the client will always use the value from the RPClass. Therefore, do not use this method to increase the capacity, because the client will not be able cope with slots that have more objects than the capacity defined in the RPClass.
      Parameters:
      capacity - new capacity
    • isFull

      public boolean isFull()
      Returns true if the slot is full.
      Returns:
      true if the slot is full.
    • isEmpty

      public boolean isEmpty()
      Is this slot empty?
      Returns:
      true if there are no objects in this slot
    • iterator

      public Iterator<RPObject> iterator()
      Iterate over the objects of the slot. We disallow removing objects from the iterator to avoid breaking delta^2 algorithm
      Specified by:
      iterator in interface Iterable<RPObject>
      Returns:
      an unmodifiable iterator object the objects.
    • equals

      public boolean equals(Object object)
      Returns true if both objects are equal
      Overrides:
      equals in class Object
      Parameters:
      object - to compare against
      Returns:
      true if both objects are equal
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • writeObject

      public void writeObject(OutputSerializer out) throws IOException
      This method serialize the object with the default level of detail, that removes private and hidden attributes
      Specified by:
      writeObject in interface Serializable
      Parameters:
      out - the output serializer
      Throws:
      IOException - in case of an IO-error
    • writeObject

      public void writeObject(OutputSerializer out, DetailLevel level) throws IOException
      This method serialize the object with the given level of detail.
      Parameters:
      out - the output serializer
      level - the level of Detail
      Throws:
      IOException - in case of an IO error
    • writeToJson

      public void writeToJson(StringBuilder out, DetailLevel level)
      This method serialize the object with the given level of detail.
      Parameters:
      out - the output buffer
      level - the level of Detail
    • readObject

      public void readObject(InputSerializer in) throws IOException
      Fills this object with the data that has been serialized.
      Specified by:
      readObject in interface Serializable
      Parameters:
      in - InputSerializer to read from
      Throws:
      IOException - in case of an IO-error
    • clone

      public Object clone()
      This method create a copy of the slot
      Overrides:
      clone in class Object
      Returns:
      a depth copy of the object.
    • resetAddedAndDeletedRPObjects

      public void resetAddedAndDeletedRPObjects()
      Clear stored delta^2 information.
    • setAddedRPObject

      public boolean setAddedRPObject(RPSlot slot)
      Copy to given slot the objects added. It does a depth copy of the objects.
      Parameters:
      slot - the slot to copy added objects.
      Returns:
      true if there is any object added.
    • setDeletedRPObject

      public boolean setDeletedRPObject(RPSlot slot)
      Copy to given slot the objects deleted. It does a depth copy of the objects.
      Parameters:
      slot - the slot to copy added objects.
      Returns:
      true if there is any object added.
    • clearVisible

      public void clearVisible(boolean sync)
      Removes the visible objects from this slot. It iterates through the slots to remove the attributes too of the contained objects if they are empty.
      Parameters:
      sync - keep the structure intact, by not removing empty slots and links.