How to get values ​​from hashmap

Discussion in 'Plugin Development' started by StatywPlay, Aug 26, 2014.

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

    StatywPlay

    My HashMap:
    Code:java
    1. package API.CustomEvents;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.Event;
    5. import org.bukkit.event.HandlerList;
    6.  
    7. import java.util.HashMap;
    8.  
    9. public class MiniEventsJoinEvent extends Event {
    10. private static final HandlerList handlers = new HandlerList();
    11.  
    12. Player player;
    13. String string;
    14. Integer newsize;
    15. Integer getTimeLeft;
    16. HashMap<String, String> players;
    17.  
    18. public MiniEventsJoinEvent(Player player, String string, Integer newsize
    19. , Integer getTimeLeft, HashMap<String, String> players) {
    20. this.player = player;
    21. this.string = string;
    22. this.newsize = newsize;
    23. this.getTimeLeft = getTimeLeft;
    24. this.players = players;
    25. }
    26.  
    27. /**
    28.   * @return the name of the event.
    29.   */
    30. public String getEventName() {
    31. return this.string;
    32. }
    33.  
    34. /**
    35.   * @return the player who joined the event.
    36.   */
    37. public Player getPlayer() {
    38. return this.player;
    39. }
    40.  
    41. /**
    42.   * @return the number of players in the event.
    43.   */
    44. public int getAmountOfPlayersInEvent() {
    45. return newsize;
    46. }
    47.  
    48. /**
    49.   * @return how long until the event auto stops.
    50.   * (seconds)
    51.   */
    52. public int getTimeLeft() {
    53. return getTimeLeft;
    54. }
    55.  
    56. /**
    57.   * @return the HashSet the contains all the player from any event.
    58.   */
    59. public HashMap<String, String> getPlayersInEvent() {
    60. return players;
    61. }
    62.  
    63. public HandlerList getHandlers() {
    64. return handlers;
    65. }
    66.  
    67. public static HandlerList getHandlerList() {
    68. return handlers;
    69. }
    70. }
    71.  


    How to get newsize ​​from hashmap
    These doesnt work
    1. MiniEventsJoinEvent.get("newsize");
     
  2. Offline

    baugh70

    You didn't give it a value. Where you create the HashMap add:

    Code:java
    1. = new HashMap<>();


    After you add this to the end of the line it should work.

    StatywPlay

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

    fireblast709

    baugh70 check his constructor, he assigns it there.
    StatywPlay MiniEventsJoinEvent.get("newsize"); isn't defined in the class. If you want the newsize variable, call getAmountOfPlayersInEvent()
     
  4. Offline

    StatywPlay

    New problem
    Code:java
    1. if (!plugin.getInfo().inevent.size() >= 5) {
    2. plugin.send(player, "full-event");
    3. return true;

    Error: The operator ! is undefined for the argument type(s) int

    File Info
    Code:java
    1. package Util.Methods;
    2.  
    3. import Mains.MiniEvents;
    4. import org.bukkit.configuration.file.FileConfiguration;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.scoreboard.Scoreboard;
    7.  
    8. import java.util.HashMap;
    9. import java.util.HashSet;
    10.  
    11. public class Info{
    12. public MiniEvents plugin;
    13.  
    14.  
    15. public Info(MiniEvents plugin) {
    16. this.plugin = plugin;
    17. }
    18. public boolean eventstarting = false;
    19. public boolean eventstarted = false;
    20. public boolean cancelled = false;
    21. public final HashSet<String> sbefore = new HashSet<>();
    22. public final HashSet<String> sblue = new HashSet<>();
    23. public final HashSet<String> sred = new HashSet<>();
    24. public final HashSet<String> sfire = new HashSet<>();
    25. public final HashMap<String, String> inevent = new HashMap<>();
    26. public final HashMap<String, Scoreboard> scoreboard = new HashMap<>();
    27.  
    28. public int eventSize(){
    29. return inevent.size();
    30. }
    31. public String getEventName(){
    32. return plugin.getEventName();
    33.  
    34. }
    35. public boolean eventStarting(){
    36. return eventstarting;
    37. }
    38. public boolean eventStarted(){
    39. return eventstarted;
    40. }
    41. public boolean inEvent(Player player){
    42. return inevent.containsKey(player.getName());
    43. }
    44. public String eventFormalEventName(String s){
    45. return plugin.getFormalName(s);
    46. }
    47. public int getTimeLeft(){
    48. return plugin.getTimerMain().getTimeLeft();
    49. }
    50. public FileConfiguration getPlayerFile(Player player){
    51. return plugin.playerFile(player.getName().toLowerCase());
    52. }
    53. public HashMap<String, String> getPlayersInEvent(){
    54. return inevent;
    55. }
    56. public boolean inSpectateMode(Player player){
    57. return plugin.getSpectateMode().inspec.contains(player.getName());
    58. }
    59. }
    60.  
    61.  
     
  5. Offline

    fireblast709

    StatywPlay why is that ! there in the first place... It seems logically incorrect regarding to the contents of the block that follows.

    Moreover you should declare your fields private ;3 (with a public accessor if you need access anywhere else)
     
Thread Status:
Not open for further replies.

Share This Page