I would like to get a (true/false) value from a player using a hashmap for a plugin i'm hoping to get done by the end of the upcoming month but i'm having trouble with it, So if someone could help me with this it would be great. Code:java package me.kingkoncaliber.test; import java.util.HashMap; import org.bukkit.ChatColor;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.plugin.java.JavaPlugin; public class commands extends JavaPlugin {HashMap<Player,String> redValue = new HashMap<Player, String>();public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){Player player = (Player) sender;if(redValue.containsValue(true)){ }return false;}}
//EDIT if you have trouble with this, how do you make plugin to the end of month ? if you wan't only value True, False. Use this: Code:java HashMap<String,Boolean> redValue = new HashMap<String,Boolean>();if(cmd.getName.equalsIgnoreCase("command")) {String playerName = sender.getName();if(redValue.containsKey(playerName) && redValue.containsValue(true)) {//code} }
When i rune the command /test1 to put the hashmap data into the server for me with the following code Code:java if(cmd.getName().equalsIgnoreCase("test1")){ redValue.put(playerName, false);} and then run /test to test the command to see if it work saying two messages for true and false it doesn't do anything with this code Code:java if(cmd.getName().equalsIgnoreCase("test")){if(redValue.containsKey(playerName) && redValue.containsValue(true)) {player.sendMessage("Yeh go get em'");redValue.put(playerName, false);}else if(redValue.containsKey(playerName) && redValue.containsValue(false)){ player.sendMessage("Bitch Please, Get back on duty");redValue.put(playerName, true); }}
KingKongC // Why are you putting to value on True or False ? Code:java if(cmd.getName().equalsIgnoreCase("test1")){// Don't forget check if HashMap contains playerName.if(redValue.containsKey(playerName)) {sender.sendMessage("have you voted");} else {redValue.put(playerName, false);} To get Value: Code:java if(cmd.getName().equalsIgnoreCase("test")){Boolean value = redValue.get(playerName); // playerName is sender.getName();if(value) {// TRUE} else {// FALSE}}
Two points here: 1. Don't save a player object as long as you don't know how top clean it up correctly. I will not explain this anymore as it was discussed about 1000 times already and getting the reason why it's as simple as searching the forum for this. 2. Why you don't simply use a HashSet instead of a HashMap? If it contains the player name it is true, and if not it is false. Changing it is as easy as adding or removing the player name.
He wants to save a Boolean, you can do it by checking if the key is in the hash set or not instead of first checking if the key is in and then checking if the value is true or not. Saves you one check and the boxing and unboxing of the boolean value.