Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package unique.core.managers;
- import java.util.Arrays;
- import org.bukkit.Bukkit;
- import org.bukkit.plugin.Plugin;
- import com.mongodb.MongoClient;
- import com.mongodb.MongoCredential;
- import com.mongodb.ServerAddress;
- import com.mongodb.client.MongoCollection;
- import com.mongodb.client.MongoDatabase;
- import redis.clients.jedis.JedisPool;
- import unique.core.Vituz;
- import unique.core.VituzAPI;
- import unique.core.punishments.redis.PunishSubscriber;
- import unique.core.ranks.redis.RankSubscriber;
- import unique.core.status.Server;
- import unique.core.status.handlers.DataSubscriptionHandler;
- import unique.core.status.handlers.ServerSubscriptionHandler;
- import unique.core.status.jedis.JedisPublisher;
- import unique.core.status.jedis.JedisSettings;
- import unique.core.status.jedis.JedisSubscriber;
- import unique.core.status.thread.UpdateThread;
- import unique.core.utilties.Color;
- import unique.core.utilties.Manager;
- import unique.core.utilties.Msg;
- import unique.core.utilties.Tasks;
- @SuppressWarnings("rawtypes")
- public class DatabaseManager extends Manager {
- private MongoClient client;
- private MongoDatabase database;
- private boolean connected, crates;
- private String username;
- private String password;
- private String db;
- private String host;
- private int port;
- public MongoCollection
- punishCollection,
- essCollection,
- crateCollection,
- meetupCollection,
- practiceCollection,
- rankGrantsCollection,
- rankProfilesCollection;
- private JedisSettings settings;
- private JedisPublisher publisher;
- private JedisSubscriber subscriber;
- private JedisSubscriber fisrtSubscriber;
- private JedisPublisher firstPublisher;
- private Server vituzServer;
- private JedisPool pool;
- private String addres;
- private PunishSubscriber punishSubscriber;
- private RankSubscriber rankSubscriber;
- public DatabaseManager(final Vituz plugin) {
- super(plugin);
- this.client = null;
- this.database = null;
- this.username = Vituz.getInstance().getConfig().getString("MONGO.USER");
- this.password = Vituz.getInstance().getConfig().getString("MONGO.PASSWORD");
- this.db = Vituz.getInstance().getConfig().getString("MONGO.DATABASE");
- this.host = Vituz.getInstance().getConfig().getString("MONGO.HOST");
- this.port = Vituz.getInstance().getConfig().getInt("MONGO.PORT");
- setupMongo(this.username, this.password, this.db, this.host, this.port);
- this.setDatabase(this.db);
- this.connected = false;
- this.crates = Vituz.getInstance().getConfig().getBoolean("CRATES");
- punishCollection = getDatabase().getCollection("punishmentProfiles");
- essCollection = getDatabase().getCollection("essentialsProfiles");
- if (crates) crateCollection = getDatabase().getCollection("crates");
- rankGrantsCollection = getDatabase().getCollection("rankGrants");
- rankProfilesCollection = getDatabase().getCollection("rankProfiles");
- practiceCollection = getDatabase().getCollection("practiceProfiles");
- meetupCollection = getDatabase().getCollection("meetupProfiles");
- Tasks.runLater(() -> {
- this.connected = true;
- Msg.logConsole("&a&lSuccessfully connected all databases to mongo!");
- }, 20L);
- }
- @SuppressWarnings("deprecation")
- public void setupMongo(final String username, final String password, final String database2, final String host, final int port) {
- final MongoCredential credential = MongoCredential.createCredential(username, database2, password.toCharArray());
- this.client = new MongoClient(new ServerAddress(host, port), Arrays.asList(credential));
- }
- public void setDatabase(String db) {
- this.database = client.getDatabase(db);
- if (this.client == null) {
- Bukkit.getServer().getPluginManager().disablePlugin((Plugin)Vituz.getInstance());
- Bukkit.getConsoleSender().sendMessage(Color.translate("&cMongo failed to connect!"));
- return;
- }
- Bukkit.getConsoleSender().sendMessage(Color.translate("&aMongo is now successfully connected!"));
- }
- public void closeConnection() {
- if (this.client != null) {
- this.client.close();
- Bukkit.getConsoleSender().sendMessage(Color.translate("&aMongo has been successfully disconnected!"));
- }
- }
- public MongoClient getMongo() {
- return this.client;
- }
- public MongoDatabase getDatabase() {
- return this.database;
- }
- public String getUsername() {
- return this.username;
- }
- public String getPassword() {
- return this.password;
- }
- public String getDb() {
- return this.db;
- }
- public String getHost() {
- return this.host;
- }
- public int getPort() {
- return this.port;
- }
- public void setMongo(final MongoClient mongo) {
- this.client = mongo;
- }
- public void setUsername(final String username) {
- this.username = username;
- }
- public void setPassword(final String password) {
- this.password = password;
- }
- public void setDb(final String db) {
- this.db = db;
- }
- public void setHost(final String host) {
- this.host = host;
- }
- public void setPort(final int port) {
- this.port = port;
- }
- public void setupRedis() {
- this.pool = new JedisPool("88.198.111.235");
- this.punishSubscriber = new PunishSubscriber();
- this.rankSubscriber = new RankSubscriber();
- }
- public void loadStatus() {
- this.vituzServer = new Server(VituzAPI.getServerName(), true);
- this.settings = new JedisSettings("88.198.111.235", 6379, "819478549245875758709257547");
- this.fisrtSubscriber = new JedisSubscriber(JedisSubscriber.INDEPENDENT, this.settings, new ServerSubscriptionHandler());
- (this.firstPublisher = new JedisPublisher(this.settings)).start();
- this.subscriber = new JedisSubscriber(JedisSubscriber.BUKKIT, this.settings, new DataSubscriptionHandler());
- (this.publisher = new JedisPublisher(this.settings)).start();
- new UpdateThread().start();
- }
- public MongoClient getClient() {
- return this.client;
- }
- public boolean isCrates() {
- return this.crates;
- }
- public boolean isConnected() {
- return this.connected;
- }
- public boolean isDevMode() {
- this.getClass();
- return false;
- }
- public JedisSettings getSettings() {
- return this.settings;
- }
- public JedisPublisher getPublisher() {
- return this.publisher;
- }
- public JedisSubscriber getSubscriber() {
- return this.subscriber;
- }
- public JedisSubscriber getFisrtSubscriber() {
- return this.fisrtSubscriber;
- }
- public JedisPublisher getFirstPublisher() {
- return this.firstPublisher;
- }
- public Server getVituzServer() {
- return this.vituzServer;
- }
- public JedisPool getPool() {
- return this.pool;
- }
- public String getAddres() {
- return this.addres;
- }
- public PunishSubscriber getPunishSubscriber() {
- return this.punishSubscriber;
- }
- public RankSubscriber getRankSubscriber() {
- return this.rankSubscriber;
- }
- public MongoCollection getCrateCollection() {
- return crateCollection;
- }
- public MongoCollection getRanksProfilesCollection() {
- return rankProfilesCollection;
- }
- public MongoCollection getRanksGrantsCollection() {
- return rankGrantsCollection;
- }
- public MongoCollection getEssentialsCollection() {
- return essCollection;
- }
- public MongoCollection getPunishCollection() {
- return punishCollection;
- }
- public MongoCollection getMeetupCollection() {
- return meetupCollection;
- }
- public MongoCollection getPracticeCollection() {
- return practiceCollection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement