Advertisement
morphesus

MessIt PlayerListener

Apr 24th, 2013
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package de.mdstv.bukkit.messit;
  2.  
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5.  
  6. import org.bukkit.OfflinePlayer;
  7. import org.bukkit.configuration.file.YamlConfiguration;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.EventHandler;
  10. import org.bukkit.event.Listener;
  11. import org.bukkit.event.player.PlayerJoinEvent;
  12. import org.bukkit.event.player.PlayerQuitEvent;
  13.  
  14. public class PlayerListener implements Listener {
  15.    
  16.     private MessIt plugin = null;
  17.    
  18.     /**
  19.      * 1st Type: The {@link Player} itself<br />
  20.      * 2nd Type: The join time
  21.      */
  22.     private HashMap<Player, Long> playerStack = new HashMap<Player, Long>();
  23.    
  24.     public PlayerListener(MessIt plugin) {
  25.         this.plugin = plugin;
  26.     }
  27.  
  28.     @EventHandler
  29.     public void onPlayerJoin(PlayerJoinEvent evt) {
  30.         Player p = evt.getPlayer();
  31.        
  32.         // List the Player
  33.         if (!this.playerStack.containsKey(p)) {
  34.             this.playerStack.put(p, System.currentTimeMillis());
  35.         }
  36.     }
  37.    
  38.     @EventHandler
  39.     public void onPlayerQuit(PlayerQuitEvent evt) {
  40.         Player p = evt.getPlayer();
  41.        
  42.         // Unlist the player and calc time difference
  43.         if (this.playerStack.containsKey(p)) {
  44.             long timeDiff = System.currentTimeMillis() - this.playerStack.get(p);
  45.             this.appendTime(p, timeDiff);
  46.            
  47.             this.playerStack.remove(p);
  48.         }
  49.     }
  50.    
  51.     /**
  52.      * Appends the given time to the entire online time of the given {@link Player}
  53.      * @param p The player which online time will be appended
  54.      * @param timeDiff The amount of time (in milliseconds) you want to add.<br />
  55.      * It need to be a positive value!
  56.      */
  57.     private void appendTime(Player p, long timeDiff) {
  58.         if (this.plugin != null) {
  59.             String            path        = String.format("OnlineTime.%s", p.getName());
  60.             YamlConfiguration timeConf    = this.plugin.getTimeConf();
  61.             long              currentTime = timeConf.getLong(path, 0L);
  62.            
  63.             // Append the time
  64.             timeConf.set(path, currentTime + timeDiff);
  65.            
  66.             try {
  67.                 timeConf.save(this.plugin.getTimeConfFile());
  68.             } catch (IOException e) {
  69.                 e.printStackTrace();
  70.             }
  71.         }
  72.     }
  73.    
  74.     /**
  75.      * Returns the online time for the given player for the current session<br />
  76.      * <b>Attention: </b>After Server reload the temporary player informations
  77.      * about the online time are <i>destroyed</i>.
  78.      * @param p The target {@link Player}
  79.      * @return The online time for the current session in milliseconds. If the
  80.      * player is not online, 0L will be returned.
  81.      */
  82.     public long getPlayerCurrentOnlineTime(Player p) {
  83.         if (this.playerStack.containsKey(p)) {
  84.             return System.currentTimeMillis() - this.playerStack.get(p);
  85.         } else {
  86.             return 0L;
  87.         }
  88.     }
  89.    
  90.     /**
  91.      * Returns the entire online time for the given player
  92.      * @param op The target {@link OfflinePlayer}
  93.      * @return The entire online time in milliseconds or 0L, if the player never
  94.      * played on this Server
  95.      */
  96.     public long getPlayerEntireOnlineTime(OfflinePlayer op) {
  97.         return this.plugin.getTimeConf().getLong("OnlineTime." + op.getName(), 0L) + (op.isOnline() ? getPlayerCurrentOnlineTime(op.getPlayer()) : 0L);
  98.     }
  99.    
  100.     /**
  101.      * Formats the given online time in a humand readable format (German!)
  102.      * @param time The time in milliseconds you want to format
  103.      * @return A formatted String
  104.      */
  105.     public String formatTime(long time) {
  106.         int sec = (int) (time / 1000);
  107.         return String.format("%d:%02d:%02d", sec/3600, (sec%3600)/60, (sec%60));
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement