001    /**
002     * ItemArray.java - Interface to jh[] so I don't have to copy+paste this a bunch
003     * of times
004     * 
005     * @author James
006     */
007    public abstract class ItemArray<C extends Container<jl>> {
008        protected C container;
009        
010        public ItemArray(C c) {
011            this.container = c;
012        }
013    
014        public int getContentsSize() {
015            return container.getContentsSize();
016        }
017        
018        /**
019         * Adds the specified item. If the item doesn't have a slot, it will get the
020         * nearest available slot. If amount is equal to 0, it will delete the item
021         * if a slot is specified.
022         * 
023         * @param item item to add
024         */
025        public void addItem(Item item) {
026            if (item == null) {
027                return;
028            }
029    
030            int slot = item.getSlot();
031            int size = getContentsSize();
032    
033            if (slot < size && slot >= 0) {
034                if (item.getAmount() <= 0) {
035                    setSlot(null, slot);
036                } else if (Item.isValidItem(item.getItemId())) {
037                    setSlot(item, slot);
038                }
039            } else if (slot == -1) {
040                int newSlot = getEmptySlot();
041                if (newSlot != -1) {
042                    setSlot(item, newSlot);
043                    item.setSlot(newSlot);
044                }
045            }
046        }
047    
048        /**
049         * Retrieves from the slot
050         * 
051         * @param slot slot to get item from
052         * @return item
053         */
054        public Item getItemFromSlot(int slot) {
055            int size = getContentsSize();
056    
057            if (slot < size && slot >= 0) {
058                jl result = container.getContentsAt(slot);
059                if (result != null) {
060                    return new Item(result, slot);
061                }
062            }
063    
064            return null;
065        }
066    
067        /**
068         * Retrieves from the slot
069         * 
070         * @param type
071         * @return item
072         */
073        public Item getItemFromId(Item.Type type) {
074            return getItemFromId(type.getId());
075        }
076    
077        /**
078         * Retrieves from the slot
079         * 
080         * @param id
081         * @return item
082         */
083        public Item getItemFromId(int id) {
084            Item[] items = getContents();
085    
086            for (Item item : items) {
087                if ((item != null) && (item.getItemId() == id)) {
088                    return item;
089                }
090            }
091    
092            return null;
093        }
094    
095        /**
096         * Retrieves from the slot
097         * 
098         * @param type
099         * @param maxAmount
100         * @return item
101         */
102        public Item getItemFromId(Item.Type type, int maxAmount) {
103            return getItemFromId(type.getId());
104        }
105    
106        /**
107         * Retrieves from the slot
108         * 
109         * @param id
110         * @param maxAmount
111         * @return item
112         */
113        public Item getItemFromId(int id, int maxAmount) {
114            Item[] items = getContents();
115    
116            for (Item item : items) {
117                if ((item != null) && (item.getItemId() == id) && (item.getAmount() <= maxAmount)) {
118                    return item;
119                }
120            }
121    
122            return null;
123        }
124    
125        /**
126         * Gets the nearest empty slot. -1 if there's no empty slots
127         * 
128         * @return nearest empty slot
129         */
130        public int getEmptySlot() {
131            int size = getContentsSize();
132    
133            for (int i = 0; size > i; i++) {
134                if (container.getContentsAt(i) != null) {
135                    continue;
136                }
137                return i;
138            }
139    
140            return -1;
141        }
142    
143        /**
144         * Removes the item from the slot
145         * 
146         * @param slot slot to remove item from
147         */
148        public void removeItem(int slot) {
149            int size = getContentsSize();
150    
151            if (slot < size && slot >= 0) {
152                container.setContentsAt(slot, null);
153            }
154        }
155    
156        /**
157         * Sets the specified slot with item
158         * 
159         * @param item item to set
160         * @param slot slot to use
161         */
162        public void setSlot(Item item, int slot) {
163            setSlot(item.getItemId(), item.getAmount(), slot);
164        }
165    
166        /**
167         * Replaces the slot with the specified item.
168         *
169         * @param type type of the item to put into the slot.
170         * @param amount amount of the item to put into the slot.
171         * @param slot the id of the slot.
172         */
173        public void setSlot(Item.Type type, int amount, int slot) {
174            setSlot(type.getId(), amount, slot);
175        }
176    
177        /**
178         * Replaces the slot with the specified item.
179         *
180         * @param itemId item id of the item to put into the slot.
181         * @param amount amount of the item to put into the slot.
182         * @param slot the id of the slot.
183         */
184        public void setSlot(int itemId, int amount, int slot) {
185            setSlot(itemId, amount, 0, slot);
186        }
187    
188        /**
189         * Replaces the slot with the specified item.
190         *
191         * @param itemId item id of the item to put into the slot.
192         * @param amount amount of the item to put into the slot.
193         * @param damage remaining damage of the item to put into the slot.
194         * @param slot the id of the slot.
195         */
196        public void setSlot(int itemId, int amount, int damage, int slot) {
197            int size = getContentsSize();
198    
199            if (slot < size && slot >= 0) {
200                container.setContentsAt(slot, new jl(itemId, (amount > 64 ? (amount == 255 ? -1 : 64) : amount), damage));
201            }
202        }
203    
204        /**
205         * Removes the item. No slot needed, it will go through the inventory until
206         * the amount specified is removed.
207         * 
208         * @param item item id and amount to remove
209         */
210        public void removeItem(Item item) {
211            removeItem(item.getItemId(), item.getAmount());
212        }
213    
214        /**
215         * Removes the item. No slot needed, it will go through the inventory until
216         * the amount specified is removed.
217         * 
218         * @param type item to remove
219         * @param amount amount to remove
220         */
221        public void removeItem(Item.Type type, int amount) {
222            removeItem(type.getId(), amount);
223        }
224    
225        /**
226         * Removes the item. No slot needed, it will go through the inventory until
227         * the amount specified is removed.
228         * 
229         * @param id item to remove
230         * @param amount amount to remove
231         */
232        public void removeItem(int id, int amount) {
233            Item[] items = getContents();
234            int remaining = amount;
235    
236            for (Item item : items) {
237                if ((item != null) && (item.getItemId() == id)) {
238                    if (item.getAmount() == remaining) {
239                        removeItem(item.getSlot());
240                        return;
241                    } else if (item.getAmount() > remaining) {
242                        setSlot(id, item.getAmount() - remaining, item.getSlot());
243                        return;
244                    } else {
245                        removeItem(item.getSlot());
246                        remaining -= item.getAmount();
247                    }
248                }
249            }
250        }
251    
252        /**
253         * Checks to see if this getArray() has one slot that has the given item type
254         * 
255         * @param type
256         * @return
257         */
258        public boolean hasItem(Item.Type type) {
259            return hasItem(type, 1);
260        }
261    
262        /**
263         * Checks to see if this getArray() has one slot that has the given item id
264         * 
265         * @param itemId
266         * @return
267         */
268        public boolean hasItem(int itemId) {
269            return hasItem(itemId, 1);
270        }
271    
272        /**
273         * Checks to see if this getArray() has one slot that has the item id and
274         * equal or more to the amount.
275         *
276         * @param type item to look for
277         * @param minimum amount of items that must be in the stack
278         * @return
279         */
280        public boolean hasItem(Item.Type type, int minimum) {
281            Item[] items = getContents();
282    
283            for (Item item : items) {
284                if ((item != null) && (item.getType() == type) && (item.getAmount() >= minimum)) {
285                    return true;
286                }
287            }
288    
289            return false;
290        }
291    
292        /**
293         * Checks to see if this getArray() has one slot that has the item id and
294         * equal or more to the amount.
295         * 
296         * @param itemId item to look for
297         * @param minimum amount of items that must be in the stack
298         * @return
299         */
300        public boolean hasItem(int itemId, int minimum) {
301            Item[] items = getContents();
302    
303            for (Item item : items) {
304                if ((item != null) && (item.getItemId() == itemId) && (item.getAmount() >= minimum)) {
305                    return true;
306                }
307            }
308            
309            return false;
310        }
311    
312        /**
313         * Checks to see if this getArray() has one slot that has the item id and
314         * equal or more to the amount.
315         * 
316         * @param itemId
317         * @param minimum
318         * @param maximum
319         * @return
320         */
321        public boolean hasItem(int itemId, int minimum, int maximum) {
322            Item[] items = getContents();
323    
324            for (Item item : items) {
325                if ((item != null) && (item.getItemId() == itemId) && (item.getAmount() >= minimum) && (item.getAmount() <= maximum)) {
326                    return true;
327                }
328            }
329            
330            return false;
331        }
332    
333        /**
334         * Returns the contents of this chest
335         * @return contents
336         */
337        public Item[] getContents() {
338            int arraySize = getContentsSize();
339            Item[] rt = new Item[arraySize];
340            
341            for (int i = 0; i < arraySize; i++) {
342                rt[i] = getItemFromSlot(i);
343            }
344    
345            return rt;
346        }
347    
348        /**
349         * Sets the contents
350         * @param contents contents to set
351         */
352        public void setContents(Item[] contents) {
353            int arraySize = getContentsSize();
354    
355            for (int i = 0; i < arraySize; i++) {
356                setSlot(contents[i], i);
357            }
358        }
359        
360        public void clearContents() {
361            int size = getContentsSize();
362    
363            for (int i = 0; size > i; i++) {
364                container.setContentsAt(i, null);
365            }
366        }
367    }