Solved Gettingif item has enchantment

Discussion in 'Plugin Development' started by Kassestral, Jun 9, 2014.

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

    Kassestral

    It doesn't seem to pass if the item I am holding has the enchantment, help?
    I tested with a pickaxe with fortune 20, and one with fortune 50

    Code:java
    1. package com.kassestral.plugins.prisonserver.listeners;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.Material;
    5. import org.bukkit.enchantments.Enchantment;
    6. //import org.bukkit.block.Block;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.BlockBreakEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.Plugin;
    13.  
    14. public class Stone implements Listener{
    15.  
    16. @SuppressWarnings("unused")
    17. private Plugin plugin;
    18. public Stone(Plugin plugin) {
    19. this.plugin = plugin;
    20. }
    21.  
    22. @EventHandler
    23. public void stoneBreak(BlockBreakEvent e){
    24. Material type = e.getBlock().getType();
    25. //Block block = e.getBlock();
    26. Location location = e.getBlock().getLocation();
    27. //@SuppressWarnings("unused")
    28. //byte blockid = e.getBlock().getData();
    29. Player p = e.getPlayer();
    30. boolean enchanted = p.getItemInHand().getEnchantments().containsKey("Enchantment.LOOT_BONUS_BLOCKS");
    31. int enchant = p.getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
    32.  
    33. if(type.equals(Material.STONE) || type.equals(Material.SMOOTH_BRICK)){
    34. if(type.equals(Material.STONE)){
    35. if(enchanted){
    36. p.getWorld().dropItemNaturally(location, new ItemStack(Material.COBBLESTONE, calculateDrops(enchant)));
    37. }
    38. }
    39. p.sendMessage(type.toString());
    40. }
    41. // Debug Message | p.sendMessage("Event Passed.");
    42. }
    43.  
    44.  
    45. public int calculateDrops(int enchant){
    46. int amount = 0;
    47.  
    48. if(enchant == 10){
    49. amount = +2;
    50. }
    51. if(enchant == 15){
    52. amount = +3;
    53. }
    54. if(enchant == 20){
    55. amount = +4;
    56. }
    57. if(enchant == 30){
    58. amount = +5;
    59. }
    60. if(enchant == 40){
    61. amount = +6;
    62. }
    63. if(enchant == 50){
    64. amount = +8;
    65. }
    66. if(enchant == 60){
    67. amount = +10;
    68. }
    69. if(enchant == 90){
    70. amount = +14;
    71. }
    72. else{
    73. amount = +0;
    74. }
    75. return amount;
    76. }
    77. }
    78.  


    Garris0n timtower

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

    Garris0n

    So, what enchantment level does it say? The indices should start at 0, so you'll have to fix that, but is it working at all?
     
  3. Offline

    xTigerRebornx

    Kassestral Try using ItemStack#containsEnchantment() to check for your enchant
    Why your code doesn't work:
    You are checking for a Key in the map that is a String, yet the Map has is a Map<Enchantment, Integer), so it can't have a key of a String
     
  4. Offline

    spy_1134

    You could try something like this:

    Code:java
    1. boolean enchanted = false;
    2. int enchant = 0;
    3. for(Entry<Enchantment, Integer> entry : p.getItemInHand().getEnchantments().entrySet()) {
    4. if(entry.getKey().equals(Enchantment.LOOT_BONUS_BLOCKS)) {
    5. enchanted = true;
    6. enchant = entry.getValue();
    7. }
    8. }
     
  5. Offline

    Kassestral

    spy_1134 xTigerRebornx
    Cheers guys this worked
     
Thread Status:
Not open for further replies.

Share This Page