Advertisement
WaffleMast3r

cevax3

Feb 19th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. package me.wafflemaster.com;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.util.logging.Logger;
  8.  
  9. import org.bukkit.ChatColor;
  10. import org.bukkit.command.Command;
  11. import org.bukkit.command.CommandSender;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.plugin.PluginDescriptionFile;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15.  
  16. public class main extends JavaPlugin{
  17. public final Logger logger = Logger.getLogger("Minecraft");
  18. public static main plugin;
  19.  
  20. @Override
  21. public void onDisable() {
  22. PluginDescriptionFile pdfFile = this.getDescription();
  23. this.logger.info(pdfFile.getName() + " s-a dezactivat!");
  24.  
  25. }
  26.  
  27. @Override
  28. public void onEnable() {
  29. PluginDescriptionFile pdfFile = this.getDescription();
  30. this.logger.info(pdfFile.getName() + " versiunea " + pdfFile.getVersion() + " s-a activat!");
  31. openConnection();
  32. createTable();
  33. closeConnection();
  34. }
  35.  
  36. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  37. Player player = (Player)sender;
  38. if(commandLabel.equalsIgnoreCase("arena")) {
  39. if(args.length == 0){
  40. player.sendMessage("Foloseste /arena join");
  41. return true;
  42. }
  43. if(args[0].equalsIgnoreCase("join")) {
  44. addPlayer(player);
  45. return true;
  46. }
  47. }
  48. return true;
  49. }
  50.  
  51. private static Connection connection;
  52. static String table = "event1";
  53.  
  54. public static void openConnection() {
  55. try
  56. {
  57. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ArenaEvent", "root", "");
  58. }
  59. catch(Exception e)
  60. {
  61. e.printStackTrace();
  62. }
  63. }
  64. public static void closeConnection() {
  65. try
  66. {
  67. connection.close();
  68. }
  69. catch(Exception e)
  70. {
  71. e.printStackTrace();
  72. }
  73. }
  74.  
  75. public static void createTable() {
  76. try{
  77. PreparedStatement sql = connection.prepareStatement("CREATE TABLE IF NOT EXISTS " + table + " (ID INT(11) NOT NULL AUTO_INCREMENT, USERNAME VARCHAR(17), primary key (ID));");
  78. sql.execute();
  79. sql.close();
  80. }catch (Exception e){
  81. e.printStackTrace();
  82. }
  83. }
  84.  
  85. public void addPlayer(Player player) {
  86. if(!checkPlayer(player)){
  87. try{
  88. openConnection();
  89. PreparedStatement sql = connection.prepareStatement("INSERT INTO `" + table + "` (USERNAME) VALUES (" + player + ");");
  90. sql.execute();
  91. sql.close();
  92. }catch (Exception e){
  93. e.printStackTrace();
  94. } finally {
  95. closeConnection();
  96. }
  97. }else{
  98. player.sendMessage(ChatColor.RED + "Esti deja inscris!");
  99. }
  100. }
  101.  
  102. public synchronized static boolean checkPlayer(Player player){
  103. try{
  104. openConnection();
  105. PreparedStatement sql = connection.prepareStatement("SELECT * FROM `" + table + "` WHERE username=" + player + ";");
  106. ResultSet resultSet = sql.executeQuery();
  107. boolean containsPlayer = resultSet.next();
  108.  
  109. sql.close();
  110. resultSet.close();
  111.  
  112. return containsPlayer;
  113. } catch(Exception e){
  114. e.printStackTrace();
  115. return false;
  116. } finally {
  117. closeConnection();
  118. }
  119. }
  120.  
  121. public void select() {
  122. try{
  123. PreparedStatement sql = connection.prepareStatement("SELECT id FROM " + table + ";");
  124. sql.execute();
  125. sql.close();
  126. } catch (Exception e){
  127. e.printStackTrace();
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement