Adding +1 Enchant to a Item

Discussion in 'Plugin Development' started by diamondcodes, Oct 19, 2014.

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

    diamondcodes

    Hello,
    So I am making a enchantment plugin, And I need to add +1 to the current enchant. For example if you have Sharpness 1 on a sword and you type /sharpness it will add +1 to the sharpness of the Sword in your hand, How would I add the +1 enchantment?

    Thanks
     
  2. Offline

    FerusGrim

    Can you show the current code that you have, to maybe give us a better idea at what you're trying to achieve?
     
  3. Offline

    diamondcodes

    FerusGrim Alright So I am giving the players a Pickaxe and I when they click the Item in the GUI I want it to add +1 Efficiency to the Pick (So if the pick(Item in hand) has Efficiency 1 when they click the item in the GUI it will be Efficiency 2)

    Code:java
    1. if (event.getCurrentItem() != null) {
    2. if (event.getCurrentItem().hasItemMeta()) {
    3. if (event.getCurrentItem().getItemMeta().getDisplayName().equals("+1 Efficiency")) {
    4. event.setCancelled(true);
    5. player.getItemInHand().addEnchantment(Enchantment.DIG_SPEED, 1); //Just for now
     
  4. Offline

    mrCookieSlime Retired Staff


    int level = 0;
    if (item.getEnchantments().containsKey(Enchantment.DIG_SPEED)) level = item.getEnchantmentLevel(Enchantment.DIG_SPEED);
    item.addUnsafeEnchantment(Enchantment.DIG_SPEED, level + 1);
     
  5. Offline

    diamondcodes

    mrCookieSlime I tried that and it did not seem to work. Here is what I have,
    Code:java
    1.  
    2. ItemStack Pick = new ItemStack(Material.DIAMOND_PICKAXE);
    3. ItemMeta PickMeta = Pick.getItemMeta();
    4. PickMeta.setDisplayName("§cImmortal§4Factions§8: §7§o(Right Click To Upgrade)");
    5. Pick.setItemMeta(PickMeta);
    6.  
    7.  
    8. @EventHandler
    9. public void invClick(InventoryClickEvent event) {
    10. Player player = (Player) event.getWhoClicked();
    11. int level = 0;
    12.  
    13. if (event.getCurrentItem() != null) {
    14. if (event.getCurrentItem().hasItemMeta()) {
    15. if (event.getCurrentItem().getItemMeta().getDisplayName().equals("+1 Efficiency")) {
    16. event.setCancelled(true);
    17. if (Pick.getEnchantments().containsKey(Enchantment.DIG_SPEED))
    18. level = Pick.getEnchantmentLevel(Enchantment.DIG_SPEED);
    19. Pick.addUnsafeEnchantment(Enchantment.DIG_SPEED, level + 1);
     
  6. Offline

    mrCookieSlime Retired Staff

    diamondcodes
    You forgot a null-check.
    if (event.getCurrentItem().getItemMeta().hasDisplayName()
    Also did you make sure that your Item is called +1 Efficiency without any Colors.
    Otherwise you have to do this:
    Code:java
    1. if (ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()).equals("+1 Efficiency")) {
     
  7. Offline

    diamondcodes

    mrCookieSlime Heres my whole code now :p Still does not seem to work
    Code:java
    1. package me.dc.main;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.enchantments.Enchantment;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.inventory.InventoryClickEvent;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. /**
    20. * Created by Matthew on 2014-10-19.
    21. */
    22. public class Main extends JavaPlugin implements Listener {
    23.  
    24. public static Inventory PickUpgrade = Bukkit.createInventory(null, 18, "§bPick Upgrade");
    25.  
    26. public static ItemStack Pick = new ItemStack(Material.DIAMOND_PICKAXE);
    27. public static ItemMeta PickMeta = Pick.getItemMeta();
    28.  
    29. public static ItemStack Eff = new ItemStack(Material.DIAMOND);
    30. public static ItemMeta EffMeta = Eff.getItemMeta();
    31.  
    32. @Override
    33. public void onEnable() {
    34. getServer().getPluginManager().registerEvents(this, this);
    35. }
    36.  
    37.  
    38. @EventHandler
    39. public void onJoin(PlayerJoinEvent event) {
    40. Player player = event.getPlayer();
    41.  
    42. PickMeta.setDisplayName("§cImmortal§4Factions§8: §7§o(Right Click To Upgrade)");
    43. Pick.setItemMeta(PickMeta);
    44.  
    45. player.getInventory().addItem(Pick);
    46. }
    47.  
    48. @EventHandler
    49. public void onClick(PlayerInteractEvent event) {
    50. Player player = event.getPlayer();
    51. Action action = event.getAction();
    52.  
    53.  
    54. EffMeta.setDisplayName("+1 Efficiency");
    55. Eff.setItemMeta(EffMeta);
    56.  
    57. if (player.getItemInHand() == Pick && action == Action.RIGHT_CLICK_BLOCK || action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
    58.  
    59. PickUpgrade.setItem(0, Eff);
    60. player.openInventory(PickUpgrade);
    61. }
    62. }
    63.  
    64. @EventHandler
    65. public void invClick(InventoryClickEvent event) {
    66. Player player = (Player) event.getWhoClicked();
    67. int level = 0;
    68.  
    69. if (event.getCurrentItem() != null) {
    70. if (event.getCurrentItem().hasItemMeta()) {
    71. if (ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()).equals("+1 Efficiency")) {
    72. event.setCancelled(true);
    73. if (Pick.getEnchantments().containsKey(Enchantment.DIG_SPEED))
    74. level = Pick.getEnchantmentLevel(Enchantment.DIG_SPEED);
    75. Pick.addUnsafeEnchantment(Enchantment.DIG_SPEED, level + 1);
    76. }
    77. }
    78. }
    79. }
    80. }
    81.  
     
  8. Offline

    mrCookieSlime Retired Staff

    diamondcodes
    What exactly is not working?
    Any Errors?
    Also, there are a couple of mistakes.

    Code:java
    1. if (player.getItemInHand() == Pick && action == Action.RIGHT_CLICK_BLOCK || action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {


    should better be
    Code:java
    1. if (player.getItemInHand().isSimiliar(Pick) && (action == Action.RIGHT_CLICK_BLOCK || action == Action.RIGHT_CLICK_AIR)) {


    An Item will never be == another Item. They can only be similiar, never the same.
    And the () are important...
    Just like you learned it in Math, * before - except there are () :D

    Also, you wanna put in this null-check as well to prevent NPEs:
    if (event.getCurrentItem().getItemMeta().hasDisplayName()
     
  9. Offline

    diamondcodes

    mrCookieSlime Thanks, It opens the GUI when I click the pick, But when I click the diamond(+1 Efficiency) It does nothing
    No errors
     
  10. Offline

    mrCookieSlime Retired Staff

    Because you are adding the Enchantment to an ItemStack, not to the Players Pickaxe.

    It is like having 2 apples and eat 1 One. The other one is still not eaten.

    You wanna set the Item in the Players Hand to the Enchanted Pick, but first check if he is actually holding the Pick, otherwise people will be able to abuse it for duplication purposes.

    Also, please remove the public static in front of your Variables...
    It causes memory leaks if you are not nullifying them onDisable. Also since you are accessing them from the same Class, it is unnecessary.
     
Thread Status:
Not open for further replies.

Share This Page