mrkirby153

Untitled

Mar 9th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.21 KB | None | 0 0
  1. package me.mrkirby153.KCNerfer.playTime;
  2.  
  3. import me.mrkirby153.KCNerfer.KCNerfer;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.nbt.CompressedStreamTools;
  6. import net.minecraft.nbt.NBTTagCompound;
  7. import net.minecraftforge.common.DimensionManager;
  8.  
  9. import java.io.*;
  10. import java.util.*;
  11.  
  12. public class PlayTimeHandler {
  13.  
  14.     private static HashMap<String, Long> lastTime = new HashMap<String, Long>();
  15.  
  16.     private static NBTTagCompound playtimeData = new NBTTagCompound();
  17.  
  18.     //TODO: Fix concurrent modification exception with more than one player online http://pastebin.com/eumXHvxg
  19.     public static void savePlaytime(String playerName, boolean save) {
  20.         NBTTagCompound currentData = playtimeData.getCompoundTag(playerName);
  21.         int currentPlaytime = currentData.getInteger("playtime");
  22.         long timeNow = System.currentTimeMillis();
  23.         long timeThen = lastTime.get(playerName);
  24.         int timePlayed = (int) Math.floor((timeNow - timeThen) / 1000);
  25.         currentPlaytime += timePlayed;
  26.         currentData.setInteger("playtime", currentPlaytime);
  27.         playtimeData.setTag(playerName, currentData);
  28.         lastTime.remove(playerName);
  29.         lastTime.put(playerName, System.currentTimeMillis());
  30.         if (save)
  31.             saveNBT();
  32.     }
  33.  
  34.     public static void savePlaytime(String playerName){
  35.         savePlaytime(playerName, true);
  36.     }
  37.  
  38.     public static void savePlaytime() {
  39.         Set<String> players = lastTime.keySet();
  40.         for (String s : players) {
  41.             savePlaytime(s, false);
  42.         }
  43.         saveNBT();
  44.     }
  45.  
  46.     public static void playerLogin(EntityPlayer player) {
  47.         lastTime.put(player.getCommandSenderName(), System.currentTimeMillis());
  48.     }
  49.  
  50.     public static void playerLogout(EntityPlayer player) {
  51.         savePlaytime(player.getCommandSenderName());
  52.         lastTime.remove(player.getCommandSenderName());
  53.     }
  54.  
  55.     public static int getPlaytime(String playerName) {
  56.         NBTTagCompound currentData = playtimeData.getCompoundTag(playerName);
  57.         int currentPlaytime = currentData.getInteger("playtime");
  58.         long timeNow = System.currentTimeMillis();
  59.         if (lastTime.containsKey(playerName)) {
  60.             long timeThen = lastTime.get(playerName);
  61.             int timePlayed = (int) Math.floor((timeNow - timeThen) / 1000);
  62.             currentPlaytime += timePlayed;
  63.         }
  64.         return currentPlaytime;
  65.     }
  66.  
  67.     public static ArrayList<String> leaderboard() {
  68.         // Sort the leaderboard
  69.         savePlaytime();
  70.         TreeMap<Integer, String> sortedMap = new TreeMap<Integer, String>();
  71.         for (String s : getAllTracked()) {
  72.             sortedMap.put(getPlaytime(s), s);
  73.         }
  74.         ArrayList<String> leaderboard = new ArrayList<String>();
  75.         for (Map.Entry<Integer, String> e : sortedMap.entrySet()) {
  76.             int playTime = e.getKey();
  77.             String playerName = e.getValue();
  78.             leaderboard.add(playerName);
  79.         }
  80.         Collections.reverse(leaderboard);
  81.         return leaderboard;
  82.     }
  83.  
  84.     public static void resetPlaytime() {
  85.         for (String s : getAllTracked()) {
  86.             resetPlaytime(s);
  87.         }
  88.     }
  89.  
  90.     public static ArrayList<String> getAllTracked() {
  91.         Set list = playtimeData.func_150296_c();
  92.         ArrayList<String> string = new ArrayList<String>();
  93.         for (Object o : list) {
  94.             if (o instanceof String) {
  95.                 String playerName = (String) o;
  96.                 string.add(playerName);
  97.             }
  98.         }
  99.         return string;
  100.     }
  101.  
  102.     public static void resetPlaytime(String playerName) {
  103.         NBTTagCompound currentData = playtimeData.getCompoundTag(playerName);
  104.         currentData.setInteger("playtime", 0);
  105.         playtimeData.setTag(playerName, currentData);
  106.         saveNBT();
  107.     }
  108.  
  109.     private static void saveNBT() {
  110.         File nbtFile = new File(DimensionManager.getCurrentSaveRootDirectory().getAbsolutePath() + File.separator + "playtime.dat");
  111.         if (!nbtFile.exists()) {
  112.             new File(nbtFile.getParent()).mkdirs();
  113.             try {
  114.                 nbtFile.createNewFile();
  115.             } catch (IOException e) {
  116.                 e.printStackTrace();
  117.             }
  118.         }
  119.         try {
  120.             CompressedStreamTools.writeCompressed(playtimeData, new FileOutputStream(nbtFile));
  121.         } catch (IOException e) {
  122.             e.printStackTrace();
  123.         }
  124.     }
  125.  
  126.     public static void loadNBT() {
  127.         File nbtFile = new File(DimensionManager.getCurrentSaveRootDirectory().getAbsolutePath() + File.separator + "playtime.dat");
  128.         if (!nbtFile.exists()) {
  129.             new File(nbtFile.getParent()).mkdirs();
  130.             try {
  131.                 nbtFile.createNewFile();
  132.             } catch (IOException e) {
  133.                 e.printStackTrace();
  134.             }
  135.         }
  136.         try {
  137.             playtimeData = CompressedStreamTools.readCompressed(new FileInputStream(nbtFile));
  138.         } catch (EOFException ex) {
  139.             KCNerfer.logger.error("Error reading nbt. caught EOF");
  140.             ex.printStackTrace();
  141.         } catch (IOException e) {
  142.             e.printStackTrace();
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment