[Lib] CustomItem - Create custom items and simply manage them

Discussion in 'Resources' started by Wombosvideo, Nov 23, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Wombosvideo

    CustomItem Library
    Hello Guys and Girs.
    I've coded a library for simply creating custom items.
    It's very easy to use and I really recommend you to use this Lib.
    http://customitem.jimdo.de/

    This is the "CustomItem.java":
    Code:java
    1. /*
    2. * Author: Luca Bosin / Wombosvideo
    3. * License: Creative Commons -> More info [url]http://customitem.jimdo.de/[/url]
    4. */
    5. package YOUR_PACKAGE_NAME;
    6.  
    7. //Needed for the Lores
    8. import java.util.List;
    9.  
    10. //Needed for every Item
    11. import org.bukkit.Material;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. //Needed to drop Items
    15. import org.bukkit.Location;
    16. //Needed for Player actions
    17. import org.bukkit.entity.Player;
    18. //Needet for Inventory actions
    19. import org.bukkit.inventory.Inventory;
    20. //Needed for Recipes
    21. import org.bukkit.inventory.ShapedRecipe;
    22. import org.bukkit.inventory.ShapelessRecipe;
    23. import org.bukkit.inventory.FurnaceRecipe;
    24. //Needed to add Recipes
    25. import org.bukkit.Bukkit;
    26.  
    27. public class CustomItem extends ItemStack {
    28.  
    29. public ShapelessRecipe shapelessRecipe;
    30. public ShapedRecipe shapedRecipe;
    31. public FurnaceRecipe furnaceRecipe;
    32.  
    33. //Item with same specifications like choosen itemstack
    34. public CustomItem(ItemStack itemStack){
    35. this.setType(itemStack.getType());
    36. this.setAmount(itemStack.getAmount());
    37. this.setDurability(itemStack.getDurability());
    38. this.setItemMeta(itemStack.getItemMeta());
    39. }
    40.  
    41. //Item with Material, DamageId, Amount, Display Name and Lores List
    42. public CustomItem(Material material, Short damageid, Integer amount, String displayName, List<String> lores){
    43. this.setType(material);
    44. this.setAmount(amount);
    45. this.setDurability((short) damageid);
    46. ItemMeta im = this.getItemMeta();
    47. im.setDisplayName(displayName);
    48. im.setLore(lores);
    49. this.setItemMeta(im);
    50. shapelessRecipe = new ShapelessRecipe(this);
    51. shapedRecipe = new ShapedRecipe(this);
    52. }
    53.  
    54. //Item with Material and DamageId
    55. public CustomItem(Material material, Short damageid){
    56. this.setType(material);
    57. this.setDurability((short) damageid);
    58. this.setAmount(1);
    59. shapelessRecipe = new ShapelessRecipe(this);
    60. shapedRecipe = new ShapedRecipe(this);
    61. }
    62.  
    63. //Item with Material and Amount
    64. public CustomItem(Material material, Integer amount){
    65. this.setType(material);
    66. this.setAmount(amount);
    67. shapelessRecipe = new ShapelessRecipe(this);
    68. shapedRecipe = new ShapedRecipe(this);
    69. }
    70.  
    71. //Item with Material and Display Name
    72. public CustomItem(Material material, String displayName){
    73. this.setType(material);
    74. this.setAmount(1);
    75. ItemMeta im = this.getItemMeta();
    76. im.setDisplayName(displayName);
    77. this.setItemMeta(im);
    78. shapelessRecipe = new ShapelessRecipe(this);
    79. shapedRecipe = new ShapedRecipe(this);
    80. }
    81.  
    82. //Item with Material and Lores List
    83. public CustomItem(Material material, List<String> lores){
    84. this.setType(material);
    85. this.setAmount(1);
    86. ItemMeta im = this.getItemMeta();
    87. im.setLore(lores);
    88. this.setItemMeta(im);
    89. shapelessRecipe = new ShapelessRecipe(this);
    90. shapedRecipe = new ShapedRecipe(this);
    91. }
    92.  
    93. //Item with Material, DamageId and Amount
    94. public CustomItem(Material material, Short damageid, Integer amount){
    95. this.setType(material);
    96. this.setAmount(amount);
    97. this.setDurability((short) damageid);
    98. shapelessRecipe = new ShapelessRecipe(this);
    99. shapedRecipe = new ShapedRecipe(this);
    100. }
    101.  
    102. //Item with Material, DamageId, Amount and Display Name
    103. public CustomItem(Material material, Short damageid, Integer amount, String displayName){
    104. this.setType(material);
    105. this.setAmount(amount);
    106. this.setDurability((short) damageid);
    107. ItemMeta im = this.getItemMeta();
    108. im.setDisplayName(displayName);
    109. this.setItemMeta(im);
    110. shapelessRecipe = new ShapelessRecipe(this);
    111. shapedRecipe = new ShapedRecipe(this);
    112. }
    113.  
    114. //Item with Material, DamageId, Amount, Display Name and Lores List
    115. public CustomItem(Material material, Short damageid, Integer amount, List<String> lores){
    116. this.setType(material);
    117. this.setAmount(amount);
    118. this.setDurability((short) damageid);
    119. ItemMeta im = this.getItemMeta();
    120. im.setLore(lores);
    121. this.setItemMeta(im);
    122. shapelessRecipe = new ShapelessRecipe(this);
    123. shapedRecipe = new ShapedRecipe(this);
    124. }
    125.  
    126. //Item with Material, DamageId and Display Name
    127. public CustomItem(Material material, Short damageid, String displayName){
    128. this.setType(material);
    129. this.setAmount(1);
    130. this.setDurability((short) damageid);
    131. ItemMeta im = this.getItemMeta();
    132. im.setDisplayName(displayName);
    133. this.setItemMeta(im);
    134. shapelessRecipe = new ShapelessRecipe(this);
    135. shapedRecipe = new ShapedRecipe(this);
    136. }
    137.  
    138. //Item with Material, DamageId, Display Name and Lores List
    139. public CustomItem(Material material, Short damageid, String displayName, List<String> lores){
    140. this.setType(material);
    141. this.setAmount(1);
    142. this.setDurability((short) damageid);
    143. ItemMeta im = this.getItemMeta();
    144. im.setDisplayName(displayName);
    145. im.setLore(lores);
    146. this.setItemMeta(im);
    147. shapelessRecipe = new ShapelessRecipe(this);
    148. shapedRecipe = new ShapedRecipe(this);
    149. }
    150.  
    151. //Item with Material, DamageId and Lores List
    152. public CustomItem(Material material, Short damageid, List<String> lores){
    153. this.setType(material);
    154. this.setAmount(1);
    155. this.setDurability((short) damageid);
    156. ItemMeta im = this.getItemMeta();
    157. im.setLore(lores);
    158. this.setItemMeta(im);
    159. shapelessRecipe = new ShapelessRecipe(this);
    160. shapedRecipe = new ShapedRecipe(this);
    161. }
    162.  
    163. //Item with Material, Amount and Display Name
    164. public CustomItem(Material material, Integer amount, String displayName){
    165. this.setType(material);
    166. this.setAmount(amount);
    167. ItemMeta im = this.getItemMeta();
    168. im.setDisplayName(displayName);
    169. this.setItemMeta(im);
    170. shapelessRecipe = new ShapelessRecipe(this);
    171. shapedRecipe = new ShapedRecipe(this);
    172. }
    173.  
    174. //Item with Material, DamageId, Amount, Display Name and Lores List
    175. public CustomItem(Material material, Integer amount, String displayName, List<String> lores){
    176. this.setType(material);
    177. this.setAmount(amount);
    178. ItemMeta im = this.getItemMeta();
    179. im.setDisplayName(displayName);
    180. im.setLore(lores);
    181. this.setItemMeta(im);
    182. shapelessRecipe = new ShapelessRecipe(this);
    183. shapedRecipe = new ShapedRecipe(this);
    184. }
    185.  
    186. //Item with Material, Amount and Lores List
    187. public CustomItem(Material material, Integer amount, List<String> lores){
    188. this.setType(material);
    189. this.setAmount(amount);
    190. ItemMeta im = this.getItemMeta();
    191. im.setLore(lores);
    192. this.setItemMeta(im);
    193. shapelessRecipe = new ShapelessRecipe(this);
    194. shapedRecipe = new ShapedRecipe(this);
    195. }
    196.  
    197. //Item with Material, Display Name and Lores List
    198. public CustomItem(Material material, String displayName, List<String> lores){
    199. this.setType(material);
    200. this.setAmount(1);
    201. ItemMeta im = this.getItemMeta();
    202. im.setDisplayName(displayName);
    203. im.setLore(lores);
    204. this.setItemMeta(im);
    205. shapelessRecipe = new ShapelessRecipe(this);
    206. shapedRecipe = new ShapedRecipe(this);
    207. }
    208.  
    209. public CustomItem getCustomItem(ItemStack itemStack){
    210. this.setType(itemStack.getType());
    211. this.setAmount(itemStack.getAmount());
    212. this.setDurability(itemStack.getDurability());
    213. this.setItemMeta(itemStack.getItemMeta());
    214. return this;
    215. }
    216.  
    217. public boolean isSameLike(CustomItem customItem){
    218. if(!(customItem.getType().equals(this.getType()))){
    219. return false;
    220. }
    221. if(this.getItemMeta().hasDisplayName()){
    222. if(!(customItem.getItemMeta().hasDisplayName())){
    223. return false;
    224. }else{
    225. if(!(this.getItemMeta().getDisplayName().equals(customItem.getItemMeta().getDisplayName()))){
    226. return false;
    227. }
    228. }
    229. }
    230. if(this.getItemMeta().hasLore()){
    231. if(!(customItem.getItemMeta().hasLore())){
    232. return false;
    233. }
    234. }
    235. if(!(this.getItemMeta().getEnchants().equals(customItem.getItemMeta().getEnchants()))){
    236. return false;
    237. }
    238. if(!(this.getDurability() == customItem.getDurability())){
    239. return false;
    240. }
    241. return true;
    242. }
    243.  
    244. public boolean isSameItemStackLike(ItemStack itemStack){
    245. if(!(itemStack.getType().equals(this.getType()))){
    246. return false;
    247. }
    248. if(this.getItemMeta().hasDisplayName()){
    249. if(!(itemStack.getItemMeta().hasDisplayName())){
    250. return false;
    251. }else{
    252. if(!(this.getItemMeta().getDisplayName().equals(itemStack.getItemMeta().getDisplayName()))){
    253. return false;
    254. }
    255. }
    256. }
    257. if(this.getItemMeta().hasLore()){
    258. if(!(itemStack.getItemMeta().hasLore())){
    259. return false;
    260. }
    261. }
    262. if(!(this.getItemMeta().getEnchants().equals(itemStack.getItemMeta().getEnchants()))){
    263. return false;
    264. }
    265. if(!(this.getDurability() == itemStack.getDurability())){
    266. return false;
    267. }
    268. return true;
    269. }
    270.  
    271.  
    272.  
    273. //Add the Item to a Player's Inventory
    274. public void add(Player player){
    275. player.getInventory().addItem(this);
    276. }
    277.  
    278. //Add the Item to an Inventory
    279. public void add(Inventory inventory){
    280. inventory.addItem(this);
    281. }
    282.  
    283. //Set the Item in a Player's Inventory on a specific slot
    284. public void set(Player player, Integer slot){
    285. player.getInventory().setItem(slot, this);
    286. }
    287.  
    288. //Set the Item in an Inventory on a specific slot
    289. public void set(Inventory inventory, Integer slot){
    290. inventory.setItem(slot, this);
    291. }
    292.  
    293. //Sets the Item as Helmet on a Player
    294. public void setHelmet(Player p){
    295. p.getInventory().setHelmet(this);
    296. }
    297.  
    298. //Sets the Item as Chestplate on a Player
    299. public void setChestplate(Player p){
    300. p.getInventory().setChestplate(this);
    301. }
    302.  
    303. //Sets the Item as Leggings on a Player
    304. public void setLeggings(Player p){
    305. p.getInventory().setLeggings(this);
    306. }
    307.  
    308. //Sets the Item as Boots on a Player
    309. public void setBoots(Player p){
    310. p.getInventory().setBoots(this);
    311. }
    312.  
    313. //Sets the Item as PlayerInHandItem
    314. public void setInHand(Player p){
    315. p.getInventory().setItemInHand(this);
    316. }
    317.  
    318. //Drops the Item on the specified Location
    319. public void dropItem(Location loc){
    320. loc.getWorld().dropItem(loc, this);
    321. }
    322.  
    323. //Drops the Item naturally on the specified Location
    324. public void dropItemNaturally(Location loc){
    325. loc.getWorld().dropItemNaturally(loc, this);
    326. }
    327.  
    328. //Registers the Item as a ShapelessRecipe
    329. public void registerAsShapelessRecipe(){
    330. Bukkit.addRecipe(shapelessRecipe);
    331. }
    332.  
    333. //Registers the Item as a ShapedRecipe
    334. public void registerAsShapedRecipe(){
    335. Bukkit.addRecipe(shapedRecipe);
    336. }
    337.  
    338. //Registers the Item as a FurnaceRecipe
    339. public void registerAsFurnaceRecipe(Material from){
    340. furnaceRecipe = new FurnaceRecipe(this, from);
    341. Bukkit.addRecipe(furnaceRecipe);
    342. }
    343.  
    344. }
    345.  


    And this is how to use it:
    Code:java
    1. CustomItem ci = new CustomItem(Material, DamageId, Amount, "Display Name", Lores);
    2.  
    3. ci.add(player);

    Just play arround with it!
     
    luigieai and Toni like this.
  2. Offline

    Toni

    Thank you for this awesome snippet/lib. It definitely saved me a lot more lines in my plugin. Was fast and easy, and has many awesome methods for different types of items!
     
  3. Offline

    Desle

    Wombosvideo
    The class could be waaaaaay simpler, by letting them insert null for certain things, like the name, so you dont need a billion methods
     
    Toni and CaLxCyMru like this.
  4. Offline

    Wombosvideo

    Thanks Toni and to Desle's Reply:
    It would be, but I like the way it is
     
Thread Status:
Not open for further replies.

Share This Page