PlayerInteractEntityEvent Help

Discussion in 'Plugin Development' started by TheRedHeadHD, Apr 21, 2014.

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

    TheRedHeadHD

    I'm trying to make it so when you right click a villager with custom name "VIPShop" it opens a shop inventory, however, it's not working correctly.
    Code:
    @EventHandler
        public void onPlayerInteractEntity(PlayerInteractEntityEvent e)
        {
            Player p = e.getPlayer();
       
            if(!(e.getRightClicked() instanceof Villager)) return;
            if(e.getRightClicked() instanceof Villager)
            {
                Villager v = (Villager) e.getRightClicked();
                if(v.getCustomName() == null) return;
                if(v.getCustomName().contains("VIPShop"))
                {
                    e.setCancelled(true);
                    if(p.hasPermission("spvp.vipshop"))
                    {
                        p.openInventory(ShopListener.shop);
                    }
                    else return;
                }
                else return;
            }
            else return;
        }
    I get this NullPointerException:
    Code:
    [20:40:23 ERROR]: Could not pass event PlayerInteractEntityEvent to VIPShop v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:471) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:1068) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInUseEntity.a(SourceFile:55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInUseEntity.handle(SourceFile:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [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:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_7_R1.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:185) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at me.theredheadhd.VIPShop.Events.PlayerInteractEntity.onPlayerInteractEntity(PlayerInteractEntity.java:28) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_45]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 13 more
    Some help would be appreciated!
     
  2. Offline

    beeselmane

    ShopListener.shop is null.. you probably didn't create the inventory.. I would create a method to return the custom inventory to open, and have the player open that
     
  3. Offline

    adam753

    ShopListener.shop is null.
     
  4. Offline

    TheRedHeadHD

    adam753
    How would I fix it? And how were you able to tell?
    ^ Sorry if I sound like a "noob" --> I'm just trying to learn as I go.
     
  5. Offline

    Are52Tap

    Keep sure the Inventory is not Null, by setting it up first, by something like so.

    Code:java
    1. private Inventory shop = Bukkit.createInventory(null, 54, "Shop"); //Make the owner null because it is a shop
     
  6. Offline

    TheRedHeadHD

    Are52Tap
    I have that in my ShopListener Class:
    Code:java
    1. public static Inventory shop;
    2.  
    3. public ShopListener()
    4. {
    5. shop = Bukkit.createInventory(null, 36, ChatColor.BLUE + "" + ChatColor.BOLD + "VIPShop");


    The rest of that class is just setting the contents of the shop.
     
  7. Offline

    beeselmane

    The problem is that you're calling it as a static method before you've created any objects to fill the field
     
  8. Offline

    Maurdekye

    TheRedHeadHD I'm pretty sure it's because when you right click on the air, the event calls with a null entity. Put some print statements in to see.
     
  9. Offline

    TheRedHeadHD

    Maurdekye
    I wasn't sure what you meant to be honest so I checked if the entity was null but the same error showed.
    Here is the code now:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteractEntity(PlayerInteractEntityEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. if(e.getRightClicked() == null) return;
    6. if(!(e.getRightClicked() instanceof Villager)) return;
    7. if(e.getRightClicked() instanceof Villager)
    8. {
    9. Villager v = (Villager) e.getRightClicked();
    10. if(v.getCustomName() == null) return;
    11. if(v.getCustomName().contains("VIPShop"))
    12. {
    13. e.setCancelled(true);
    14. if(p.hasPermission("spvp.vipshop"))
    15. {
    16. p.openInventory(ShopListener.shop);
    17. }
    18. else return;
    19. }
    20. else return;
    21. }
    22. else return;
    23. }
     
  10. Offline

    Are52Tap

    No because It is an "PlayerInteractEntityEvent" So the only way to call it would be by hitting a entity, which of course did.

    Is the code ever called?

    Ever by something?
    If so find a way to activate it onEnable() or somewhere else where it is called upon;
     
  11. Offline

    TheRedHeadHD

    Are52Tap
    Thank you. I had initialized and created the Shop in the ShopListener class without ever linking the class to the Main class.
     
Thread Status:
Not open for further replies.

Share This Page