Getting Rid of Duplicate Entries in a File

Discussion in 'Plugin Development' started by AppleBabies, Nov 18, 2015.

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

    AppleBabies

    Hello everyone. My goal of this plugin is to get every time somebody joins a server, and then update a player count and add it to a file along with the date. Well, I only need one entry per date obviously, which just adds up for that specific day and then resets the next day.

    I don't know how I could reset it, or how I could get rid of duplicate dates and just update the player count for that day. Any help would be much appreciated!
    Code:
    package me.applebabies.counter;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
    
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
        public int playerCount;
        
        
       
    
         public void onEnable(){
             Bukkit.getServer().getPluginManager().registerEvents(this, this);
         }
         public void onDisable(){
            
         }
       
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
                playerCount++;
                 logToFile("Number of players that joined on " + format.format(now) + ": " + playerCount);
    
           
        }
       
        public void logToFile(String message)
        
        {
           
            try
            {
                File dataFolder = getDataFolder();
                if(!dataFolder.exists())
                {
                    dataFolder.mkdir();
                }
                File saveTo = new File(getDataFolder(), "usercount.txt");
                if (!saveTo.exists())
                {
                    saveTo.createNewFile();
                }
             
             
               
              
                FileWriter fw = new FileWriter(saveTo, true);
               
               
                PrintWriter pw = new PrintWriter(fw);
                pw.println(message);
               
              
                pw.flush();
                pw.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
       
       
       
    }
     
  2. Offline

    teej107

  3. Offline

    AppleBabies

  4. Offline

    teej107

    Like what?
     
  5. Offline

    AppleBabies

    @teej107 I want the file to not have duplicate dates. I want it so when there is a duplicate date, it will update the player count, and delete the old duplicate entry. Than, after one day, the player count is reset to 0.
     
  6. Offline

    teej107

    @AppleBabies So you want only one "Number of players that joined on... " in the file at all times?
     
  7. Offline

    AppleBabies

    @teej107 Well, sorta. I want more than one, but only one for each date.
     
  8. Offline

    teej107

    Last edited: Nov 18, 2015
  9. Offline

    AppleBabies

    @teej107 That looks doable. I'll try it tomorrow.
     
  10. @AppleBabies why don't you count till the end of the day how much player there have been and then when the date changes you write a single save-action. this way you wouldn't have to overwrite it over and over again :) hold a map<date, integer> where you save per date a specific amount and when the date changes you save only the current date. this way you can always use the data in your runtime and you got it backed up in your file. for reloads you create a second file which only saves the current amount which is kinda like your harddrive-cache which will survive reloads or shutdowns
     
  11. Offline

    AppleBabies

    @Shmobi Brilliant idea! I will try that!
     
  12. Ofc you also gotta save the date in your harddrive-cache since the server can be shut down during the date-change. or for multiple days. so you should always check between the last recorded date and the current date and fill all other dates either with 0 or maybe even -1 so you can tell that during this time the server was shutdown instead of running. this way you can give even more information
     
  13. Offline

    AppleBabies

    @Shmobi Which is why I think this plugin would be very unstable...I'll try it though.
     
  14. @AppleBabies why unstable?

    1. A Map<Long, Integer> saves for every date the amount of people. (Long because dates in java work with longs. so you can use their long-value to save them and to create them if you want to show them)

    2. The main-textfile contains all dates which are older than the current date. Every time the day changes you save the long-value of the current/old date and the matching integervalue. then you insert the new one into the map and keep counting up that one
    longvalue1:integervalue1
    longvalue2:integervalue2
    ...

    3. If the plugin gets disabled you save the long-value of the current date and the matching integervalue in a seperate file. that file always only has 1 line and is always overwritten. looks same like the example above
    longvalue:integervalue

    4. If the plugin gets enabled it reads all old dates from the main-textfile and adds them with their matching integervalues into the map. then it loads the one date from the seperate file where only one line is in and creates a dateobject out of the long-value. then it checks if the dateobject is of the current day. if so just keep counting. if its older save it in the map, walk through every single day which is between that one and the current one and add the long-value and 0 as the integer-value to the map. then keep going with the current date.

    This works 100%. Only way to destroy the system is to delete the files(which is not how you are supposed to use the plugin, so your fault then not the plugins) or to change the os-date to a previous day. then the cyclus would weird out (also not the fault of the plugin because you broke it by changing the time)

    Everything clear so far?
     
Thread Status:
Not open for further replies.

Share This Page