001package fr.aumgn.bukkitutils.itemtype; 002 003import org.apache.commons.lang.builder.HashCodeBuilder; 004import org.bukkit.Material; 005import org.bukkit.block.Block; 006import org.bukkit.inventory.ItemStack; 007 008/** 009 * Represents an item type. 010 */ 011public class ItemType { 012 013 private final Material material; 014 private final short data; 015 016 public ItemType(Material material) { 017 this(material, 0); 018 } 019 020 public ItemType(Material material, int data) { 021 this(material, (short) data); 022 } 023 024 public ItemType(Material material, short data) { 025 if (material == null) { 026 throw new IllegalArgumentException("Material can't be null."); 027 } 028 029 this.material = material; 030 this.data = data; 031 } 032 033 public ItemType(ItemStack stack) { 034 this(stack.getType(), stack.getData().getData()); 035 } 036 037 public ItemType(Block block) { 038 this(block.getType(), block.getData()); 039 } 040 041 public Material getMaterial() { 042 return material; 043 } 044 045 public ItemType setMaterial(Material material) { 046 return new ItemType(material, data); 047 } 048 049 public short getData() { 050 return data; 051 } 052 053 public ItemType setData(byte data) { 054 return new ItemType(material, data); 055 } 056 057 public int getMaxStackSize() { 058 return material.getMaxStackSize(); 059 } 060 061 public int getMaxDurability() { 062 return material.getMaxDurability(); 063 } 064 065 public ItemStack toMaxItemStack() { 066 return toItemStack(getMaxStackSize()); 067 } 068 069 public ItemStack toItemStack() { 070 return toItemStack(1); 071 } 072 073 public ItemStack toItemStack(int amount) { 074 return new ItemStack(material, amount, data); 075 } 076 077 @Override 078 public int hashCode() { 079 return new HashCodeBuilder(31, 13) 080 .append(data) 081 .append(material) 082 .toHashCode(); 083 } 084 085 @Override 086 public boolean equals(Object obj) { 087 if (this == obj) { 088 return true; 089 } 090 091 if (!(obj instanceof ItemType)) { 092 return false; 093 } 094 095 ItemType other = (ItemType) obj; 096 if (data != other.data || material != other.material) { 097 return false; 098 } 099 100 return true; 101 } 102 103 public String toString() { 104 return "(" + material + ":" + data + ")"; 105 } 106}