Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package de.freakyonline.uconeplugin;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Collection;
- import java.util.UUID;
- import static java.util.logging.Level.INFO;
- import java.util.logging.Logger;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitRunnable;
- /**
- *
- * @author uwe
- */
- public class UCOnePlugin extends JavaPlugin {
- private Connection mySQLConnection;
- private String host, database, username, password;
- private int port;
- Statement statement;
- @Override
- public void onEnable() {
- Logger logger = Bukkit.getLogger();
- host = "localhost";
- port = 3306;
- database = "UCOnePlugin";
- username = "UCOnePlugin";
- password = "p9PC967UgQmUPK2t";
- try {
- openMySQLConnection();
- } catch(ClassNotFoundException e) {
- e.printStackTrace();
- } catch(SQLException e) {
- e.printStackTrace();
- }
- logger.log(INFO, "Before Thread ...");
- Thread ucOnePluginServer = new Thread(new UCOnePluginServer());
- ucOnePluginServer.run();
- logger.log(INFO, "After Thread ...");
- getServer().getPluginManager().registerEvents(new UCOnePluginListener(mySQLConnection), this);
- }
- @Override
- public void onDisable() {
- PreparedStatement updateOnDisable = null; // Update last quit on disable
- PreparedStatement qryIfExists = null;
- ResultSet result;
- Collection<?extends Player> onlinePlayerList = Bukkit.getOnlinePlayers();
- for(Player player : onlinePlayerList) {
- String uuid = player.getUniqueId().toString();
- try {
- qryIfExists = mySQLConnection.prepareStatement("SELECT pid FROM PlayerList WHERE uuid = ?");
- qryIfExists.setString(1, uuid);
- result = qryIfExists.executeQuery();
- if(result.next()) {
- updateOnDisable = mySQLConnection.prepareStatement("UPDATE PlayerList SET lastquit = NOW() WHERE uuid = ?");
- updateOnDisable.setString(1,uuid);
- updateOnDisable.executeUpdate();
- } else {
- // consoleLogger(SEVERE,"User has no row in table. Should have after joining.");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public void openMySQLConnection() throws SQLException, ClassNotFoundException {
- if (mySQLConnection != null && !mySQLConnection.isClosed()) {
- return;
- }
- synchronized (this) {
- if (mySQLConnection != null && !mySQLConnection.isClosed()) {
- return;
- }
- Class.forName("com.mysql.jdbc.Driver");
- mySQLConnection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
- }
- }
- }
- UCOneListener.java:
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package de.freakyonline.uconeplugin;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.UUID;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.event.player.PlayerQuitEvent;
- /**
- *
- * @author uwe
- */
- public class UCOnePluginListener implements Listener
- {
- Statement statement;
- Connection mySQLConnection;
- public UCOnePluginListener(Connection mySQLConnection) {
- this.mySQLConnection = mySQLConnection;
- }
- @EventHandler
- public void onPlayerJoin(PlayerJoinEvent event) throws SQLException
- {
- ResultSet result;
- PreparedStatement qryIfExists = null;
- PreparedStatement insertNewOnJoin = null;
- UUID uuid = event.getPlayer().getUniqueId();
- String nickName = event.getPlayer().getName();
- try {
- qryIfExists = mySQLConnection.prepareStatement("SELECT pid FROM PlayerList WHERE uuid = ?");
- qryIfExists.setString(1,uuid.toString());
- result = qryIfExists.executeQuery();
- if(!result.next()) {
- insertNewOnJoin = mySQLConnection.prepareStatement("INSERT INTO PlayerList (nickname, uuid, firstjoined) VALUES (?,?,CURDATE())");
- insertNewOnJoin.setString(1,nickName);
- insertNewOnJoin.setString(2,uuid.toString());
- insertNewOnJoin.executeUpdate();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- @EventHandler
- public void OnPlayerQuit(PlayerQuitEvent event) {
- PreparedStatement updateOnQuit = null;
- PreparedStatement qryIfExists = null;
- ResultSet result;
- String uuid = event.getPlayer().getUniqueId().toString();
- try {
- qryIfExists = mySQLConnection.prepareStatement("SELECT pid FROM PlayerList WHERE uuid = ?");
- qryIfExists.setString(1, uuid);
- result = qryIfExists.executeQuery();
- if(result.next()) {
- updateOnQuit = mySQLConnection.prepareStatement("UPDATE PlayerList SET lastquit = NOW() WHERE uuid = ?");
- updateOnQuit.setString(1,uuid);
- updateOnQuit.executeUpdate();
- } else {
- // consoleLogger(SEVERE,"User has no row in table. Should have after joining.");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package de.freakyonline.uconeplugin;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.ArrayList;
- import static java.util.logging.Level.INFO;
- import java.util.logging.Logger;
- import org.bukkit.Bukkit;
- /**
- *
- * @author uwe
- */
- // TODO: Make Singleton
- public class UCOnePluginServer implements Runnable {
- ArrayList<ObjectOutputStream> clientsConnected;
- public class ClientHandler implements Runnable {
- ObjectInputStream in;
- Socket clientSocket;
- public ClientHandler (Socket socket) {
- try {
- clientSocket = socket;
- in = new ObjectInputStream(clientSocket.getInputStream());
- } catch (Exception ex) { ex.printStackTrace(); }
- }
- public void run() {
- Object o1 = null;
- ObjectOutputStream toClient;
- try {
- toClient = new ObjectOutputStream(clientSocket.getOutputStream());
- o1 = in.readObject(); // TODO: Get 2 Objects
- String command = o1.toString().toLowerCase();
- Logger logger = Bukkit.getLogger();
- switch (command) {
- default: logger.log(INFO,"Got command.");
- }
- toClient.writeObject("Test");
- // TODO: more server code here.
- } catch (Exception ex) { ex.printStackTrace(); }
- }
- }
- public void run() {
- clientsConnected = new ArrayList<ObjectOutputStream>();
- try {
- ServerSocket serverSock = new ServerSocket(2009);
- while(true) {
- Socket clientSocket = serverSock.accept();
- ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
- clientsConnected.add(out);
- Thread t = new Thread(new ClientHandler(clientSocket));
- }
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement