Solved Bukkit Individual User Data

Discussion in 'Plugin Development' started by AngryCupcake274, Jul 26, 2015.

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

    AngryCupcake274

    Hello all,
    I am creating a plugin that is storing individual user data. I have set up a per-user file system, but as I am training to work in the IT field, I know that that is very slow, trying to read and write files 10 times a second. Is there a way to still keep the file system in place, but mess with local variables and save them every few minutes, keeping stress off the server?
     
  2. Offline

    khave

    Make your own custom object with all the values you want and store them in a Map.
     
  3. Offline

    Airbornz

    You could do something like this to store data, then use bukkit runnable every minute or so to save such data.
    Code:
    public class PlayerData {
    
        private static List<PlayerData> data = new ArrayList<PlayerData>();
       
        private UUID uuid;
        private List<String> someotherfield;
        private int somerandomint;
       
        public PlayerData(UUID uuid, List<String> arg0, int arg1){
            this.uuid = uuid;
            someotherfield = arg0;
            somerandomint = arg1;
            data.add(this);
        }
       
        public UUID getUUID(){
            return uuid;
        }
       
        public List<String> getSomething(){
            return someotherfield;
        }
       
        public int getInt(){
            return somerandomint;
        }
       
        public static List<PlayerData> getData(){
            return data;
        }
    }
     
  4. Offline

    AngryCupcake274

    I'm new to HashMaps, does anyone know of a good tutorial that focuses on Bukkit examples? I'm not asking for code, I'm asking for a tutorial or link to one.
     
  5. Offline

    khave

    Try using these videos for reference:
    - PogoStick29Dev
    - TheBCBroz
     
  6. Offline

    AngryCupcake274

    @khave thank you so much! Will watch these and report back!
     
  7. Offline

    567legodude

    @AngryCupcake274 Whenever I have config data like this, I always load it on startup, store it locally, and don't save it until the plugin is disabled. So the only times you write to the file is when your plugin is enabled/disabled.
     
  8. Offline

    AngryCupcake274

  9. Offline

    mythbusterma

    @AngryCupcake274

    Code:
    FileConfiguration playerFile = new FileConfiguration(new File(<directory> + uuid.toString()));
    
    Read and write from the Object as you please, and only save it when the server shuts down (or every few minutes, if you're concerned with crashes).

    Yes, it really is that easy.

    The FileConfiguration Objects are backed by HashMaps in memory, so you don't have to worry about reading and writing to them, because the changes won't be saved until a call to FileConfiguration#save() (or whatever it's called).

    Also, the best tutorial I've ever seen on Maps: https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html

    @khave

    Really? You're going to link to that insolent non-sense?
     
  10. Offline

    AngryCupcake274

    @mythbusterma First of all, thank you for this *real* link. I started out learning Atari BASIC, so I'm used to using only arrays inside of arrays. So with these examples, I could create a HashMap with values like ([playerUUID], new HashMap<id, value>)? It's like arrays inside of arrays, but BETTER!
     
  11. Offline

    Eos

    Avoid watching youtube videos, you'll see significant results in your java skills if you avoid watching videos. Better to learn it yourself.
     
  12. Offline

    khave

    @mythbusterma Hmm I'm sorry. I did not watch the tutorials, but after watching them I can see what you mean. Should have linked to the Java Docs like you did.

    @AngryCupcake274 As mythbusterma just said, you do not even need to store the values in-memory if you're using a file sytem. It would be redundant as far as I can understand.
     
  13. Offline

    AngryCupcake274

    @mythbusterma @khave it seems like running a localhost server makes it lag, but running on a dedicated machine works better, even without SSDs.

    @Eos I learned Java off a book, and it seemed like I understood it better and learned it a lot easier after trying videos.
     
  14. Offline

    mythbusterma

    @AngryCupcake274

    It shouldn't lag at all as long as you're only saving when the server closes.
     
  15. Offline

    AngryCupcake274

    @mythbusterma It's saving 10 times a second with virtually no lag...I'll have to test it on a server with more people...
     
  16. Offline

    mythbusterma

    @AngryCupcake274

    Don't save the data that often and you'll never have to worry about it. Save the data that often and waste disk I/O.

    I don't see why this is a difficult decision.
     
  17. Offline

    AngryCupcake274

    @mythbusterma I've already decided to save it not so often, what I was saying was that I wasn't having any trouble on a separate server. I know that with more people, it will start to lag. I'm going to use the HashMaps with Arrays and stuffs inside of it.
     
  18. Offline

    mythbusterma

    @AngryCupcake274

    Why don't you just use the FileConfiguration object without making HashMaps and arrays etc.? It's far simpler to just read and write from that object than it is to create a mess of nested Collections (also, as a general rule, Collections and arrays to not get along).
     
  19. Offline

    AngryCupcake274

    @mythbusterma I am using YamlConfiguration for this. Here is my logic for if I were to use nested Maps:
    Code:
    dataMap = new HashMap<String, Map<String, Object>>();
    tempMap = new HashMap<String, Object>();
    
    player_1:
        tempMap.put("lastlogin.date", pcp.getInteger(p, "lastlogin.date"));
        tempMap.put("lastlogin.time", pcp.getInteger(p, "lastlogin.time"));
        tempMap.put("pvptimer", pcp.getDouble(p, "pvptimer"));
        tempMap.put("enderpearl", pcp.getDouble(p, "enderpearl"));
        tempMap.put("combattag", pcp.getDouble(p, "combattag"));
        tempMap.put("kit", pcp.getString(p, "kit"));
        tempMap.put("energy", pcp.getDouble(p, "energy"));
        tempMap.put("archermark", pcp.getDouble(p, "archermark"));
       
        plugin.dataMap.put(p.getUniqueId().toString(), tempMap);
    
    player_2:
        tempMap.put("lastlogin.date", pcp.getInteger(p, "lastlogin.date"));
        tempMap.put("lastlogin.time", pcp.getInteger(p, "lastlogin.time"));
        tempMap.put("pvptimer", pcp.getDouble(p, "pvptimer"));
        tempMap.put("enderpearl", pcp.getDouble(p, "enderpearl"));
        tempMap.put("combattag", pcp.getDouble(p, "combattag"));
        tempMap.put("kit", pcp.getString(p, "kit"));
        tempMap.put("energy", pcp.getDouble(p, "energy"));
        tempMap.put("archermark", pcp.getDouble(p, "archermark"));
       
        plugin.dataMap.put(p.getUniqueId().toString(), tempMap);
    
    get player_1 info:
        (Integer/Double/String) (plugin.dataMap.get("[player_1 UUID]")).get("lastlogin.date"); -
            - returns (pcp.getDouble(p, "lastlogin.date"))
            - returns YYYY/MM/DD lastlogin for player
    I know it's long....just a theory...
     
  20. Offline

    AngryCupcake274

    @mythbusterma I ended up using the Map and saving every 5 minutes. No more lag! Thank you all.

    NOTE: I got this working on my first try. THAT NEVER HAPPENS!
     
Thread Status:
Not open for further replies.

Share This Page