Advertisement
Guest User

Untitled

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