Hasmap Help

Discussion in 'Plugin Development' started by KingKongC, Mar 9, 2013.

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

    KingKongC

    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
    1. package me.kingkoncaliber.test;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class commands extends JavaPlugin {
    12. HashMap<Player,String> redValue = new HashMap<Player, String>();
    13. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    14. Player player = (Player) sender;
    15. if(redValue.containsValue(true)){
    16.  
    17. }
    18. return false;
    19. }
    20. }
    21.  
     
    sionzee likes this.
  2. Offline

    sionzee

    //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
    1.  
    2. HashMap<String,Boolean> redValue = new HashMap<String,Boolean>();
    3. if(cmd.getName.equalsIgnoreCase("command")) {
    4. String playerName = sender.getName();
    5. if(redValue.containsKey(playerName) && redValue.containsValue(true)) {
    6. //code
    7. }
    8.  
    9. }
     
  3. Offline

    KingKongC

    When i rune the command /test1 to put the hashmap data into the server for me with the following code
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("test1")){
    2.  
    3. redValue.put(playerName, false);
    4. }


    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
    1. if(cmd.getName().equalsIgnoreCase("test")){
    2. if(redValue.containsKey(playerName) && redValue.containsValue(true)) {
    3. player.sendMessage("Yeh go get em'");
    4. redValue.put(playerName, false);
    5. }else if(redValue.containsKey(playerName) && redValue.containsValue(false)){
    6.  
    7. player.sendMessage("Bitch Please, Get back on duty");
    8. redValue.put(playerName, true);
    9.  
    10. }
    11. }
     
  4. Offline

    sionzee

    KingKongC
    // Why are you putting to value on True or False ?
    Code:java
    1.  
    2. if(cmd.getName().equalsIgnoreCase("test1")){
    3. // Don't forget check if HashMap contains playerName.
    4. if(redValue.containsKey(playerName)) {
    5. sender.sendMessage("have you voted");
    6. } else {
    7. redValue.put(playerName, false);
    8. }
    9.  


    To get Value:
    Code:java
    1.  
    2. if(cmd.getName().equalsIgnoreCase("test")){
    3. Boolean value = redValue.get(playerName); // playerName is sender.getName();
    4. if(value) {
    5. // TRUE
    6. } else {
    7. // FALSE
    8. }
    9. }
     
  5. 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.
     
  6. Offline

    sionzee

    He wan't store, PlayerName, Value, this is not possible in HashSet.
     
  7. 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.
     
Thread Status:
Not open for further replies.

Share This Page