Solved My plugin gives error on console but it works

Discussion in 'Plugin Help/Development/Requests' started by Krumb069, Apr 21, 2015.

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

    Krumb069

    I made a plugin that add bows , it is working normally but when I shoot an arrow with normal bow it gives this error on console(but it shoots arrow and everything is normal):
    Using spigot 1.8
    I tried craftbukkit 1.8 too but it still gives same error
    Code:
    Could not pass event EntityShootBowEvent to YayArti v2.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:297) ~[craftbukkit.jar:git-Bukkit-33d5de3]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-33d5de3]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-33d5de3]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-33d5de3]
        at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callEntityShootBowEvent(CraftEventFactory.java:227) [craftbukkit.jar:git-Bukkit-33d5de3]
        at net.minecraft.server.v1_8_R1.ItemBow.a(ItemBow.java:61) [craftbukkit.jar:git-Bukkit-33d5de3]
        at net.minecraft.server.v1_8_R1.ItemStack.b(ItemStack.java:472) [craftbukkit.jar:git-Bukkit-33d5de3]
        at net.minecraft.server.v1_8_R1.EntityHuman.bT(EntityHuman.java:108) [craftbukkit.jar:git-Bukkit-33d5de3]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:555) [craftbukkit.jar:git-Bukkit-33d5de3]
        at net.minecraft.server.v1_8_R1.PacketPlayInBlockDig.a(SourceFile:40) [craftbukkit.jar:git-Bukkit-33d5de3]
        at net.minecraft.server.v1_8_R1.PacketPlayInBlockDig.a(SourceFile:10) [craftbukkit.jar:git-Bukkit-33d5de3]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-33d5de3]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_79]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:656) [craftbukkit.jar:git-Bukkit-33d5de3]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit.jar:git-Bukkit-33d5de3]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:609) [craftbukkit.jar:git-Bukkit-33d5de3]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:517) [craftbukkit.jar:git-Bukkit-33d5de3]
        at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
        at com.yay.Main.okatilinca(Main.java:78) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_79]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:295) ~[craftbukkit.jar:git-Bukkit-33d5de3]
        ... 18 more
    Plugin's code:
    Code:
    package com.yay;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    public class Main extends JavaPlugin implements Listener{
         public static final Logger log = Logger.getLogger("Minecraft");
            public void onEnable() {
                Bukkit.getServer().getPluginManager().registerEvents(this, this);
                log.log(Level.INFO, "[Yay+]Yay Plugini Calisiyor!");
            }
            public void onDisable() {
                log.log(Level.INFO, "[Yay+]Yay Plugini Calisiyor!");         
            }
         
         public boolean onCommand(CommandSender gonderen, Command komut, String label, String[] args) {
             if (komut.getName().equalsIgnoreCase("yay")){
                 Player oyuncu = (Player) gonderen;
                 if (oyuncu.hasPermission("yay.al")) {
                   if(args.length == 0){
                      oyuncu.sendMessage(ChatColor.RED + "Yay Listesi:");
                      oyuncu.sendMessage(ChatColor.GREEN + "tntyay, isinlayiciyay");
    
                   }
                 }
                 if(args.length == 1){
                     if(args[0].equalsIgnoreCase("tntyay")) {
                 ItemStack tntyay = new ItemStack(Material.BOW);
                 ItemMeta tntyayMeta = tntyay.getItemMeta();
                 tntyayMeta.setDisplayName(ChatColor.RED+ "TNTYay");
                 tntyay.setItemMeta(tntyayMeta);
                 Inventory oyuncuEnvanter = oyuncu.getInventory();
                 oyuncuEnvanter.addItem(tntyay);
                 oyuncu.sendMessage(ChatColor.RED + "TNT Yay alındı.");
             }
                 }
              
                  if(args.length == 1){
                     if(args[0].equalsIgnoreCase("isinlayiciyay")) {
                 ItemStack iy = new ItemStack(Material.BOW);
                 ItemMeta iyMeta = iy.getItemMeta();
                 iyMeta.setDisplayName(ChatColor.RED+ "Işınlayıcı Yay");
                 iy.setItemMeta(iyMeta);
                 Inventory oyuncuEnvanter = oyuncu.getInventory();
                 oyuncuEnvanter.addItem(iy);
                 oyuncu.sendMessage(ChatColor.RED + "Işınlayıcı Yay alındı.");
             }
                  }
      
                    }
             return false;
         }
      @EventHandler
      public void okatilinca(EntityShootBowEvent e){
        if ((e.getEntity() instanceof Player)){
            Player o = (Player) e.getEntity();
            ItemStack yay = o.getItemInHand();
            ItemMeta yayMeta = yay.getItemMeta();
             if (yayMeta.getDisplayName().equalsIgnoreCase(ChatColor.RED + "Işınlayıcı Yay")){
                    (e.getProjectile()).setPassenger(o);
                }
             if (yayMeta.getDisplayName().equalsIgnoreCase(ChatColor.RED + "TNTYay")){
                 if(o.getGameMode() == GameMode.CREATIVE){
                         Entity ent = o.getWorld().spawnEntity(o.getLocation(), EntityType.PRIMED_TNT);
                         ent.setVelocity(e.getProjectile().getVelocity());
                         e.setProjectile(ent);
                     }
                 if (o.getInventory().contains(Material.TNT)){                  
                         o.getInventory().removeItem(new ItemStack(Material.TNT));
                         Entity ent = o.getWorld().spawnEntity(o.getLocation(), EntityType.PRIMED_TNT);
                         ent.setVelocity(e.getProjectile().getVelocity());
                         e.setProjectile(ent);
                     }
                }
        }
      }
    }
    
    Note: It doesnt give error on console when I use a special bow( like tntbow)
     
  2. Offline

    FisheyLP

    Not every item has ItemMeta. You need to use something like this:
    Code:
    if(item.hasItemMeta() && item.getItemMeta().hasDisplayName()) {
    String displayName = item.getItemMeta().getDisplayName();
    }
     
  3. Offline

    Krumb069

    can you give an example code ? or fixed plugin ?
     
  4. Offline

    eyamaz

    He just gave you an example. Don't ask to be spoon fed with the code itself. :/
     
    FisheyLP likes this.
  5. Offline

    Krumb069

    I am new to plugin devolopment and I tried but I couldn't fix error
     
  6. Offline

    bwfcwalshy Retired Staff

    @Krumb069 Please post your updated code, he gave you more than enough code there.
     
  7. Offline

    Krumb069

    This is the code I have last tried
    Code:
     @EventHandler
         public void okatilinca(EntityShootBowEvent e){
                if ((e.getEntity() instanceof Player)){
                    Player o = (Player) e.getEntity();
                    ItemStack yay = o.getItemInHand();
                    ItemMeta yayMeta = yay.getItemMeta();
                  if(yay.hasItemMeta() && yay.getItemMeta().hasDisplayName()){
                      if (yayMeta.getDisplayName().equalsIgnoreCase("tntyay")){
                          String displayName = yay.getItemMeta().getDisplayName();
                        Entity ent = o.getWorld().spawnEntity(o.getLocation(), EntityType.PRIMED_TNT);
                         ent.setVelocity(e.getProjectile().getVelocity());
                         e.setProjectile(ent);
                        }
    }
         }
         }
    }
    
    because this didnt work
    Code:
    @EventHandler
         public void okatilinca(EntityShootBowEvent e){
                if ((e.getEntity() instanceof Player)){
                    Player o = (Player) e.getEntity();
                    ItemStack yay = o.getItemInHand();
                    ItemMeta yayMeta = yay.getItemMeta();
                  if(yay.hasItemMeta() && yay.getItemMeta().hasDisplayName("tntyay")){
                          String displayName = yay.getItemMeta().getDisplayName();
                        Entity ent = o.getWorld().spawnEntity(o.getLocation(), EntityType.PRIMED_TNT);
                         ent.setVelocity(e.getProjectile().getVelocity());
                         e.setProjectile(ent);
    I didnt post all of the code just eventhandler
     
  8. Offline

    bwfcwalshy Retired Staff

    See an issue here?
     
    Krumb069 likes this.
  9. Offline

    FisheyLP

    You tried to access ItemMeta before you checked if the Item has ItemMeta
    EDIT: @bwfcwalshy you were just a sec faster .. :p
     
    Krumb069 likes this.
  10. Offline

    Krumb069

    Still same error
    Code:
     @EventHandler
         public void okatilinca(EntityShootBowEvent e){
                if ((e.getEntity() instanceof Player)){
                    Player o = (Player) e.getEntity();
                   if(o.getItemInHand().hasItemMeta() && o.getItemInHand().getItemMeta().hasDisplayName()){
                   String displayName = o.getItemInHand().getItemMeta().getDisplayName();
                    ItemStack yay = o.getItemInHand();
                    ItemMeta yayMeta = yay.getItemMeta();
                      if (yayMeta.getDisplayName().equalsIgnoreCase("tntyay")){
                        Entity ent = o.getWorld().spawnEntity(o.getLocation(), EntityType.PRIMED_TNT);
                         ent.setVelocity(e.getProjectile().getVelocity());
                         e.setProjectile(ent);
                        }
    }
         }
         }
     
  11. Offline

    FisheyLP

    I think e.setProjectile can't be any entity. It needs to be: Arrow, Egg, EnderPearl, Fireball, Fish, FishHook, LargeFireball, SmallFireball, Snowball, ThrownExpBottle, ThrownPotion or WitherSkull
    but you can try using
    Code:
    e.getProjectile().setPassenger(ent);
     
    Krumb069 likes this.
  12. Offline

    Krumb069

    Solved
    Code:
     @EventHandler
         public void okatilinca(EntityShootBowEvent e){
                if ((e.getEntity() instanceof Player)){
                    Player o = (Player) e.getEntity();
                   if(o.getInventory().getItemInHand().hasItemMeta()) {
                    ItemStack yay = o.getItemInHand();
                    ItemMeta yayMeta = yay.getItemMeta();
                      if (yayMeta.getDisplayName().equalsIgnoreCase("tntyay")){
                        Entity ent = o.getWorld().spawnEntity(o.getLocation(), EntityType.PRIMED_TNT);
                         ent.setVelocity(e.getProjectile().getVelocity());
                         e.setProjectile(ent);
                        }
    }
         }
         }
     
Thread Status:
Not open for further replies.

Share This Page