Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. package me.mikept.shieldtempo;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.util.Set;
  8. import java.util.Timer;
  9.  
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.ChatColor;
  12. import org.bukkit.OfflinePlayer;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.plugin.Plugin;
  15. import org.bukkit.plugin.java.JavaPlugin;
  16. import org.bukkit.scheduler.BukkitRunnable;
  17.  
  18.  
  19. public class Main extends JavaPlugin {
  20.  
  21. public String host, database, username, password;
  22. public int port;
  23.  
  24. public static Comandos c = new Comandos();
  25.  
  26. private Timer timer;
  27.  
  28. static Main plugin;
  29. private static Main instance;
  30. public static Main instance() {
  31. return instance;
  32. }
  33.  
  34. public void onEnable() {
  35. new BukkitRunnable() {
  36. public void run() {
  37. int online = 0;
  38. String onlineplayer = "";
  39. if (!c.data.isEmpty()) {
  40. Set<String> names = c.data.keySet();
  41. for (String playerName : names) {
  42. int on = c.data.get(playerName);
  43. if (on > online) {
  44. online = on;
  45. onlineplayer = playerName;
  46.  
  47. OfflinePlayer pname = Bukkit.getOfflinePlayer(playerName);
  48.  
  49. Main.plugin.getConfig().set("TOPTAG.JOGADORTOP", pname.getName());
  50. Main.plugin.saveConfig();
  51. }
  52. }
  53. }
  54.  
  55. }
  56. }.runTaskTimer(this, 5 * 20, 10 * 600);
  57.  
  58.  
  59. instance = this;
  60. plugin = this;
  61. this.saveDefaultConfig();
  62. mysqlSetup();
  63. getCommand("tempo").setExecutor(c);
  64. getCommand("shieldtempo").setExecutor(new Reload());
  65. Bukkit.getPluginManager().registerEvents(new Join(), this);
  66. Bukkit.getPluginManager().registerEvents(new InventoryClick(), this);
  67. Bukkit.getPluginManager().registerEvents(new Falar(), this);
  68. this.timer = new Timer();
  69. this.timer.scheduleAtFixedRate(new TempoTask(), 0L, 60000L);
  70.  
  71. getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "ShieldTempo foi ativado.");
  72. }
  73.  
  74. public void onDisable() {
  75. getServer().getConsoleSender().sendMessage(ChatColor.RED + "ShieldTempo foi desativado.");
  76. if (!(this.timer == null)) {
  77. this.timer = null;
  78. this.timer.cancel();
  79. for (Player p : Bukkit.getOnlinePlayers()) {
  80. if (p == null) {
  81. continue;
  82. }
  83. if (c.data.containsKey(p.getName())) {
  84. int minutos = c.data.get(p.getName());
  85. MySQLAPI.setMinutos(p.getName(), minutos);
  86. c.data.remove(p.getName());
  87. closeConnection();
  88. }
  89. }
  90. }
  91. }
  92.  
  93. public static Plugin getPlugin() {
  94. return Main.getPlugin(Main.class);
  95. }
  96.  
  97. public static Connection con = null;
  98.  
  99. public void mysqlSetup() {
  100. String host = Main.plugin.getConfig().getString("MYSQL.HOST");
  101. String database = Main.plugin.getConfig().getString("MYSQL.DATABASE");
  102. String username = Main.plugin.getConfig().getString("MYSQL.USERNAME");
  103. String password = Main.plugin.getConfig().getString("MYSQL.PASSWORD");
  104. Integer port = Main.plugin.getConfig().getInt("MYSQL.PORT");
  105.  
  106.  
  107. String URL = "jdbc:mysql://" + host + ":" + port + "/" + database;
  108.  
  109. try {
  110. con = DriverManager.getConnection(URL, username, password);
  111. createTable();
  112. Bukkit.getConsoleSender().sendMessage("§aConexao com o MYSQL foi aberta!");
  113. } catch (SQLException e) {
  114. e.printStackTrace();
  115. Bukkit.getConsoleSender().sendMessage("§cConexao com o MYSQL foi falhou!");
  116. Main.getPlugin().getPluginLoader().disablePlugin(Main.getPlugin());
  117. }
  118.  
  119. }
  120.  
  121. public Connection getConnection() {
  122. return con;
  123. }
  124.  
  125. public void setConnection(Connection connection) {
  126. this.con = connection;
  127. }
  128.  
  129. public static void createTable() {
  130. PreparedStatement stm = null;
  131. try {
  132. stm = con.prepareStatement("CREATE TABLE IF NOT EXISTS ShieldTempo (NOME VARCHAR(16) NOT NULL, MINUTOS int(23) NOT NULL)");
  133. stm.execute();
  134. stm.close();
  135. Bukkit.getConsoleSender().sendMessage("§aTabela ShieldTempo criada/carregada com sucesso!");
  136. } catch (SQLException e) {
  137. e.printStackTrace();
  138. Bukkit.getConsoleSender().sendMessage("§cNao foi possivel criar a tabela ShieldTempo!");
  139. Main.getPlugin().getPluginLoader().disablePlugin(Main.getPlugin());
  140. }
  141. }
  142.  
  143. public void closeConnection() {
  144. if (con != null) {
  145. try {
  146. con.close();
  147. con = null;
  148. Bukkit.getConsoleSender().sendMessage("§aConexao com o MYSQL foi fechada.");
  149. } catch (SQLException e) {
  150. e.printStackTrace();
  151. Bukkit.getConsoleSender().sendMessage("§cNao foi possivel fechar a conexao.");
  152. }
  153. }
  154. }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement