Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. package mertero.coinapi;
  2.  
  3. import java.io.File;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.configuration.file.FileConfiguration;
  9. import org.bukkit.configuration.file.YamlConfiguration;
  10. import org.bukkit.event.EventHandler;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14.  
  15.  
  16.  
  17.  
  18. public class Main extends JavaPlugin implements Listener {
  19.  
  20. public static MySQL mysql;
  21.  
  22.  
  23. public void onEnable() {
  24. Bukkit.getPluginManager().registerEvents(this, this);
  25. loadConfig();
  26. ConnectMySQL();
  27. }
  28.  
  29. public void loadConfig() {
  30.  
  31. FileConfiguration cfg = this.getConfig();
  32.  
  33. cfg.addDefault("mysql.host", "localhost");
  34. cfg.addDefault("mysql.database", "skypvp");
  35. cfg.addDefault("mysql.user", "root");
  36. cfg.addDefault("mysql.password", "123456");
  37.  
  38.  
  39. cfg.options().copyDefaults(true);
  40.  
  41. saveConfig();
  42. }
  43.  
  44. private void ConnectMySQL() {
  45.  
  46.  
  47. File file = new File("plugins//CoinsAPI//config.yml");
  48. FileConfiguration mcfg = YamlConfiguration.loadConfiguration(file);
  49.  
  50. String host = mcfg.getString("mysql.host");
  51. String database = mcfg.getString("mysql.database");
  52. String user = mcfg.getString("mysql.user");
  53. String password = mcfg.getString("mysql.password");
  54. mysql = new MySQL(host, database, user, password);
  55. mysql.connect();
  56. mysql.update("CREATE TABLE IF NOT EXISTS coins(name varchar(100) UNIQUE, coins int)");
  57. }
  58.  
  59. public static int getCoins(String name) {
  60.  
  61. ResultSet rs = Main.mysql.query("SELECT coins FROM coins WHERE name = '" + name + "' LIMIT 1");
  62.  
  63. try {
  64. while (rs.next()){
  65. return rs.getInt("coins"); }
  66. } catch (SQLException e) {
  67. }
  68. return -1;
  69. }
  70.  
  71. public static void setCoins(String name, int amount) {
  72. Main.mysql.update("UPDATE coins SET coins = " + amount + " WHERE name = '" + name + "'");
  73. }
  74.  
  75. public static boolean isRegistered(String name) {
  76.  
  77. ResultSet rs = Main.mysql.query("SELECT * FROM coins WHERE name = '" + name + "' LIMIT 1");
  78. System.out.println(rs == null);
  79. try {
  80. return rs.next();
  81. } catch (SQLException e) {
  82. }
  83. return false;
  84. }
  85.  
  86. public static void register(String name) {
  87. Main.mysql.update("INSERT INTO coins (name, coins) VALUES ('" + name + "', 0)");
  88.  
  89. }
  90.  
  91. @EventHandler
  92. public void onJoin(AsyncPlayerPreLoginEvent e) {
  93. if (!MySQLStats.isRegistered(e.getName())) {
  94. MySQLStats.register(e.getName());
  95. }
  96.  
  97.  
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement