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):

        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
            at$1.execute( ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a( [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$ [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at java.util.concurrent.Executors$ [?:1.8.0_101]
            at [?: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( [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D( [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C( [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at [spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at [?:1.8.0_101]
    Caused by: java.lang.IllegalArgumentException: Name cannot be null
            at org.apache.commons.lang.Validate.notNull( ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.getPlayer( ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.Bukkit.getPlayer( ~[spigot.jar:git-Spigot-1e4dd71-5e5cf84]
            at com.-.-.-.user.-.onInventoryClick( ~[?:?]
            at sun.reflect.GeneratedMethodAccessor912.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_101]
            at java.lang.reflect.Method.invoke( ~[?:1.8.0_101]
            at$1.execute( ~[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.

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