PageRenderTime 115ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/co/thecomet/redis/bungee/CRBungee.java

https://gitlab.com/TheComet/CometRedis
Java | 142 lines | 117 code | 25 blank | 0 comment | 6 complexity | 040f36e431eb81b35da2e239ddb6501b MD5 | raw file
  1. package co.thecomet.redis.bungee;
  2. import co.thecomet.db.mongodb.ResourceManager;
  3. import co.thecomet.redis.bungee.db.ProxySettings;
  4. import co.thecomet.redis.bungee.db.User;
  5. import co.thecomet.redis.bungee.server.DynamicRegistrationModule;
  6. import co.thecomet.redis.bungee.server.ProxySettingsManager;
  7. import co.thecomet.redis.redis.RedisHandler;
  8. import co.thecomet.redis.utils.JsonConfig;
  9. import com.google.gson.Gson;
  10. import com.google.gson.GsonBuilder;
  11. import net.jodah.expiringmap.ExpiringMap;
  12. import net.md_5.bungee.api.ProxyServer;
  13. import net.md_5.bungee.api.connection.ProxiedPlayer;
  14. import net.md_5.bungee.api.plugin.Plugin;
  15. import org.bson.types.ObjectId;
  16. import org.mongodb.morphia.Datastore;
  17. import org.mongodb.morphia.dao.BasicDAO;
  18. import java.io.File;
  19. import java.util.ArrayList;
  20. import java.util.HashSet;
  21. import java.util.UUID;
  22. import java.util.concurrent.TimeUnit;
  23. public class CRBungee extends Plugin {
  24. private static Gson gson = new GsonBuilder().create();
  25. private static CRBungee instance;
  26. private static boolean maintenance = false;
  27. private static ExpiringMap<UUID, User> users = ExpiringMap.builder().expiration(3, TimeUnit.MINUTES).expirationListener(new ExpiringMap.ExpirationListener<UUID, User>() {
  28. @Override
  29. public void expired(UUID uuid, User user) {
  30. ProxiedPlayer player = ProxyServer.getInstance().getPlayer(uuid);
  31. if (player != null) {
  32. ProxyServer.getInstance().getScheduler().schedule(CRBungee.getInstance(), () -> users.put(uuid, user), 0L, TimeUnit.MILLISECONDS);
  33. }
  34. }
  35. }).build();
  36. private static Datastore datastore;
  37. private static BasicDAO<User, ObjectId> userDAO;
  38. private static BasicDAO<ProxySettings, ObjectId> proxySettingsDAO;
  39. private DynamicSettings dynamicSettings;
  40. private RedisHandler<Plugin> redis;
  41. private DynamicRegistrationModule dynamicRegistrationModule;
  42. private ProxySettings proxySettings;
  43. public void onEnable() {
  44. instance = this;
  45. initMongoDB();
  46. loadConfig();
  47. redis = init("redis.json");
  48. new ProxySettingsManager(proxySettings);
  49. dynamicRegistrationModule = new DynamicRegistrationModule(this);
  50. }
  51. private RedisHandler init(String file) {
  52. return new RedisHandler<>(getLogger(), dynamicSettings.getRedis(), this, ProxyServer.getInstance().getScheduler()::runAsync);
  53. }
  54. public void initMongoDB() {
  55. datastore = ResourceManager.getInstance().getDatastore(new HashSet<Class>(new ArrayList<Class>(){{
  56. add(User.class);
  57. }}));
  58. datastore.ensureCaps();
  59. datastore.ensureIndexes();
  60. userDAO = new BasicDAO<>(User.class, datastore);
  61. proxySettingsDAO = new BasicDAO<>(ProxySettings.class, datastore);
  62. proxySettings = loadProxySettings();
  63. }
  64. public void onDisable() {
  65. redis.disable();
  66. }
  67. private DynamicSettings loadConfig() {
  68. if (!getDataFolder().exists()) {
  69. getLogger().info("Config folder not found! Creating...");
  70. getDataFolder().mkdir();
  71. }
  72. File file = new File(getDataFolder(), "settings.json");
  73. dynamicSettings = JsonConfig.load(file, DynamicSettings.class);
  74. if (!file.exists()) {
  75. dynamicSettings.save(file);
  76. }
  77. return dynamicSettings;
  78. }
  79. private ProxySettings loadProxySettings() {
  80. ProxySettings settings = proxySettingsDAO.find().get();
  81. if (settings == null) {
  82. settings = new ProxySettings();
  83. proxySettingsDAO.save(settings);
  84. }
  85. return settings;
  86. }
  87. public RedisHandler getRedis() {
  88. return redis;
  89. }
  90. public DynamicRegistrationModule getDynamicRegistrationModule() {
  91. return dynamicRegistrationModule;
  92. }
  93. public static CRBungee getInstance() {
  94. return instance;
  95. }
  96. public DynamicSettings getDynamicSettings() {
  97. return dynamicSettings;
  98. }
  99. public static boolean isMaintenance() {
  100. return maintenance;
  101. }
  102. public static void setMaintenance(boolean m) {
  103. maintenance = m;
  104. }
  105. public static ExpiringMap<UUID, User> getUsers() {
  106. return users;
  107. }
  108. public static BasicDAO<User, ObjectId> getUserDAO() {
  109. return userDAO;
  110. }
  111. public static BasicDAO<ProxySettings, ObjectId> getProxySettingsDAO() {
  112. return proxySettingsDAO;
  113. }
  114. public ProxySettings getProxySettings() {
  115. return proxySettings;
  116. }
  117. }