001    import java.util.HashMap;
002    import java.util.Map;
003    
004    /**
005     * Item.java - Item stuff.
006     * @author James
007     */
008    public class Item {
009        /**
010         * Type - Used to identify items
011         */
012        public enum Type {
013            Air (0),
014            Stone (1),
015            Grass(2),
016            Dirt(3),
017            Cobblestone(4),
018            Wood(5),
019            Sapling(6),
020            Bedrock(7),
021            Water(8),
022            StationaryWater(9),
023            Lava(10),
024            StationaryLava(11),
025            Sand(12),
026            Gravel(13),
027            GoldOre(14),
028            IronOre(15),
029            CoalOre(16),
030            Log(17),
031            Leaves(18),
032            Sponge(19),
033            Glass(20),
034            Cloth(35),
035            YellowFlower(37),
036            RedRose(38),
037            BrownMushroom(39),
038            RedMushroom(40),
039            GoldBlock(41),
040            IronBlock(42),
041            DoubleStep(43),
042            Step(44),
043            Brick(45),
044            TNT(46),
045            BookShelf(47),
046            MossyCobblestone(48),
047            Obsidian(49),
048            Torch(50),
049            Fire(51),
050            MobSpawner(52),
051            WoodStairs(53),
052            Chest(54),
053            RedstoneWire(55),
054            DiamondOre(56),
055            DiamondBlock(57),
056            Workbench(58),
057            Crops(59),
058            Soil(60),
059            Furnace(61),
060            BurningFurnace(62),
061            SignPost(63),
062            WoodenDoor(64),
063            Ladder(65),
064            Rails(66),
065            CobblestoneStairs(67),
066            WallSign(68),
067            Lever(69),
068            StonePlate(70),
069            IronDoorBlock(71),
070            WoodPlate(72),
071            RedstoneOre(73),
072            GlowingRedstoneOre(74),
073            RedstoneTorchOff(75),
074            RedstoneTorchOn(76),
075            StoneButton(77),
076            Snow(78),
077            Ice(79),
078            SnowBlock(80),
079            Cactus(81),
080            Clay(82),
081            ReedBlock(83),
082            Jukebox(84),
083            Fence(85),
084            Pumpkin(86),
085            Netherstone(87),
086            SlowSand(88),
087            LightStone(89),
088            Portal(90),
089            JackOLantern(91),
090    
091            IronSpade(256),
092            IronPickaxe(257),
093            IronAxe(258),
094            FlintAndSteel(259),
095            Apple(260),
096            Bow(261),
097            Arrow(262),
098            Coal(263),
099            Diamond(264),
100            IronIngot(265),
101            GoldIngot(266),
102            IronSword(267),
103            WoodSword(268),
104            WoodSpade(269),
105            WoodPickaxe(270),
106            WoodAxe(271),
107            StoneSword(272),
108            StoneSpade(273),
109            StonePickaxe(274),
110            StoneAxe(275),
111            DiamondSword(276),
112            DiamondSpade(277),
113            DiamondPickaxe(278),
114            DiamondAxe(279),
115            Stick(280),
116            Bowl(281),
117            MushroomSoup(282),
118            GoldSword(283),
119            GoldSpade(284),
120            GoldPickaxe(285),
121            GoldAxe(286),
122            String(287),
123            Feather(288),
124            Gunpowder(289),
125            WoodHoe(290),
126            StoneHoe(291),
127            IronHoe(292),
128            DiamondHoe(293),
129            GoldHoe(294),
130            Seeds(295),
131            Wheat(296),
132            Bread(297),
133            LeatherHelmet(298),
134            LeatherChestplate(299),
135            LeatherLeggings(300),
136            LeatherBoots(301),
137            ChainmailHelmet(302),
138            ChainmailChestplate(303),
139            ChainmailLeggings(304),
140            ChainmailBoots(305),
141            IronHelmet(306),
142            IronChestplate(307),
143            IronLeggings(308),
144            IronBoots(309),
145            DiamondHelmet(310),
146            DiamondChestplate(311),
147            DiamondLeggings(312),
148            DiamondBoots(313),
149            GoldHelmet(314),
150            GoldChestplate(315),
151            GoldLeggings(316),
152            GoldBoots(317),
153            Flint(318),
154            Pork(319),
155            GrilledPork(320),
156            Painting(321),
157            GoldenApple(322),
158            Sign(323),
159            WoodDoor(324),
160            Bucket(325),
161            WaterBucket(326),
162            LavaBucket(327),
163            Minecart(328),
164            Saddle(329),
165            IronDoor(330),
166            RedStone(331),
167            SnowBall(332),
168            Boat(333),
169            Leather(334),
170            MilkBucket(335),
171            ClayBrick(336),
172            ClayBall(337),
173            Reed(338),
174            Paper(339),
175            Book(340),
176            SlimeBall(341),
177            StorageMinecart(342),
178            PoweredMinecart(343),
179            Egg(344),
180            Compass(345),
181            FishingRod(346),
182            Watch(347),
183            LightstoneDust(348),
184            RawFish(349),
185            CookedFish(350),
186    
187            GoldRecord(2256),
188            GreenRecord(2257)
189            ;
190    
191            private int id;
192            private static Map<Integer, Type> map;
193    
194            private Type(int id){
195                this.id = id;
196                add( id, this );
197            }
198    
199            private static void add( int type, Type name ) {
200                if (map == null) {
201                    map = new HashMap<Integer, Type>();
202                }
203                
204                map.put(type, name);
205            }
206    
207            public int getId() {
208                return id;
209            }
210    
211            public static Type fromId(final int id) {
212                return map.get(id);
213            }
214        }
215    
216        private int itemId = 1, amount = 1, slot = -1, damage = 0;
217    
218        public Type itemType = Type.fromId(itemId);
219        /**
220         * Create an item with an id of 1 and amount of 1
221         */
222        public Item() {
223        }
224    
225        /**
226         * Create a new item.
227         * @param itemType type of item.
228         */
229        public Item(Type itemType) {
230            this(itemType, 1);
231        }
232        
233        public Item(Type itemType, int amount) {
234            this(itemType.getId(), amount);
235        }
236    
237        /**
238         * Creates an item with specified id and amount
239         * @param itemId
240         * @param amount
241         */
242        public Item(int itemId, int amount) {
243            this.itemId = itemId;
244            this.amount = amount;
245            this.itemType= Type.fromId(itemId);
246        }
247    
248        /**
249         * Creates an item with specified id, amount and slot
250         * @param itemId
251         * @param amount
252         * @param slot
253         */
254        public Item(int itemId, int amount, int slot) {
255            this.itemId = itemId;
256            this.amount = amount;
257            this.slot = slot;
258            this.itemType = Type.fromId(itemId);
259        }
260    
261        /**
262         * Creates an item from the actual item class
263         * @param hn
264         */
265        public Item(jl hn) {
266            itemId = hn.c;
267            amount = hn.a;
268            damage = hn.g();
269            this.itemType = Type.fromId(itemId);
270        }
271    
272        /**
273         * Creates an item from the actual item class at the given slot
274         * @param hn
275         * @param slot
276         */
277        public Item(jl hn, int slot) {
278            this(hn);
279            this.slot = slot;
280        }
281    
282        /**
283         * Returns the item id
284         * @return item id
285         */
286        public int getItemId() {
287            return itemId;
288        }
289    
290        /**
291         * Sets item id to specified id
292         * @param itemId
293         */
294        public void setItemId(int itemId) {
295            this.itemId = itemId;
296            this.itemType = Type.fromId(itemId);
297        }
298    
299        /**
300         * Returns the amount
301         * @return amount
302         */
303        public int getAmount() {
304            return amount;
305        }
306    
307        /**
308         * Sets the amount
309         * @param amount
310         */
311        public void setAmount(int amount) {
312            this.amount = amount;
313        }
314    
315        /**
316         * Returns true if specified item id is a valid item id.
317         * @param itemId
318         * @return
319         */
320        public static boolean isValidItem(int itemId) {
321            if (itemId < hg.c.length) {
322                return hg.c[itemId] != null;
323            }
324            return false;
325        }
326    
327        /**
328         * Returns this item's current slot. -1 if no slot is specified
329         * @return slot
330         */
331        public int getSlot() {
332            return slot;
333        }
334    
335        /**
336         * Sets this item's slot
337         * @param slot
338         */
339        public void setSlot(int slot) {
340            this.slot = slot;
341        }
342    
343        /**
344         * Returns this item's current damage. 0 if no damage is specified
345         * @return damage
346         */
347        public int getDamage() {
348            return damage;
349        }
350    
351        /**
352         * Sets this item's damage
353         * @param damage
354         */
355        public void setDamage(int damage) {
356            this.damage = damage;
357        }
358    
359        /**
360         * Returns a String value representing this object
361         * 
362         * @return String representation of this object
363         */
364        @Override
365        public String toString() {
366            return String.format("Item[id=%d, amount=%d, slot=%d]", itemId, amount, slot);
367        }
368    
369        /**
370         * Tests the given object to see if it equals this object
371         * 
372         * @param obj the object to test
373         * @return true if the two objects match
374         */
375        @Override
376        public boolean equals(Object obj) {
377            if (obj == null) {
378                return false;
379            }
380            if (getClass() != obj.getClass()) {
381                return false;
382            }
383            final Item other = (Item) obj;
384            if (this.itemId != other.itemId) {
385                return false;
386            }
387            if (this.amount != other.amount) {
388                return false;
389            }
390            if (this.slot != other.slot) {
391                return false;
392            }
393            return true;
394        }
395    
396        /**
397         * Returns a semi-unique hashcode for this object
398         * 
399         * @return hashcode
400         */
401        @Override
402        public int hashCode() {
403            int hash = 7;
404            hash = 97 * hash + this.itemId;
405            hash = 97 * hash + this.amount;
406            hash = 97 * hash + this.slot;
407            return hash;
408        }
409    
410        /**
411         * Returns this item type
412         * @return the item type
413         */
414        public Type getType(){
415            return this.itemType;
416        }
417    
418        /**
419         * Set the item type
420         * @param itemType the item type
421         */
422        public void setType(Type itemType){
423            this.itemType = itemType;
424            this.itemId = itemType.getId();
425        }
426    }