2021-01-03 07:21:15 +00:00
package dev.plex ;
2020-10-26 03:55:49 +00:00
2022-01-27 09:00:50 +00:00
import dev.plex.admin.Admin ;
2021-01-03 07:21:15 +00:00
import dev.plex.admin.AdminList ;
2022-01-27 09:00:50 +00:00
import dev.plex.cache.DataUtils ;
2022-04-11 17:56:26 +00:00
import dev.plex.cache.PlayerCache ;
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 ;
2022-01-27 09:00:50 +00:00
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 ;
2022-04-13 02:22:17 +00:00
import dev.plex.storage.permission.SQLPermissions ;
import dev.plex.storage.player.MongoPlayerData ;
import dev.plex.storage.player.SQLPlayerData ;
import dev.plex.storage.punishment.SQLNotes ;
import dev.plex.storage.punishment.SQLPunishment ;
2022-05-10 05:08:45 +00:00
import dev.plex.util.BuildInfo ;
import dev.plex.util.BungeeUtil ;
import dev.plex.util.PlexLog ;
import dev.plex.util.PlexUtils ;
import dev.plex.util.UpdateChecker ;
2022-08-03 00:03:04 +00:00
import dev.plex.util.redis.MessageUtil ;
2021-01-03 07:21:15 +00:00
import dev.plex.world.CustomWorld ;
2022-04-13 02:22:17 +00:00
import java.io.File ;
2022-01-04 03:04:39 +00:00
import lombok.Getter ;
import lombok.Setter ;
2022-08-02 12:08:52 +00:00
import net.milkbowl.vault.chat.Chat ;
import net.milkbowl.vault.permission.Permission ;
2022-02-01 06:31:06 +00:00
import org.bstats.bukkit.Metrics ;
2022-01-27 09:00:50 +00:00
import org.bukkit.Bukkit ;
2022-08-02 12:08:52 +00:00
import org.bukkit.plugin.RegisteredServiceProvider ;
import org.bukkit.plugin.java.JavaPlugin ;
2020-10-26 03:55:49 +00:00
2020-10-27 18:14:34 +00:00
@Getter
@Setter
2022-08-02 12:08:52 +00:00
public class Plex extends JavaPlugin
2022-03-17 22:16:17 +00:00
{
2022-08-02 12:08:52 +00:00
public static final BuildInfo build = new BuildInfo ( ) ;
2020-10-29 02:35:14 +00:00
private static Plex plugin ;
2020-10-31 15:09:13 +00:00
public Config config ;
2020-11-02 00:06:08 +00:00
public Config messages ;
2022-02-28 06:28:00 +00:00
public Config indefBans ;
2022-04-08 07:20:17 +00:00
public Config commands ;
2022-05-19 21:11:45 +00:00
public Config toggles ;
2022-03-06 01:04:34 +00:00
public File modulesFolder ;
2020-10-27 20:12:38 +00:00
private StorageType storageType = StorageType . SQLITE ;
2020-10-27 18:14:34 +00:00
private SQLConnection sqlConnection ;
private MongoConnection mongoConnection ;
2020-10-27 21:21:57 +00:00
private RedisConnection redisConnection ;
2022-04-07 00:38:15 +00:00
2022-05-18 10:31:15 +00:00
private PlayerCache playerCache ;
2020-10-27 20:12:38 +00:00
private MongoPlayerData mongoPlayerData ;
2020-10-27 21:04:05 +00:00
private SQLPlayerData sqlPlayerData ;
2022-04-07 00:38:15 +00:00
2022-04-04 08:36:50 +00:00
private SQLPunishment sqlPunishment ;
2022-04-07 00:38:15 +00:00
private SQLNotes sqlNotes ;
2022-04-11 17:56:26 +00:00
private SQLPermissions sqlPermissions ;
2022-04-07 00:38:15 +00:00
2022-03-06 01:04:34 +00:00
private ModuleManager moduleManager ;
2020-10-27 21:56:05 +00:00
private RankManager rankManager ;
2020-11-10 02:47:03 +00:00
private ServiceManager serviceManager ;
2020-11-06 18:51:47 +00:00
private PunishmentManager punishmentManager ;
2022-04-07 00:38:15 +00:00
2020-11-05 21:17:14 +00:00
private AdminList adminList ;
2022-03-03 21:45:19 +00:00
private UpdateChecker updateChecker ;
2022-02-01 06:31:06 +00:00
private String system ;
2022-01-29 22:35:48 +00:00
2022-08-02 12:08:52 +00:00
private Permission permissions ;
private Chat chat ;
2022-04-11 00:22:22 +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 ( )
{
2022-05-06 04:27:32 +00:00
super . onLoad ( ) ;
2020-10-27 18:14:34 +00:00
plugin = this ;
2020-10-31 15:09:13 +00:00
config = new Config ( this , " config.yml " ) ;
2020-11-02 00:06:08 +00:00
messages = new Config ( this , " messages.yml " ) ;
2022-02-28 06:28:00 +00:00
indefBans = new Config ( this , " indefbans.yml " ) ;
2022-04-08 07:20:17 +00:00
commands = new Config ( this , " commands.yml " ) ;
2022-05-19 21:11:45 +00:00
toggles = new Config ( this , " toggles.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 ( ) ;
2022-05-06 04:27:32 +00:00
2022-08-02 12:08:52 +00:00
//this.setChatHandler(new ChatListener.ChatHandlerImpl());
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 ( ) ;
2020-11-02 00:06:08 +00:00
messages . load ( ) ;
2022-05-19 21:11:45 +00:00
toggles . load ( ) ;
2022-05-18 10:31:15 +00:00
2022-05-26 04:10:20 +00:00
// Don't add default entries to these files
2022-03-06 01:20:51 +00:00
indefBans . load ( false ) ;
2022-04-08 07:20:17 +00:00
commands . load ( false ) ;
2022-03-05 23:44:38 +00:00
2022-04-04 08:36:50 +00:00
sqlConnection = new SQLConnection ( ) ;
mongoConnection = new MongoConnection ( ) ;
redisConnection = new RedisConnection ( ) ;
2022-05-18 10:40:29 +00:00
playerCache = new PlayerCache ( ) ;
2022-04-01 07:54:22 +00:00
system = config . getString ( " system " ) ;
2020-10-28 19:14:44 +00:00
2022-04-07 23:23:38 +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-13 02:22:17 +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
2022-08-02 12:08:52 +00:00
if ( system . equals ( " permissions " ) )
2022-04-11 00:22:22 +00:00
{
2022-08-02 12:08:52 +00:00
if ( ! getServer ( ) . getPluginManager ( ) . isPluginEnabled ( " Vault " ) )
2022-05-11 05:48:47 +00:00
{
2022-08-02 12:08:52 +00:00
throw new RuntimeException ( " Vault is required to run on the server if you use permissions! " ) ;
2022-05-11 05:48:47 +00:00
}
2022-04-11 00:22:22 +00:00
2022-08-02 12:08:52 +00:00
permissions = setupPermissions ( ) ;
chat = setupChat ( ) ;
2022-05-11 05:48:47 +00:00
}
2022-05-04 23:23:04 +00:00
2022-03-03 21:45:19 +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-08-03 00:03:04 +00:00
MessageUtil . subscribe ( ) ;
2022-04-13 02:22:17 +00:00
}
else
2022-03-17 22:16:17 +00:00
{
2022-02-04 08:18:07 +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 )
{
2020-10-27 20:12:38 +00:00
mongoPlayerData = new MongoPlayerData ( ) ;
2022-04-13 02:22:17 +00:00
}
else
2022-03-17 22:16:17 +00:00
{
2020-10-27 21:04:05 +00:00
sqlPlayerData = new SQLPlayerData ( ) ;
2022-04-04 08:36:50 +00:00
sqlPunishment = new SQLPunishment ( ) ;
2022-04-07 00:38:15 +00:00
sqlNotes = new SQLNotes ( ) ;
2022-04-11 17:56:26 +00:00
sqlPermissions = new SQLPermissions ( ) ;
2020-10-27 20:12:38 +00:00
}
2022-01-30 20:56:08 +00:00
new ListenerHandler ( ) ;
2020-10-31 08:55:27 +00:00
new CommandHandler ( ) ;
2020-10-27 21:56:05 +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-10-29 02:10:47 +00:00
2020-11-06 18:51:47 +00:00
punishmentManager = new PunishmentManager ( ) ;
2022-02-28 06:28:00 +00:00
punishmentManager . mergeIndefiniteBans ( ) ;
2020-11-10 02:47:03 +00:00
PlexLog . log ( " Punishment System initialized " ) ;
2022-01-30 20:56:08 +00:00
generateWorlds ( ) ;
2020-11-10 02:47:03 +00:00
serviceManager = new ServiceManager ( ) ;
PlexLog . log ( " Service Manager initialized " ) ;
serviceManager . startServices ( ) ;
PlexLog . log ( " Started " + serviceManager . serviceCount ( ) + " services. " ) ;
2020-11-06 18:51:47 +00:00
2022-01-27 09:00:50 +00:00
reloadPlayers ( ) ;
2022-05-10 01:05:31 +00:00
PlexLog . debug ( " Registered Bukkit -> BungeeCord Plugin Messaging Channel " ) ;
PlexLog . debug ( " Velocity Support? " + BungeeUtil . isVelocity ( ) ) ;
PlexLog . debug ( " BungeeCord Support? " + BungeeUtil . isBungeeCord ( ) ) ;
if ( BungeeUtil . isBungeeCord ( ) & & BungeeUtil . isVelocity ( ) )
{
PlexLog . warn ( " It seems you have both velocity and bungeecord configuration options enabled! When running Velocity, you do NOT need to enable bungeecord. " ) ;
}
this . getServer ( ) . getMessenger ( ) . registerOutgoingPluginChannel ( this , " BungeeCord " ) ;
2022-05-06 00:30:37 +00:00
moduleManager . enableModules ( ) ;
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 - >
{
2022-05-18 10:31:15 +00:00
PlexPlayer plexPlayer = playerCache . getPlexPlayerMap ( ) . get ( player . getUniqueId ( ) ) ; //get the player because it's literally impossible for them to not have an object
2022-02-22 06:55:59 +00:00
2022-03-17 22:16:17 +00:00
if ( plugin . getRankManager ( ) . isAdmin ( plexPlayer ) )
{
2022-04-07 03:55:01 +00:00
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-13 02:22:17 +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
{
2022-02-04 08:18:07 +00:00
PlexLog . log ( " Disabling Redis/Jedis. No memory leaks in this Anarchy server! " ) ;
2020-10-27 21:21:57 +00:00
redisConnection . getJedis ( ) . close ( ) ;
2022-02-04 08:18:07 +00:00
}
2022-03-06 01:04:34 +00:00
2022-05-10 01:05:31 +00:00
this . getServer ( ) . getMessenger ( ) . unregisterOutgoingPluginChannel ( this ) ;
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 ) )
{
2020-10-31 15:09:13 +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! " ) ;
}
2022-01-27 09:00:50 +00:00
2022-03-17 22:16:17 +00:00
private void reloadPlayers ( )
{
2022-01-27 09:00:50 +00:00
Bukkit . getOnlinePlayers ( ) . forEach ( player - >
{
PlexPlayer plexPlayer = DataUtils . getPlayer ( player . getUniqueId ( ) ) ;
2022-05-18 10:31:15 +00:00
playerCache . getPlexPlayerMap ( ) . put ( player . getUniqueId ( ) , plexPlayer ) ; //put them into the cache
2022-03-17 22:16:17 +00:00
if ( plugin . getRankManager ( ) . isAdmin ( plexPlayer ) )
{
2022-04-07 03:55:01 +00:00
Admin admin = new Admin ( plexPlayer . getUuid ( ) ) ;
2022-01-27 09:00:50 +00:00
admin . setRank ( plexPlayer . getRankFromString ( ) ) ;
plugin . getAdminList ( ) . addToCache ( admin ) ;
}
} ) ;
}
2022-05-18 10:31:15 +00:00
2022-08-02 12:08:52 +00:00
private Permission setupPermissions ( )
{
RegisteredServiceProvider < Permission > rsp = Bukkit . getServicesManager ( ) . getRegistration ( Permission . class ) ;
permissions = rsp . getProvider ( ) ;
return permissions ;
}
private Chat setupChat ( )
2022-05-18 10:31:15 +00:00
{
2022-08-02 12:08:52 +00:00
RegisteredServiceProvider < Chat > rsp = Bukkit . getServicesManager ( ) . getRegistration ( Chat . class ) ;
chat = rsp . getProvider ( ) ;
return chat ;
2022-05-18 10:31:15 +00:00
}
2022-04-10 18:33:49 +00:00
}