Store player UUID for InventoryClickEvent

Discussion in 'Spigot Plugin Development' started by _diam, Oct 5, 2016.

  1. Hey there!

    I've looked around a fair bit so far, and I'm on a tight schedule to get my plugin finished. I don't want anyone to spoonfeed me code, and I'm not new to Java or plugin development. However, I rarely ever use HashMaps or ArrayLists, and I was wondering if anyone could help me out.

    What I want to do is store a player's UUID from an inventory method into a HashMap (or ArrayList, whatever's better), and grab it in the InventoryClickEvent as a target player.
    In specific: Inventory method > Stores target's UUID > InventoryClickEvent grabs UUID and sets as player called 'target'

    I've tried

    Code (Text):

    private static HashMap<UUID, String> TargetUUID = new HashMap<UUID, String>();
     
    Code (Text):

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player player = (Player)e.getWhoClicked();
            Player target = Bukkit.getPlayer(TargetUUID.get(1));
     
    Code (Text):

    public static Inventory inventoryName(Player player, Player target) {
            TargetUUID.put(target.getUniqueId(), "target");
     
    Which generates an error:
    Code (Text):

    [19:23:44 ERROR]: Could not pass event InventoryClickEvent to -v4.23.53-release
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1854) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_101]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_101]
            at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:732) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
    Caused by: java.lang.IllegalArgumentException: Name cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.getPlayer(CraftServer.java:408) ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.Bukkit.getPlayer(Bukkit.java:384) ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at com.-.-.-.user.-.onInventoryClick(-.java:65) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor912.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
    ... the stacktrace is telling me that the player I got from the clickEvent is null, I need to solve this, I have no idea how.
     
  2. @_diam
    • First off, the fields shouldn't be static.
    • Secondly, you'd use an abstract type rather than an implementation as field type (so rather Map than HashMap), for maintainability.
    • Fields should be using camel case.
    • You're trying to get the value associated with the key 1 (an integer) from a Map which associates UUIDs with Strings. This will never work, because you never insert integers in the first place. use Map#get(Object) with the actual key type (so use an UUID in your case).
    As for Lists vs Maps, they have their own specific purposes. You should definitely read about them, since they're probably two of the most common data structures in Java (together with Sets, that makes three of the most common data structures)
     
    • Agree Agree x 1
  3. First hint: Java is 0-based, so your first entry is get(0).
    But please post your whole code if that does not help you, and also mark im what line the error appeared.
     
  4. Player target = Bukkit.getPlayer(TargetUUID.get(0));
    Code (Text):
     Player target = Bukkit.getPlayer(TargetUUID.get(0));
     
    Could you expand on that?
     
  5. Yeah, that's not going to work.

    Your essentially telling Java to look in your Map and get the Value associated with the Key "0", and last time I checked, "0" is not a valid UUID, so Java cannot find that key in the Map and returns null.

    Maps do not function as Lists. If you're looking to get the first element of something, use a List.

    https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html

    Really should read over it, Maps can be VERY useful. I can't function without them.
     
    • Like Like x 1