2021-05-13 12:11:59 +00:00
package com.alttd.chat.config ;
2021-05-10 08:45:43 +00:00
import com.google.common.base.Throwables ;
import com.google.common.collect.Lists ;
import com.google.common.reflect.TypeToken ;
import ninja.leaping.configurate.ConfigurationNode ;
2021-05-25 12:49:15 +00:00
import ninja.leaping.configurate.ConfigurationOptions ;
2021-05-10 08:45:43 +00:00
import ninja.leaping.configurate.objectmapping.ObjectMappingException ;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader ;
import org.yaml.snakeyaml.DumperOptions ;
import java.io.File ;
import java.io.IOException ;
import java.lang.reflect.InvocationTargetException ;
import java.lang.reflect.Method ;
import java.lang.reflect.Modifier ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.regex.Pattern ;
public final class Config {
private static final Pattern PATH_PATTERN = Pattern . compile ( " \\ . " ) ;
private static final String HEADER = " " ;
private static File CONFIG_FILE ;
public static ConfigurationNode config ;
public static YAMLConfigurationLoader configLoader ;
static int version ;
static boolean verbose ;
2021-05-24 07:53:45 +00:00
public static void init ( File path ) {
CONFIG_FILE = new File ( path , " config.yml " ) ; ;
// public static void init() { // todo setup share for the config
// CONFIG_FILE = new File(new File(System.getProperty("user.home")+File.separator+"ChatPlugin"), "config.yml");;
2021-05-10 08:45:43 +00:00
configLoader = YAMLConfigurationLoader . builder ( )
. setFile ( CONFIG_FILE )
. setFlowStyle ( DumperOptions . FlowStyle . BLOCK )
. build ( ) ;
if ( ! CONFIG_FILE . getParentFile ( ) . exists ( ) ) {
if ( ! CONFIG_FILE . getParentFile ( ) . mkdirs ( ) ) {
return ;
}
}
if ( ! CONFIG_FILE . exists ( ) ) {
try {
if ( ! CONFIG_FILE . createNewFile ( ) ) {
return ;
}
} catch ( IOException error ) {
error . printStackTrace ( ) ;
}
}
try {
2021-05-25 12:49:15 +00:00
config = configLoader . load ( ConfigurationOptions . defaults ( ) . setHeader ( HEADER ) ) ;
2021-05-10 08:45:43 +00:00
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
verbose = getBoolean ( " verbose " , true ) ;
version = getInt ( " config-version " , 1 ) ;
readConfig ( Config . class , null ) ;
try {
configLoader . save ( config ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
public static void readConfig ( Class < ? > clazz , Object instance ) {
for ( Method method : clazz . getDeclaredMethods ( ) ) {
if ( Modifier . isPrivate ( method . getModifiers ( ) ) ) {
if ( method . getParameterTypes ( ) . length = = 0 & & method . getReturnType ( ) = = Void . TYPE ) {
try {
method . setAccessible ( true ) ;
method . invoke ( instance ) ;
} catch ( InvocationTargetException | IllegalAccessException ex ) {
throw Throwables . propagate ( ex . getCause ( ) ) ;
}
}
}
}
try {
configLoader . save ( config ) ;
} catch ( IOException ex ) {
throw Throwables . propagate ( ex . getCause ( ) ) ;
}
}
public static void saveConfig ( ) {
try {
configLoader . save ( config ) ;
} catch ( IOException ex ) {
throw Throwables . propagate ( ex . getCause ( ) ) ;
}
}
private static Object [ ] splitPath ( String key ) {
return PATH_PATTERN . split ( key ) ;
}
private static void set ( String path , Object def ) {
if ( config . getNode ( splitPath ( path ) ) . isVirtual ( ) )
config . getNode ( splitPath ( path ) ) . setValue ( def ) ;
}
private static void setString ( String path , String def ) {
try {
if ( config . getNode ( splitPath ( path ) ) . isVirtual ( ) )
config . getNode ( splitPath ( path ) ) . setValue ( TypeToken . of ( String . class ) , def ) ;
} catch ( ObjectMappingException ex ) {
}
}
private static boolean getBoolean ( String path , boolean def ) {
set ( path , def ) ;
return config . getNode ( splitPath ( path ) ) . getBoolean ( def ) ;
}
private static double getDouble ( String path , double def ) {
set ( path , def ) ;
return config . getNode ( splitPath ( path ) ) . getDouble ( def ) ;
}
private static int getInt ( String path , int def ) {
set ( path , def ) ;
return config . getNode ( splitPath ( path ) ) . getInt ( def ) ;
}
private static String getString ( String path , String def ) {
setString ( path , def ) ;
return config . getNode ( splitPath ( path ) ) . getString ( def ) ;
}
private static Long getLong ( String path , Long def ) {
set ( path , def ) ;
return config . getNode ( splitPath ( path ) ) . getLong ( def ) ;
}
private static < T > List < String > getList ( String path , T def ) {
try {
set ( path , def ) ;
return config . getNode ( splitPath ( path ) ) . getList ( TypeToken . of ( String . class ) ) ;
} catch ( ObjectMappingException ex ) {
}
return new ArrayList < > ( ) ;
}
2021-05-13 21:05:26 +00:00
private static ConfigurationNode getNode ( String path ) {
if ( config . getNode ( splitPath ( path ) ) . isVirtual ( ) ) {
2021-05-25 12:49:15 +00:00
//new RegexConfig("Dummy");
2021-05-13 21:05:26 +00:00
}
2021-05-25 12:49:15 +00:00
config . getChildrenMap ( ) ;
2021-05-13 21:05:26 +00:00
return config . getNode ( splitPath ( path ) ) ;
}
2021-05-10 08:45:43 +00:00
/** ONLY EDIT ANYTHING BELOW THIS LINE **/
public static List < String > PREFIXGROUPS = new ArrayList < > ( ) ;
2021-05-15 01:02:52 +00:00
public static List < String > STAFFGROUPS = new ArrayList < > ( ) ;
2021-05-15 19:39:51 +00:00
public static String MINIMIUMSTAFFRANK = " trainee " ;
2021-05-13 12:11:29 +00:00
public static String CONSOLENAME = " Console " ;
2021-05-10 08:45:43 +00:00
private static void settings ( ) {
PREFIXGROUPS = getList ( " settings.prefix-groups " ,
Lists . newArrayList ( " discord " , " socialmedia " , " eventteam " , " eventleader " , " youtube " , " twitch " , " developer " ) ) ;
2021-05-15 01:02:52 +00:00
STAFFGROUPS = getList ( " settings.staff-groups " ,
Lists . newArrayList ( " trainee " , " moderator " , " headmod " , " admin " , " manager " , " owner " ) ) ;
2021-05-13 12:11:29 +00:00
CONSOLENAME = getString ( " settings.console-name " , CONSOLENAME ) ;
2021-05-15 19:39:51 +00:00
MINIMIUMSTAFFRANK = getString ( " settings.minimum-staff-rank " , MINIMIUMSTAFFRANK ) ;
2021-05-10 08:45:43 +00:00
}
public static List < String > MESSAGECOMMANDALIASES = new ArrayList < > ( ) ;
public static List < String > REPLYCOMMANDALIASES = new ArrayList < > ( ) ;
2021-05-25 14:54:19 +00:00
public static String MESSAGESENDER = " <hover:show_text:Click to reply><click:suggest_command:/msg <receiver> ><light_purple>(Me -> <gray><receiver></gray>) <message> " ;
public static String MESSAGERECIEVER = " <hover:show_text:Click to reply><click:suggest_command:/msg <sender> ><light_purple>(<gray><sender></gray> on <server> -> Me) <message> " ;
2021-05-10 08:45:43 +00:00
private static void messageCommand ( ) {
MESSAGECOMMANDALIASES . clear ( ) ;
REPLYCOMMANDALIASES . clear ( ) ;
MESSAGECOMMANDALIASES = getList ( " commands.message.aliases " , Lists . newArrayList ( " msg " , " whisper " , " tell " ) ) ;
REPLYCOMMANDALIASES = getList ( " commands.reply.aliases " , Lists . newArrayList ( " r " ) ) ;
MESSAGESENDER = getString ( " commands.message.sender-message " , MESSAGESENDER ) ;
MESSAGERECIEVER = getString ( " commands.message.reciever-message " , MESSAGERECIEVER ) ;
}
2021-05-24 07:53:45 +00:00
///broadcast <white><light_purple><prefix></light_purple> <gray>Momlly</gray> <hover:show_text:on Atoll><yellow>to Global</yellow></hover><gray>: We Love <gold>Teri</gold> and <light_purple>Kappa</light_purple></gray></white>
2021-05-25 14:54:19 +00:00
public static String GCFORMAT = " <white><light_purple><prefix></light_purple> <gray><sender></gray> <hover:show_text:on <server>><yellow>to Global</yellow></hover><gray>: <message> " ;
2021-05-10 08:45:43 +00:00
public static String GCPERMISSION = " proxy.globalchat " ;
2021-05-22 18:34:32 +00:00
public static List < String > GCALIAS = new ArrayList < > ( ) ;
public static String GCNOTENABLED = " You don't have global chat enabled. " ;
2021-05-10 08:45:43 +00:00
private static void globalChat ( ) {
MESSAGERECIEVER = getString ( " commands.globalchat.format " , MESSAGERECIEVER ) ;
GCPERMISSION = getString ( " commands.globalchat.view-chat-permission " , GCPERMISSION ) ;
2021-05-22 18:34:32 +00:00
GCALIAS . clear ( ) ;
GCALIAS = getList ( " commands.globalchat.alias " , Lists . newArrayList ( " gc " , " global " ) ) ;
GCNOTENABLED = getString ( " commands.globalchat.not-enabled " , GCNOTENABLED ) ;
2021-05-10 08:45:43 +00:00
}
public static List < String > GACECOMMANDALIASES = new ArrayList < > ( ) ;
2021-05-25 14:54:19 +00:00
public static String GACFORMAT = " <hover:show_text:Click to reply><click:suggest_command:/acg ><yellow>(<sender> on <server> -> Team) <message> " ;
2021-05-10 08:45:43 +00:00
private static void globalAdminChat ( ) {
GACECOMMANDALIASES = getList ( " commands.globaladminchat.aliases " , Lists . newArrayList ( " acg " ) ) ;
GACFORMAT = getString ( " commands.globaladminchat.format " , GACFORMAT ) ;
}
2021-05-13 18:27:20 +00:00
public static String MESSAGECHANNEL = " altitude:chatplugin " ;
private static void messageChannels ( ) {
MESSAGECHANNEL = getString ( " settings.message-channel " , MESSAGECHANNEL ) ;
}
2021-05-13 21:05:26 +00:00
public static ConfigurationNode REGEXNODE = null ;
private static void RegexNOde ( ) {
REGEXNODE = getNode ( " regex-settings " ) ;
}
2021-05-25 14:54:19 +00:00
public static String SERVERSWTICHMESSAGEFROM = " <gray>* <player> comes from <from_server>... " ;
2021-05-15 11:26:31 +00:00
public static String SERVERSWTICHMESSAGETO = " <gray>* <player> leaves to <to_server>... " ;
public static String SERVERJOINMESSAGE = " <green>* <player> appears from thin air... " ;
public static String SERVERLEAVEMESSAGE = " <red>* <player> vanishes in the mist... " ;
2021-05-15 09:42:54 +00:00
private static void JoinLeaveMessages ( ) {
SERVERSWTICHMESSAGEFROM = getString ( " messages.switch-server-from " , SERVERSWTICHMESSAGEFROM ) ;
SERVERSWTICHMESSAGETO = getString ( " messages.switch-server-to " , SERVERSWTICHMESSAGETO ) ;
SERVERJOINMESSAGE = getString ( " messages.join-server " , SERVERJOINMESSAGE ) ;
SERVERLEAVEMESSAGE = getString ( " messages.leave-server " , SERVERLEAVEMESSAGE ) ;
}
2021-05-15 11:26:31 +00:00
public static String DRIVER = " mysql " ;
2021-05-15 09:42:54 +00:00
public static String IP = " 0.0.0.0 " ;
public static String PORT = " 3306 " ;
public static String DATABASE = " database " ;
public static String USERNAME = " root " ;
public static String PASSWORD = " root " ;
private static void database ( ) {
DRIVER = getString ( " database.driver " , DRIVER ) ;
IP = getString ( " database.ip " , IP ) ;
PORT = getString ( " database.port " , PORT ) ;
DATABASE = getString ( " database.name " , DATABASE ) ;
USERNAME = getString ( " database.username " , USERNAME ) ;
PASSWORD = getString ( " database.password " , PASSWORD ) ;
}
2021-05-10 08:45:43 +00:00
}