Advertisement
Guest User

Untitled

a guest
May 31st, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. package se.doodlemeat;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5.  
  6. import org.bukkit.Location;
  7.  
  8. import java.sql.PreparedStatement;
  9.  
  10. import se.doodlemeat.BasicLoggingTypes.BasicLoggingType;
  11. import se.doodlemeat.BasicLoggingTypes.DeathLog;
  12.  
  13. public class DataLogger implements Runnable {
  14.    
  15.     private PlayersOnlineME plugin;
  16.     private int tasksPerRun = 5;
  17.    
  18.     public DataLogger(PlayersOnlineME instance)
  19.     {
  20.         plugin = instance;
  21.     }
  22.    
  23.     @Override
  24.     public void run() {
  25.         plugin.getLogger().info("Logs count: " + plugin.logs.size());
  26.        
  27.         // Perform all tasks
  28.         //BasicLoggingType task = null;
  29.         for(int i = 0; i < tasksPerRun; i++)
  30.         {
  31.             BasicLoggingType task = null;
  32.             try {
  33.                 task = plugin.logs.take();
  34.             } catch (InterruptedException e1) {
  35.                 e1.printStackTrace();
  36.             }
  37.            
  38.             /*
  39.             // Generic data
  40.             String player_name = task.getPlayername();
  41.             Location player_location = task.getLocation();
  42.             long time_stamp = task.getTimestamp();
  43.            
  44.             plugin.getLogger().info(player_name + " " + player_location.getX() + "," + player_location.getZ() + " : " + time_stamp);
  45.            
  46.             switch(task.getType())
  47.             {
  48.             case PLAYER_DEATH:
  49.                 DeathLog log = (DeathLog) task;
  50.                 PreparedStatement ps;
  51.                 try {
  52.                     ps = plugin.database.connection
  53.                             .prepareStatement("INSERT INTO player_deaths (user_id, x, y, z, time, cause) VALUES(?, ?, ?, ?, ?, ?);");
  54.                     ps.setInt(1, getUserID(player_name));
  55.                     ps.setInt(2, (int)player_location.getX());
  56.                     ps.setInt(3, (int)player_location.getY());
  57.                     ps.setInt(4, (int)player_location.getZ());
  58.                     ps.setLong(5, time_stamp);
  59.                     ps.setString(6, log.getDeathCause().getCause().name());
  60.                     ps.execute();
  61.                 } catch (SQLException e) {
  62.                     e.printStackTrace();
  63.                 }
  64.                 break;
  65.             case PLAYER_LOGIN:
  66.                 // same here
  67.                 break;
  68.             case PLAYER_QUIT:
  69.                 // and here
  70.                 break;
  71.             default:
  72.                 break;
  73.             }
  74.         */
  75.         }
  76.     }
  77.  
  78.     private int getUserID(String player_name) {
  79.         PreparedStatement ps;
  80.         ResultSet rs = null;
  81.         int id = 0;
  82.        
  83.         try {
  84.             ps = plugin.database.connection
  85.                     .prepareStatement("SELECT id FROM " + plugin.table_name + " WHERE name = ?;");
  86.             ps.setString(1, player_name);
  87.             rs = ps.executeQuery();
  88.         } catch (SQLException e) {
  89.             e.printStackTrace();
  90.         }
  91.        
  92.         /*if(plugin.database.numRows(rs) == 0)
  93.         {
  94.             try {
  95.                 ps = plugin.database.connection
  96.                         .prepareStatement("INSERT INTO " + plugin.table_name + " (name, online) VALUES(?, 1);", Statement.RETURN_GENERATED_KEYS);
  97.                 ps.setString(1, player_name);
  98.                 rs = ps.executeQuery();
  99.                 rs.next();
  100.                 id = rs.getInt(1);
  101.             } catch (SQLException e) {
  102.                 e.printStackTrace();
  103.             }
  104.         }
  105.         else
  106.         {*/
  107.             try {
  108.                 rs.next();
  109.                 id = rs.getInt("id");
  110.             } catch (SQLException e) {
  111.                 e.printStackTrace();
  112.             }
  113.         //}
  114.         plugin.getLogger().info("user-id: " + id);
  115.        
  116.         return id;
  117.     }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement