Advertisement
Guest User

Untitled

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