Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.mrkirby153.KCNerfer.playTime;
- import me.mrkirby153.KCNerfer.KCNerfer;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.nbt.CompressedStreamTools;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraftforge.common.DimensionManager;
- import java.io.*;
- import java.util.*;
- public class PlayTimeHandler {
- private static HashMap<String, Long> lastTime = new HashMap<String, Long>();
- private static NBTTagCompound playtimeData = new NBTTagCompound();
- //TODO: Fix concurrent modification exception with more than one player online http://pastebin.com/eumXHvxg
- public static void savePlaytime(String playerName, boolean save) {
- NBTTagCompound currentData = playtimeData.getCompoundTag(playerName);
- int currentPlaytime = currentData.getInteger("playtime");
- long timeNow = System.currentTimeMillis();
- long timeThen = lastTime.get(playerName);
- int timePlayed = (int) Math.floor((timeNow - timeThen) / 1000);
- currentPlaytime += timePlayed;
- currentData.setInteger("playtime", currentPlaytime);
- playtimeData.setTag(playerName, currentData);
- lastTime.remove(playerName);
- lastTime.put(playerName, System.currentTimeMillis());
- if (save)
- saveNBT();
- }
- public static void savePlaytime(String playerName){
- savePlaytime(playerName, true);
- }
- public static void savePlaytime() {
- Set<String> players = lastTime.keySet();
- for (String s : players) {
- savePlaytime(s, false);
- }
- saveNBT();
- }
- public static void playerLogin(EntityPlayer player) {
- lastTime.put(player.getCommandSenderName(), System.currentTimeMillis());
- }
- public static void playerLogout(EntityPlayer player) {
- savePlaytime(player.getCommandSenderName());
- lastTime.remove(player.getCommandSenderName());
- }
- public static int getPlaytime(String playerName) {
- NBTTagCompound currentData = playtimeData.getCompoundTag(playerName);
- int currentPlaytime = currentData.getInteger("playtime");
- long timeNow = System.currentTimeMillis();
- if (lastTime.containsKey(playerName)) {
- long timeThen = lastTime.get(playerName);
- int timePlayed = (int) Math.floor((timeNow - timeThen) / 1000);
- currentPlaytime += timePlayed;
- }
- return currentPlaytime;
- }
- public static ArrayList<String> leaderboard() {
- // Sort the leaderboard
- savePlaytime();
- TreeMap<Integer, String> sortedMap = new TreeMap<Integer, String>();
- for (String s : getAllTracked()) {
- sortedMap.put(getPlaytime(s), s);
- }
- ArrayList<String> leaderboard = new ArrayList<String>();
- for (Map.Entry<Integer, String> e : sortedMap.entrySet()) {
- int playTime = e.getKey();
- String playerName = e.getValue();
- leaderboard.add(playerName);
- }
- Collections.reverse(leaderboard);
- return leaderboard;
- }
- public static void resetPlaytime() {
- for (String s : getAllTracked()) {
- resetPlaytime(s);
- }
- }
- public static ArrayList<String> getAllTracked() {
- Set list = playtimeData.func_150296_c();
- ArrayList<String> string = new ArrayList<String>();
- for (Object o : list) {
- if (o instanceof String) {
- String playerName = (String) o;
- string.add(playerName);
- }
- }
- return string;
- }
- public static void resetPlaytime(String playerName) {
- NBTTagCompound currentData = playtimeData.getCompoundTag(playerName);
- currentData.setInteger("playtime", 0);
- playtimeData.setTag(playerName, currentData);
- saveNBT();
- }
- private static void saveNBT() {
- File nbtFile = new File(DimensionManager.getCurrentSaveRootDirectory().getAbsolutePath() + File.separator + "playtime.dat");
- if (!nbtFile.exists()) {
- new File(nbtFile.getParent()).mkdirs();
- try {
- nbtFile.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- try {
- CompressedStreamTools.writeCompressed(playtimeData, new FileOutputStream(nbtFile));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void loadNBT() {
- File nbtFile = new File(DimensionManager.getCurrentSaveRootDirectory().getAbsolutePath() + File.separator + "playtime.dat");
- if (!nbtFile.exists()) {
- new File(nbtFile.getParent()).mkdirs();
- try {
- nbtFile.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- try {
- playtimeData = CompressedStreamTools.readCompressed(new FileInputStream(nbtFile));
- } catch (EOFException ex) {
- KCNerfer.logger.error("Error reading nbt. caught EOF");
- ex.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment