Advertisement
Guest User

Untitled

a guest
Nov 4th, 2015
25
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.assist.uniquelastseen;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.concurrent.TimeUnit;
  6.  
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.configuration.ConfigurationSection;
  9. import org.bukkit.configuration.file.FileConfiguration;
  10. import org.bukkit.configuration.file.YamlConfiguration;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.event.EventHandler;
  13. import org.bukkit.event.Listener;
  14. import org.bukkit.event.player.PlayerJoinEvent;
  15. import org.bukkit.event.player.PlayerKickEvent;
  16. import org.bukkit.event.player.PlayerQuitEvent;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18.  
  19. public class Main extends JavaPlugin implements Listener {
  20.  
  21.     private File playerFile;
  22.     private YamlConfiguration playerConfiguration;
  23.  
  24.     private String joinMessage;
  25.     private String newJoinMessage;
  26.  
  27.     public void onEnable() {
  28.         saveDefaultConfig();
  29.  
  30.         FileConfiguration config = getConfig();
  31.         joinMessage = ChatColor.translateAlternateColorCodes('&', config.getString("joinMessage", "&eWelcome back, &b{player}&e. Last seen {timeday} day[s], {timehour} hour[s], {timeminute} minute[s] and {timesecond} second[s] ago."));
  32.         newJoinMessage = ChatColor.translateAlternateColorCodes('&', config.getString("newJoinMessage", "&eWelcome, &b{player} &eto our server! We have had &b{unique} &eunique visitors."));
  33.  
  34.         playerFile = new File(getDataFolder(), "players.yml");
  35.  
  36.         if (!playerFile.exists()) {
  37.             try {
  38.                 playerFile.createNewFile();
  39.                 getLogger().info("Created player data file.");
  40.             } catch (IOException e) {
  41.                 getLogger().severe("Failed to create player data file. Disabling plugin...");
  42.                 getServer().getPluginManager().disablePlugin(this);
  43.                 return;
  44.             }
  45.         }
  46.  
  47.         playerConfiguration = YamlConfiguration.loadConfiguration(playerFile);
  48.         getServer().getPluginManager().registerEvents(this, this);
  49.     }
  50.  
  51.     @SuppressWarnings("deprecation")
  52.     public void onDisable() {
  53.         for (Player player : getServer().getOnlinePlayers()) {
  54.             doQuit(player, false);
  55.         }
  56.  
  57.         savePlayerFile();
  58.     }
  59.  
  60.     @EventHandler
  61.     public void onPlayerJoin(PlayerJoinEvent event) {
  62.         Player player = event.getPlayer();
  63.  
  64.         if (!player.hasPlayedBefore()) {
  65.             sendNewPlayer(event, player);
  66.         } else {
  67.             String id = player.getUniqueId().toString();
  68.             ConfigurationSection section;
  69.  
  70.             if (!playerConfiguration.contains(id)) {
  71.                 section = playerConfiguration.createSection(id);
  72.                 section.set("name", player.getName());
  73.                 savePlayerFile();
  74.  
  75.                 sendNewPlayer(event, player);
  76.             } else {
  77.                 section = playerConfiguration.getConfigurationSection(player.getUniqueId().toString());
  78.  
  79.                 long lastQuit = section.getLong("lastQuit");
  80.                 long seconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) - TimeUnit.MILLISECONDS.toSeconds(lastQuit);
  81.  
  82.                 int day = (int) TimeUnit.SECONDS.toDays(seconds);
  83.                 long hour = TimeUnit.SECONDS.toHours(seconds) - (day * 24);
  84.                 long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds) * 60);
  85.                 long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) * 60);
  86.  
  87.                 String message = joinMessage.replace("{player}", player.getName()).replace("{timeday}", String.valueOf(day)).replace("day[s]", "day" + (day == 0 || day > 1 ? "s" : "")).replace("{timehour}", String.valueOf(hour)).replace("hour[s]", "hour" + (hour == 0 || hour > 1 ? "s" : "")).replace("{timeminute}", String.valueOf(minute)).replace("minute[s]", "minute" + (minute == 0 || minute > 1 ? "s" : "")).replace("{timesecond}", String.valueOf(second)).replace("second[s]", "second" + (second == 0 || second > 1 ? "s" : ""));
  88.                 event.setJoinMessage(message);
  89.             }
  90.         }
  91.     }
  92.  
  93.     @SuppressWarnings("deprecation")
  94.     private void sendNewPlayer(PlayerJoinEvent event, Player player) {
  95.         int uniquePlayers = getServer().getOfflinePlayers().length + getServer().getOnlinePlayers().length;
  96.         event.setJoinMessage(newJoinMessage.replace("{player}", player.getName()).replace("{unique}", Integer.toString(uniquePlayers)));
  97.     }
  98.  
  99.     @EventHandler
  100.     public void onQuit(PlayerQuitEvent event) {
  101.         doQuit(event.getPlayer(), true);
  102.     }
  103.  
  104.     @EventHandler
  105.     public void onKick(PlayerKickEvent event) {
  106.         doQuit(event.getPlayer(), true);
  107.     }
  108.  
  109.     private void doQuit(Player player, boolean save) {
  110.         String id = player.getUniqueId().toString();
  111.         ConfigurationSection section;
  112.  
  113.         if (!playerConfiguration.contains(id)) {
  114.             section = playerConfiguration.createSection(id);
  115.             section.set("name", player.getName());
  116.         } else {
  117.             section = playerConfiguration.getConfigurationSection(id);
  118.         }
  119.  
  120.         section.set("lastQuit", System.currentTimeMillis());
  121.  
  122.         if (save) {
  123.             savePlayerFile();
  124.         }
  125.     }
  126.  
  127.     private void savePlayerFile() {
  128.         try {
  129.             playerConfiguration.save(playerFile);
  130.         } catch (IOException e) {
  131.             getLogger().warning("Failed to save player data file:");
  132.             e.printStackTrace();
  133.         }
  134.     }
  135. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement