2022-05-06 02:28:39 +00:00
package dev.plex ;
import dev.plex.command.GuildCommand ;
import dev.plex.data.SQLGuildManager ;
import dev.plex.data.SQLManager ;
import dev.plex.guild.Guild ;
import dev.plex.guild.GuildHolder ;
2022-05-07 22:52:04 +00:00
import dev.plex.handler.ChatHandlerImpl ;
2022-05-07 22:49:50 +00:00
import dev.plex.listener.impl.ChatListener ;
2022-05-06 02:28:39 +00:00
import dev.plex.module.PlexModule ;
import dev.plex.storage.StorageType ;
import dev.plex.util.PlexLog ;
2022-05-07 22:49:50 +00:00
import io.papermc.paper.event.player.AsyncChatEvent ;
2022-05-06 02:28:39 +00:00
import lombok.Getter ;
import java.util.Arrays ;
//TODO: Implement mongodb
@Getter
public class Guilds extends PlexModule
{
private static Guilds module ;
2022-05-07 22:49:50 +00:00
private final GuildHolder guildHolder = new GuildHolder ( ) ;
2022-05-06 02:28:39 +00:00
private SQLGuildManager sqlGuildManager ;
@Override
public void load ( )
{
module = this ;
}
@Override
public void enable ( )
{
if ( getPlex ( ) . getStorageType ( ) = = StorageType . MONGODB )
{
getPlex ( ) . getMongoConnection ( ) . getDatastore ( ) . getMapper ( ) . map ( Guild . class ) ;
getPlex ( ) . getMongoConnection ( ) . getDatastore ( ) . ensureIndexes ( ) ;
} else {
SQLManager . makeTables ( ) ;
sqlGuildManager = new SQLGuildManager ( ) ;
sqlGuildManager . getGuilds ( ) . whenComplete ( ( guilds , throwable ) - > {
PlexLog . debug ( " Finished loading {0} guilds " , guilds . size ( ) ) ;
guilds . forEach ( guildHolder : : addGuild ) ;
this . registerCommand ( new GuildCommand ( ) ) ;
} ) ;
}
2022-05-07 22:52:04 +00:00
this . getPlex ( ) . setChatHandler ( new ChatHandlerImpl ( ) ) ;
2022-05-06 02:28:39 +00:00
this . addDefaultMessage ( " guildsHelpCommand " , " <gradient:gold:yellow>======</gradient>Guild Menu<gradient:gold:yellow>======</gradient><newline> \ n " +
" <newline><gold>/guild <gray>Returns this menu \ n " +
" <newline><gold>/guild help " ) ;
2022-05-08 05:54:22 +00:00
this . addDefaultMessage ( " guildCommandNotFound " , " <red>'{0}'<gold> is not a valid sub command! " , " 0 - The sub command " ) ;
2022-05-06 02:28:39 +00:00
this . addDefaultMessage ( " guildNotFound " , " <red>You're currently not a part of a guild! " ) ;
this . addDefaultMessage ( " alreadyInGuild " , " <red>You're currently in a guild. Please do <gold>/guild leave<red> if you're a member, or if you're an owner with members, <gold>/guild promote <player><red> then <gold>/guild leave<red>, or just an owner, <gold>/guild disband<red>. " ) ;
2022-05-08 05:54:22 +00:00
this . addDefaultMessage ( " guildNotOwner " , " <red>You're not the owner of this guild! " ) ;
this . addDefaultMessage ( " guildPrefixSet " , " <green>You have changed the guild prefix to '<gold>{0}</gold>' " , " 0 - The new prefix " ) ;
this . addDefaultMessage ( " guildPrefixCleared " , " <green>Your guild's prefix has been cleared. " ) ;
2022-05-06 02:28:39 +00:00
}
@Override
public void disable ( )
{
// Unregistering listeners / commands is handled by Plex
2022-05-08 05:54:22 +00:00
this . getGuildHolder ( ) . getGuilds ( ) . forEach ( sqlGuildManager : : updateGuild ) ;
this . getPlex ( ) . setChatHandler ( new ChatListener . ChatHandlerImpl ( ) ) ;
2022-05-06 02:28:39 +00:00
}
private void addDefaultMessage ( String message , Object initValue )
{
if ( Plex . get ( ) . messages . getString ( message ) = = null )
{
Plex . get ( ) . messages . set ( message , initValue ) ;
Plex . get ( ) . messages . save ( ) ;
PlexLog . debug ( " '{0}' message added from TFMExtras module " , message ) ;
}
}
private void addDefaultMessage ( String message , Object initValue , String . . . comments )
{
if ( Plex . get ( ) . messages . getString ( message ) = = null )
{
Plex . get ( ) . messages . set ( message , initValue ) ;
Plex . get ( ) . messages . save ( ) ;
Plex . get ( ) . messages . setComments ( message , Arrays . asList ( comments ) ) ;
Plex . get ( ) . messages . save ( ) ;
PlexLog . debug ( " '{0}' message added from Plex-Guilds module " , message ) ;
}
}
public static Guilds get ( )
{
return module ;
}
}