Event Exception?

Discussion in 'Plugin Development' started by BillyBobJoe168, Apr 6, 2014.

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

    BillyBobJoe168

    Hi guys. I made a plugin that shoots fireballs when you right click with a blaze rod. It works perfectly fine in game but not for the console. When I right click a block without a blaze rod, it throws an event exception on line 31. My code is:
    Code:java
    1. package ericwang.fireball;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.player.PlayerInteractEvent;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class PlayerListener implements Listener {
    12.  
    13. public PlayerListener(Fireball plugin) {
    14. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    15. }
    16.  
    17.  
    18.  
    19. @EventHandler
    20. public void onInteraction(PlayerInteractEvent event){
    21. ItemStack itemstack = event.getItem();
    22. Player player = event.getPlayer();
    23. if (event.getAction() == Action.RIGHT_CLICK_AIR){
    24. if(itemstack.getType().equals(Material.BLAZE_ROD)){
    25.  
    26. player.getLocation().getDirection().normalize().multiply(1);
    27. player.launchProjectile(org.bukkit.entity.Fireball.class);
    28. }
    29. }
    30. if (event.getAction() == Action.RIGHT_CLICK_BLOCK){
    31. if(itemstack.getType().equals(Material.BLAZE_ROD)){
    32.  
    33. player.getLocation().getDirection().normalize().multiply(1);
    34. player.launchProjectile(org.bukkit.entity.Fireball.class);
    35.  
    36. }
    37. }
    38. }
    39.  
    40.  
    41.  
    42.  
    43. }
    44.  


    Also, is there a way to make the fireballs travel faster?

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

    SainttX

    Code:java
    1. ItemStack itemstack = event.getItem();

    This line returns null if there's nothing in the players hand, so when you call itemstack.getType() it results in an NPE.
     
  3. Offline

    BillyBobJoe168

    SainttX Ok thanks do you know how to fix it? Also, it doesn't throw an NPE, its an Event Exception and it never happens when I right click air without an item. It throws the Event Exception at line 31 not 21.
     
  4. Offline

    SainttX

    BillyBobJoe168
    Code:java
    1. @EventHandler
    2. public void onInteraction(PlayerInteractEvent event){
    3. ItemStack itemstack = event.getItem();
    4. Player player = event.getPlayer();
    5.  
    6. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK && itemstack != null) {
    7. if (itemstack.getType().equals(Material.BLAZE_ROD)) {
    8. player.getLocation().getDirection().normalize().multiply(1);
    9. player.launchProjectile(org.bukkit.entity.Fireball.class);
    10. }
    11. }
    12. }

    I shortened your code a bit by including an OR in the first if statement, and I verified that itemstack isn't null to prevent the NPE.
     
    BillyBobJoe168 likes this.
  5. Offline

    BillyBobJoe168

    SainttX Thanks a bunch! I was going to shorten with the or just never got to itXD
     
  6. Offline

    SainttX

    BillyBobJoe168 No problem :). I think you could change the speed by setting the velocity of the fireball.
     
  7. Offline

    BillyBobJoe168

    SainttX Yeah I know but do you know what to do? I know you have to do something like fireball.setVelocity(getVelocity().multiply(2)); or something but that doesn't actually work if you put it into eclipse. I'll try searching it up.
     
  8. Offline

    nuclearmissile

    Just as an extra note, if you don't want to worry about checking for null pointers or just can't find where it's happening, assuming the NPE isn't breaking your plugin's functionality, you can just stick all the affected code inside a try{<Code Here>}catch(NullPointerException e){}
     
  9. Offline

    BillyBobJoe168

    nuclearmissile ok thanks new to java I think I mentioned it in a previous thread but I'm assuming try catch just try the code and if it throws a NPE then it just won't say it in the console right?
     
  10. Offline

    nuclearmissile

    Precisely. Sometimes I have NPE's that occur despite not interfering with the plugin's functionality, so I just put them in a try catch.
     
  11. Offline

    BillyBobJoe168

    ok cool thanks:D
     
Thread Status:
Not open for further replies.

Share This Page