Errors in Console

Discussion in 'Plugin Help/Development/Requests' started by shotgun528, Apr 9, 2015.

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

    shotgun528

    I develope now a Villager Inventory ...

    If i click on my developed Villager i dont get Errors! but if i click on Villagers that have Custom Names or other normal Villagers i get This Error:

    [13:07:39 ERROR]: Could not pass event PlayerInteractEntityEvent to GreenlandCore v0.4
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:1268) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.PacketPlayInUseEntity.a(SourceFile:52) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.PacketPlayInUseEntity.a(SourceFile:11) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_40]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_40]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot.jar:git-Spigot-8a983f9-e81edfc]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_40]
    Caused by: java.lang.NullPointerException
    at Listener.VillagerClass.onInteract(VillagerClass.java:30) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_40]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_40]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot.jar:git-Spigot-8a983f9-e81edfc]
    ... 14 more

    and The code is:

    Code:
    package Listener;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Villager;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    import Main.Main;
    
    public class VillagerClass implements Listener{
       
    private Main plugin;
       
        public VillagerClass(Main plugin) {
            this.plugin = plugin;
        }
        @EventHandler
        public void onInteract(PlayerInteractEntityEvent e){
           
            Villager v = (Villager) e.getRightClicked();
           
           
           
            if(v.getCustomName().equalsIgnoreCase("§c§lShop assistant")){
               
               
                e.setCancelled(true);           
                Inventory inv = Bukkit.createInventory(null, 9, "Shop");
                inv.setItem(0, new ItemStack(Material.DIAMOND));
                inv.setItem(1, new ItemStack(Material.DIRT, 100));
                e.getPlayer().openInventory(inv);
                       
       
            }
            else{
               
            }
       
    
    }
    }
    
     
  2. @shotgun528 If the villager does not have a custom name, it will return null. (Check if the custom name is null first)
     
    CrystallFTW likes this.
  3. According to what @DJSkepter said, you can use this nice trick, to prevent that.
    If the string (custom name) is null, none of the methods (for example .equalsIgnoreCase) will work. But if you use the method for a valid string ("§c§lShop assistant") it will work :D
    Here is an example:
    Code:java
    1. if ("§c§lShop assistant".equalsIgnoreCase(v.getCustomName()) {
     
  4. Offline

    MexMaster

    Also make sure e.getRightClicked is an instance of Villager and not a Zombie or so.
    But for this code you don't even have to cast it to Villager.

    @shotgun528 @shotgun528
     
Thread Status:
Not open for further replies.

Share This Page