Plugin Problem

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

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

    99storm2

    Hey guys, the following code enchants the sword in your hand when you right click a sign.
    My problem is that 10-30% of the time it doesn't enchant the item.
    Also if you guys have any suggestions on how to improve my code and made it less messy that would be great but this was the only option i could think of and it works so what ever. Its a private plugin so the mess is really only my problem.

    Code:java
    1. @EventHandler
    2. public void onPlayerClick(PlayerInteractEvent e) {
    3. Player player = e.getPlayer();
    4. Block b = e.getClickedBlock();
    5. Random r = new Random();
    6. int swordench = r.nextInt(8) +1;
    7. int swordench2 = r.nextInt(8) +1;
    8. int swordench3 = r.nextInt(8) +1;
    9. int enchlev = r.nextInt(30) +1;
    10. int enchlev2 = r.nextInt(30) +1;
    11. int enchlev3 = r.nextInt(30) +1;
    12. Enchantment sword = null;
    13. Enchantment sword2 = null;
    14. Enchantment sword3 = null;
    15. if(swordench == 1) {
    16. sword = Enchantment.DAMAGE_ALL;
    17. }else if(swordench == 2) {
    18. sword = Enchantment.DAMAGE_ARTHROPODS;
    19. }else if(swordench == 3) {
    20. sword = Enchantment.DAMAGE_UNDEAD;
    21. }else if(swordench == 4) {
    22. sword = Enchantment.FIRE_ASPECT;
    23. }else if(swordench == 5) {
    24. sword = Enchantment.DURABILITY;
    25. }else if(swordench == 6) {
    26. sword = Enchantment.KNOCKBACK;
    27. }else if(swordench == 7) {
    28. sword = Enchantment.LOOT_BONUS_MOBS;
    29. }else if(swordench == 8) {
    30. sword3 = Enchantment.DAMAGE_ALL;
    31. }
    32.  
    33. if(swordench2 == 1) {
    34. sword2 = Enchantment.DAMAGE_ALL;
    35. }else if(swordench2 == 2) {
    36. sword2 = Enchantment.DAMAGE_ARTHROPODS;
    37. }else if(swordench2 == 3) {
    38. sword2 = Enchantment.DAMAGE_UNDEAD;
    39. }else if(swordench2 == 4) {
    40. sword2 = Enchantment.FIRE_ASPECT;
    41. }else if(swordench2 == 5) {
    42. sword2 = Enchantment.DURABILITY;
    43. }else if(swordench2 == 6) {
    44. sword2 = Enchantment.KNOCKBACK;
    45. }else if(swordench2 == 7) {
    46. sword2 = Enchantment.LOOT_BONUS_MOBS;
    47. }else if(swordench2 == 8) {
    48. sword3 = Enchantment.DAMAGE_ALL;
    49. }
    50.  
    51. if(swordench3 == 1) {
    52. sword3 = Enchantment.DAMAGE_ALL;
    53. }else if(swordench3 == 2) {
    54. sword3 = Enchantment.DAMAGE_ARTHROPODS;
    55. }else if(swordench3 == 3) {
    56. sword3 = Enchantment.DAMAGE_UNDEAD;
    57. }else if(swordench3 == 4) {
    58. sword3 = Enchantment.FIRE_ASPECT;
    59. }else if(swordench3 == 5) {
    60. sword3 = Enchantment.DURABILITY;
    61. }else if(swordench3 == 6) {
    62. sword3 = Enchantment.KNOCKBACK;
    63. }else if(swordench3 == 7) {
    64. sword3 = Enchantment.LOOT_BONUS_MOBS;
    65. }else if(swordench3 == 8) {
    66. sword3 = Enchantment.DAMAGE_ALL;
    67. }
    68.  
    69. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    70.  
    71. if(e.getClickedBlock().getType() == Material.WALL_SIGN) {
    72.  
    73. Sign sign = (Sign) b.getState();
    74.  
    75. if(sign.getLine(0).equalsIgnoreCase("[DazeEnchant]") && sign.getLine(1).equalsIgnoreCase("Sword")){
    76. Material inhand = player.getInventory().getItemInHand().getType();
    77.  
    78. if(inhand.equals(Material.DIAMOND_SWORD) || inhand.equals(Material.WOOD_SWORD) || inhand.equals(Material.IRON_SWORD) || inhand.equals(Material.STONE_SWORD) || inhand.equals(Material.GOLD_SWORD)){
    79.  
    80. if(player.getInventory().getItemInHand().getEnchantments().size() == 0) {
    81.  
    82. ItemMeta im = player.getItemInHand().getItemMeta();
    83. im.addEnchant(sword2, enchlev, true);
    84. im.addEnchant(sword, enchlev2, true);
    85. im.addEnchant(sword3, enchlev3, true);
    86. player.getItemInHand().setItemMeta(im);
    87.  
    88. }
    89. }
    90. }
    91. }
    92. }
    93. }
     
  2. Offline

    99storm2

    Still looking for help not quite sure why its doing this
     
Thread Status:
Not open for further replies.

Share This Page