Hashmap is asking for odd Syntax

Discussion in 'Plugin Development' started by minepress, Aug 16, 2014.

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

    minepress

    Hi all. Having a bit of an issue with a hashtag. I believe I've written it correctly. Trying to store an integer with someones username.

    Code:java
    1.  
    2. HashMap<String,Integer> i = new HashMap<String,Integer>();
    3.  
    4. for(Player player : Bukkit.getOnlinePlayers()){
    5. i.put(player.getName(), 0);
    6. }
    7.  
    8.  


    From here I am getting a sytax error.

    I know that it's HashMap<Number of Keys (In this instance, the players),The Integer which will increment>.

    But even after reading: http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html I can't see an issue. Would someone mind pointing it out to me?

    Thanks
     
  2. Offline

    techboy291

    What is the error?
     
  3. Offline

    _Filip

    What is your code you're using to put the values?
     
  4. Offline

    minepress


    I am simply receiving a sytax error on the ";" part.


    Updated OP.
     
  5. Offline

    xTigerRebornx

    minepress You are getting an error, what does the error specifically say. Also, is the snippet you've posted (the for loop) located within a method or outside?
     
  6. Offline

    minepress

    Outside a method. Within the class. Would that not make it a global map integer?

    And the error specifically is a syntax error only around the ";" as mentioned above.
     
  7. Offline

    xTigerRebornx

    minepress If the for loop is located outside a method, that may be the problem, try putting it in a method and see if the error persists.
     
  8. Offline

    minepress


    Just tried putting it into an @EventHandler method (Specifically why I created it).

    Here is the code:

    Code:java
    1. @EventHandler
    2. //PlayerInteractEvent event
    3. public void onSpawn(PlayerInteractEvent event){
    4.  
    5. for(Player player : Bukkit.getOnlinePlayers()){
    6. wolfcount.put(player.getName(), 0);
    7. }
    8.  
    9. Location spawn = event.getPlayer().getLocation();
    10. if(event.getAction() == Action.RIGHT_CLICK_BLOCK&&event.getPlayer().getItemInHand().getType() == Material.LEASH){
    11. if (wolfcount == 0) {
    12. wolfcount++;
    13. System.out.println("Spawned a wolf");
    14. Entity e = getServer().getWorld("world").spawnEntity(spawn, EntityType.WOLF);
    15. Wolf wolf = (Wolf) e;
    16. wolf.setTamed(true);
    17. System.out.println("wolfcount: " + wolfcount);
    18. } else {
    19. Player p = event.getPlayer();
    20. p.sendMessage(ChatColor.AQUA + "You can't have more than one wolf! per game!");
    21. System.out.println("Wolfcount is maximum: " + wolfcount);
    22. }
    23.  
    24. }
    25.  
    26. }


    Mind explaining how I can use a hashmap inside an if statement?
     
  9. Offline

    xTigerRebornx

  10. Offline

    techboy291

    minepress It's probably just your IDE bugging out. Did you try restarting it?
     
  11. Offline

    minepress

    Hey Tiger, Thanks for that. It actually managed to get me further (as in it started to somewhat work).

    Code:java
    1. @EventHandler
    2. //PlayerInteractEvent event
    3. public void onSpawn(PlayerInteractEvent event){
    4.  
    5. for(Player player : Bukkit.getOnlinePlayers()){
    6. wolfcount.put(player.getName(), 0);
    7. }
    8.  
    9. Location spawn = event.getPlayer().getLocation();
    10. if(event.getAction() == Action.RIGHT_CLICK_BLOCK&&event.getPlayer().getItemInHand().getType() == Material.LEASH){
    11. if(wolfcount.containsKey(0)){
    12. //if (map.wolfcount == 0) {
    13. for(Player player : Bukkit.getOnlinePlayers()){
    14. wolfcount.put(player.getName(), 1);
    15. }
    16. System.out.println("wolfcount: " + wolfcount);
    17. System.out.println("Spawned a wolf");
    18. Entity e = getServer().getWorld("world").spawnEntity(spawn, EntityType.WOLF);
    19. Wolf wolf = (Wolf) e;
    20. wolf.setTamed(true);
    21. System.out.println("After wolfcount: " + wolfcount);
    22. } else {
    23. Player p = event.getPlayer();
    24. p.sendMessage(ChatColor.AQUA + "You can't have more than one wolf! per game!");
    25. System.out.println("Wolfcount is maximum: " + wolfcount);
    26. }
    27.  
    28. }
    29.  
    30. }

    Am I right in thinking the for loop actually sets the map for the String value (Of the player) and the number I want it to be (which is "1")
     
  12. Offline

    fireblast709

    minepress Did you just try to increment your HashMap :confused:... Or put values in your int
     
  13. Offline

    minepress


    I was thinking about incrementing the value of the HashMap. But then I decided as I'm only querying whether it is 1 or 0 I could just set it again?

    I thought put() was put(Players, integer Value); ?
     
  14. Offline

    wreed12345

    minepress what does incrementing a hashmap even mean? It is not some type of number
     
  15. Offline

    minepress

    I was under the impression I could use a hashmap as an associated integer with a player. Then increment the Value associated with it.

    Hence <String, Integer>
     
  16. Offline

    fireblast709

    minepress
    • get the value
    • check if it's null. If so, set it to 0
    • increment
    • put it back into the Map
    [addition] You seem to have a very distorted idea about objects. You might want to (re)visit some Java tutorials.
     
  17. Offline

    wreed12345

    minepress didnt your IDE give you some type of error on that one? And where had you seen that you could do that
     
  18. Offline

    minepress

    I think objects have finally just clicked... I understood making new objects but not 100% how they work. I looked over the page again and thought "Well, I'm creating a new hashmap... so I should use the exaamples in the documentation with it!"

    Code:java
    1.  
    2. int wolfcount = 0;
    3. @EventHandler
    4. //PlayerInteractEvent event
    5. public void onSpawn(PlayerInteractEvent event){
    6.  
    7. for(Player player : Bukkit.getOnlinePlayers()){
    8. Hashwolfcount.put(event.getPlayer().toString(), wolfcount);
    9. }
    10.  
    11. Location spawn = event.getPlayer().getLocation();
    12. if(event.getAction() == Action.RIGHT_CLICK_BLOCK&&event.getPlayer().getItemInHand().getType() == Material.LEASH){
    13. if(Hashwolfcount.containsValue(0)){
    14. //if (map.wolfcount == 0)
    15. System.out.println("wolfcount.put(event.getPlayer().toString(), wolfcount");
    16. wolfcount++;
    17. System.out.println("wolfcount.put(event.getPlayer().toString()," + wolfcount);
    18. System.out.println("wolfcount: " + wolfcount);
    19. System.out.println("Spawned a wolf");
    20. Entity e = getServer().getWorld("world").spawnEntity(spawn, EntityType.WOLF);
    21. Wolf wolf = (Wolf) e;
    22. wolf.setTamed(true);
    23. System.out.println("After wolfcount: " + wolfcount);
    24. } else {
    25. Player p = event.getPlayer();
    26. p.sendMessage(ChatColor.AQUA + "You can't have more than one wolf! per game!");
    27. System.out.println("Wolfcount is maximum: " + wolfcount);
    28. }
    29.  
    30. }
    31.  
    32. }


    Wouldn't you know it works :)
     
Thread Status:
Not open for further replies.

Share This Page