001 package org.bukkit.material; 002 003 import org.bukkit.inventory.ItemStack; 004 import org.bukkit.Material; 005 006 /** 007 * Handles specific metadata for certain items or blocks 008 */ 009 public class MaterialData implements Cloneable { 010 private final int type; 011 private byte data = 0; 012 013 /** 014 * 015 * @deprecated Magic value 016 */ 017 @Deprecated 018 public MaterialData(final int type) { 019 this(type, (byte) 0); 020 } 021 022 public MaterialData(final Material type) { 023 this(type, (byte) 0); 024 } 025 026 /** 027 * 028 * @deprecated Magic value 029 */ 030 @Deprecated 031 public MaterialData(final int type, final byte data) { 032 this.type = type; 033 this.data = data; 034 } 035 036 /** 037 * 038 * @deprecated Magic value 039 */ 040 @Deprecated 041 public MaterialData(final Material type, final byte data) { 042 this(type.getId(), data); 043 } 044 045 /** 046 * Gets the raw data in this material 047 * 048 * @return Raw data 049 * @deprecated Magic value 050 */ 051 @Deprecated 052 public byte getData() { 053 return data; 054 } 055 056 /** 057 * Sets the raw data of this material 058 * 059 * @param data New raw data 060 * @deprecated Magic value 061 */ 062 @Deprecated 063 public void setData(byte data) { 064 this.data = data; 065 } 066 067 /** 068 * Gets the Material that this MaterialData represents 069 * 070 * @return Material represented by this MaterialData 071 */ 072 public Material getItemType() { 073 return Material.getMaterial(type); 074 } 075 076 /** 077 * Gets the Material Id that this MaterialData represents 078 * 079 * @return Material Id represented by this MaterialData 080 * @deprecated Magic value 081 */ 082 @Deprecated 083 public int getItemTypeId() { 084 return type; 085 } 086 087 /** 088 * Creates a new ItemStack based on this MaterialData 089 * 090 * @return New ItemStack containing a copy of this MaterialData 091 */ 092 public ItemStack toItemStack() { 093 return new ItemStack(type, 0, data); 094 } 095 096 /** 097 * Creates a new ItemStack based on this MaterialData 098 * 099 * @param amount The stack size of the new stack 100 * @return New ItemStack containing a copy of this MaterialData 101 */ 102 public ItemStack toItemStack(int amount) { 103 return new ItemStack(type, amount, data); 104 } 105 106 @Override 107 public String toString() { 108 return getItemType() + "(" + getData() + ")"; 109 } 110 111 @Override 112 public int hashCode() { 113 return ((getItemTypeId() << 8) ^ getData()); 114 } 115 116 @Override 117 public boolean equals(Object obj) { 118 if (obj != null && obj instanceof MaterialData) { 119 MaterialData md = (MaterialData) obj; 120 121 return (md.getItemTypeId() == getItemTypeId() && md.getData() == getData()); 122 } else { 123 return false; 124 } 125 } 126 127 @Override 128 public MaterialData clone() { 129 try { 130 return (MaterialData) super.clone(); 131 } catch (CloneNotSupportedException e) { 132 throw new Error(e); 133 } 134 } 135 }