Advertisement
Guest User

Untitled

a guest
May 27th, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. package se.doodlemeat;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.concurrent.BlockingQueue;
  7. import java.util.concurrent.LinkedBlockingQueue;
  8.  
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12. import org.bukkit.scheduler.BukkitTask;
  13.  
  14. import se.doodlemeat.BasicLoggingTypes.BasicLoggingType;
  15. import se.doodlemeat.BasicLoggingTypes.DeathLog;
  16.  
  17. public class PlayersOnlineME extends JavaPlugin {
  18.     public MySQL database;
  19.     public PlayerListener playerListener;
  20.     private BukkitTask dataLogger;
  21.     public String table_name = "players_online_me";
  22.     public String players_online_table = "CREATE TABLE IF NOT EXISTS `" + table_name + "` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(128) NOT NULL,`online` tinyint(1) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
  23.     public String players_death_table = "CREATE TABLE IF NOT EXISTS `player_deaths` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `x` int(11) NOT NULL, `y` int(11) NOT NULL, `z` int(11) NOT NULL, `time` bigint(20) NOT NULL, `cause` varchar(32) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
  24.     public BlockingQueue<BasicLoggingType> logs = new LinkedBlockingQueue<BasicLoggingType>();
  25.    
  26.     public void onEnable()
  27.     {
  28.         this.saveDefaultConfig();
  29.         this.database = new MySQL(this,
  30.                 getConfig().getString("database.host"),
  31.                 getConfig().getInt("database.port"),
  32.                 getConfig().getString("database.user"),
  33.                 getConfig().getString("database.password"),
  34.                 getConfig().getString("database.database"));
  35.         this.database.openConnection();
  36.        
  37.         this.dataLogger = Bukkit.getServer().getScheduler().runTaskTimerAsynchronously(this, new DataLogger(this), 0L, 40L); // 1 second = 20 ticks
  38.        
  39.         try {
  40.             setupTables();
  41.         } catch (SQLException e) {
  42.             e.printStackTrace();
  43.         }
  44.        
  45.         updateAllPlayersState(true);
  46.        
  47.         this.playerListener = new PlayerListener(this);
  48.         getServer().getPluginManager().registerEvents(playerListener, this);
  49.     }
  50.  
  51.     public void onDisable()
  52.     {
  53.         // TODO Fix so that remaining tasks are executed
  54.        
  55.        
  56.         Bukkit.getServer().getScheduler().cancelTask(this.dataLogger.getTaskId()); // Cancel task
  57.         updateAllPlayersState(false);
  58.         this.saveDefaultConfig();
  59.         this.database.closeConnection();
  60.     }
  61.    
  62.     public void updatePlayerState(Player player, boolean state) throws SQLException
  63.     {
  64.         if(this.database.connectionIsGood())
  65.         {
  66.             int iState = 0;
  67.             if(state) iState = 1;
  68.             PreparedStatement ps = this.database.createStatement("UPDATE " + this.table_name + " SET online = ? WHERE name = ?");
  69.             ps.setInt(1, iState);
  70.             ps.setString(2, player.getName());
  71.             ps.execute();
  72.         }
  73.     }
  74.    
  75.     public void insertPlayer(Player player, boolean state) throws SQLException
  76.     {
  77.         if(this.database.connectionIsGood())
  78.         {
  79.             PreparedStatement ps = this.database.createStatement("INSERT INTO " + this.table_name + " (name, online) VALUES(?, ?);");
  80.             ps.setString(1, player.getName());
  81.             ps.setInt(2, 1);
  82.             ps.execute();
  83.         }
  84.     }
  85.    
  86.     public void addPlayerDeath(Player player, int amount) throws SQLException
  87.     {
  88.         if(this.database.connectionIsGood())
  89.         {
  90.             PreparedStatement ps = this.database.createStatement("UPDATE " + this.table_name + " SET deaths = deaths + ? WHERE name = ?;");
  91.             ps.setInt(1, amount);
  92.             ps.setString(2, player.getName());
  93.             ps.execute();
  94.         }
  95.     }
  96.    
  97.     public boolean playerExists(Player player) throws SQLException
  98.     {
  99.         if(this.database.connectionIsGood())
  100.         {
  101.             PreparedStatement ps = this.database.createStatement("SELECT * FROM " + this.table_name + " WHERE name = ?");
  102.             ps.setString(1, player.getName());
  103.             ResultSet rs = ps.executeQuery();
  104.             if(this.database.numRows(rs) == 1)
  105.             {
  106.                 return true;
  107.             }
  108.         }
  109.         return false;
  110.     }
  111.    
  112.     public void setupTables() throws SQLException
  113.     {
  114.         if(this.database.connectionIsGood())
  115.         {
  116.             PreparedStatement ps = this.database.createStatement(this.players_online_table);
  117.             ps.execute();
  118.         }
  119.     }
  120.    
  121.     private void updateAllPlayersState(boolean state)
  122.     {
  123.         Player players[] = getServer().getOnlinePlayers();
  124.         for(Player player: players)
  125.         {
  126.             try {
  127.                 if(playerExists(player))
  128.                 {
  129.                     updatePlayerState(player, state);
  130.                 }
  131.                 else
  132.                 {
  133.                     insertPlayer(player, state);
  134.                 }
  135.             } catch (SQLException e) {
  136.                 e.printStackTrace();
  137.             }
  138.         }
  139.     }
  140.  
  141.     public void addLog(DeathLog log) {
  142.         logs.add(log);
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement