Solved Could not pass PlayerInteractEvent

Discussion in 'Plugin Development' started by moo3oo3oo3, Sep 22, 2014.

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

    moo3oo3oo3

    Anyone know why it keeps giving me the error when I left/right click a regular stick? It seems to only do it to a regular stick with no meta.

    Code:java
    1. HashMap<String, Long> countdown = new HashMap<String, Long>();
    2. HashMap<String, Long> countdown1 = new HashMap<String, Long>();
    3.  
    4. @EventHandler
    5. public void Hammer(PlayerInteractEvent e) {
    6.  
    7. final Player player = e.getPlayer();
    8. final Location blockLocation = player.getTargetBlock(null, 27).getLocation();
    9.  
    10. if (player.hasPermission("use.hammer")) {
    11. if (player.getItemInHand().getType() == Material.STICK && player.getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§5Magic Wand") && e.getAction() == e.getAction().RIGHT_CLICK_AIR || player.getItemInHand().getType() == Material.STICK && player.getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§5Magic Wand") && e.getAction() == e.getAction().RIGHT_CLICK_BLOCK) {
    12. if (player.getInventory().contains(Material.IRON_INGOT) && player.getInventory().contains(Material.FLINT)) {
    13.  
    14.  
    15.  
    16. //Checks if you pass the cool down
    17. if(System.currentTimeMillis() - (countdown1.containsKey(player.getName()) ? countdown1.get(player.getName()) : 0) >= 5000) {
    18. player.getInventory().removeItem(new ItemStack(Material.IRON_INGOT, 1));
    19. player.getInventory().removeItem(new ItemStack(Material.FLINT, 1));
    20. player.updateInventory();
    21. final org.bukkit.inventory.ItemStack ThorHammer = new ItemStack(Material.STONE_AXE);
    22. ItemMeta meta = ThorHammer.getItemMeta();
    23. meta.setDisplayName("§6Thor's §8Hammer");
    24. ThorHammer.setItemMeta(meta);
    25. player.setItemInHand(ThorHammer);
    26. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 85, 10));
    27. player.getWorld().strikeLightning(blockLocation);
    28.  
    29. //delay
    30. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    31. public void run() {
    32.  
    33. org.bukkit.inventory.ItemStack MagicWand = new ItemStack(Material.STICK);
    34. ItemMeta meta1 = MagicWand.getItemMeta();
    35. List<String> lores = new ArrayList<String>();
    36. lores.add("§7§oMagical Abilities");
    37. meta1.setDisplayName("§5Magic Wand");
    38. meta1.setLore(lores);
    39. MagicWand.setItemMeta(meta1);
    40. player.getInventory().remove(ThorHammer);
    41. player.getInventory().addItem(MagicWand);
    42. }
    43. }, 10L);
    44.  
    45. //puts your in the cool down
    46. countdown1.put(player.getName(), System.currentTimeMillis());
    47. } else {player.sendMessage("§4Please wait for cooldown");}
    48. } else player.sendMessage("§7You Need More Ammo (Iron Ingot and Flint)");
    49. }
    50. } else player.sendMessage("§4You Do Not Have Permission");
    51. }
    52.  
    53. @EventHandler
    54. public void FireWand(PlayerInteractEvent e) {
    55.  
    56. final Player player = e.getPlayer();
    57. Location location = player.getEyeLocation();
    58. Vector velocity = location.getDirection();
    59.  
    60. if (player.hasPermission("use.firewand")) {
    61. if (player.getItemInHand().getType() == Material.STICK && player.getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§5Magic Wand") && e.getAction() == e.getAction().LEFT_CLICK_BLOCK || player.getItemInHand().getType() == Material.STICK && player.getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§5Magic Wand") && e.getAction() == e.getAction().LEFT_CLICK_AIR) {
    62. if (player.getInventory().contains(Material.FIREBALL)) {
    63.  
    64. //Checks if you pass the cool down
    65. if(System.currentTimeMillis() - (countdown.containsKey(player.getName()) ? countdown.get(player.getName()) : 0) >= 5000) {
    66. player.getInventory().removeItem(new ItemStack(Material.FIREBALL, 1));
    67. player.updateInventory();
    68. final org.bukkit.inventory.ItemStack FireWand = new ItemStack(Material.REDSTONE_TORCH_ON);
    69. ItemMeta meta = FireWand.getItemMeta();
    70. meta.setDisplayName("§4Fire Wand");
    71. FireWand.setItemMeta(meta);
    72. player.setItemInHand(FireWand);
    73. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 70, 3));
    74. player.launchProjectile(SmallFireball.class, velocity);
    75.  
    76. //delay
    77. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    78. public void run() {
    79.  
    80. org.bukkit.inventory.ItemStack MagicWand = new ItemStack(Material.STICK);
    81. ItemMeta meta1 = MagicWand.getItemMeta();
    82. List<String> lores = new ArrayList<String>();
    83. lores.add("§7§oMagical Abilities");
    84. meta1.setDisplayName("§5Magic Wand");
    85. meta1.setLore(lores);
    86. MagicWand.setItemMeta(meta1);
    87. player.getInventory().remove(FireWand);
    88. player.getInventory().addItem(MagicWand);
    89. }
    90. }, 18L);
    91.  
    92. //puts your in the cool down
    93. countdown.put(player.getName(), System.currentTimeMillis());
    94. } else player.sendMessage("§4Please wait for cooldown");
    95. } else player.sendMessage("§7You Need More Ammo (Fire Charge)");
    96. }
    97. } else player.sendMessage("§4You Do Not Have Permission");
    98. }
     
  2. Offline

    Unica

    moo3oo3oo3

    Obv.
    What if the stick has no displayname? Check for null

    Code:java
    1. if(p.getItemInHand != null && p.getItemInHand.getType != Material.AIR){
    2. ItemStack hand = p.getItemInHand();
    3. if(hand.hasItemMeta() && hand.getItemMeta().hasDisplayName()){
    4. if(hand.getItemMeta().getDisplayName().equals("YourName")){
    5. //Do something ;)
    6. }
    7. }
    8. }
     
Thread Status:
Not open for further replies.

Share This Page