Solved InventoryClickEvent problem

Discussion in 'Plugin Development' started by tkuiyeager1, Sep 29, 2015.

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

    tkuiyeager1

    Ok so i have my plugin Fake Alerts but when player clicks on the outside box of the inventory then this error comes.
    Error:
    Code:
    [14:56:49 ERROR]: Could not pass event InventoryClickEvent to FakeAlerts v2.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :1603) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31)
    [spigot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9)
    [spigot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_60]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_60]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spig
    ot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    18) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    67) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    57) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :560) [spigot.jar:git-Spigot-f928e7a-f27339c]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    Caused by: java.lang.NullPointerException
            at com.TKUIYEAGER1.FakeAlerts.Main.clickinventory(Main.java:67) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _60]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _60]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_60]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_60]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
            ... 15 more
    Code:
    Code:
    @EventHandler
        public void clickinventory(InventoryClickEvent e) {
            Player t = Bukkit.getPlayer(e.getInventory().getName().split(":")[0]);
            Player p = (Player) e.getWhoClicked();
            if (!(e.getInventory().getType().equals(InventoryType.CHEST) && e
                    .getInventory().getName()
                    .equalsIgnoreCase(t.getName() + ": Menu.")))
                return;
            if (e.getInventory().getType().equals(InventoryType.CHEST)
                    && e.getInventory().getName()
                            .equalsIgnoreCase(t.getName() + ": Menu.")) {
                if (!e.getCurrentItem().hasItemMeta())
                    return;
                if (e.getCurrentItem().hasItemMeta()) {
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakekill")) {
                        p.chat("/fakekill " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakeop")) {
                        p.chat("/fakeop " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakeblind")) {
                        p.chat("/fakeblind " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakejoin")) {
                        p.chat("/fakejoin " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakeban")) {
                        p.chat("/fakeban " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakejump")) {
                        p.chat("/fakejump " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakemove")) {
                        p.chat("/fakemove " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakeleave")) {
                        p.chat("/fakeleave " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakekick")) {
                        p.chat("/fakekick " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakemute")) {
                        p.chat("/fakemute " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Fakegamemode")) {
                        p.chat("/fakegamemode " + t.getName());
                        p.closeInventory();
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.GOLD + "Credits:")) {
                        e.setCancelled(true);
                        return;
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName()
                            .equals(ChatColor.RED + "None")) {
                        e.setCancelled(true);
                        return;
                    }
                }
            }
        }
    Anyone knows how to fix this problem?
    Edit:
    Edited the version number of my plugin in the console

    Edit:
    @timtower im saying your name because i think you can help me with this error

    Ok So I Fixed The Stuff You Said, The Code Line Is A Part Of If Statement So Here Is All The If Statement:
    Code:
    if (!e.getCurrentItem().hasItemMeta()) return;
    Yes, there is only one inventory with ':'
    Inventory will not open if the player isnt online.
    Removed the second if.
    changed the package name to: me.TKUIYEAGER1.FakeAlerts

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 26, 2017
  2. Offline

    mine-care

    So you are sure that all inventories contain a ':' in their name. Thats not the case.

    http://bukkit.org/threads/how-to-re...ubleshoot-your-own-plugins-by-yourself.32457/

    So line 67 in your Main class apears to be null somewhere. What is this line?
    Could it be:
    That returns null when the player isnt online :/



    A litle logic error here, you dont need the second if :/

    And here:
    This is not logic.

    Follow Java Naming Convention

    Also i see a massive ammount of if's in the event that arent nessesary there. If you are to execute a command that is the same as the item name, how about:
    Code:
    p.chat(ChatColor.stripColor(e.getCurrentItem().getItemMeta().getDisplayName()));
    instead. That overrides a lot of your if's, if you dont want this way still, for the sake of efficiency turn the if's to if - else if - else statements because the displayname of an item cant be 2 things at the same time (not Schrödinger's cat).
     
  3. Offline

    RoboticPlayer

    Still not following Java Naming Conventions. Package names should be all lowercase.

    Useless cast to player; e.getWhoClicked() returns a HumanEntity
    Player p = (Player) e.getWhoClicked();
     
  4. @tkuiyeager1
    If a player clicks a blank slot e.getCurrentItem() returns null, make a null check
     
  5. Offline

    tkuiyeager1

    Thanks @megamichiel it solved my problem, but when player clicks on other types of inventories (anvil, furnace, enchantment table and more) then i get other error.
     
  6. @tkuiyeager1
    Would u mind showing the error and the line on which it happens?
     
  7. Offline

    tkuiyeager1

    Sure,
    Code:
    Code:
    if (!(e.getInventory().getName().equalsIgnoreCase(t.getName() + ": FakeAlerts Menu."))) return;
    I think the error is the t (target player) is equals to null but i dont know how to do this check on other inventories.
     
  8. Offline

    RoboticPlayer

    if (t == null) {
    // Stuff
    }
     
  9. Offline

    tkuiyeager1

    @henderry2019 i know but if player is using the fake alerts inventory that means t isnt null so i dont know how to get the other inventories for this check.
    Edit:
    It still dont work and here is the error code line:
    Code:
    if (!(e.getInventory().getName().equalsIgnoreCase(t.getName() + ": FakeAlerts Menu."))) return;
     
  10. Offline

    mine-care

    Not what i ment, I was trying to point out that not all inventories contain a ':' in their name but the event fires for all of them and it will cause errors if the inventory isnt the custom one.

    Yeah but if someone has it open and the player it refers to disconnects then the error will occur.

    http://www.oracle.com/technetwork/java/codeconventions-135099.html
     
  11. Offline

    tkuiyeager1

    Look At This, This Should Stop the event if the inventory name doesnt contains this words
    Code:
    if (!(e.getInventory().getName().equalsIgnoreCase(t.getName()+ ": FakeAlerts Menu."))) {
                return;
            }
    Ok so how can i fix it?
    So you mean that the package should be called: me.tkuiyeager1.fakealerts?


    EDIT:
    i fixed it by writing this code:
    Code:
    if (!(e.getInventory().getName().contains(": FakeAlerts Menu."))) {
                return;
            }
    but i still have the issue that @mine-care told about, how can i fix it?

    @mine-care do you know how to fix the problem you talked about?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  12. Offline

    mine-care

    Pretty much.

    What is the issue?
     
  13. Offline

    tkuiyeager1

    This Error:
     
  14. Offline

    mine-care

    @tkuiyeager1
    So how about checking if this player object is null?
     
  15. Offline

    tkuiyeager1

    @mine-care
    ok so do you mean
    Code:
    if(t == null) {
        p.closeinventory();
        return;
    }
    Ok i got it fixed thank you @mine-care and thank you @megamichiel and thank you everyone who replied to this thread.

    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 30, 2015
Thread Status:
Not open for further replies.

Share This Page