Sending players hashmap information

Discussion in 'Plugin Development' started by SantaClawz69, Aug 14, 2016.

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

    SantaClawz69

    So I have a hashmap of wantedLevel. Which contains <String, Integer> (Obviously for player name, and wanted level).

    Now whenever a player types "/wanted list" I want the information of the hashmap to be sent to the player. So it will display something like this: SantaClawz69: 1

    Santa being my name, and the integer determining the wanted level he's on. Here is my code for the 2 classes I'm using. At the moment I'm making the hashmaps static, later will make constructors and what not.
    Wanted class:
    Code:
    package me.bryan.Police.Commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.bryan.Police.Main;
    import me.bryan.Police.Wanted.WantedSystem;
    
    
    public class Wanted implements CommandExecutor{
        Main main;
        public Wanted(Main main) {
            this.main = main;
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
                Player player = (Player)sender;
                if(Main.policeMode.contains(player.getName())) {
                    if(args.length == 0) {
                        player.sendMessage("/wanted list: Gives out all players that is wanted");
                        player.sendMessage("/wanted set <player name>: Sets a player as wanted stage 1 default");
                        return true;
                    }
                    if(args[0].equalsIgnoreCase("list")) {
                        //WHERE I NEED THE INFO OUTPUTTED
                        player.sendMessage(ChatColor.RED + "[WANTED]");
                        player.sendMessage(ChatColor.GOLD + "");
                    } else if(args[0].equalsIgnoreCase("set")) {
                        if(args.length == 1) {
                            player.sendMessage(ChatColor.RED + "You must type a player name");
                            return true;
                        }
                        String message = "";
                        for(int i = 1; i<args.length; i++) {
                            message += args[i] + " ";
                        }
                        message.trim();
                        if(Bukkit.getServer().getPlayer(message)!=null) {
                          
                        } else {
                            player.sendMessage(ChatColor.RED + "Player null");
                            return true;
                        }
                        WantedSystem.wanted.put(message, 1);
                    } else {
                        player.sendMessage(ChatColor.RED + "Unknown argument!");
                    }
                      
                  
                } else {
                    player.sendMessage("You must be a policeman, or be on duty in order to use this command");
                }
            }
          
            return true;
        }
    
    }
    

    WantedSystem class: (NOTE: Hashmap wanted is just a hashmap for #ofkills
    Code:
    package me.bryan.Police.Wanted;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    
    import me.bryan.Police.Main;
    
    public class WantedSystem implements Listener {
        Main main;
        public static HashMap<String, Integer> wanted = new HashMap<String, Integer>();
        public static HashMap<String, Integer> wantedLevel = new HashMap<String, Integer>();
        public WantedSystem(Main main) {
            this.main = main;
        }
    
        @EventHandler
        public void onPlayerkill(PlayerDeathEvent event) {
            Player killer = event.getEntity().getKiller();
            Player killed = event.getEntity();
            if (!(killer instanceof Player)) {
                return;
            }
            if (!(killed instanceof Player)) {
                return;
            }
            if (!wanted.containsKey(killer.getName())) {
                wanted.put(killer.getName(), 0);
            }
            if (wanted.get(killer.getName()) >= 1) {
                killer.sendMessage("§4You're currently wanted, if there are any police on you're being hunted");
                Bukkit.broadcastMessage(killer.getName() + "Is now wanted: Level 1.");
                wantedLevel.put(killer.getName(), 1);
            } else if (wanted.get(killer.getName()) >= 3) {
                killer.sendMessage("§4You're wanted level just raised.");
                Bukkit.broadcastMessage(killer.getName() + "Is now wanted: Level 2");
                wantedLevel.put(killer.getName(), 2);
            } else if (wanted.get(killer.getName()) >= 6) {
                killer.sendMessage("§4You're now max priority");
                Bukkit.broadcastMessage(killer.getName() + "Is now wanted: Level 3 TOP PRIORITY");
                wantedLevel.put(killer.getName(), 3);
            }
            wanted.put(killer.getName(), wanted.get(killer.getName() + 1));
    
        }
    }
    
     
  2. Offline

    MCMastery

    use UUIDs instead of player names. what if the player changes their name?

    to get the wanted level, just get the info from the wantedLevel hashmap.... WantedSystem.wantedLevel.get(playerName)
     
  3. Offline

    SantaClawz69

    Will it also display the wanted level? I posted this cause I don't have my test server, so I wasn't really sure.

    Also, reason why I don't use UUID's because it's not really needed. Being wanted is like a temp thing. So once you die or something wanted is gone, or if you're arrested. So unless you found a new way of changing your name every 5 minutes you can't really bypass it.
     
    MCMastery likes this.
  4. Offline

    MCMastery

    wantedLevel.get(playerName) returns their wanted level
     
  5. Offline

    SantaClawz69

    Thank you sir, by the way. Did you see any errors in my code while you looked at it? Like I said I can't really test any of it xD So if you can just skim through that would be nice.

    player.sendMessage(ChatColor.GOLD + ""+WantedSystem.wantedLevel.get(player.getName()));
    So that'll just send them the info? Interesting.
     
  6. Offline

    MCMastery

    Yep. I'll skim through it probably when i get a chance
     
  7. MCMastery likes this.
  8. Offline

    SantaClawz69

    I'm going to change the statics later on, I just needed to test if it was working because I needed it quickly.
     
Thread Status:
Not open for further replies.

Share This Page