Solved Trying to reset Player's kit when he dies or leaves.

Discussion in 'Spigot Plugin Development' started by Fep310, Feb 9, 2020.

  1. I'm making a simple plugin where you can choose a kit with "/kit <kit>". The player will only be allowed to choose another kit after his death or if he leaves the server, but that's not working correctly.
    Here's where I'm at: the player can no longer choose a kit after having already chosen one, but when he dies or leaves the server, nothing changes (he still can't change his kit)

    To store this temporary data about the player and the kit that hes using, I'm trying to use HashMaps. One class is handeling the "/kit" command (called KitTestCmd), where the HashMap is created; and another is handeling the event where the player dies (called PvPEvents).

    I'll only be showing code that is relevant to this thread, so I erased some stuff from PassiveEvents.

    The command class:
    Code (Text):
    public class KitTestCmd implements CommandExecutor {

        private HashMap<String, String> pKit = new HashMap<>();
        public HashMap<String, String> getpKit() { return pKit; }

        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if (sender instanceof Player) {
                Player p = (Player) sender;

                if (args.length == 0) {
                    if (pKit.get(p.getName()) == null) {
                        p.sendMessage(ChatColor.RED + "[!] You need to specify a kit! Use: /kit <pvp>");
                    } else p.sendMessage(ChatColor.RED + "[!] You've already chose the " + pKit.get(p.getName()) + " kit.");

                if (args.length == 1) {
                    if (pKit.get(p.getName()) == null) {

                        if (args[0].equalsIgnoreCase("pvp")) {
                            pKit.put(p.getName(), "pvp");
                            p.sendMessage(ChatColor.GREEN + "[!] You selected the 'pvp' kit!");
                        } else p.sendMessage(ChatColor.RED + "[!] This kit doesn't exists.");

                    } else p.sendMessage(ChatColor.RED + "[!] You've already chose the " + pKit.get(p.getName()) + " kit.");

                if (args.length > 1) p.sendMessage(ChatColor.RED + "[!] Too many arguments! Use: /kit <kit>");

            } else sender.sendMessage(ChatColor.RED + "[!] This command is for players only.");
            return true;
    The event class:
    Code (Text):
    public class PvPEvents implements Listener {

        KitTestCmd kitMap = new KitTestCmd();
        HashMap<String, String> pKit = kitMap.getpKit();

        public void playerKill (PlayerDeathEvent e) {


            Player dead = (Player) e.getEntity();
            Player killer = dead.getKiller();

            if (pKit.get(dead.getName()) != null) pKit.remove(dead.getName());
    Sorry if I made a stupid Java mistake, I just learned OOP basics.
    Thanks for your attention and time! ^^
  2. You're creating a new instance of KitTestCmd inside of your listener class PvpEvents therefore when you're getting the hashmap it is empty.
    • Like Like x 1
    • Agree Agree x 1
  3. I got what you said, but I don't know how to not create a new empty instance and just get the HashMap in use that was generated in another class.
  4. There's many ways to do it. The simplest one (ignoring static Map) - transfer Map through constructor.
    • Like Like x 1
  5. assuming your main class is named Main...

    private Main plugin;

    public PvPEvents(Main plugin){
    this.plugin = plugin;

    Now create your map in Main then create a getter for it and do plugin.getMyMap();

    when you register your listener instead of new PvPEvents() use new PvPEvents(this)
    • Useful Useful x 1
  6. I got everything working correctly with no error logs.
    Thank you guys so much for your time and attention! I've never seen a greater and more active forum community than this one <3