Solved Some help with HashMaps

Discussion in 'Plugin Development' started by Fuzzwolf, Oct 5, 2012.

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

    Fuzzwolf

    I am trying to set a flag for a player which allows them to do something regarding a PlayerInteractEvent by having them perform a command. Both event listeners work as intended, but I am having troubles with checking whether or not the player has a true value for the HashMap "selectMode" within the onBlockClick method below. I am currently using test input, but none of my code within any of the if statements executes except for "YOLO1"


    I typecasted the commandsender within onCommand to the Player variable exec, and placed that in to the hashmap. I am now trying to search for the same value within the hashmap, but contained within the variable pl, taken right out of the event listener.


    Any and all help is appreciated. I've read through the documentation, but truth-be-told I did not understand much of it. This is a concept I have only learned about 15 minutes ago.


    Code:
        @EventHandler
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("rb")){
                if(sender instanceof Player){
                    if(args.length >= 0){
                    if(args[0].equalsIgnoreCase("set")){
                        Player exec = (Player) sender;
                        selectMode.put(exec, true);
                        sender.sendMessage("Please select a block now by clicking on it.");
                        return true;
                    }
                }
                }
           
                if(!(sender instanceof Player)){
                    sender.sendMessage("Please execute the command from a player!");
                }
            }
            return false;
        }
     
        @EventHandler
        public void onBlockClick(PlayerInteractEvent b){
            Player pl = b.getPlayer();
            System.out.println("YOLO1");
     
            if(selectMode.containsKey(pl)){
                System.out.println("Yolo2?");
                if(selectMode.get(pl)){
                    pl.sendMessage("YOLO3!");
                }
           
                b.setCancelled(true);
                selectMode.put(pl, false);
            }
        }
     
  2. Offline

    Tirelessly

    Use a list, not a hashmap.
     
  3. Offline

    Fuzzwolf

    Why? (serious question)
     
  4. Offline

    Tirelessly

    Because you're just putting their name and then true. (By the way, store their name, not their player instance.)

    See, <string, boolean> hashmaps are kind of useless in this scenario because you can just add the player to a list. Then on the interact event, if the list contains the players' name, continue. It does same thing, but easier.
     
    Fuzzwolf likes this.
  5. Offline

    CorrieKay

    Alternatively, you could probably work with metadata, however using a set/list might be faster.
     
  6. Offline

    Fuzzwolf

    Ah. Thanks for the help.
     
Thread Status:
Not open for further replies.

Share This Page