aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/game/items/Inventory.java
blob: 04f5a0b6458daa30d8f3057ec0e1ea6b0f75723e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package esieequest.game.items;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import lombok.Getter;
import net.pacien.util.IntrinsicMap;

import org.json.simple.JSONArray;

import esieequest.engine.utils.EnumUtils;
import esieequest.engine.utils.ListUtils;
import esieequest.engine.utils.SerialisableList;
import esieequest.game.Text;

/**
 * Symbolises an inventory (ItemList) containing multiple Item-s.
 * 
 * @author Pacien TRAN-GIRARD
 * @author Benoît LUBRANO DI SBARAGLIONE
 */
public class Inventory implements SerialisableList {

	@Getter
	private final IntrinsicMap<String, Item> items;

	/**
	 * Creates an empty Inventory.
	 */
	public Inventory() {
		this.items = new IntrinsicMap<>();
	}

	/**
	 * Tells if the Inventory contains an Item referred by its name.
	 * 
	 * @param itemName
	 *            the name of the Item
	 * 
	 * @return true if the Player have the Item
	 */
	public boolean hasItem(final String itemName) {
		return this.items.containsKey(itemName);
	}

	/**
	 * Tells if the Inventory contains a given Item.
	 * 
	 * @param item
	 *            the Item
	 * 
	 * @return true if the Player have the Item
	 */
	public boolean hasItem(final Item item) {
		return this.items.containsValue(item);
	}

	/**
	 * Returns the total weight of the Inventory.
	 * 
	 * @return the sum of all the Item-s
	 */
	public int getTotalWeight() {
		int totalWeight = 0;
		for (final Item item : this.items.values()) {
			totalWeight += item.getWeight();
		}
		return totalWeight;
	}

	/**
	 * Puts an Item in the Inventory.
	 * 
	 * @param item
	 *            the Item
	 */
	public void putItem(final Item item) {
		this.items.put(item);
	}

	/**
	 * Removes an Item, referred by its name, from the inventory.
	 * 
	 * @param itemName
	 *            the name of the Item to remove
	 */
	public void removeItem(final String itemName) {
		this.items.remove(itemName);
	}

	/**
	 * Removes an Item from the inventory.
	 * 
	 * @param item
	 *            the Item to remove
	 */
	public void removeItem(final Item item) {
		this.items.values().remove(item);
	}

	/**
	 * Removes all items from the Inventory.
	 */
	public void empty() {
		this.items.clear();
	}

	/**
	 * Lists all the Item-s names in a String.
	 * 
	 * @return the list of the Item-s in a String
	 */
	public String listItemsNamesString() {
		return ListUtils.listToString(new ArrayList<String>(this.items.keySet()),
				Text.INVENTORY_PREFIX.toString(), Text.ITEMS_NO_ITEM.toString(),
				Text.INVENTORY_SUFFIX.toString());
	}

	/**
	 * Retrieves an Item, referred by its name, from the inventory.
	 * 
	 * @param itemName
	 *            the name of the Item
	 * 
	 * @return the Item
	 */
	public Item getItem(final String itemName) {
		return this.items.get(itemName);
	}

	/**
	 * Retrieves one Item from the Inventory.
	 * 
	 * @return an item
	 */
	public Item getItem() {
		for (final Item item : this.items.values()) {
			return item;
		}
		return null;
	}

	/**
	 * Retrieves and removes an Item, referred by its name, from the inventory.
	 * 
	 * @param itemName
	 *            the name of the Item
	 * 
	 * @return the Item
	 */
	public Item takeItem(final String itemName) {
		return this.items.remove(itemName);
	}

	/**
	 * Retrieves and removes one Item from the Inventory.
	 * 
	 * @return the item
	 */
	public Item takeItem() {
		final Item item = this.getItem();
		this.removeItem(item);
		return item;
	}

	/**
	 * Lists all Item-s in the Inventory.
	 * 
	 * @return the list of the Item-s
	 */
	public Collection<Item> listItems() {
		return this.items.values();
	}

	/**
	 * Returns informations about the inventory weight.
	 * 
	 * @return informations about the inventory weight
	 */
	public String getWeightString() {
		return Text.INVENTORY_WEIGHT_PREFIX.toString() + this.getTotalWeight()
				+ Text.INVENTORY_WEIGHT_SUFFIX.toString();
	}

	/**
	 * Returns the number of Item-s contained in the Inventory.
	 * 
	 * @return the size of the Inventory
	 */
	public int getSize() {
		return this.items.size();
	}

	@Override
	public JSONArray serialise() {
		final JSONArray a = new JSONArray();

		a.addAll(Arrays.asList(EnumUtils.nameAll(this.items.values().toArray(new Item[0]))));

		return a;
	}

	@Override
	public void deserialise(final JSONArray a) {
		this.items.clear();
		if (a != null) {
			this.items.putAll(EnumUtils.valuesOf(Item.class, a.toArray(new String[0])));
		}
	}

}