Item's not enchanting via set metadata

Discussion in 'Plugin Development' started by gamer8756, Jul 3, 2014.

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

    gamer8756

    So I am currently working on a plugin to upgrade items through tiers, and update the enchantments at each. Currently, all armor is working, however, the sword only applies the first 4 enchantments, and the pickaxe does not apply any of the enchantments upon upgrade. This is probably a very simple bug, and I apologize in advance if it is. Any help would be greatly appreciated.

    main.java:

    Code:java
    1. package me.gamer8756.autoUpgrade;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5. import java.util.List;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.PlayerInventory;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class main extends JavaPlugin
    18. {
    19.  
    20. public String itemSets[] = {"Dungeon"};
    21. public String itemTypes[] = {"Helmet","Chestplate","Leggings","Boots","Sword","Pickaxe"};
    22. public String numerals[] = {"I","II","III","IV","V","VI","VII","VIII","IX","X"};
    23. public String rarities[] = {"Common","Common","Uncommon","Uncommon","Rare","Rare","Ultra Rare","Ultra Rare","Legendary","Legendary"};
    24.  
    25. /**
    26.   * Enchantment Mapping:
    27.   * ln1-helmet
    28.   * ln2-chest
    29.   * ln3-leggings
    30.   * ln4-boots
    31.   * ln5-sword
    32.   * ln6-pickaxe
    33.   *
    34.   * Enchant order
    35.   * armor [unbreaking,protection,fireprotection,projectileprotection,explosionprotection]
    36.   * swords [unbreaking,sharpness,smite,baneofarthropods,fireaspect,knockback,looting]
    37.   * ttools [unbreaking,efficiency,fortune]
    38.   */
    39.  
    40. public int dungeonEnchantments[][][] = {{{0,3,2,2,2},{1,3,3,3,3},{2,4,3,3,3},{3,4,4,4,4},{4,5,4,4,4},{5,5,5,5,5},{6,6,6,6,6},{7,7,7,7,7},{8,8,8,8,8},{9,9,9,9,9}},
    41. {{0,3,2,2,2},{1,3,3,3,3},{2,4,3,3,3},{3,4,4,4,4},{4,5,4,4,4},{5,5,5,5,5},{6,6,6,6,6},{7,7,7,7,7},{8,8,8,8,8},{9,9,9,9,9}},
    42. {{0,3,2,2,2},{1,3,3,3,3},{2,4,3,3,3},{3,4,4,4,4},{4,5,4,4,4},{5,5,5,5,5},{6,6,6,6,6},{7,7,7,7,7},{8,8,8,8,8},{9,9,9,9,9}},
    43. {{0,3,2,2,2},{1,3,3,3,3},{2,4,3,3,3},{3,4,4,4,4},{4,5,4,4,4},{5,5,5,5,5},{6,6,6,6,6},{7,7,7,7,7},{8,8,8,8,8},{9,9,9,9,9}},
    44. {{3,4,2,2,2,2,1},{4,4,4,4,3,3,2},{5,5,5,5,4,4,3},{6,6,6,6,5,5,4},{7,7,7,7,6,6,5},{8,8,8,8,7,7,6},{9,9,9,9,8,8,7},{10,10,10,10,9,9,8},{10,11,10,10,10,10,10},{11,11,11,11,11,11,11}},
    45. {{10,10,10},{15,15,15},{20,20,20},{25,25,25},{30,30,30},{35,35,35},{40,40,40},{45,45,45},{50,50,50},{55,55,55}}};
    46.  
    47. public String dungeonMagic[] = {ChatColor.WHITE + "" + ChatColor.MAGIC + "" + ChatColor.RESET,
    48. ChatColor.GRAY + "" + ChatColor.MAGIC + "" + ChatColor.RESET,
    49. ChatColor.DARK_GRAY + "" + ChatColor.MAGIC + "" + ChatColor.RESET,
    50. ChatColor.DARK_BLUE + "" + ChatColor.MAGIC + "!" + ChatColor.RESET,
    51. ChatColor.DARK_GREEN + "" + ChatColor.MAGIC + "!" + ChatColor.RESET,
    52. ChatColor.RED + "" + ChatColor.MAGIC + "!!" + ChatColor.RESET,
    53. ChatColor.DARK_RED + "" + ChatColor.MAGIC + "!!" + ChatColor.RESET,
    54. ChatColor.GREEN + "" + ChatColor.MAGIC + "!!!" + ChatColor.RESET,
    55. ChatColor.DARK_GREEN + "" + ChatColor.MAGIC + "!!!" + ChatColor.RESET,
    56. ChatColor.DARK_BLUE + "" + ChatColor.MAGIC + "!!!" + ChatColor.RESET};
    57.  
    58. public ChatColor dungeonColors[] = {ChatColor.WHITE,
    59. ChatColor.GRAY,
    60. ChatColor.DARK_GRAY,
    61. ChatColor.DARK_AQUA,
    62. ChatColor.BLUE,
    63. ChatColor.LIGHT_PURPLE,
    64. ChatColor.DARK_PURPLE,
    65. ChatColor.YELLOW,
    66. ChatColor.GOLD,
    67. ChatColor.AQUA};
    68.  
    69. @Override
    70. public void onEnable()
    71. {
    72. getLogger().info("AutoUpgrade v1.0 has been enabled!");
    73. }
    74.  
    75. @Override
    76. public void onDisable()
    77. {
    78.  
    79. }
    80.  
    81. @Override
    82. public boolean onCommand(CommandSender sender, Command cmd, String label, String args[])
    83. {
    84. if(cmd.getName().equalsIgnoreCase("upgrade"))
    85. {
    86. if(sender instanceof Player)
    87. {
    88. Player player = (Player)sender;
    89.  
    90. if(player.hasPermission("autoupgrade.upgrade"))
    91. {
    92. if(player.getInventory().getItemInHand() != null)
    93. {
    94.  
    95. PlayerInventory inv = player.getInventory();
    96.  
    97. ItemStack heldItem = inv.getItemInHand();
    98. ItemMeta heldItemMeta = heldItem.getItemMeta();
    99. String heldItemName = ChatColor.stripColor(heldItemMeta.getDisplayName());
    100. List<String> heldItemLore = heldItemMeta.getLore();
    101. boolean isValidItem = false;
    102.  
    103. for(String lore : heldItemLore)
    104. {
    105.  
    106. if(lore != null)
    107. {
    108. isValidItem = true;
    109. break;
    110. }
    111. else
    112. {
    113. sender.sendMessage(ChatColor.RED + "This is not a currently valid item to upgrade!");
    114. }
    115. }
    116.  
    117. /**Armor Set ID's:
    118.   *
    119.   * Dungeon - 0
    120.   *
    121.   */
    122.  
    123. int itemSet = -1;
    124.  
    125. if(isValidItem)
    126. {
    127. for(int x = 0; x < itemSets.length; x++)
    128. {
    129. if(heldItemName.contains(itemSets[x]))
    130. {
    131. itemSet = x;
    132. }
    133. }
    134.  
    135.  
    136. int itemRank = -1;
    137. int itemType = -1;
    138.  
    139. if(itemSet != -1)
    140. {
    141.  
    142. for(int y = 0; y < numerals.length; y++)
    143. {
    144.  
    145. for(int z = 0; z < itemTypes.length; z++)
    146. {
    147. if(heldItemName.contains(itemSets[itemSet] + " " + itemTypes[z] + " " + numerals[y]))
    148. {
    149. itemRank = y;
    150. itemType = z;
    151. }
    152. }
    153. }
    154. }
    155.  
    156.  
    157.  
    158. int validCopies = 0;
    159.  
    160. for(int z = 0; z < 35; z++)
    161. {
    162. if(player.getInventory().getItem(z) != null)
    163. {
    164. if(player.getInventory().getItem(z).equals(player.getInventory().getItemInHand()))
    165. {
    166. validCopies++;
    167. }
    168. }
    169. }
    170.  
    171.  
    172. if(validCopies > 3)
    173. {
    174. int output = validCopies / 4;
    175. int refund = validCopies % 4;
    176.  
    177. for(int x = 0; x < 35; x++)
    178. {
    179. if(player.getInventory().getItem(x) != null)
    180. {
    181. if(player.getInventory().getItem(x).equals(heldItem))
    182. {
    183. player.getInventory().clear(x);
    184. }
    185. }
    186. }
    187.  
    188. for(int x = 0; x < refund; x++)
    189. {
    190. player.getInventory().addItem(heldItem);
    191. }
    192.  
    193. ItemStack upgradedItem = heldItem;
    194. ItemMeta upgradedMeta = heldItemMeta;
    195.  
    196. if(itemSet == 0)
    197. {
    198. upgradedMeta.setDisplayName(dungeonMagic[itemRank + 1] + dungeonColors[itemRank + 1] + "Dungeon " + itemTypes[itemType] + " " + numerals[itemRank + 1] + dungeonMagic[itemRank + 1]);
    199.  
    200. List<String> lore = new ArrayList<String>();
    201. lore.add(dungeonColors[itemRank + 1] + rarities[itemRank + 1] + " Dungeon Reward");
    202.  
    203. upgradedMeta.setLore(lore);
    204.  
    205. switch (itemType)
    206. {
    207. case 0:
    208. upgradedMeta.addEnchant(Enchantment.DURABILITY, dungeonEnchantments[0][itemRank + 1][0], false);
    209. upgradedMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, dungeonEnchantments[0][itemRank + 1][1], false);
    210. upgradedMeta.addEnchant(Enchantment.PROTECTION_FIRE, dungeonEnchantments[0][itemRank + 1][2], false);
    211. upgradedMeta.addEnchant(Enchantment.PROTECTION_PROJECTILE, dungeonEnchantments[0][itemRank + 1][3], false);
    212. upgradedMeta.addEnchant(Enchantment.PROTECTION_EXPLOSIONS, dungeonEnchantments[0][itemRank + 1][4], false);
    213. break;
    214. case 1:
    215. upgradedMeta.addEnchant(Enchantment.DURABILITY, dungeonEnchantments[1][itemRank + 1][0], false);
    216. upgradedMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, dungeonEnchantments[1][itemRank + 1][1], false);
    217. upgradedMeta.addEnchant(Enchantment.PROTECTION_FIRE, dungeonEnchantments[1][itemRank + 1][2], false);
    218. upgradedMeta.addEnchant(Enchantment.PROTECTION_PROJECTILE, dungeonEnchantments[1][itemRank + 1][3], false);
    219. upgradedMeta.addEnchant(Enchantment.PROTECTION_EXPLOSIONS, dungeonEnchantments[1][itemRank + 1][4], false);
    220. break;
    221. case 2:
    222. upgradedMeta.addEnchant(Enchantment.DURABILITY, dungeonEnchantments[2][itemRank + 1][0], false);
    223. upgradedMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, dungeonEnchantments[2][itemRank + 1][1], false);
    224. upgradedMeta.addEnchant(Enchantment.PROTECTION_FIRE, dungeonEnchantments[2][itemRank + 1][2], false);
    225. upgradedMeta.addEnchant(Enchantment.PROTECTION_PROJECTILE, dungeonEnchantments[2][itemRank + 1][3], false);
    226. upgradedMeta.addEnchant(Enchantment.PROTECTION_EXPLOSIONS, dungeonEnchantments[2][itemRank + 1][4], false);
    227. break;
    228. case 3:
    229. upgradedMeta.addEnchant(Enchantment.DURABILITY, dungeonEnchantments[3][itemRank + 1][0], false);
    230. upgradedMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, dungeonEnchantments[3][itemRank + 1][1], false);
    231. upgradedMeta.addEnchant(Enchantment.PROTECTION_FIRE, dungeonEnchantments[3][itemRank + 1][2], false);
    232. upgradedMeta.addEnchant(Enchantment.PROTECTION_PROJECTILE, dungeonEnchantments[3][itemRank + 1][3], false);
    233. upgradedMeta.addEnchant(Enchantment.PROTECTION_EXPLOSIONS, dungeonEnchantments[3][itemRank + 1][4], false);
    234. break;
    235. case 4:
    236. upgradedMeta.addEnchant(Enchantment.DURABILITY, dungeonEnchantments[4][itemRank + 1][0], false);
    237. upgradedMeta.addEnchant(Enchantment.DAMAGE_ALL, dungeonEnchantments[4][itemRank + 1][1], false);
    238. upgradedMeta.addEnchant(Enchantment.DAMAGE_UNDEAD, dungeonEnchantments[4][itemRank + 1][2], false);
    239. upgradedMeta.addEnchant(Enchantment.DAMAGE_ARTHROPODS, dungeonEnchantments[4][itemRank + 1][3], false);
    240. upgradedMeta.addEnchant(Enchantment.FIRE_ASPECT, dungeonEnchantments[4][itemRank + 1][4], false);
    241. upgradedMeta.addEnchant(Enchantment.KNOCKBACK, dungeonEnchantments[4][itemRank + 1][5], false);
    242. upgradedMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, dungeonEnchantments[4][itemRank + 1][6], false);
    243. break;
    244. case 5:
    245. upgradedMeta.addEnchant(Enchantment.DURABILITY, dungeonEnchantments[5][itemRank + 1][0], false);
    246. upgradedMeta.addEnchant(Enchantment.DIG_SPEED, dungeonEnchantments[5][itemRank + 1][1], false);
    247. upgradedMeta.addEnchant(Enchantment.LOOT_BONUS_BLOCKS, dungeonEnchantments[5][itemRank + 1][2], false);
    248. break;
    249. }
    250.  
    251.  
    252. upgradedItem.setItemMeta(upgradedMeta);
    253. }
    254. else
    255. {
    256. sender.sendMessage(ChatColor.RED + "You require " + (4-validCopies) + " more of this item to upgrade it!");
    257. }
    258.  
    259. for(int x = 0; x < output; x++)
    260. {
    261. player.getInventory().addItem(upgradedItem);
    262. }
    263. }
    264. }
    265. }
    266. else
    267. {
    268. sender.sendMessage(ChatColor.RED + "You must hold an item to upgrade!");
    269. }
    270. }
    271. }
    272. else
    273. {
    274. sender.sendMessage("This command can only be run by a player!");
    275. }
    276. }
    277.  
    278. return false;
    279. }
    280. }
    281.  
     
  2. Offline

    JaguarJo

    Moved to a more appropriate forum.
     
  3. Offline

    unon1100

    You should add some debug code and see if the enchants are even being added to the item meta in that SwitchCase
     
  4. Offline

    gamer8756

    It says that the switch statement that applies the enchants is running, but it appears that the issue is with the metadata or enchantment add methods

    The plugin adds sharpness, fireaspect,knockback, and baneofarthropods to the sword, but the pick still wont work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    gamer8756

    unon1100 I checked and the debug statements verify that the methods to add the enchantment are indeed running. Do you have any other ideas as to what the issue may be?
     
  6. Offline

    xTigerRebornx

    gamer8756 Link
    Read the desc of that 3rd argument. You are passing in false, so unsafe enchants won't get applied.
     
  7. Offline

    gamer8756

    i knew it was a simple mistake. Thanks for the help.
     
  8. Why not creating a simple itemstack and add the unsafe Enchantments... That is a lot simpler i think
     
Thread Status:
Not open for further replies.

Share This Page