Solved Problem with a PlayerInteractEvent

Discussion in 'Spigot Plugin Development' started by Th3Wh1teG0d, Jan 7, 2020.

Thread Status:
Not open for further replies.
  1. Im trying to make a gun but im getting an error and dont know why.

    Error:

    [14:59:51 ERROR]: Could not pass event PlayerInteractEvent to Airsoft v1.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:235) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:202) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:198) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:991) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:26) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_231]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_231]
    at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.NullPointerException
    at de.th3wh1teg0d.guns.Weapon.onInteract(Weapon.java:34) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_231]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
    ... 18 more


    Code

    Code (Java):


        public void onInteract(PlayerInteractEvent event){
            Player player = (Player)event.getPlayer();          
            if(cd.containsKey(player.getUniqueId()) && cd.get(player.getUniqueId()) > System.currentTimeMillis()) {
                event.setCancelled(true);
               
            } else if(event.getAction() == Action.RIGHT_CLICK_AIR){
                if(player.getItemInHand().getType() == WeaponHandler.Pistole.getMaterial()){
                    if (player.getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§8Airsoft Pistole")) {
                        Snowball snowball = player.getWorld().spawn(player.getEyeLocation(), Snowball.class);
                        snowball.setVelocity(player.getLocation().getDirection().multiply(3));
                        snowball.setShooter(player);
                        player.getWorld().playSound(player.getLocation(), WeaponHandler.Pistole.getSound(), 10, 10);
                        cd.put(player.getUniqueId(), System.currentTimeMillis() + (1 * 500));
                    }
                }
            }
        }
     
  2. The important part is the „18 more“ section, especially where it says „Caused by“.
    I would assume it‘s player.getItemInHand() since if the player has nothing in his hands it’s null, but it could be almost everything

    Edit: reading on a phone can be hard... What is line 34?
     
    #2 Schottky, Jan 7, 2020
    Last edited: Jan 7, 2020
  3. SteelPhoenix

    Moderator

    You're assuming
    - the player is holding an item
    - the item has item meta
    - the item has a display name

    Also, use the ChatColor enum. It exists for a reason...
     
  4. I have edited it now but it still makes a Error

    Error:

    [15:38:09 ERROR]: Could not pass event PlayerInteractEvent to Airsoft v1.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:235) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:202) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:198) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:991) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:26) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_231]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_231]
    at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.NullPointerException
    at de.th3wh1teg0d.guns.Weapon.onInteract(Weapon.java:35) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_231]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
    ... 18 more


    Code (Java):


        public void onInteract(PlayerInteractEvent event){
            Player player = (Player)event.getPlayer();          
            if(cd.containsKey(player.getUniqueId()) && cd.get(player.getUniqueId()) > System.currentTimeMillis()) {
                event.setCancelled(true);
               
            } else if(event.getAction() == Action.RIGHT_CLICK_AIR){
                if(player.getItemInHand().getType() == WeaponHandler.Pistole.getMaterial()){
                    if (player.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.DARK_GRAY + "P99")) {
                        Snowball snowball = player.getWorld().spawn(player.getEyeLocation(), Snowball.class);
                        snowball.setVelocity(player.getLocation().getDirection().multiply(3));
                        snowball.setShooter(player);
                        player.getWorld().playSound(player.getLocation(), WeaponHandler.Pistole.getSound(), 10, 10);
                        cd.put(player.getUniqueId(), System.currentTimeMillis() + (1 * 500));
                    }
                }
            }
        }

    Line 35 is:
     if (player.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.DARK_GRAY + "P99")) {
     
  5. getItemMeta() returns null, everything else is marked as NotNull.

    Either check if the Item can have an ItemMeta (this is not the case for e.g. Air or something like that which is what probably went wrong here) or check if the ItemMeta is null
     
  6. How do i do that ? (Im started learning Java 3 Months ago)
     
  7. Code (Java):
    if (player.getInventory().getItemInMainHand().getItemMeta() != null) {
        // ItemMeta not null
    }
    Other than that I would suggest you refactor your code a bit for readability. That huge statement (player.getA().getB()...) is not readable and you see that it's kind of unpredictable. I would suggest something like this:

    Code (Java):
    final ItemStack itemInHand = player.getItemInHand();
    if (itemInHand.getType() == WeaponHandler.Pistole.getMaterial()) {
        final ItemMeta meta = itemInHand.getItemMeta()
        if (meta != null)
        {
            // here you can continue to work with your meta
            meta.getDisplayName()...
        }
    }
    also, don't do that display name thing, instead use a Map.

    Edit: or at least something that does not rely on something as fragile as the display name
     
  8. I dont know what a Map is or how to use it
     
  9. Then what is this?
     
  10. This is a cooldown that the gun shoot only 2 Snowballs per second
     
  11. SteelPhoenix

    Moderator

    He means you're using a Map, so what do you mean you don't know what a map is.
     
  12. I have found this "Cooldown" in the internet and pasted it in my code so that it is making a cooldown but i dont know what a map is i dont know that he means a "HashMap"
     
  13. Well, you could have somehow deduced by the word "Map" inside "HashMap", that a "HashMap" might at least have something to do with a Map. Anyhow I feel like someone should write a comprehensive tutorial on maps, this topic is broadly misunderstood and abused (especially for people that are fairly new to coding)
     
    • Winner Winner x 1
    • Funny Funny x 1
  14. Yeah... what now? I have the problem anyways :/
     
  15. As I said previously, getItemMeta() returns null, so you first have to check whether it's null or not. If it's null, the problem might be inside the class WeaponHandler, can you, therefore, show us what Pistole is?

    Also this (the part without the map)
     
  16. Code (Java):


    public enum WeaponHandler {
         
       
        Pistole(Material.STONE_HOE,"Pistole",10,Sound.BLOCK_ANVIL_LAND);
       
        Material material;
        String display;
        int damage;
        Sound sound;
       
        WeaponHandler(Material material,String display,int damage,Sound sound){
            this.material = material;
            this.display = display;
            this.damage = damage;
            this.sound = sound;
        }
        public Material getMaterial(){
            return this.material;
        }  
        public String getDisplay(){
            return this.display;
        }
        public int getDamage(){
            return this.damage;
        }  
        public Sound getSound(){
            return this.sound;
        }  
    }
     
  17. What version are you building this on?
     
Thread Status:
Not open for further replies.