Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.mikept.shieldtempo;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.Set;
- import java.util.Timer;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.OfflinePlayer;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.Plugin;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitRunnable;
- public class Main extends JavaPlugin {
- public String host, database, username, password;
- public int port;
- public static Comandos c = new Comandos();
- private Timer timer;
- static Main plugin;
- private static Main instance;
- public static Main instance() {
- return instance;
- }
- public void onEnable() {
- this.saveDefaultConfig();
- if (Main.getPlugin().getConfig().getBoolean("MYSQL.ATIVADO")) {
- mysqlSetup();
- } else {
- Bukkit.getServer().getPluginManager().disablePlugin(this);
- Bukkit.getServer().getScheduler().cancelTasks(this);
- return;
- }
- instance = this;
- plugin = this;
- getCommand("tempo").setExecutor(c);
- getCommand("shieldtempo").setExecutor(new Reload());
- Bukkit.getPluginManager().registerEvents(new Join(), this);
- Bukkit.getPluginManager().registerEvents(new InventoryClick(), this);
- Bukkit.getPluginManager().registerEvents(new Falar(), this);
- this.timer = new Timer();
- this.timer.scheduleAtFixedRate(new TempoTask(), 0L, 60000L);
- getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "ShieldTempo foi ativado.");
- new BukkitRunnable() {
- public void run() {
- int online = 0;
- String onlineplayer = "";
- if (!c.data.isEmpty()) {
- Set<String> names = c.data.keySet();
- for (String playerName : names) {
- int on = c.data.get(playerName);
- if (on > online) {
- online = on;
- onlineplayer = playerName;
- OfflinePlayer pname = Bukkit.getOfflinePlayer(playerName);
- Main.plugin.getConfig().set("TOPTAG.JOGADORTOP", pname.getName());
- Main.plugin.saveConfig();
- }
- }
- }
- }
- }.runTaskTimer(this, 5 * 20, 10 * 600);
- }
- public void onDisable() {
- getServer().getConsoleSender().sendMessage(ChatColor.RED + "ShieldTempo foi desativado.");
- if (!(this.timer == null)) {
- this.timer = null;
- this.timer.cancel();
- for (Player p : Bukkit.getOnlinePlayers()) {
- if (p == null) {
- continue;
- }
- if (c.data.containsKey(p.getName())) {
- int minutos = c.data.get(p.getName());
- MySQLAPI.setMinutos(p.getName(), minutos);
- c.data.remove(p.getName());
- closeConnection();
- }
- }
- }
- }
- public static Plugin getPlugin() {
- return Main.getPlugin(Main.class);
- }
- public static Connection con = null;
- public void mysqlSetup() {
- String host = Main.plugin.getConfig().getString("MYSQL.HOST");
- String database = Main.plugin.getConfig().getString("MYSQL.DATABASE");
- String username = Main.plugin.getConfig().getString("MYSQL.USERNAME");
- String password = Main.plugin.getConfig().getString("MYSQL.PASSWORD");
- Integer port = Main.plugin.getConfig().getInt("MYSQL.PORT");
- String URL = "jdbc:mysql://" + host + ":" + port + "/" + database;
- try {
- con = DriverManager.getConnection(URL, username, password);
- createTable();
- Bukkit.getConsoleSender().sendMessage("§aConexao com o MYSQL foi aberta!");
- } catch (SQLException e) {
- e.printStackTrace();
- Bukkit.getConsoleSender().sendMessage("§cConexao com o MYSQL foi falhou!");
- Main.getPlugin().getPluginLoader().disablePlugin(Main.getPlugin());
- }
- }
- public Connection getConnection() {
- return con;
- }
- public void setConnection(Connection connection) {
- this.con = connection;
- }
- public static void createTable() {
- PreparedStatement stm = null;
- try {
- stm = con.prepareStatement("CREATE TABLE IF NOT EXISTS ShieldTempo (NOME VARCHAR(16) NOT NULL, MINUTOS int(23) NOT NULL)");
- stm.execute();
- stm.close();
- Bukkit.getConsoleSender().sendMessage("§aTabela ShieldTempo criada/carregada com sucesso!");
- } catch (SQLException e) {
- e.printStackTrace();
- Bukkit.getConsoleSender().sendMessage("§cNao foi possivel criar a tabela ShieldTempo!");
- Main.getPlugin().getPluginLoader().disablePlugin(Main.getPlugin());
- }
- }
- public void closeConnection() {
- if (con != null) {
- try {
- con.close();
- con = null;
- Bukkit.getConsoleSender().sendMessage("§aConexao com o MYSQL foi fechada.");
- } catch (SQLException e) {
- e.printStackTrace();
- Bukkit.getConsoleSender().sendMessage("§cNao foi possivel fechar a conexao.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement