Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. package unique.core.managers;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.plugin.Plugin;
  7.  
  8. import com.mongodb.MongoClient;
  9. import com.mongodb.MongoCredential;
  10. import com.mongodb.ServerAddress;
  11. import com.mongodb.client.MongoCollection;
  12. import com.mongodb.client.MongoDatabase;
  13.  
  14. import redis.clients.jedis.JedisPool;
  15. import unique.core.Vituz;
  16. import unique.core.VituzAPI;
  17. import unique.core.punishments.redis.PunishSubscriber;
  18. import unique.core.ranks.redis.RankSubscriber;
  19. import unique.core.status.Server;
  20. import unique.core.status.handlers.DataSubscriptionHandler;
  21. import unique.core.status.handlers.ServerSubscriptionHandler;
  22. import unique.core.status.jedis.JedisPublisher;
  23. import unique.core.status.jedis.JedisSettings;
  24. import unique.core.status.jedis.JedisSubscriber;
  25. import unique.core.status.thread.UpdateThread;
  26. import unique.core.utilties.Color;
  27. import unique.core.utilties.Manager;
  28. import unique.core.utilties.Msg;
  29. import unique.core.utilties.Tasks;
  30.  
  31. @SuppressWarnings("rawtypes")
  32. public class DatabaseManager extends Manager {
  33.  
  34. private MongoClient client;
  35. private MongoDatabase database;
  36. private boolean connected, crates;
  37. private String username;
  38. private String password;
  39. private String db;
  40. private String host;
  41. private int port;
  42. public MongoCollection
  43. punishCollection,
  44. essCollection,
  45. crateCollection,
  46. meetupCollection,
  47. practiceCollection,
  48. rankGrantsCollection,
  49. rankProfilesCollection;
  50. private JedisSettings settings;
  51. private JedisPublisher publisher;
  52. private JedisSubscriber subscriber;
  53. private JedisSubscriber fisrtSubscriber;
  54. private JedisPublisher firstPublisher;
  55. private Server vituzServer;
  56. private JedisPool pool;
  57. private String addres;
  58. private PunishSubscriber punishSubscriber;
  59. private RankSubscriber rankSubscriber;
  60.  
  61. public DatabaseManager(final Vituz plugin) {
  62. super(plugin);
  63. this.client = null;
  64. this.database = null;
  65. this.username = Vituz.getInstance().getConfig().getString("MONGO.USER");
  66. this.password = Vituz.getInstance().getConfig().getString("MONGO.PASSWORD");
  67. this.db = Vituz.getInstance().getConfig().getString("MONGO.DATABASE");
  68. this.host = Vituz.getInstance().getConfig().getString("MONGO.HOST");
  69. this.port = Vituz.getInstance().getConfig().getInt("MONGO.PORT");
  70. setupMongo(this.username, this.password, this.db, this.host, this.port);
  71. this.setDatabase(this.db);
  72. this.connected = false;
  73. this.crates = Vituz.getInstance().getConfig().getBoolean("CRATES");
  74. punishCollection = getDatabase().getCollection("punishmentProfiles");
  75. essCollection = getDatabase().getCollection("essentialsProfiles");
  76. if (crates) crateCollection = getDatabase().getCollection("crates");
  77. rankGrantsCollection = getDatabase().getCollection("rankGrants");
  78. rankProfilesCollection = getDatabase().getCollection("rankProfiles");
  79. practiceCollection = getDatabase().getCollection("practiceProfiles");
  80. meetupCollection = getDatabase().getCollection("meetupProfiles");
  81.  
  82. Tasks.runLater(() -> {
  83. this.connected = true;
  84. Msg.logConsole("&a&lSuccessfully connected all databases to mongo!");
  85. }, 20L);
  86. }
  87.  
  88. @SuppressWarnings("deprecation")
  89. public void setupMongo(final String username, final String password, final String database2, final String host, final int port) {
  90. final MongoCredential credential = MongoCredential.createCredential(username, database2, password.toCharArray());
  91. this.client = new MongoClient(new ServerAddress(host, port), Arrays.asList(credential));
  92. }
  93.  
  94. public void setDatabase(String db) {
  95. this.database = client.getDatabase(db);
  96. if (this.client == null) {
  97. Bukkit.getServer().getPluginManager().disablePlugin((Plugin)Vituz.getInstance());
  98. Bukkit.getConsoleSender().sendMessage(Color.translate("&cMongo failed to connect!"));
  99. return;
  100. }
  101. Bukkit.getConsoleSender().sendMessage(Color.translate("&aMongo is now successfully connected!"));
  102. }
  103.  
  104. public void closeConnection() {
  105. if (this.client != null) {
  106. this.client.close();
  107. Bukkit.getConsoleSender().sendMessage(Color.translate("&aMongo has been successfully disconnected!"));
  108. }
  109. }
  110.  
  111. public MongoClient getMongo() {
  112. return this.client;
  113. }
  114.  
  115. public MongoDatabase getDatabase() {
  116. return this.database;
  117. }
  118.  
  119. public String getUsername() {
  120. return this.username;
  121. }
  122.  
  123. public String getPassword() {
  124. return this.password;
  125. }
  126.  
  127. public String getDb() {
  128. return this.db;
  129. }
  130.  
  131. public String getHost() {
  132. return this.host;
  133. }
  134.  
  135. public int getPort() {
  136. return this.port;
  137. }
  138.  
  139. public void setMongo(final MongoClient mongo) {
  140. this.client = mongo;
  141. }
  142.  
  143. public void setUsername(final String username) {
  144. this.username = username;
  145. }
  146.  
  147. public void setPassword(final String password) {
  148. this.password = password;
  149. }
  150.  
  151. public void setDb(final String db) {
  152. this.db = db;
  153. }
  154.  
  155. public void setHost(final String host) {
  156. this.host = host;
  157. }
  158.  
  159. public void setPort(final int port) {
  160. this.port = port;
  161. }
  162.  
  163. public void setupRedis() {
  164. this.pool = new JedisPool("88.198.111.235");
  165. this.punishSubscriber = new PunishSubscriber();
  166. this.rankSubscriber = new RankSubscriber();
  167. }
  168.  
  169. public void loadStatus() {
  170. this.vituzServer = new Server(VituzAPI.getServerName(), true);
  171. this.settings = new JedisSettings("88.198.111.235", 6379, "819478549245875758709257547");
  172. this.fisrtSubscriber = new JedisSubscriber(JedisSubscriber.INDEPENDENT, this.settings, new ServerSubscriptionHandler());
  173. (this.firstPublisher = new JedisPublisher(this.settings)).start();
  174. this.subscriber = new JedisSubscriber(JedisSubscriber.BUKKIT, this.settings, new DataSubscriptionHandler());
  175. (this.publisher = new JedisPublisher(this.settings)).start();
  176. new UpdateThread().start();
  177. }
  178.  
  179. public MongoClient getClient() {
  180. return this.client;
  181. }
  182.  
  183. public boolean isCrates() {
  184. return this.crates;
  185. }
  186.  
  187. public boolean isConnected() {
  188. return this.connected;
  189. }
  190.  
  191. public boolean isDevMode() {
  192. this.getClass();
  193. return false;
  194. }
  195.  
  196. public JedisSettings getSettings() {
  197. return this.settings;
  198. }
  199.  
  200. public JedisPublisher getPublisher() {
  201. return this.publisher;
  202. }
  203.  
  204. public JedisSubscriber getSubscriber() {
  205. return this.subscriber;
  206. }
  207.  
  208. public JedisSubscriber getFisrtSubscriber() {
  209. return this.fisrtSubscriber;
  210. }
  211.  
  212. public JedisPublisher getFirstPublisher() {
  213. return this.firstPublisher;
  214. }
  215.  
  216. public Server getVituzServer() {
  217. return this.vituzServer;
  218. }
  219.  
  220. public JedisPool getPool() {
  221. return this.pool;
  222. }
  223.  
  224. public String getAddres() {
  225. return this.addres;
  226. }
  227.  
  228. public PunishSubscriber getPunishSubscriber() {
  229. return this.punishSubscriber;
  230. }
  231.  
  232. public RankSubscriber getRankSubscriber() {
  233. return this.rankSubscriber;
  234. }
  235.  
  236. public MongoCollection getCrateCollection() {
  237. return crateCollection;
  238. }
  239.  
  240. public MongoCollection getRanksProfilesCollection() {
  241. return rankProfilesCollection;
  242. }
  243.  
  244. public MongoCollection getRanksGrantsCollection() {
  245. return rankGrantsCollection;
  246. }
  247.  
  248. public MongoCollection getEssentialsCollection() {
  249. return essCollection;
  250. }
  251.  
  252. public MongoCollection getPunishCollection() {
  253. return punishCollection;
  254. }
  255.  
  256. public MongoCollection getMeetupCollection() {
  257. return meetupCollection;
  258. }
  259.  
  260. public MongoCollection getPracticeCollection() {
  261. return practiceCollection;
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement