turt2live

Untitled

Dec 8th, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.42 KB | None | 0 0
  1. package mea.Logger;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.text.DateFormat;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15. import java.util.Vector;
  16.  
  17. import mea.plugin.Loader;
  18. import mea.plugin.MultiFunction;
  19.  
  20. import org.bukkit.entity.Player;
  21. import org.bukkit.plugin.java.JavaPlugin;
  22. import org.bukkit.util.config.Configuration;
  23.  
  24. @SuppressWarnings("deprecation")
  25. public class MeaLogger {
  26.    
  27.     private JavaPlugin plugin;
  28.     private Loader loader;
  29.    
  30.     private File meaConfiguration;
  31.     @SuppressWarnings("unused")
  32.     private Configuration configuration;
  33.    
  34.     private File log;
  35.     private File meaChatLog;
  36.     private File meaShopLog;
  37.     private File meaEconomyLog;
  38.     private File meaExternalLog;
  39.    
  40.     private File logDirectory;
  41.     @SuppressWarnings("unused")
  42.     private File meaChatLogDirectory;
  43.     @SuppressWarnings("unused")
  44.     private File meaShopLogDirectory;
  45.     @SuppressWarnings("unused")
  46.     private File meaEconomyLogDirectory;
  47.     @SuppressWarnings("unused")
  48.     private File meaExternalLogDirectory;
  49.    
  50.     @SuppressWarnings("unused")
  51.     private File meaAPILocation;
  52.     @SuppressWarnings("unused")
  53.     private File meaBroadcasterLocation;
  54.     @SuppressWarnings("unused")
  55.     private File meaChatLocation;
  56.     @SuppressWarnings("unused")
  57.     private File meaEconomyLocation;
  58.     @SuppressWarnings("unused")
  59.     private File meaEconomyAPILocation;
  60.     @SuppressWarnings("unused")
  61.     private File meaExternalLocation;
  62.     @SuppressWarnings("unused")
  63.     private File meaFlarfLocation;
  64.     @SuppressWarnings("unused")
  65.     private File meaFreezerLocation;
  66.     @SuppressWarnings("unused")
  67.     private File meaGoodiesLocation;
  68.     @SuppressWarnings("unused")
  69.     private File meaGraveyardLocation;
  70.     @SuppressWarnings("unused")
  71.     private File meaGreylisterLocation;
  72.     @SuppressWarnings("unused")
  73.     private File meaGUILocation;
  74.     @SuppressWarnings("unused")
  75.     private File meaHookLocation;
  76.     @SuppressWarnings("unused")
  77.     private File meaLoggerLocation;
  78.     @SuppressWarnings("unused")
  79.     private File meaLotteryLocation;
  80.     @SuppressWarnings("unused")
  81.     private File meaMathLocation;
  82.     @SuppressWarnings("unused")
  83.     private File meaPluginLocation;
  84.     @SuppressWarnings("unused")
  85.     private File meaRandomTPLocation;
  86.     @SuppressWarnings("unused")
  87.     private File meaSamplesLocation;
  88.     @SuppressWarnings("unused")
  89.     private File meaShopLocation;
  90.     @SuppressWarnings("unused")
  91.     private File meaSQLLocation;
  92.     @SuppressWarnings("unused")
  93.     private File meaLocation;
  94.  
  95.     private URL pastebinURL;
  96.     private URL emailSenderURL;
  97.    
  98.     private String meaSuite;
  99.    
  100.     public MeaLogger(JavaPlugin plugin, Loader loader){
  101.         this.plugin = plugin;
  102.         this.loader = loader;
  103.     }
  104.    
  105.     public void startup(){
  106.         meaConfiguration = new File(plugin.getDataFolder()+"/config.yml");
  107.         configuration = plugin.getConfiguration();
  108.        
  109.         meaSuite = "meaSuite is recorded as \""+plugin.getDescription().getFullName()+"\" build number \""+loader.version+"\".";
  110.        
  111.         log = new File(plugin.getDataFolder()+"/meaLogger/log.txt");
  112.         meaChatLog = new File(plugin.getDataFolder()+"/meaChat/log.txt");
  113.         meaShopLog = new File(plugin.getDataFolder()+"/meaShop/logs/transactions.txt");
  114.         meaEconomyLog = new File(plugin.getDataFolder()+"/meaEconomy/logs/transactions.txt");
  115.         meaExternalLog = new File(plugin.getDataFolder()+"/meaLogger/data_transfer.txt");
  116.        
  117.         logDirectory = new File(plugin.getDataFolder()+"/meaLogger/");
  118.         meaChatLogDirectory = new File(plugin.getDataFolder()+"/meaChat/");
  119.         meaShopLogDirectory = new File(plugin.getDataFolder()+"/meaShop/logs/");
  120.         meaEconomyLogDirectory = new File(plugin.getDataFolder()+"/meaEconomy/logs/");
  121.         meaExternalLogDirectory = new File(plugin.getDataFolder()+"/meaLogger/");
  122.        
  123.         meaAPILocation = new File(plugin.getDataFolder()+"/meaAPI/");
  124.         meaBroadcasterLocation = new File(plugin.getDataFolder()+"/meaBroadcaster/");
  125.         meaChatLocation = new File(plugin.getDataFolder()+"/meaChat/");
  126.         meaEconomyLocation = new File(plugin.getDataFolder()+"/meaEconomy/");
  127.         meaEconomyAPILocation = new File(plugin.getDataFolder()+"/meaEconomyAPI/");
  128.         meaExternalLocation = new File(plugin.getDataFolder()+"/meaExternal/");
  129.         meaFlarfLocation = new File(plugin.getDataFolder()+"/meaFlarf/");
  130.         meaFreezerLocation = new File(plugin.getDataFolder()+"/meaFreezer/");
  131.         meaGoodiesLocation = new File(plugin.getDataFolder()+"/meaGoodies/");
  132.         meaGraveyardLocation = new File(plugin.getDataFolder()+"/meaGraveyard/");
  133.         meaGreylisterLocation = new File(plugin.getDataFolder()+"/meaGreylister/");
  134.         meaGUILocation = new File(plugin.getDataFolder()+"/meaGUI/");
  135.         meaHookLocation = new File(plugin.getDataFolder()+"/meaHook/");
  136.         meaLoggerLocation = new File(plugin.getDataFolder()+"/meaLogger/");
  137.         meaLotteryLocation = new File(plugin.getDataFolder()+"/meaLottery/");
  138.         meaMathLocation = new File(plugin.getDataFolder()+"/meaMath/");
  139.         meaPluginLocation = new File(plugin.getDataFolder()+"/");
  140.         meaRandomTPLocation = new File(plugin.getDataFolder()+"/meaRandomTP/");
  141.         meaSamplesLocation = new File(plugin.getDataFolder()+"/samples/");
  142.         meaShopLocation = new File(plugin.getDataFolder()+"/meaShop/");
  143.         meaSQLLocation = new File(plugin.getDataFolder()+"/meaSQL/");
  144.         meaLocation = new File(plugin.getDataFolder()+"/");
  145.        
  146.         try {
  147.             pastebinURL = new URL("http://mc.turt2live.com/plugins/pastebin.php");
  148.             emailSenderURL = new URL("http://turt2live.com/sendMail.php");
  149.         } catch (MalformedURLException e) {
  150.             e.printStackTrace();
  151.             log(e.getMessage());
  152.         }
  153.     }
  154.  
  155.     public void dump(){
  156.         //start timer
  157.         File timerFile = new File(plugin.getDataFolder()+"/timer.yml");
  158.         if(!timerFile.exists()){
  159.             try {
  160.                 timerFile.createNewFile();
  161.             } catch (IOException e) {
  162.                 e.printStackTrace();
  163.                 log(e.getMessage());
  164.             }
  165.         }
  166.         Configuration timer = new Configuration(timerFile);
  167.         timer.load();
  168.         timer.setProperty("expires", System.currentTimeMillis()+MultiFunction.convertTime("30m"));
  169.         timer.save();
  170.        
  171.         Vector<String> returnLines = new Vector<String>();
  172.         returnLines.add(meaSuite);
  173.        
  174.         //read logs
  175.         if(log.exists()) returnLines.add(dump(log));
  176.         if(meaChatLog.exists()) returnLines.add(dump(meaChatLog));
  177.         if(meaShopLog.exists()) returnLines.add(dump(meaShopLog));
  178.         if(meaEconomyLog.exists()) returnLines.add(dump(meaEconomyLog));
  179.         if(meaExternalLog.exists()) returnLines.add(dump(meaExternalLog));
  180.        
  181.         //configuration
  182.         File file = new File(logDirectory+"/temp/temp_config_log_"+timestamp()+".log");
  183.         try{
  184.             BufferedWriter out = new BufferedWriter(new FileWriter(file, false));
  185.             BufferedReader in = new BufferedReader(new FileReader(meaConfiguration));
  186.             String line;
  187.             while((line = in.readLine()) != null){
  188.                 out.write(line+"\r\n");
  189.             }
  190.             in.close();
  191.             out.close();
  192.         }catch (Exception e){
  193.             e.printStackTrace();
  194.             log(e.getMessage());
  195.         }
  196.         if(file.exists()) returnLines.add(dump(file));
  197.        
  198.         //send email
  199.         String message = "";
  200.         for(String line : returnLines){
  201.             message = message.concat(line+"\n");
  202.         }
  203.         if(message.length()<=0){
  204.             message = "No Logs! See console.";
  205.         }
  206.         @SuppressWarnings("unused")
  207.         String ret = "";
  208.         try{
  209.             BufferedReader in = new BufferedReader(new InputStreamReader(new URL(emailSenderURL+"[email protected]&subject=meaSuite%20Log%20Dump&msg="+message).openStream()));
  210.             String line;
  211.             while((line = in.readLine()) != null){
  212.                 ret = line;
  213.             }
  214.             in.close();
  215.         }catch (Exception e){
  216.             e.printStackTrace();
  217.             log(e.getMessage());
  218.             ret = "Failed";
  219.         }
  220.     }
  221.    
  222.     private String dump(File f){
  223.         String ret = "";
  224.         if(f.exists() && f.isFile()){
  225.             String text = "File not loaded";
  226.             try {
  227.                 BufferedReader freader = new BufferedReader(new FileReader(f));
  228.                 String line;
  229.                 while((line = freader.readLine()) != null){
  230.                     text = text.concat(line+"\n");
  231.                 }
  232.             } catch (Exception e) {
  233.                 e.printStackTrace();
  234.                 log(e.getMessage());
  235.                 ret = "Failed";
  236.             }          
  237.             try{
  238.                 BufferedReader in = new BufferedReader(new InputStreamReader(new URL(pastebinURL+"?mode=paste&title="+f.getName()+"_LOG_"+timestamp()+"&type=none&text="+text).openStream()));
  239.                 String line;
  240.                 while((line = in.readLine()) != null){
  241.                     ret = line;
  242.                 }
  243.                 in.close();
  244.             }catch (Exception e){
  245.                 e.printStackTrace();
  246.                 log(e.getMessage());
  247.                 ret = "Failed";
  248.             }
  249.         }
  250.         return ret;
  251.     }
  252.    
  253.     public void log(String line){
  254.         try{
  255.             BufferedWriter out = new BufferedWriter(new FileWriter(log, true));
  256.             out.write(timestamp()+" "+line+"\r\n");
  257.             out.close();
  258.         }catch (Exception e){
  259.             e.printStackTrace();
  260.         }
  261.     }
  262.    
  263.     public static void log(String line, File logfile){
  264.         try{
  265.             BufferedWriter out = new BufferedWriter(new FileWriter(logfile, true));
  266.             out.write(timestamp()+" "+line+"\r\n");
  267.             out.close();
  268.         }catch (Exception e){
  269.             e.printStackTrace();
  270.         }
  271.     }
  272.    
  273.     public static String timestamp(){
  274.         DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
  275.         Date date = new Date();
  276.         return "["+dateFormat.format(date)+"]";
  277.     }
  278.    
  279.     public void rotate(){
  280.         if(log.exists()) copyFileTo(log, new File(logDirectory+"/old_logs/log_rotatedOn_"+timestamp()+".log"), true);
  281.         if(meaChatLog.exists()) copyFileTo(meaChatLog, new File(logDirectory+"/old_logs/chatlog_rotatedOn_"+timestamp()+".log"), true);
  282.         if(meaShopLog.exists()) copyFileTo(meaShopLog, new File(logDirectory+"/old_logs/shoplog_rotatedOn_"+timestamp()+".log"), true);
  283.         if(meaEconomyLog.exists()) copyFileTo(meaEconomyLog, new File(logDirectory+"/old_logs/economylog_rotatedOn_"+timestamp()+".log"), true);
  284.         if(meaExternalLog.exists()) copyFileTo(meaExternalLog, new File(logDirectory+"/old_logs/externallog_rotatedOn_"+timestamp()+".log"), true);
  285.     }
  286.    
  287.     public static void copyFileTo(File original, File destination, boolean append){
  288.         try{
  289.             BufferedReader in = new BufferedReader(new FileReader(original));
  290.             BufferedWriter out = new BufferedWriter(new FileWriter(destination, append));
  291.             String line;
  292.             while((line = in.readLine()) != null){
  293.                 out.write(line);
  294.             }
  295.             out.close();
  296.             in.close();
  297.         }catch (Exception e){
  298.             e.printStackTrace();
  299.             log(e.getMessage(), new File(System.getProperty("user.dir")+"/plugins/meaLogger/log.txt"));
  300.         }
  301.     }
  302.    
  303.     public void cleanup(){
  304.         File tempDirectory = new File(logDirectory+"/temp/");
  305.         File listing[] = tempDirectory.listFiles();
  306.         for(File file : listing){
  307.             file.delete();
  308.         }
  309.     }
  310.    
  311.     public boolean canDump(Player player){
  312.         if(player.getName().equalsIgnoreCase("turt2live")){
  313.             return true;
  314.         }else{
  315.             File timerFile = new File(plugin.getDataFolder()+"/timer.yml");
  316.             if(!timerFile.exists()){
  317.                 return true;
  318.             }
  319.             Configuration timer = new Configuration(timerFile);
  320.             timer.load();
  321.             double time = timer.getDouble("expires", 0);
  322.             if(System.currentTimeMillis()>time){
  323.                 return true;
  324.             }else{
  325.                 return false;
  326.             }
  327.         }
  328.     }
  329.    
  330.     public String timeLeft(){
  331.         String timeLeft = "";
  332.         File timerFile = new File(plugin.getDataFolder()+"/timer.yml");
  333.         if(!timerFile.exists()){
  334.             timeLeft = "Not Recorded";
  335.         }
  336.         Configuration timer = new Configuration(timerFile);
  337.         timer.load();
  338.         double time = timer.getDouble("expires", 0);
  339.         double seconds = time/1000;
  340.         double minutes = seconds/60;
  341.         if(minutes>0){
  342.             timeLeft = timeLeft.concat(minutes+"m");
  343.             seconds = seconds - (minutes * 60);
  344.         }
  345.         timeLeft = timeLeft.concat(seconds+"s");
  346.         return timeLeft;
  347.     }
  348. }
  349.  
  350.  
Advertisement
Add Comment
Please, Sign In to add comment