Advertisement
Guest User

Untitled

a guest
Feb 20th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. package de.luckylemon.stats;
  2.  
  3. import de.luckylemon.stats.listener.JoinQuitListener;
  4. import de.luckylemon.stats.utils.Cfg;
  5. import de.luckylemon.stats.utils.User;
  6. import de.luckylemon.stats.utils.mysql.MySQLManager;
  7. import de.luckylemon.stats.utils.mysql.MySQLStats;
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.plugin.java.JavaPlugin;
  11.  
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. import java.util.UUID;
  15.  
  16. /**
  17. * Created by Jan (Lorenz) on 20.02.2016.
  18. */
  19. public class Main extends JavaPlugin implements StatsAPI {
  20.  
  21. private static Map<UUID, User> users = new HashMap<>();
  22.  
  23.  
  24.  
  25. public static String host;
  26. public static String port;
  27. public static String user;
  28. public static String password;
  29. public static String database;
  30. public static String table;
  31. public MySQLManager manager;
  32. public MySQLStats stats;
  33. public static boolean disable;
  34.  
  35. public MySQLManager getMySQL() {
  36. return manager;
  37. }
  38.  
  39. public MySQLStats getStats() {
  40. return stats;
  41. }
  42.  
  43. @Override
  44. public void onEnable() {
  45. disable = false;
  46. load();
  47. manager = new MySQLManager();
  48. stats = new MySQLStats();
  49. manager.connect();
  50. for (Player all : Bukkit.getOnlinePlayers()) {
  51. join(all);
  52. }
  53. Bukkit.getPluginManager().registerEvents(new JoinQuitListener(), this);
  54. }
  55.  
  56. private void load() {
  57. Cfg cfg = new Cfg("plugins/Stats/MySQLConfig.yml");
  58. if (!cfg.exist()) {
  59. cfg.putString("host", "host");
  60. cfg.putString("port", "3306");
  61. cfg.putString("user", "root");
  62. cfg.putString("password", "password");
  63. cfg.putString("database", "database");
  64. cfg.putString("table", "table");
  65. cfg.save();
  66. for (int i = 0; i < 10; i++) {
  67. System.err.println("MySQL Configuration not enabled");
  68. }
  69. Bukkit.shutdown();
  70. return;
  71. }
  72. cfg.load();
  73. host = cfg.getString("host");
  74. port = cfg.getString("port");
  75. user = cfg.getString("user");
  76. password = cfg.getString("password");
  77. database = cfg.getString("database");
  78. table = cfg.getString("table");
  79. }
  80.  
  81. @Override
  82. public void onDisable() {
  83. disable = true;
  84. for (Player all : Bukkit.getOnlinePlayers()) {
  85. quit(all);
  86. }
  87. manager.disconnect();
  88. }
  89.  
  90. public static void quit(Player p) {
  91. User user = getUser(p.getUniqueId());
  92. user.save();
  93. users.remove(p.getUniqueId());
  94. }
  95.  
  96. public static void join(Player p) {
  97. async(() -> {
  98. User user1 = new User(p.getUniqueId(), true, p.getName());
  99. user1.load();
  100. users.put(p.getUniqueId(), user1);
  101. });
  102. }
  103.  
  104. public static User getUser(UUID uuid) {
  105. if (users.containsKey(uuid)) {
  106. return users.get(uuid);
  107. }
  108. User user = new User(uuid, false);
  109. users.put(uuid, user);
  110. user.load();
  111. return user;
  112. }
  113.  
  114. public static Main getInstance() {
  115. return Main.getPlugin(Main.class);
  116. }
  117.  
  118. public static void async(Runnable runnable) {
  119. if (disable) {
  120. runnable.run(); // Ist besser.. :)
  121. // Bukkit.getScheduler().runTask(getInstance(), runnable);
  122. } else {
  123. Bukkit.getScheduler().runTaskAsynchronously(getInstance(), runnable);
  124. }
  125. }
  126.  
  127. public static int getKills(UUID uuid) {
  128. return getUser(uuid).getKills();
  129. }
  130.  
  131. public static int getDeaths(UUID uuid) {
  132. return getUser(uuid).getDeaths();
  133. }
  134.  
  135. public static int getCubes(UUID uuid) {
  136. return getUser(uuid).getCubes();
  137. }
  138.  
  139. public static void setKills(UUID uuid, int i) {
  140. if (i < 0) {
  141. throw new IllegalArgumentException("value cannot be lower than 0");
  142. }
  143. getUser(uuid).setKills(i);
  144. }
  145.  
  146. public static void setDeaths(UUID uuid, int i) {
  147. if (i < 0) {
  148. throw new IllegalArgumentException("value cannot be lower than 0");
  149. }
  150. getUser(uuid).setDeaths(i);
  151. }
  152.  
  153. public static void setCubes(UUID uuid, int i) {
  154. if (i < 0) {
  155. throw new IllegalArgumentException("value cannot be lower than 0");
  156. }
  157. getUser(uuid).setCubes(i);
  158. }
  159.  
  160. public static void debug(String text) {
  161. System.err.println(text);
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement