Solved How to get a player from a specific player from a hashmap that was set by another player? ICE

Discussion in 'Spigot Plugin Development' started by fujiboy4, Jun 6, 2016.

Thread Status:
Not open for further replies.
  1. ICE = InventoryClickEvent

    Alright, This is what i am trying to make...
    A punish system
    Here is a screenshot of the menu.

    2016-06-06_16.46.10.png
    Now, to open this menu you have to type /punish <Player>
    Although, when I click on an item in there, I want to do actions against the current player typed in chat. Thanks to @Hunky524 I was able to store the player in a hashmap and be able to call it later. But, apparently i'm getting a NPE and I don't know why. Here is the code and the error:

    Menu.java:
    Code (Text):
    public HashMap<Player, Player> players = new HashMap<>();

        public Player getPunished(Player player) {
            return players.get(player);
        }

        public void setPunished(Player player, Player punished) {
            if (Bukkit.getPlayer(punished.getName()) != null) {
                players.put(player, punished);
            }
        }

        public void openMenu(Player p, Player t) { ... }
    }
    PunishCommand.java:
    Code (Text):
    package mmc.punish.commands;

    import mmc.punish.main.Main;
    import mmc.punish.menus.Menus;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class Punish implements CommandExecutor {

        Main plugin;
     
        Menus menu;
     
        public Punish(Main pl) {
            plugin = pl;
            menu = new Menus();
        }
     
     
       
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            Player p = (Player) sender;

            if (p instanceof Player) {
                if (p.hasPermission("masterymc.owner")
                        || p.hasPermission("masterymc.admin")
                        || p.hasPermission("masterymc.mod")) {
                    if (args.length == 0) {
                        p.sendMessage("§6Punish> §7Please add these arguments. /punish <Player>");
                    } else {
                        if (args.length == 1) {
                            Player target = Bukkit.getPlayer(args[0]);
                            if (!Bukkit.getOnlinePlayers().contains(target)) {
                                p.sendMessage("§6Punish> §7Player Not Found : "
                                        + target.getName());
                            } else {
                                menu.openMenu(p, target);
                                menu.setPunished(p, target);
                            }
                        }
                    }
                    return true;
                }

            }
            return false;
        }

    }
     
    Events:
    Code (Text):
    package mmc.punish.events;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import mmc.punish.commands.Punish;
    import mmc.punish.main.Main;
    import mmc.punish.menus.Menus;

    public class Events implements Listener {

        Punish punish;
        Menus menu;
     
        Main plugin;
        public Events(Main pl) {
            plugin = pl;
         
            punish = new Punish(plugin);
            menu = new Menus();
        }
     
        @EventHandler
        public void onClick(InventoryClickEvent e) {
            final Player p = (Player) e.getWhoClicked();
            final ItemStack item = e.getCurrentItem();
            final Player target = menu.getPunished(p);
            final Inventory inv = e.getInventory();
         
            if (inv.getName().contains("Punishing")) {
                e.setCancelled(true);
                if (target != null) {
                    target.sendMessage("Test");
                }
            }
        }
     
    }
     
    Error:
    Code (Text):
    [16:31:37 ERROR]: Could not pass event InventoryClickEvent to PunishmentSystem v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-d3e0b6f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.NullPointerException
            at mmc.punish.events.Events.onClick(Events.java:35) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_77]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-d3e0b6f]
            ... 15 more
    Which is on line:
    Code (Text):
    target.sendMessage("Test");
     
  2. Wait sorry. I'm not getting an error, but it is putting the item back where it was and not typing "test" in chat.
     
  3. Choco

    Moderator

    Just a few things
    What are you doing with that if statement? Checking if the player is online? Use player.isOnline().

    These should be private. Fields should be private if they are not intended to be visible to other classes

    Use ChatColor. Section symbol is not guaranteed to work on all platforms due to UTF formatting
     
  4. The reason it probably isn't picking up on the inventory name is because inventory names have colors in them.
     
    • Agree Agree x 1
  5. Well, that may be the case, but the inventory items are cancelled when I click them, so that means its picking up the inventory.
     
  6. That doesn't help with the NPE. I did make the changes too.
     
    • Like Like x 1
  7. Dont use players in HashMaps, use UUIDS. Thats like 10000 times told on this forum. Also, please check again which line is the cause of the NPE. If i use your Events.java, Line 35 is this:

    Code (Text):
    if (inv.getName().contains("Punishing")) {

    And besides: Use inv.getTitle() . getName does not return the title of the Inventory afaik.
     
    • Agree Agree x 1
  8. Choco

    Moderator

    Correction,
    Do not use Player objects as the key of a Map. You may very well need a Player object as a value to a key, which would be less memory intensive as getting the Player object from Bukkit.getPlayer(UUID).

    Besides, using a Player in a Map or any sort of Collection is not bad practice. It's a matter of whether the data is cleared on disable or when it's not in use. Same thing goes for UUID's
     
  9. I was going to convert to UUID eventually, I just like to make sure 1 thing works before changing it. Will do when I have time later.
     
  10. Line 35 is the message that is trying to send to the player... But it never sends and there is no error.
     
  11. Solved - I was renewing the Menus class in Punish and in Events and therefore creating a new HashMap. I added a stacktrace into when it adds the player on command and then when I typed out the command it would put the name, but in events, it didnt do anything because the hashmap was reset. :p
     
Thread Status:
Not open for further replies.