Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package se.doodlemeat;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.LinkedBlockingQueue;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitTask;
- import se.doodlemeat.BasicLoggingTypes.BasicLoggingType;
- import se.doodlemeat.BasicLoggingTypes.DeathLog;
- public class PlayersOnlineME extends JavaPlugin {
- public MySQL database;
- public PlayerListener playerListener;
- private BukkitTask dataLogger;
- public String table_name = "players_online_me";
- 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;";
- 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;";
- public BlockingQueue<BasicLoggingType> logs = new LinkedBlockingQueue<BasicLoggingType>();
- public void onEnable()
- {
- this.saveDefaultConfig();
- this.database = new MySQL(this,
- getConfig().getString("database.host"),
- getConfig().getInt("database.port"),
- getConfig().getString("database.user"),
- getConfig().getString("database.password"),
- getConfig().getString("database.database"));
- this.database.openConnection();
- this.dataLogger = Bukkit.getServer().getScheduler().runTaskTimerAsynchronously(this, new DataLogger(this), 0L, 40L); // 1 second = 20 ticks
- try {
- setupTables();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- updateAllPlayersState(true);
- this.playerListener = new PlayerListener(this);
- getServer().getPluginManager().registerEvents(playerListener, this);
- }
- public void onDisable()
- {
- // TODO Fix so that remaining tasks are executed
- Bukkit.getServer().getScheduler().cancelTask(this.dataLogger.getTaskId()); // Cancel task
- updateAllPlayersState(false);
- this.saveDefaultConfig();
- this.database.closeConnection();
- }
- public void updatePlayerState(Player player, boolean state) throws SQLException
- {
- if(this.database.connectionIsGood())
- {
- int iState = 0;
- if(state) iState = 1;
- PreparedStatement ps = this.database.createStatement("UPDATE " + this.table_name + " SET online = ? WHERE name = ?");
- ps.setInt(1, iState);
- ps.setString(2, player.getName());
- ps.execute();
- }
- }
- public void insertPlayer(Player player, boolean state) throws SQLException
- {
- if(this.database.connectionIsGood())
- {
- PreparedStatement ps = this.database.createStatement("INSERT INTO " + this.table_name + " (name, online) VALUES(?, ?);");
- ps.setString(1, player.getName());
- ps.setInt(2, 1);
- ps.execute();
- }
- }
- public void addPlayerDeath(Player player, int amount) throws SQLException
- {
- if(this.database.connectionIsGood())
- {
- PreparedStatement ps = this.database.createStatement("UPDATE " + this.table_name + " SET deaths = deaths + ? WHERE name = ?;");
- ps.setInt(1, amount);
- ps.setString(2, player.getName());
- ps.execute();
- }
- }
- public boolean playerExists(Player player) throws SQLException
- {
- if(this.database.connectionIsGood())
- {
- PreparedStatement ps = this.database.createStatement("SELECT * FROM " + this.table_name + " WHERE name = ?");
- ps.setString(1, player.getName());
- ResultSet rs = ps.executeQuery();
- if(this.database.numRows(rs) == 1)
- {
- return true;
- }
- }
- return false;
- }
- public void setupTables() throws SQLException
- {
- if(this.database.connectionIsGood())
- {
- PreparedStatement ps = this.database.createStatement(this.players_online_table);
- ps.execute();
- }
- }
- private void updateAllPlayersState(boolean state)
- {
- Player players[] = getServer().getOnlinePlayers();
- for(Player player: players)
- {
- try {
- if(playerExists(player))
- {
- updatePlayerState(player, state);
- }
- else
- {
- insertPlayer(player, state);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public void addLog(DeathLog log) {
- logs.add(log);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement