Advertisement
Guest User

Untitled

a guest
Mar 5th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package de.freakyonline.uconeplugin;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.Collection;
  15. import java.util.UUID;
  16. import static java.util.logging.Level.INFO;
  17. import java.util.logging.Logger;
  18. import org.bukkit.Bukkit;
  19. import org.bukkit.entity.Player;
  20. import org.bukkit.event.Listener;
  21. import org.bukkit.event.player.PlayerJoinEvent;
  22. import org.bukkit.plugin.java.JavaPlugin;
  23. import org.bukkit.scheduler.BukkitRunnable;
  24.  
  25. /**
  26. *
  27. * @author uwe
  28. */
  29. public class UCOnePlugin extends JavaPlugin {
  30.  
  31. private Connection mySQLConnection;
  32. private String host, database, username, password;
  33. private int port;
  34. Statement statement;
  35.  
  36. @Override
  37. public void onEnable() {
  38.  
  39. Logger logger = Bukkit.getLogger();
  40.  
  41. host = "localhost";
  42. port = 3306;
  43. database = "UCOnePlugin";
  44. username = "UCOnePlugin";
  45. password = "p9PC967UgQmUPK2t";
  46.  
  47. try {
  48. openMySQLConnection();
  49. } catch(ClassNotFoundException e) {
  50. e.printStackTrace();
  51. } catch(SQLException e) {
  52. e.printStackTrace();
  53. }
  54.  
  55. logger.log(INFO, "Before Thread ...");
  56. Thread ucOnePluginServer = new Thread(new UCOnePluginServer());
  57. ucOnePluginServer.run();
  58. logger.log(INFO, "After Thread ...");
  59.  
  60. getServer().getPluginManager().registerEvents(new UCOnePluginListener(mySQLConnection), this);
  61.  
  62. }
  63.  
  64. @Override
  65. public void onDisable() {
  66. PreparedStatement updateOnDisable = null; // Update last quit on disable
  67. PreparedStatement qryIfExists = null;
  68. ResultSet result;
  69.  
  70. Collection<?extends Player> onlinePlayerList = Bukkit.getOnlinePlayers();
  71.  
  72. for(Player player : onlinePlayerList) {
  73.  
  74. String uuid = player.getUniqueId().toString();
  75. try {
  76. qryIfExists = mySQLConnection.prepareStatement("SELECT pid FROM PlayerList WHERE uuid = ?");
  77. qryIfExists.setString(1, uuid);
  78. result = qryIfExists.executeQuery();
  79.  
  80. if(result.next()) {
  81. updateOnDisable = mySQLConnection.prepareStatement("UPDATE PlayerList SET lastquit = NOW() WHERE uuid = ?");
  82. updateOnDisable.setString(1,uuid);
  83. updateOnDisable.executeUpdate();
  84. } else {
  85. // consoleLogger(SEVERE,"User has no row in table. Should have after joining.");
  86. }
  87. } catch (SQLException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. }
  92.  
  93. public void openMySQLConnection() throws SQLException, ClassNotFoundException {
  94. if (mySQLConnection != null && !mySQLConnection.isClosed()) {
  95. return;
  96. }
  97.  
  98. synchronized (this) {
  99. if (mySQLConnection != null && !mySQLConnection.isClosed()) {
  100. return;
  101. }
  102. Class.forName("com.mysql.jdbc.Driver");
  103. mySQLConnection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
  104. }
  105. }
  106.  
  107. }
  108.  
  109. UCOneListener.java:
  110.  
  111. /*
  112. * To change this license header, choose License Headers in Project Properties.
  113. * To change this template file, choose Tools | Templates
  114. * and open the template in the editor.
  115. */
  116. package de.freakyonline.uconeplugin;
  117.  
  118. import java.sql.Connection;
  119. import java.sql.PreparedStatement;
  120. import java.sql.ResultSet;
  121. import java.sql.SQLException;
  122. import java.sql.Statement;
  123. import java.util.UUID;
  124. import org.bukkit.event.EventHandler;
  125. import org.bukkit.event.Listener;
  126. import org.bukkit.event.player.PlayerJoinEvent;
  127. import org.bukkit.event.player.PlayerQuitEvent;
  128.  
  129. /**
  130. *
  131. * @author uwe
  132. */
  133. public class UCOnePluginListener implements Listener
  134. {
  135. Statement statement;
  136. Connection mySQLConnection;
  137.  
  138.  
  139. public UCOnePluginListener(Connection mySQLConnection) {
  140. this.mySQLConnection = mySQLConnection;
  141. }
  142.  
  143. @EventHandler
  144. public void onPlayerJoin(PlayerJoinEvent event) throws SQLException
  145. {
  146. ResultSet result;
  147. PreparedStatement qryIfExists = null;
  148. PreparedStatement insertNewOnJoin = null;
  149.  
  150. UUID uuid = event.getPlayer().getUniqueId();
  151. String nickName = event.getPlayer().getName();
  152.  
  153. try {
  154. qryIfExists = mySQLConnection.prepareStatement("SELECT pid FROM PlayerList WHERE uuid = ?");
  155. qryIfExists.setString(1,uuid.toString());
  156. result = qryIfExists.executeQuery();
  157.  
  158. if(!result.next()) {
  159. insertNewOnJoin = mySQLConnection.prepareStatement("INSERT INTO PlayerList (nickname, uuid, firstjoined) VALUES (?,?,CURDATE())");
  160. insertNewOnJoin.setString(1,nickName);
  161. insertNewOnJoin.setString(2,uuid.toString());
  162. insertNewOnJoin.executeUpdate();
  163. }
  164. } catch (SQLException e) {
  165. e.printStackTrace();
  166. }
  167.  
  168. }
  169.  
  170. @EventHandler
  171. public void OnPlayerQuit(PlayerQuitEvent event) {
  172. PreparedStatement updateOnQuit = null;
  173. PreparedStatement qryIfExists = null;
  174. ResultSet result;
  175.  
  176. String uuid = event.getPlayer().getUniqueId().toString();
  177.  
  178. try {
  179. qryIfExists = mySQLConnection.prepareStatement("SELECT pid FROM PlayerList WHERE uuid = ?");
  180. qryIfExists.setString(1, uuid);
  181. result = qryIfExists.executeQuery();
  182.  
  183. if(result.next()) {
  184. updateOnQuit = mySQLConnection.prepareStatement("UPDATE PlayerList SET lastquit = NOW() WHERE uuid = ?");
  185. updateOnQuit.setString(1,uuid);
  186. updateOnQuit.executeUpdate();
  187. } else {
  188. // consoleLogger(SEVERE,"User has no row in table. Should have after joining.");
  189. }
  190. } catch (SQLException e) {
  191. e.printStackTrace();
  192. }
  193. }
  194. }
  195.  
  196. /*
  197. * To change this license header, choose License Headers in Project Properties.
  198. * To change this template file, choose Tools | Templates
  199. * and open the template in the editor.
  200. */
  201. package de.freakyonline.uconeplugin;
  202.  
  203. import java.io.ObjectInputStream;
  204. import java.io.ObjectOutputStream;
  205. import java.net.ServerSocket;
  206. import java.net.Socket;
  207. import java.util.ArrayList;
  208. import static java.util.logging.Level.INFO;
  209. import java.util.logging.Logger;
  210. import org.bukkit.Bukkit;
  211.  
  212. /**
  213. *
  214. * @author uwe
  215. */
  216.  
  217. // TODO: Make Singleton
  218. public class UCOnePluginServer implements Runnable {
  219. ArrayList<ObjectOutputStream> clientsConnected;
  220.  
  221. public class ClientHandler implements Runnable {
  222. ObjectInputStream in;
  223. Socket clientSocket;
  224.  
  225. public ClientHandler (Socket socket) {
  226. try {
  227. clientSocket = socket;
  228. in = new ObjectInputStream(clientSocket.getInputStream());
  229. } catch (Exception ex) { ex.printStackTrace(); }
  230. }
  231.  
  232. public void run() {
  233. Object o1 = null;
  234. ObjectOutputStream toClient;
  235.  
  236. try {
  237. toClient = new ObjectOutputStream(clientSocket.getOutputStream());
  238. o1 = in.readObject(); // TODO: Get 2 Objects
  239.  
  240. String command = o1.toString().toLowerCase();
  241.  
  242. Logger logger = Bukkit.getLogger();
  243.  
  244. switch (command) {
  245. default: logger.log(INFO,"Got command.");
  246.  
  247. }
  248.  
  249. toClient.writeObject("Test");
  250.  
  251. // TODO: more server code here.
  252.  
  253. } catch (Exception ex) { ex.printStackTrace(); }
  254. }
  255. }
  256.  
  257. public void run() {
  258. clientsConnected = new ArrayList<ObjectOutputStream>();
  259.  
  260. try {
  261. ServerSocket serverSock = new ServerSocket(2009);
  262.  
  263. while(true) {
  264. Socket clientSocket = serverSock.accept();
  265. ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
  266. clientsConnected.add(out);
  267.  
  268. Thread t = new Thread(new ClientHandler(clientSocket));
  269. }
  270.  
  271. } catch(Exception ex) {
  272. ex.printStackTrace();
  273. }
  274. }
  275.  
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement