Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 104 lines 2.4 kB view raw
1package org.bukkit.material; 2 3import org.bukkit.Material; 4import org.bukkit.inventory.ItemStack; 5 6/** 7 * Handles specific metadata for certain items or blocks 8 */ 9public class MaterialData { 10 private final int type; 11 private byte data = 0; 12 13 public MaterialData(final int type) { 14 this(type, (byte) 0); 15 } 16 17 public MaterialData(final Material type) { 18 this(type, (byte) 0); 19 } 20 21 public MaterialData(final int type, final byte data) { 22 this.type = type; 23 this.data = data; 24 } 25 26 public MaterialData(final Material type, final byte data) { 27 this(type.getId(), data); 28 } 29 30 /** 31 * Gets the raw data in this material 32 * 33 * @return Raw data 34 */ 35 public byte getData() { 36 return data; 37 } 38 39 /** 40 * Sets the raw data of this material 41 * 42 * @param data New raw data 43 */ 44 public void setData(byte data) { 45 this.data = data; 46 } 47 48 /** 49 * Gets the Material that this MaterialData represents 50 * 51 * @return Material represented by this MaterialData 52 */ 53 public Material getItemType() { 54 return Material.getMaterial(type); 55 } 56 57 /** 58 * Gets the Material Id that this MaterialData represents 59 * 60 * @return Material Id represented by this MaterialData 61 */ 62 public int getItemTypeId() { 63 return type; 64 } 65 66 /** 67 * Creates a new ItemStack based on this MaterialData 68 * 69 * @return New ItemStack containing a copy of this MaterialData 70 */ 71 public ItemStack toItemStack() { 72 return new ItemStack(type, 0, data); 73 } 74 75 /** 76 * Creates a new ItemStack based on this MaterialData 77 * 78 * @return New ItemStack containing a copy of this MaterialData 79 */ 80 public ItemStack toItemStack(int amount) { 81 return new ItemStack(type, amount, data); 82 } 83 84 @Override 85 public String toString() { 86 return getItemType() + "(" + getData() + ")"; 87 } 88 89 @Override 90 public int hashCode() { 91 return ((getItemTypeId() << 8) ^ getData()); 92 } 93 94 @Override 95 public boolean equals(Object obj) { 96 if (obj != null && obj instanceof MaterialData) { 97 MaterialData md = (MaterialData) obj; 98 99 return (md.getItemTypeId() == getItemTypeId() && md.getData() == getData()); 100 } else { 101 return false; 102 } 103 } 104}