Advertisement
Guest User

Untitled

a guest
Aug 11th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. package me.xlysander12.testes;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.UUID;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.command.CommandSender;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.event.Listener;
  15. import org.bukkit.plugin.PluginManager;
  16. import org.bukkit.plugin.java.JavaPlugin;
  17. import org.bukkit.scheduler.BukkitRunnable;
  18.  
  19. import me.xlysander12.testes.comandos.AtTopCommand;
  20. import me.xlysander12.testes.comandos.SetTopCommand;
  21. import me.xlysander12.testes.comandos.TopCommand;
  22. import me.xlysander12.testes.eventos.JoinEvent;
  23. import me.xlysander12.testes.eventos.PlayerKillEvent;
  24. import net.md_5.bungee.api.ChatColor;
  25.  
  26. public class Main extends JavaPlugin implements Listener {
  27. public Connection connection;
  28. public String host, database, username, password, table;
  29. public int port;
  30. public ArrayList<String> map = new ArrayList<String>();
  31.  
  32. public void onEnable() {
  33. loadConfig();
  34. setupMysql();
  35. loadEvents();
  36. loadCommands();
  37. Bukkit.getConsoleSender().sendMessage("\n\n§aTopKills Ativado!\n");
  38. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "top");
  39. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "npc remove all");
  40. loadEntities();
  41.  
  42. }
  43.  
  44. public void onDisable() {
  45. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "npc remove all");
  46. try {
  47. connection.close();
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. } finally {
  51. Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "MYSQL DISCONNECTED");
  52. }
  53. }
  54.  
  55. private void setupMysql() {
  56. host = getConfig().getString("host");
  57. port = getConfig().getInt("port");
  58. database = getConfig().getString("database");
  59. username = getConfig().getString("username");
  60. password = getConfig().getString("password");
  61. table = getConfig().getString("table");
  62.  
  63. try {
  64. openConnection();
  65. } catch (SQLException | ClassNotFoundException e) {
  66. e.printStackTrace();
  67. }
  68.  
  69. try {
  70. synchronized (this) {
  71. if (connection != null && !connection.isClosed()) {
  72. return;
  73. }
  74. }
  75.  
  76. // If No Open Connection Is Found
  77. Class.forName("com.mysql.jdbc.Driver");
  78. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  79. Bukkit.broadcastMessage(ChatColor.GREEN + "MSQL CONNECTED");
  80. } catch(SQLException | ClassNotFoundException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84.  
  85. private void openConnection() throws SQLException, ClassNotFoundException{
  86. // Id Connection is Open, Stop Execution
  87. if (connection != null && !connection.isClosed()) {
  88. return;
  89. }
  90. synchronized (this) {
  91. if (connection != null && !connection.isClosed()) {
  92. return;
  93. }
  94. }
  95.  
  96. // If No Open Connection Is Found
  97. Class.forName("com.mysql.jdbc.Driver");
  98. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  99. }
  100.  
  101. public void addKills(UUID uuid) {
  102. try {
  103. PreparedStatement getKills = connection.prepareStatement("SELECT * FROM " + table + " WHERE uuid='" + uuid.toString() + "'");
  104. ResultSet results = getKills.executeQuery();
  105.  
  106. if (results.next()) {
  107. results.getInt("kills");
  108. PreparedStatement addKills = connection.prepareStatement("UPDATE " + table + " SET kills = " + (results.getInt("kills") + 1) + " WHERE uuid='" + uuid + "'");
  109. addKills.executeUpdate();
  110. }
  111. } catch (SQLException e) {
  112. // TODO Auto-generated catch block
  113. e.printStackTrace();
  114. }
  115. }
  116.  
  117. public void getTop(CommandSender player) {
  118. try {
  119. PreparedStatement order = connection.prepareStatement("SELECT * FROM " + table + " ORDER BY kills DESC LIMIT 3");
  120. ResultSet results = order.executeQuery();
  121.  
  122. player.sendMessage(ChatColor.GREEN + "TOP 3:");
  123. map.clear();
  124. while (results.next()) {
  125. map.add(results.getString("name"));
  126. player.sendMessage(results.getString("name") + ": " + results.getInt("kills"));
  127. }
  128. } catch (SQLException e) {
  129. e.printStackTrace();
  130. }
  131.  
  132. }
  133.  
  134.  
  135. public void createPlayer(UUID uuid, Player player) {
  136. try {
  137. PreparedStatement tableCreate = connection.prepareStatement("CREATE TABLE IF NOT EXISTS " + table + "(uuid VARCHAR(36), name VARCHAR(60), kills INTEGER)");
  138. tableCreate.execute();
  139.  
  140. PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE uuid=?");
  141. statement.setString(1, uuid.toString());
  142.  
  143. ResultSet results = statement.executeQuery();
  144.  
  145. if (!results.next()) {
  146.  
  147. PreparedStatement insert = connection.prepareStatement("INSERT INTO " + table + "(UUID,NAME,KILLS) VALUE ('" + uuid.toString() + "', '" + player.getName() + "', " + "0)");
  148. insert.execute();
  149.  
  150. getServer().broadcastMessage(ChatColor.GREEN + "Player Inserted");
  151. }
  152.  
  153. Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "DATA INSERTED");
  154. } catch (SQLException e) {
  155. e.printStackTrace();
  156. }
  157. }
  158.  
  159. public void loadConfig(){
  160. getConfig().options().copyDefaults(true);
  161. saveConfig();
  162. }
  163.  
  164. public void loadEvents() {
  165. PluginManager pm = Bukkit.getPluginManager();
  166.  
  167. pm.registerEvents(new PlayerKillEvent(), this);
  168. pm.registerEvents(new JoinEvent(), this);
  169. }
  170.  
  171. public void loadCommands() {
  172. getCommand("top").setExecutor(new TopCommand());
  173. getCommand("settop").setExecutor(new SetTopCommand());
  174. getCommand("attop").setExecutor(new AtTopCommand());
  175. }
  176.  
  177. public void loadEntities() {
  178. new BukkitRunnable() {
  179.  
  180. @Override
  181. public void run() {
  182.  
  183. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "top");
  184. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "npc remove all");
  185. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "npc create " + map.get(1) + " --at " + getConfig().getDouble("Locations.TOP1.X") + ":" + getConfig().getDouble("Locations.TOP1.Y") + ":" + getConfig().getDouble("Locations.TOP1.Z") + ":" + getConfig().getString("Locations.TOP1.World"));
  186. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "npc lookclose");
  187. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "npc create " + map.get(2) + " --at " + getConfig().getDouble("Locations.TOP2.X") + ":" + getConfig().getDouble("Locations.TOP2.Y") + ":" + getConfig().getDouble("Locations.TOP2.Z") + ":" + getConfig().getString("Locations.TOP2.World"));
  188. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "npc lookclose");
  189. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "npc create " + map.get(3) + " --at " + getConfig().getDouble("Locations.TOP3.X") + ":" + getConfig().getDouble("Locations.TOP3.Y") + ":" + getConfig().getDouble("Locations.TOP3.Z") + ":" + getConfig().getString("Locations.TOP3.World"));
  190. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "npc lookclose");
  191. map.clear();
  192.  
  193. }
  194. }.runTaskTimer(this, 0L, 3000L);
  195. }
  196.  
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement