/** * Return true if the Item <> exists. * * @param id - Name of the item. * @return true if the room as an item with that name. */ public boolean existsItem(String id) { Iterator<Item> it = items.iterator(); while (it.hasNext()) { Item aux = it.next(); if (aux.getId().equalsIgnoreCase(id)) return true; } return false; // NO SE HA ENCONTRADO EL ITEM CON ESE ID }
/** * Returns the room description and a list with the ids of the items and their description that * belongs to this room. <> <> * * @return The room description */ public String getDescription() { String aux = ""; String infoItems = ""; Iterator<Item> it = this.items.iterator(); if (it.hasNext()) { aux = this.description + Msg.room_items; ; while (it.hasNext()) { Item auxItem = it.next(); infoItems = infoItems + auxItem.toString() + "\n"; } } else aux = this.description + Msg.room_empty; return aux + infoItems; }
/** * Pick an item from the room and add it to the player's inventory. * * @param who - the player * @param id - the identifier of the item * @return true if the action was successfully performed. */ public boolean pickItem(Player who, String id) { if (!existsItem(id)) { return false; } else { Iterator<Item> it = items.iterator(); while (it.hasNext()) { Item aux = it.next(); if (aux.getId().equalsIgnoreCase(id)) if (who.addItem(aux)) { items.remove(aux); return true; } else return false; } } return false; }
/** * Add a given item. The name (id) of the item should be unique in the room. * * @param it - Item to be added * @return true if the action was completed. */ public boolean addItem(Item it) { if (existsItem(it.getId())) { return false; } else { items.add(it); return true; } }