Plex/src/main/java/dev/plex/Plex.java

175 lines
5.0 KiB
Java
Raw Normal View History

2021-01-03 07:21:15 +00:00
package dev.plex;
2020-10-26 03:55:49 +00:00
import dev.plex.admin.Admin;
2021-01-03 07:21:15 +00:00
import dev.plex.admin.AdminList;
import dev.plex.banning.BanManager;
import dev.plex.cache.DataUtils;
2021-01-03 07:21:15 +00:00
import dev.plex.cache.MongoPlayerData;
import dev.plex.cache.PlayerCache;
2021-01-03 07:21:15 +00:00
import dev.plex.cache.SQLPlayerData;
import dev.plex.config.Config;
import dev.plex.handlers.CommandHandler;
import dev.plex.handlers.ListenerHandler;
import dev.plex.player.PlexPlayer;
import dev.plex.player.PunishedPlayer;
2021-01-03 07:21:15 +00:00
import dev.plex.punishment.PunishmentManager;
import dev.plex.rank.RankManager;
import dev.plex.services.ServiceManager;
import dev.plex.storage.MongoConnection;
import dev.plex.storage.RedisConnection;
import dev.plex.storage.SQLConnection;
import dev.plex.storage.StorageType;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import dev.plex.world.CustomWorld;
2022-01-27 21:23:01 +00:00
import java.util.UUID;
2022-01-04 03:04:39 +00:00
import lombok.Getter;
import lombok.Setter;
2022-02-01 06:31:06 +00:00
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
2020-10-26 03:55:49 +00:00
import org.bukkit.plugin.java.JavaPlugin;
@Getter
@Setter
2020-10-26 03:55:49 +00:00
public class Plex extends JavaPlugin
{
2020-10-29 02:35:14 +00:00
private static Plex plugin;
public Config config;
public Config messages;
private StorageType storageType = StorageType.SQLITE;
private SQLConnection sqlConnection;
private MongoConnection mongoConnection;
private RedisConnection redisConnection;
private MongoPlayerData mongoPlayerData;
private SQLPlayerData sqlPlayerData;
private RankManager rankManager;
private ServiceManager serviceManager;
2020-10-31 04:51:22 +00:00
2020-11-06 18:51:47 +00:00
private PunishmentManager punishmentManager;
private BanManager banManager;
2020-11-06 18:51:47 +00:00
2020-11-05 21:17:14 +00:00
private AdminList adminList;
2022-02-01 06:31:06 +00:00
private String system;
2022-01-29 22:35:48 +00:00
2020-10-28 03:49:56 +00:00
public static Plex get()
{
return plugin;
}
2020-10-26 03:55:49 +00:00
@Override
public void onLoad()
{
plugin = this;
config = new Config(this, "config.yml");
messages = new Config(this, "messages.yml");
sqlConnection = new SQLConnection();
mongoConnection = new MongoConnection();
redisConnection = new RedisConnection();
/*try {
redisConnection.openPool();
PlexLog.log("Successfully opened redis pool. Closing.");
} catch (Exception e) {
e.printStackTrace();
}
redisConnection.getJedis().close();*/
2020-10-26 03:55:49 +00:00
}
@Override
public void onEnable()
{
2020-10-28 03:49:56 +00:00
config.load();
messages.load();
2022-02-01 06:31:06 +00:00
system = config.getString("commands.permissions");
2020-10-28 19:14:44 +00:00
2020-10-29 00:54:23 +00:00
try
{
2020-10-28 19:14:44 +00:00
PlexUtils.testConnections();
PlexLog.log("Connected to " + storageType.name().toUpperCase());
2022-01-29 22:35:48 +00:00
}
catch (Exception e)
2020-10-28 19:14:44 +00:00
{
PlexLog.error("Failed to connect to " + storageType.name().toUpperCase());
e.printStackTrace();
}
2020-10-28 03:49:56 +00:00
2022-02-01 06:31:06 +00:00
// https://bstats.org/plugin/bukkit/Plex/14143
Metrics metrics = new Metrics(this, 14143);
PlexLog.log("Enabled Metrics");
2021-01-03 07:21:15 +00:00
if (storageType == StorageType.MONGODB)
{
mongoPlayerData = new MongoPlayerData();
2022-01-29 22:35:48 +00:00
}
else
2020-10-28 03:49:56 +00:00
{
sqlPlayerData = new SQLPlayerData();
}
new ListenerHandler();
new CommandHandler();
2022-01-29 22:35:48 +00:00
2022-01-30 01:53:22 +00:00
rankManager = new RankManager();
rankManager.generateDefaultRanks();
rankManager.importDefaultRanks();
adminList = new AdminList();
PlexLog.log("Rank Manager initialized");
2020-11-06 18:51:47 +00:00
punishmentManager = new PunishmentManager();
banManager = new BanManager();
PlexLog.log("Punishment System initialized");
generateWorlds();
serviceManager = new ServiceManager();
PlexLog.log("Service Manager initialized");
serviceManager.startServices();
PlexLog.log("Started " + serviceManager.serviceCount() + " services.");
2020-11-06 18:51:47 +00:00
reloadPlayers();
2020-10-26 03:55:49 +00:00
}
@Override
public void onDisable()
{
/*if (redisConnection.getJedis().isConnected())
{
PlexLog.log("Disabling Redis/Jedis. No memory leaks in this Anarchy server !");
redisConnection.getJedis().close();
}*/
2020-10-26 03:55:49 +00:00
}
2020-10-31 04:51:22 +00:00
private void generateWorlds()
{
PlexLog.log("Generating any worlds if needed...");
for (String key : config.getConfigurationSection("worlds").getKeys(false))
2020-11-06 01:29:38 +00:00
{
CustomWorld.generateConfigFlatWorld(key);
2020-11-06 01:29:38 +00:00
}
2020-10-31 04:51:22 +00:00
PlexLog.log("Finished with world generation!");
}
private void reloadPlayers()
{
Bukkit.getOnlinePlayers().forEach(player ->
{
PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId());
PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache
PlayerCache.getPunishedPlayerMap().put(player.getUniqueId(), new PunishedPlayer(player.getUniqueId()));
if (plugin.getRankManager().isAdmin(plexPlayer))
{
Admin admin = new Admin(UUID.fromString(plexPlayer.getUuid()));
admin.setRank(plexPlayer.getRankFromString());
plugin.getAdminList().addToCache(admin);
}
});
}
2020-10-26 03:55:49 +00:00
}