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

268 lines
8.2 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.cache.DataUtils;
import dev.plex.cache.player.MongoPlayerData;
import dev.plex.cache.player.PlayerCache;
import dev.plex.cache.player.SQLPlayerData;
2022-04-07 00:38:15 +00:00
import dev.plex.cache.sql.SQLNotes;
import dev.plex.cache.sql.SQLPunishment;
2022-04-09 02:23:22 +00:00
import dev.plex.command.blocker.CommandBlockerManager;
2021-01-03 07:21:15 +00:00
import dev.plex.config.Config;
import dev.plex.handlers.CommandHandler;
import dev.plex.handlers.ListenerHandler;
2022-03-06 01:04:34 +00:00
import dev.plex.module.ModuleManager;
import dev.plex.player.PlexPlayer;
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.util.UpdateChecker;
2021-01-03 07:21:15 +00:00
import dev.plex.world.CustomWorld;
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;
2022-04-07 00:38:15 +00:00
import java.io.File;
import java.io.InputStream;
import java.util.Properties;
@Getter
@Setter
2022-03-17 22:16:17 +00:00
public class Plex extends JavaPlugin
{
2022-04-04 20:18:20 +00:00
public static final BuildProperties build = new BuildProperties();
2020-10-29 02:35:14 +00:00
private static Plex plugin;
2022-04-07 00:38:15 +00:00
public Config config;
public Config messages;
public Config indefBans;
2022-04-08 05:16:05 +00:00
public Config blockedCommands;
2022-04-07 00:38:15 +00:00
2022-03-06 01:04:34 +00:00
public File modulesFolder;
private StorageType storageType = StorageType.SQLITE;
2022-04-07 00:38:15 +00:00
private SQLConnection sqlConnection;
private MongoConnection mongoConnection;
private RedisConnection redisConnection;
2022-04-07 00:38:15 +00:00
private MongoPlayerData mongoPlayerData;
private SQLPlayerData sqlPlayerData;
2022-04-07 00:38:15 +00:00
private SQLPunishment sqlPunishment;
2022-04-07 00:38:15 +00:00
private SQLNotes sqlNotes;
2022-03-06 01:04:34 +00:00
private ModuleManager moduleManager;
private RankManager rankManager;
private ServiceManager serviceManager;
2020-11-06 18:51:47 +00:00
private PunishmentManager punishmentManager;
2022-04-08 05:16:05 +00:00
private CommandBlockerManager commandBlockerManager;
2022-04-07 00:38:15 +00:00
2020-11-05 21:17:14 +00:00
private AdminList adminList;
private UpdateChecker updateChecker;
2022-02-01 06:31:06 +00:00
private String system;
2022-01-29 22:35:48 +00:00
2022-03-17 22:16:17 +00:00
public static Plex get()
{
2020-10-28 03:49:56 +00:00
return plugin;
}
2020-10-26 03:55:49 +00:00
@Override
2022-03-17 22:16:17 +00:00
public void onLoad()
{
plugin = this;
2022-04-10 06:55:20 +00:00
config = new Config(this, "config.yml");
messages = new Config(this, "messages.yml");
indefBans = new Config(this, "indefbans.yml");
blockedCommands = new Config(this, "commands.yml");
2022-03-26 22:22:08 +00:00
build.load(this);
2022-03-06 01:04:34 +00:00
modulesFolder = new File(this.getDataFolder() + File.separator + "modules");
2022-03-17 22:16:17 +00:00
if (!modulesFolder.exists())
{
modulesFolder.mkdir();
}
2022-03-06 01:04:34 +00:00
moduleManager = new ModuleManager();
moduleManager.loadAllModules();
moduleManager.loadModules();
2020-10-26 03:55:49 +00:00
}
@Override
2022-03-17 22:16:17 +00:00
public void onEnable()
{
2020-10-28 03:49:56 +00:00
config.load();
messages.load();
2022-03-17 22:16:17 +00:00
// Don't add default entries to indefinite ban file
2022-03-06 01:20:51 +00:00
indefBans.load(false);
2022-04-08 05:16:05 +00:00
blockedCommands.load();
sqlConnection = new SQLConnection();
mongoConnection = new MongoConnection();
redisConnection = new RedisConnection();
2022-03-06 01:04:34 +00:00
moduleManager.enableModules();
2022-04-01 07:54:22 +00:00
system = config.getString("system");
2020-10-28 19:14:44 +00:00
PlexLog.log("Attempting to connect to DB: {0}", plugin.config.getString("data.central.db"));
2022-03-17 22:16:17 +00:00
try
{
2020-10-28 19:14:44 +00:00
PlexUtils.testConnections();
PlexLog.log("Connected to " + storageType.name().toUpperCase());
2022-04-09 05:08:18 +00:00
}
catch (Exception e)
2022-03-17 22:16:17 +00:00
{
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
updateChecker = new UpdateChecker();
PlexLog.log("Update checking enabled");
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");
2022-04-07 07:37:31 +00:00
if (redisConnection != null && redisConnection.isEnabled())
2022-03-17 22:16:17 +00:00
{
2022-02-04 19:30:05 +00:00
redisConnection.getJedis();
2022-02-04 20:13:56 +00:00
PlexLog.log("Connected to Redis!");
2022-04-09 05:08:18 +00:00
}
else
2022-03-17 22:16:17 +00:00
{
PlexLog.log("Redis is disabled in the configuration file, not connecting.");
}
2022-02-01 06:31:06 +00:00
2022-03-17 22:16:17 +00:00
if (storageType == StorageType.MONGODB)
{
mongoPlayerData = new MongoPlayerData();
2022-04-09 05:08:18 +00:00
}
else
2022-03-17 22:16:17 +00:00
{
sqlPlayerData = new SQLPlayerData();
sqlPunishment = new SQLPunishment();
2022-04-07 00:38:15 +00:00
sqlNotes = new SQLNotes();
}
new ListenerHandler();
new CommandHandler();
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();
punishmentManager.mergeIndefiniteBans();
PlexLog.log("Punishment System initialized");
2022-04-08 05:16:05 +00:00
commandBlockerManager = new CommandBlockerManager();
PlexLog.log("Command Blocker 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
2022-03-17 22:16:17 +00:00
public void onDisable()
{
2022-02-22 06:55:59 +00:00
Bukkit.getOnlinePlayers().forEach(player ->
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId()); //get the player because it's literally impossible for them to not have an object
2022-03-17 22:16:17 +00:00
if (plugin.getRankManager().isAdmin(plexPlayer))
{
plugin.getAdminList().removeFromCache(plexPlayer.getUuid());
2022-02-22 06:55:59 +00:00
}
if (mongoPlayerData != null) //back to mongo checking
{
mongoPlayerData.update(plexPlayer); //update the player's document
2022-04-09 05:08:18 +00:00
}
else if (sqlPlayerData != null) //sql checking
2022-02-22 06:55:59 +00:00
{
sqlPlayerData.update(plexPlayer);
}
});
2022-04-07 07:37:31 +00:00
if (redisConnection != null && redisConnection.isEnabled() && redisConnection.getJedis().isConnected())
2022-03-17 22:16:17 +00:00
{
PlexLog.log("Disabling Redis/Jedis. No memory leaks in this Anarchy server!");
redisConnection.getJedis().close();
}
2022-03-06 01:04:34 +00:00
moduleManager.disableModules();
2020-10-26 03:55:49 +00:00
}
2020-10-31 04:51:22 +00:00
2022-03-17 22:16:17 +00:00
private void generateWorlds()
{
2020-10-31 04:51:22 +00:00
PlexLog.log("Generating any worlds if needed...");
2022-03-17 22:16:17 +00:00
for (String key : config.getConfigurationSection("worlds").getKeys(false))
{
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!");
}
2022-03-17 22:16:17 +00:00
private void reloadPlayers()
{
Bukkit.getOnlinePlayers().forEach(player ->
{
PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId());
PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache
2022-03-17 22:16:17 +00:00
if (plugin.getRankManager().isAdmin(plexPlayer))
{
Admin admin = new Admin(plexPlayer.getUuid());
admin.setRank(plexPlayer.getRankFromString());
plugin.getAdminList().addToCache(admin);
}
});
}
2022-03-26 21:36:29 +00:00
public static class BuildProperties
{
public String number;
2022-03-26 22:22:08 +00:00
public String author;
public String date;
public String head;
2022-03-26 21:36:29 +00:00
public void load(Plex plugin)
{
try
{
final Properties props;
2022-04-10 06:47:33 +00:00
try (InputStream in = plugin.getResource("resources/build.properties"))
2022-03-26 21:36:29 +00:00
{
props = new Properties();
props.load(in);
}
number = props.getProperty("buildNumber", "unknown");
2022-03-26 22:22:08 +00:00
author = props.getProperty("buildAuthor", "unknown");
date = props.getProperty("buildDate", "unknown");
head = props.getProperty("buildHead", "unknown");
2022-04-09 05:08:18 +00:00
}
catch (Exception ex)
2022-03-26 21:36:29 +00:00
{
PlexLog.error("Could not load build properties! Did you compile with NetBeans/Maven?");
}
}
}
2020-10-26 03:55:49 +00:00
}