Using Hashsets

Discussion in 'Plugin Development' started by FlareLine, Sep 20, 2013.

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

    FlareLine

    Hello everyone.

    I am currently creating a plugin that includes several values that need to be stored in some sort of list for each player. These values could include:
    • isingame (boolean)
    • isinlobby (boolean)
    • totalpoints (int)
    • hasvipitem (boolean)
    • totalwins(int)
    • totaldraw (int)
    • totalloss (int)
    Then, if isingame is true and isinlobby is false, assign these values:
    • arenaid (int)
    • teamid (int)
    • gamehits (int)
    • enemyblockcap (int)
    • neutralblockcap (int)
    • startblockcap (int)
    Assigning these values during games is easy enough, however I'm not sure which to go with, Hashmaps, Arraylists or Hashsets.
    Could someone please enlighten me on these topics and how I would go about creating each, as well as which one is best for my current situation.

    A link to an easy to understand sourcecode of a plugin may be better than explaining, as I learn best from example.
     
  2. Offline

    MCForger

    FlareLine
    You could make a HashSet with a custom player object you make yourself to keep track of stats.
    So like HashSet<ArenaPlayer>
     
  3. Offline

    The_Doctor_123

    I would recommend creating some sort of game class and putting methods in like, for example, inGame(Player).
     
  4. Offline

    FlareLine

    MCForger So to make two Hashsets for each player that has joined, you would do the following?
    Code:java
    1. //...Player join event...
    2. player.toString(playername);
    3. HashSet<playername> = new HashSet<String, String>;
    4. playername.put("isinarena", "false");
    5. //blahblahblah make another HashSet...

    The_Doctor_123 Ah I did not see your post...
    By creating a class, you mean the methods to execute at a certain time, such as when the player joins an arena, update isinarena, etc. in the HashSet? Edit: Like a player listener

    Are you required to assign a null value to a variable in the HashSet before 'put'ting values to it via another method?

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

    The_Doctor_123

    FlareLine
    Typically, when you create a minigame you might have a class called "Game" or something like that. That class will store all the data and have methods and such to process/get/add to/subtract to the data. So in that class, you'll want methods that can get information about a particular player.

    I don't think HashSets are the best idea.. they're used in favor of performance so they lack methods and such.
     
  6. Offline

    FlareLine

  7. Offline

    The_Doctor_123

    FlareLine
    Well, nothing, because I believe you should create that Game class just to make it easier to code your game overall. But if you don't want to do that, HashMaps would be your best bet.
     
  8. Offline

    FlareLine

    The_Doctor_123 How would you store such variables in another class when there are multiple values for multiple things?
     
  9. Offline

    The_Doctor_123

    FlareLine
    If you know enough about OOP, it's quite simple. But here's the basics of how that would work:

    Game class:

    Code:java
    1. import java.util.ArrayList;
    2. import java.util.HashMap;
    3.  
    4. import org.bukkit.entity.Player;
    5.  
    6. public class Game
    7. {
    8. private WhateverYourMainClassIs plugin;
    9.  
    10. private ArrayList<Player> Players = new ArrayList<Player>();
    11.  
    12. private HashMap<Player, HashMap<String, Integer>> PlayerData = new HashMap<Player, HashMap<String, Integer>>();
    13.  
    14. public Game(WhateverYourMainClassIs instance)
    15. {
    16. plugin = instance;
    17. }
    18.  
    19. public void addPlayer(Player player)
    20. {
    21. Players.add(player);
    22. }
    23.  
    24. public void removePlayer(Player player)
    25. {
    26. Players.remove(player);
    27. }
    28.  
    29. public boolean inGame(Player player)
    30. {
    31. if (Players.contains(player))
    32. {
    33. return true;
    34. }
    35. return false;
    36. }
    37. }


    Main class:

    Code:java
    1. import org.bukkit.Bukkit;
    2.  
    3. public class WhateverYourMainClassIs
    4. {
    5. public ArrayList<Game> Games = new ArrayList<Game>();
    6.  
    7. @Override
    8. public void onEnable()
    9. {
    10. Games.add(new Game(this));
    11.  
    12. Games.get(0).addPlayer(Bukkit.getPlayer("lol.. don't use this, just showin' you the point"));
    13. }
    14. }


    So that's an example of how the Game class is used.
     
  10. Offline

    FlareLine

    @The_Doctor_123 Thanks for that insight, I'll see how I go! :)

    So understanding what you have said I'll need Classes:
    • Main Class
    • Player Listener
    • Game Class
    • (Perhaps) BlockListener
     
  11. Offline

    The_Doctor_123

    FlareLine
    Nowadays, you can have all types of Listeners in one class. But, it's still good for organization if that's what you'd like.
     
  12. Offline

    FlareLine

    @The_Doctor_123 Correct me if I am wrong, but on first login, a player would be assigned an Arraylist, and obviously given default values, and the Command listener would update the list (When a game is in progress or joining etc.), passing the new values to the player listener?
     
  13. Offline

    The_Doctor_123

    FlareLine
    That would have nothing to do with first logging in. It's when a player joins the game you would set some default values.
     
  14. Offline

    FlareLine

    Sorry for not explaining it correctly, silly me...
    When a player first logs in, they are assigned default values in a default Arraylist, as this list is used for other things such as displaying highscores and shop balances etc. The arraylist values are 'got' by these events, and thus it needs to be accessible outside of a game. The only time the values will change are when the player is ingame, or when the player purchases something from the shop.
    Going on your idea of performance, the reason I thought HashSets would be better is because during the games, some of the values will be changing quite rapidly, and for more than one player at a time.
    I will still attempt your example of assigning the lists. :)
     
  15. Offline

    The_Doctor_123

    FlareLine
    Let me ask you this, do these values carry over from server restarts or are they just per session?

    Oh, the hit of performance would be incredibly tiny for what you're doing. There's got to be a whole lot of changing around to make a bit of difference.
     
  16. Offline

    FlareLine

    @The_Doctor_123 The values are carried over, because they are lifetime values (Except when reset using a command somehow).
     
  17. Offline

    The_Doctor_123

    FlareLine
    Oh, well, do you have a plan for storing that data?
     
  18. Offline

    FlareLine

    Flatfile until I can implement MySQL. Storing the data into the files is no problem. I think I can achieve that.
     
  19. Offline

    The_Doctor_123

    FlareLine
    In that case, store the data in your main class and reference from that in your Game class. Also, how many games are going on at once?
     
  20. Offline

    FlareLine

    One for now, I can work on extra, unnecessarily integral features later on in development. Would you be interested in helping me throughout this project? I'd be happy to reference you in someway as a big help.

    Would it just be easier writing each change to the file straight away? I thought to improve it a little, you could write the data each time a game ends?

    The_Doctor_123 Using a flat file to store data for each player would be very, storage intensive? You do agree that MySQL is the way to go?

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

    The_Doctor_123

    Sure, we can discuss on Skype if you'd like.

    Personally, I like flat files better than MySQL just because.. I don't like MySQL. :p
     
  22. Offline

    FlareLine

    The_Doctor_123 Sure. Files containing around 20 lines shouldn't take up that much space.
     
Thread Status:
Not open for further replies.

Share This Page