Solved Toggle Boolean with hashmap

Discussion in 'Plugin Development' started by GeorgeMarx, Apr 17, 2014.

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

    GeorgeMarx

    Could someone explain how i use hash-maps effectively
    what i want to do is to add a player to this hashmap

    (name, is-enabled(false))

    when they type the command

    then toggle it to true this seems really simple but i cant get my head round the actual specifics of it
     
  2. Offline

    nlthijs48

    GeorgeMarx First create a HashMap<String, boolean>:
    Code:java
    1. HashMap<String, Boolean> map = new HashMap<String, Boolean>();

    Then to add a player to the map use the following:
    Code:java
    1. map.put(playername, true);

    And to get the value:
    Code:java
    1. map.get(playername);

    playername is a String containing the name of the player.

    If you have more question just reply :)
     
  3. Code:java
    1. HashMap<String, Boolean> toggle = new HashMap<String, Boolean>();
    2.  

    Add:
    Code:java
    1. toggle.put(Playername, true)

    Check:
    Code:java
    1. if(toggle.get(Playername) == Boolean.TRUE){
    2. toggle.put(Playername, false);
    3. }
     
  4. Offline

    GeorgeMarx

  5. Offline

    nlthijs48

    GeorgeMarx Yes that is what I suggested, 'playername' is just a placeholder for your actual variable that contains the playername. Just replace it with thePlayer.getName() and you should be fine.
     
  6. Offline

    GeorgeMarx

    good that's what i was doing must have missed that bit in your first post thanks again.

    Code:java
    1. HashMap<String,Boolean> map = new HashMap<String, Boolean>();
    2. map.put(ourPlayer,false);
    3.  
    4. // when /cgod is typed
    5. if(commandLabel.equalsIgnoreCase("cgod"))
    6. {
    7.  
    8. thePlayer.sendMessage(ChatColor.GOLD+ourPlayer);
    9. if(map.get(ourPlayer) == Boolean.FALSE){
    10.  
    11. thePlayer.sendMessage(ChatColor.GREEN+"Cgod enabled");
    12. map.put(ourPlayer, true);
    13. }
    14. else if (map.get(ourPlayer) == Boolean.TRUE){
    15.  
    16.  
    17. thePlayer.sendMessage(ChatColor.DARK_RED+"Cgod disabled");
    18. map.put(ourPlayer,false);
    19. }


    @sohardhun15 @nlthijs48 Any idea why the Boolean value is not toggling the message
    Cgod enabled is seen so it knows its false but it does not toggle so i am guessing that the below is not being called but i am not sure why
    map.put(ourPlayer, true); EDIT i think i know what im doing wrong i am forgettign to retrieve the boolean and or the map

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  7. So, it's working?
     
  8. Offline

    GeorgeMarx

  9. Offline

    Code0

    GeorgeMarx Where is ourPlayer defined?

    Post your FULL code and I'll fix it.
     
  10. Offline

    Rocoty

    GeorgeMarx Why don't you use a List or a Set instead? Store the player names and check if they are in the list. Adding the extra boolean really does no good.
     
  11. Offline

    GeorgeMarx

    Rocoty partly because i wanted to use hash maps as i have not before and i thought toggling would be more complex but i guess you say if name in list do this if not dont

    Code0

    Code:java
    1. package me.GeorgeMarx;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Bukkit;
    7.  
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Damageable;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class PvpGod extends JavaPlugin
    17. {
    18.  
    19. Logger PvpLogger = Bukkit.getLogger();
    20.  
    21.  
    22.  
    23.  
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35. @Override
    36. public void onEnable()
    37. {
    38. // Startup message
    39. PvpLogger.info("Starting PvpGod");
    40. PvpLogger.warning("Be sure to have FUN");
    41.  
    42. }
    43.  
    44.  
    45.  
    46.  
    47. @Override
    48. public void onDisable()
    49. {
    50. // Closing message
    51. PvpLogger.info("Disabling PvpGod");
    52. PvpLogger.severe("Disabling PvpGod HELP");
    53. }
    54.  
    55. //@SuppressWarnings("deprecation")
    56. public boolean onCommand (CommandSender theSender, Command cmd, String commandLabel, String[] args)
    57. {
    58.  
    59. if(theSender instanceof Player){
    60. Player thePlayer =(Player) theSender;
    61. String ourPlayer = thePlayer.getName();
    62.  
    63. HashMap<String,Boolean> map = new HashMap<String, Boolean>();
    64. map.put(ourPlayer,false);
    65.  
    66. // when /cgod is typed
    67. if(commandLabel.equalsIgnoreCase("cgod"))
    68. {
    69.  
    70. thePlayer.sendMessage(ChatColor.GOLD+ourPlayer);
    71. map.get(ourPlayer);
    72.  
    73. if(map.get(ourPlayer) == false){
    74.  
    75. thePlayer.sendMessage(ChatColor.GREEN+"Cgod enabled");
    76. map.get(ourPlayer);
    77. map.put(ourPlayer, true);
    78.  
    79.  
    80. }
    81. else if (map.get(ourPlayer) == true){
    82.  
    83.  
    84. thePlayer.sendMessage(ChatColor.DARK_RED+"Cgod disabled");
    85. map.put(ourPlayer,false);
    86. }
    87.  
    88.  
    89.  
    90. //double health = ((Damageable) thePlayer).getHealth();
    91. //if (health < 5)
    92. //{
    93. // ((Damageable) thePlayer).setHealth(20);
    94. //}
    95.  
    96.  
    97.  
    98.  
    99.  
    100.  
    101.  
    102.  
    103. }
    104. }
    105.  
    106.  
    107.  
    108.  
    109.  
    110.  
    111.  
    112.  
    113.  
    114. return true;
    115. }
    116.  
    117.  
    118.  
    119.  
    120.  
    121.  
    122.  
    123.  
    124.  
    125.  
    126.  
    127. }


    Rocoty tried your idea same problem it refuses to toggle.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  12. Offline

    Rocoty

    GeorgeMarx This is because you are declaring the list/map inside the method. Causing it to only exist in the scope of that method. So when a command is run you declare a new map, put some data into it and then throw it away.

    You should already know why this happens if you know basic Java or programming in general. If you don't know basic Java, then you are getting ahead of yourself by trying to make Bukkit plugins.
     
  13. Offline

    GeorgeMarx

    Rocoty oh i see so whenever that method is activated i am making a new one so it will always be at its default value.

    i know basic java i am just very rusty i might need to refresh myself

    Rocoty I am declaring it at the start now but it still does it what else needs to be outside the method aside from HashMap<String,Boolean> map =new HashMap<String, Boolean>();

    ? As i try and move the the line i decalred outPlayer but it has to go after the sender

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  14. Offline

    nlthijs48

    GeorgeMarx In you if statements you use this:
    Code:java
    1. if(map.get(ourPlayer) == false) {

    But you don't need the '== false' part, Java will outbox Boolean types to boolean and the other way around automatically. You still need to invert the statement, so place a '!' before the statement, like this:
    Code:java
    1. if(!map.get(ourPlayer)) {
     
  15. Offline

    GeorgeMarx

    nlthijs48 thanks any idea how to fix the rest?
    i need to move my ourplayer declaration outside my method but it does not let me
     
  16. Offline

    nlthijs48

    GeorgeMarx Can you post your current code? Then I try to figure out what is wrong.
     
  17. Offline

    GeorgeMarx

    nlthijs48

    Code:java
    1. package me.GeorgeMarx;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Bukkit;
    7.  
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Damageable;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class PvpGod extends JavaPlugin
    17. {
    18. HashMap<String,Boolean> map = new HashMap<String, Boolean>();
    19.  
    20. Logger PvpLogger = Bukkit.getLogger();
    21.  
    22.  
    23.  
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35.  
    36. @Override
    37. public void onEnable()
    38. {
    39. // Startup message
    40. PvpLogger.info("Starting PvpGod");
    41. PvpLogger.warning("Be sure to have FUN");
    42.  
    43. }
    44.  
    45.  
    46.  
    47.  
    48. @Override
    49. public void onDisable()
    50. {
    51. // Closing message
    52. PvpLogger.info("Disabling PvpGod");
    53. PvpLogger.severe("Disabling PvpGod HELP");
    54. }
    55.  
    56. //@SuppressWarnings("deprecation")
    57. public boolean onCommand (CommandSender theSender, Command cmd, String commandLabel, String[] args)
    58. {
    59.  
    60. Player thePlayer =(Player) theSender;
    61. String ourPlayer = thePlayer.getName();
    62.  
    63.  
    64. map.put(ourPlayer,false);
    65.  
    66.  
    67.  
    68.  
    69. if(theSender instanceof Player){
    70.  
    71.  
    72. // when /cgod is typed
    73. if(commandLabel.equalsIgnoreCase("cgod"))
    74. {
    75.  
    76. thePlayer.sendMessage(ChatColor.GOLD+ourPlayer);
    77.  
    78.  
    79. if(!map.get(ourPlayer))
    80. {
    81.  
    82. thePlayer.sendMessage(ChatColor.GREEN+"Cgod enabled");
    83. map.get(ourPlayer);
    84. map.put(ourPlayer, true);
    85.  
    86.  
    87. }
    88. else if (map.get(ourPlayer))
    89.  
    90.  
    91. thePlayer.sendMessage(ChatColor.DARK_RED+"Cgod disabled");
    92. map.put(ourPlayer,false);
    93. }
    94.  
    95.  
    96.  
    97. //double health = ((Damageable) thePlayer).getHealth();
    98. //if (health < 5)
    99. //{
    100. // ((Damageable) thePlayer).setHealth(20);
    101. //}
    102.  
    103.  
    104.  
    105.  
    106.  
    107.  
    108.  
    109.  
    110. }
    111.  
    112.  
    113.  
    114.  
    115.  
    116.  
    117.  
    118.  
    119.  
    120.  
    121. return true;
    122. }
    123.  
    124.  
    125.  
    126.  
    127.  
    128.  
    129.  
    130.  
    131.  
    132.  
    133.  
    134. }
    135.  
     
  18. Offline

    AoH_Ruthless

    GeorgeMarx
    You are probably incorrectly globalizing your variable.

    You clearly have not learned Java in any basic form to the point where you should be working with Bukkit, as Rocoty pointed out. You can't simply just forget enough information to be at the stage where you are right now ..
     
  19. Offline

    GeorgeMarx

    AoH_Ruthless I was told in my course we would not be using java so i didn't touch it for ages it was only last week i was asked if i still know the basics and if i wanted to try make a plugin i will admit i am not that experienced but i have made games such as guess the number and a class extensive shooter like galaga
     
  20. Offline

    JBoss925

    Listen, I don't see what the problem is. It should be working. I'm guessing AoH_Ruthless is right but if not you could do:
    Code:java
    1. HashMap<String, Integer> toggle = new HashMap<String, Integer>();

    then:
    Code:java
    1. //0 is off, 1 is on
    2. toggle.put(playername, 1);

    then check like:
    Code:java
    1. if(toggle.get(playername) == 1){
    2. //do stuff
    3. }
     
  21. Offline

    1Rogue


    This will throw a null pointer when pulling a null value from the map, you should use a comparison check:

    Code:java
    1. if (map.get(ourPlayer) == true) {
    2. //...
    3. }
     
  22. Offline

    GeorgeMarx

    JBoss925 AoH_Ruthless 1Rogue I know what the problem is i just don't know how to fix it the problem is that i have declared our player in the wrong place and as a result it creates a new new one with the same value every time the command is typed

    so i need to know how i can declare and where to stop it from resetting every-time it's typed

    i tried it with numbers aswell i get the same problem as o matter what i use to say true or false those values are being reset before the toggle making it go false true false before the if false statement
     
  23. Offline

    nlthijs48

    List of problems with your code:
    1. Initializing your map in the class itself, would be better if you do that in the onEnable().
    2. Using the general logger instead of the logger of your own plugin (also think about removing the "Starting PvpGod" message, something like that will already be printed to the console, same for the disable message).
    3. Casting the CommandSender argument to a Player before you check if it is a Player.
    4. You did set the boolean in the HashMap to false at every start of the onCommand, so it would always be disabled when entering your code.
    5. Checking which command was used by the commandLabel, this is the alias that is used, not the actual command.
    6. Random "map.get(ourPlayer);" line in your code, this does nothing.
    7. Used a "else if (" when a "else" is better and also this else did not have brackets (those -> {}), which caused that the "map.put(outPlayer, false);" was always executed, preventing you from turning it on.
    8. Also as a extra tip, use CTRL+SHIFT+F in Eclipse to automatically format your code, you can read and uderstand it way better if it is formatted correctly.
    I only want to help you getting better at coding, I'm not angry at you at all (I'm not native English so my use of words is wrong sometimes). Hope this helps you, with all the improvements the code will be like this:
    Code:java
    1. package me.GeorgeMarx;
    2. import java.util.HashMap;
    3. import java.util.logging.Logger;
    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 PvpGod extends JavaPlugin {
    12. HashMap<String, Boolean> map;
    13.  
    14. Logger PvpLogger = this.getLogger();
    15.  
    16. @Override
    17. public void onEnable() {
    18. // Startup message
    19. PvpLogger.info("Starting PvpGod");
    20. PvpLogger.warning("Be sure to have FUN");
    21. map = new HashMap<String, Boolean>();
    22.  
    23. }
    24.  
    25. @Override
    26. public void onDisable() {
    27. // Closing message
    28. PvpLogger.info("Disabling PvpGod");
    29. PvpLogger.severe("Disabling PvpGod HELP");
    30. }
    31.  
    32. public boolean onCommand(CommandSender theSender, Command cmd,
    33. String commandLabel, String[] args) {
    34.  
    35. if (!(theSender instanceof Player)) {
    36. theSender.sendMessage("Only a player can use this command, sorry!");
    37. return true;
    38. }
    39.  
    40. Player thePlayer = (Player) theSender;
    41. String ourPlayer = thePlayer.getName();
    42.  
    43. // when /cgod is typed
    44. if (cmd.getName().equalsIgnoreCase("cgod")) {
    45.  
    46. thePlayer.sendMessage(ChatColor.GOLD + ourPlayer);
    47.  
    48. if ((map.get(ourPlayer) == null) || !map.get(ourPlayer)) {
    49. thePlayer.sendMessage(ChatColor.GREEN + "Cgod enabled");
    50. map.put(ourPlayer, true);
    51. } else {
    52. thePlayer.sendMessage(ChatColor.DARK_RED + "Cgod disabled");
    53. map.put(ourPlayer, false);
    54. }
    55. }
    56.  
    57. // double health = ((Damageable) thePlayer).getHealth();
    58. // if (health < 5)
    59. // {
    60. // ((Damageable) thePlayer).setHealth(20);
    61. // }
    62.  
    63.  
    64.  
    65. return true;
    66. }
    67.  
    68. }
     
  24. Offline

    JBoss925

    Check if the map contains the key and if so don't add them.
     
  25. Offline

    GeorgeMarx

    JBoss925 i did that but the problem is it makes a new hashmap every time i type the command
     
  26. Offline

    theguynextdoor

    If it is a simple toggle, i.e. it's either active or it's not. ... then why not use a list as opposed to a hashmap?

    Code:
    List<UUID> toggle = new ArrayList<UUID>();
     
    public boolean isToggled(Player player) {
      return toggle.contains(player.getUniqueId());
    }
    Then to toggle it you would simply add them to the list if they aren't in it, and vice versa otherwise.

    Note I do use UUID in this example as opposed to String. The reason for this should be pretty well known by now.
     
  27. Offline

    JBoss925

    Wat? How?

    They want to use a hash map. Lists were already suggested.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  28. Offline

    theguynextdoor

    It does seem you are correct, my apologies.

    I am (now) aware that this is being done for 'hashmap practice' but I would say that a toggling feature is not really that good of a hashmap practice because it isn't really a practical use for hashmaps. If you were to make a toggle, then using a List/Set would be the way to do it, not a hashmap.

    You do have a 'right' (I use that word loosely) to use whatever method you want do achieve your goal and feel free to ignore me, but I am just going to say that this is not the (best, nor optimal) way to practice hashmaps. If you are going to practice using a data structure, at least use it in a context where it actually makes sense to use it.

    I can provide you with a common hashmap use. A lot of plugins use hashmaps to relate a custom user class to a player. So they will have a class called User and will add that to a hashmap with the name of the player as the key (Now you would use UUID) and then the user class as the value. Then to get the user class they would just search the hashmap for the player's name.
     
  29. Offline

    GeorgeMarx

    theguynextdoor I did try using a list but it just made another list the problem lies with ourPlayer as it is decalred in the method and i cant move it out of the method it says thePlayer can not be resolved.

    and yes it did start off testing hashmaps but then became a why do i have this error lets see if this works game and nothing works.

    nlthijs48 Thank you so much, you do not know how long i spent on this. Your English is good by the way ,also is there a form of likes or hearts because you Sir deserve one. Thanks again

    Thank you everyone who helped have not been on this forum long at all already convinced its a good place

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  30. Offline

    nlthijs48

    GeorgeMarx Glad to help you :D, below a post is a little 'like' button, you could use that, but just because you got it working I'm already happy. Also you can mark the thread as 'Solved' by using the thread tools button next to the title and changing it to solved.
     
    GeorgeMarx likes this.
Thread Status:
Not open for further replies.

Share This Page