is saving money and ranks in hashmap good method?

Discussion in 'Spigot Plugin Help' started by exglez, Apr 17, 2018.

  1. hi i made my money and ranks plugin and i saved them in hashmap and save in config on disable and enabls is that bad
     
  2. It doesn't matter for small plugins, but as soon as you want to save other things it's going to suck
     
  3. what you mean save other things
     
  4. Bad in what way?
     
  5. Use OOP. Make an User class where you store all info, money ranks etc. And then use the Map to store it (Map<UUID, User> users = new hashmap)
     
  6. Exactly. Use OOP to your advantage.

    Here is an example of a simple PlayerData class.
    You might want to change the Player to UUID and split the static Hashmap and method into appropriate classes. This was just something I quickly plotted down.

    Code (Java):
    public class PlayerData {

        public static HashMap<Player, PlayerData> playerData = new HashMap<>();

        public Player player;
        public double money;
        public Rank rank;

        private PlayerData(Player player) {
            this.player = player;
        }

        public static PlayerData getPlayerData(Player p) {
            if (p == null) return null;
            if (!playerData.containsKey(p)) {
                PlayerData data = new PlayerData(p);
                playerData.put(p, data);
                return data;
            }
            return playerData.get(p);
        }
    }
     
    • Useful Useful x 1
  7. Do not store money in a "double". Never ever do that. If you do not feel ready to do it correctly ussing the currency api. At least use BigDecimal (Here)
     
  8. This is Minecraft...

    But sure that is the correct way.

    Storing it in a double was never the point of my post.
     
  9. Maximvdw

    Benefactor

    so if your server crashes and the ondisable method is never called ->

    "Bob: MY MONEY IS GONE!!! MODDDD MY MONEY IS GONE"
    "*Bob fell off a cliff*"

    Use regularly timed save intervals. Following things you can do
    1) Automatically save all data once it changes
    Big data->Many changes
    would only recommend it for non-flatfile databases
    2) Stage changes
    Keep changes in memory and save at specific intervals. Better
    enough would be to only 'initiate' a save when there are actually changes
     
    • Agree Agree x 1
  10. isn't saving data to config once it changes resource sensitive?
     
  11. Resource intensive* ;)

    Do it at an interval. E.g. every 5 minutes or so. You’ll be fine. I’d go for sql though; not sure saving the configs asynchronous is thread safe.
     
  12. I mean using OOP to store different types of data in a single object.
     
  13. whats oop
     
  14. MiniDigger

    Supporter

    Object Oriented Programming, a core principle of java. if you don't understand, go back to oracles nuts and bolts tutorial...
     
  15. Haha that’s cute, assuming they’ve already been there
     
    • Funny Funny x 1

Share This Page