Exploding snowballs

Discussion in 'Plugin Development' started by Sladd_Cool, Apr 12, 2014.

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

    Sladd_Cool

    Hello
    I am quite new to plugin development, I'm currently coding a grenade plugin for my server but I get some errors in the console (tell me if you need me to post the error). I have tried adding some else statements but didn't help (maybe did something wrong).
    Can you see if you find any errors in this code:
    Code:java
    1. package me.sladdcool.maestro;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.World;
    10. import org.bukkit.entity.HumanEntity;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.ProjectileHitEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class XplodeArrows extends JavaPlugin implements Listener {
    18. private static ArrayList<String> allowed = new ArrayList<String>(Arrays.asList("CraftEgg", "CraftArrow", "CraftSnowball"));
    19.  
    20. @Override
    21. public void onEnable() {
    22. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25. @EventHandler
    26. public void onHit(ProjectileHitEvent e) {
    27. if(allowed.contains(e.getEntity().getClass().getSimpleName()) && e.getEntity().getShooter() instanceof Player) {
    28. if(((Player) e.getEntity().getShooter()).hasPermission("projectile.xplode")) {
    29. if(((Player) e.getEntity().getShooter()).getItemInHand().getType() == Material.SNOW_BALL) {
    30. if(((Player) e.getEntity().getShooter()).getItemInHand().getItemMeta().getDisplayName().contains("Grenade")) {
    31. World world = e.getEntity().getWorld();
    32. Location loc = e.getEntity().getLocation();
    33.  
    34. int x = loc.getBlockX();
    35. int y = loc.getBlockY();
    36. int z = loc.getBlockZ();
    37.  
    38. world.createExplosion(x, y, z, 2f, true, true);
    39. }
    40. }
    41. }
    42. }
    43. }
    44. }
     
  2. What's the errors? And does it work?
     
  3. Offline

    Sladd_Cool

    Yes the plugin works and here's what errors the console gives (only when i throw normal snowballs):
    Code:
    [18:00:05 ERROR]: Could not pass event ProjectileHitEvent to XplodeArrows v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:320) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:471) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callProjectile
    HitEvent(CraftEventFactory.java:609) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b302
    0jnks]
            at net.minecraft.server.v1_7_R1.EntityProjectile.h(EntityProjectile.java
    :158) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.World.entityJoinedWorld(World.java:1348)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.World.playerJoinedWorld(World.java:1329)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.World.tickEntities(World.java:1217) [cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.WorldServer.tickEntities(WorldServer.jav
    a:480) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    37) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
            at me.sladdcool.maestro.XplodeArrows.onHit(XplodeArrows.java:31) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:318) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            ... 14 more
    >
    driver-e

    When I have your attention:) Do you know what I should be using instead of getShooter nowadays?
    Eclipse says it's deprecated.

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

    PluginStudios

    Sladd_Cool if I am not mistaken, getShooter is the only way to do that, there is no alternative. Just suppress deprecation warnings.
     
  5. Offline

    Sladd_Cool

  6. Sladd_Cool Is your problem solved? If it is not, try using:

    Code:java
    1. try {
    2.  
    3. // code
    4.  
    5. }catch (NullPointerException ex) {
    6. // not needed to put anything here
    7. }
     
  7. Do some checks to make sure stuff isn't null. The error you have is a null pointer exception.
     
  8. Offline

    Sladd_Cool

    Sorry for being so nooby Connor2weirdness but how do i check if stuff is null?
     
  9. Offline

    N00BHUN73R

  10. Offline

    Sladd_Cool

    So... I tried doing this
    Code:
    if(e.getEntity() == null);
        System.out.println(ChatColor.DARK_RED + "ERROR");
    Changing e.getEntity to other stuff (I don't know if I did right), and when i throw a snowball named Grenade it displays the ERROR message I made and I didn't find any solution, the problem is still that if I throw Grenades it works fine and if I throw snowballs the error occurs:(

    Forgot to tag you so I do it here (easier than editing the post:)).
    Connor2weirdness
    @NOOBHUN73R

    @N00BHUN73R
    Hmm I can't tag you...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  11. N00BHUN73R misunderstood what you needed. He gave you the code to check is a value IS null. You need to use this instead which checks if stuff isn't null, because the exclamation mark means 'not':
    if(value != null){
     
Thread Status:
Not open for further replies.

Share This Page