Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.mdstv.bukkit.messit;
- import java.io.IOException;
- import java.util.HashMap;
- import org.bukkit.OfflinePlayer;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.event.player.PlayerQuitEvent;
- public class PlayerListener implements Listener {
- private MessIt plugin = null;
- /**
- * 1st Type: The {@link Player} itself<br />
- * 2nd Type: The join time
- */
- private HashMap<Player, Long> playerStack = new HashMap<Player, Long>();
- public PlayerListener(MessIt plugin) {
- this.plugin = plugin;
- }
- @EventHandler
- public void onPlayerJoin(PlayerJoinEvent evt) {
- Player p = evt.getPlayer();
- // List the Player
- if (!this.playerStack.containsKey(p)) {
- this.playerStack.put(p, System.currentTimeMillis());
- }
- }
- @EventHandler
- public void onPlayerQuit(PlayerQuitEvent evt) {
- Player p = evt.getPlayer();
- // Unlist the player and calc time difference
- if (this.playerStack.containsKey(p)) {
- long timeDiff = System.currentTimeMillis() - this.playerStack.get(p);
- this.appendTime(p, timeDiff);
- this.playerStack.remove(p);
- }
- }
- /**
- * Appends the given time to the entire online time of the given {@link Player}
- * @param p The player which online time will be appended
- * @param timeDiff The amount of time (in milliseconds) you want to add.<br />
- * It need to be a positive value!
- */
- private void appendTime(Player p, long timeDiff) {
- if (this.plugin != null) {
- String path = String.format("OnlineTime.%s", p.getName());
- YamlConfiguration timeConf = this.plugin.getTimeConf();
- long currentTime = timeConf.getLong(path, 0L);
- // Append the time
- timeConf.set(path, currentTime + timeDiff);
- try {
- timeConf.save(this.plugin.getTimeConfFile());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * Returns the online time for the given player for the current session<br />
- * <b>Attention: </b>After Server reload the temporary player informations
- * about the online time are <i>destroyed</i>.
- * @param p The target {@link Player}
- * @return The online time for the current session in milliseconds. If the
- * player is not online, 0L will be returned.
- */
- public long getPlayerCurrentOnlineTime(Player p) {
- if (this.playerStack.containsKey(p)) {
- return System.currentTimeMillis() - this.playerStack.get(p);
- } else {
- return 0L;
- }
- }
- /**
- * Returns the entire online time for the given player
- * @param op The target {@link OfflinePlayer}
- * @return The entire online time in milliseconds or 0L, if the player never
- * played on this Server
- */
- public long getPlayerEntireOnlineTime(OfflinePlayer op) {
- return this.plugin.getTimeConf().getLong("OnlineTime." + op.getName(), 0L) + (op.isOnline() ? getPlayerCurrentOnlineTime(op.getPlayer()) : 0L);
- }
- /**
- * Formats the given online time in a humand readable format (German!)
- * @param time The time in milliseconds you want to format
- * @return A formatted String
- */
- public String formatTime(long time) {
- int sec = (int) (time / 1000);
- return String.format("%d:%02d:%02d", sec/3600, (sec%3600)/60, (sec%60));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement