Solved Get error when item isn't equipped

Discussion in 'Plugin Development' started by plisov, Feb 4, 2017.

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

    plisov

    So I'm trying to run a particle effect when a player has an item on. When they have it on it works great and when they take the item off it turns the particles off which is fine. However I get a null pointer exception error in the console when the item is not equipped. Here is the code.
    Code:
    package me.plisov.shards.particles;
    
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    
    import com.massivecraft.massivecore.particleeffect.ParticleEffect;
    
    import me.plisov.shards.Enable;
    import net.md_5.bungee.api.ChatColor;
    
    public class Boots implements Listener {
    
        Enable plugin;
        public Boots(Enable plugin) {
            this.plugin = plugin;
        }
    
        @EventHandler
        public void playerInventory(PlayerMoveEvent event) {
    
            Player player = (Player) event.getPlayer();
    
            if(player.getInventory().getBoots().getItemMeta().getDisplayName().contains(ChatColor.RED + "" + ChatColor.BOLD + "Artifact Boots")) {
    
                Location location1 = player.getEyeLocation();
                Location location2 = player.getEyeLocation();
                Location location3 = player.getEyeLocation();
                int particles = 50;
                float radius = 0.7f;
                for (int i = 0; i < particles; i++) {
                    double angle, x, z;
                    angle = 2 * Math.PI * i / particles;
                    x = Math.cos(angle) * radius;
                    z = Math.sin(angle) * radius;
                    location1.add(x, 0, z);
                    location2.add(x, -0.66, z);
                    location3.add(x, -1.33, z);
                    //ParticleEffect.FLAME.display(location1, 0, 0, 0, 0, 1);
                    //ParticleEffect.FLAME.display(location2, 0, 0, 0, 0, 1);
                    ParticleEffect.FLAME.display(location3, 0, 0, 0, 0, 1);
                    location1.subtract(x, 0, z);
                    location2.subtract(x, -0.66, z);
                    location3.subtract(x, -1.33, z);           
                }
            } else {
                return;
            }
        }
    }
    And here is the error
    Code:
    [01:30:58 ERROR]: Could not pass event PlayerMoveEvent to Shards v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:615) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at net.minecraft.server.v1_11_R1.PacketPlayInFlying.a(SourceFile:126) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at net.minecraft.server.v1_11_R1.PacketPlayInFlying$PacketPlayInPosition.a(SourceFile:57) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
            at me.plisov.shards.particles.Boots.playerInventory(Boots.java:26) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.11.22.jar:git-Spigot-6de2fbc-80dd971]
            ... 15 more
    Line 26 is
    Code:
            if(player.getInventory().getBoots().getItemMeta().getDisplayName().contains(ChatColor.RED + "" + ChatColor.BOLD + "Artifact Boots")) {
     
  2. Offline

    timtower Administrator Administrator Moderator

    @plisov First check if there are boots, then if the boots have item meta, then if they have a display name.
     
  3. Offline

    Drkmaster83

    How do you know:
    1. They have boots on at all
    2. Their boots have item meta
    3. Their boots have a display name
     
  4. Offline

    plisov

Thread Status:
Not open for further replies.

Share This Page