2021-12-29 02:28:50 +00:00
package de.jatitv.opsecurity.config.config ;
2022-01-06 01:08:20 +00:00
import de.jatitv.opsecurity.Util ;
2021-12-29 02:28:50 +00:00
import de.jatitv.opsecurity.system.Main ;
import de.jatitv.opsecurity.system.NameHistory ;
2022-01-06 01:08:20 +00:00
import net.t2code.lib.Spigot.Lib.messages.send ;
2021-12-29 02:28:50 +00:00
import org.bukkit.configuration.file.YamlConfiguration ;
import java.io.File ;
import java.io.IOException ;
import java.nio.file.Files ;
import java.nio.file.Path ;
import java.nio.file.Paths ;
import java.util.List ;
public class ConfigConvert {
public static void convert ( ) {
try {
renameConfig ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
try {
configConvert ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
try {
renameLanguages ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
private static void renameConfig ( ) {
Path configOld = Paths . get ( Main . getPath ( ) + " /Config.yml " ) ;
Path config = Paths . get ( Main . getPath ( ) + " /config.yml " ) ;
if ( Files . exists ( configOld ) & & ! Files . isDirectory ( configOld ) ) {
if ( Files . exists ( config ) & & ! Files . isDirectory ( config ) ) {
return ;
}
String replace = Main . getPath ( ) . toString ( ) + " / " ;
2022-01-06 01:08:20 +00:00
convert ( configOld . toString ( ) . replace ( replace , " " ) , config . toString ( ) . replace ( replace , " " ) ) ;
2021-12-29 02:28:50 +00:00
try {
Files . move ( configOld , config ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
private static void configConvert ( ) {
File config = new File ( Main . plugin . getDataFolder ( ) . getPath ( ) , " config.yml " ) ;
YamlConfiguration yamlConfiguration = YamlConfiguration . loadConfiguration ( config ) ;
if ( yamlConfiguration . get ( " ConfigVersion " ) = = null ) {
try {
if ( yamlConfiguration . get ( " Plugin.language " ) ! = null ) {
switch ( yamlConfiguration . getString ( " Plugin.language " ) ) {
case " de_DE " :
yamlConfiguration . set ( " Plugin.language " , " german " ) ;
2022-01-06 01:08:20 +00:00
convert ( " config.yml " , " Plugin.language: de_DE " , " Plugin.language: german " ) ;
2021-12-29 02:28:50 +00:00
try {
yamlConfiguration . save ( config ) ;
} catch ( IOException tac ) {
tac . printStackTrace ( ) ;
}
break ;
case " en_EN " :
yamlConfiguration . set ( " Plugin.language " , " english " ) ;
2022-01-06 01:08:20 +00:00
convert ( " config.yml " , " Plugin.language: en_EN " , " Plugin.language: english " ) ;
2021-12-29 02:28:50 +00:00
try {
yamlConfiguration . save ( config ) ;
} catch ( IOException tac ) {
tac . printStackTrace ( ) ;
}
break ;
case " no_NO " :
yamlConfiguration . set ( " Plugin.language " , " norwegian " ) ;
2022-01-06 01:08:20 +00:00
convert ( " config.yml " , " Plugin.language: no_NO " , " Plugin.language: norwegian " ) ;
2021-12-29 02:28:50 +00:00
try {
yamlConfiguration . save ( config ) ;
} catch ( IOException tac ) {
tac . printStackTrace ( ) ;
}
break ;
}
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
if ( yamlConfiguration . get ( " Timer " ) ! = null ) {
Boolean enable = yamlConfiguration . getBoolean ( " Timer.Enable " ) ;
Integer time = yamlConfiguration . getInt ( " Timer.RefreshTime_inSec " ) ;
yamlConfiguration . set ( " Check.Timer.Enable " , enable ) ;
2022-01-06 01:08:20 +00:00
convert ( " config.yml " , " Timer.Enable: " + enable , " Check.Timer.Enable: " + enable ) ;
2021-12-29 02:28:50 +00:00
yamlConfiguration . set ( " Check.Timer.RefreshTime_inSec " , time ) ;
2022-01-06 01:08:20 +00:00
convert ( " config.yml " , " Timer.RefreshTime_inSec: " + time , " Check.Timer.RefreshTime_inSec: " + time ) ;
2021-12-29 02:28:50 +00:00
yamlConfiguration . set ( " Timer " , null ) ;
try {
yamlConfiguration . save ( config ) ;
} catch ( IOException tac ) {
tac . printStackTrace ( ) ;
}
}
List < String > oldOPs = yamlConfiguration . getStringList ( " OP_Whitelist.Whitelist " ) ;
yamlConfiguration . set ( " OP_Whitelist.Whitelist " , null ) ;
convertPlayer ( " OP_Whitelist.Whitelist " , " OP_Whitelist.Whitelist. " , oldOPs , yamlConfiguration ) ;
List < String > oldPerms = yamlConfiguration . getStringList ( " Permission_Whitelist.Player " ) ;
yamlConfiguration . set ( " Permission_Whitelist.Player " , null ) ;
convertPlayer ( " Permission_Whitelist.Player " , " Permission_Whitelist.Whitelist. " , oldPerms , yamlConfiguration ) ;
try {
yamlConfiguration . save ( config ) ;
} catch ( IOException tac ) {
tac . printStackTrace ( ) ;
}
}
}
2022-01-06 01:08:20 +00:00
public static void convert ( String oldConfig , String newConfig ) {
send . console ( Util . getPrefix ( ) + " §5Convert: §4 " + oldConfig + " §5--> §2 " + newConfig ) ;
}
public static void convert ( String config , String oldConfig , String newConfig ) {
send . console ( Util . getPrefix ( ) + " §5Convert: §e " + config + " §4 " + oldConfig + " §5--> §2 " + newConfig ) ;
}
2021-12-29 02:28:50 +00:00
private static void convertPlayer ( String oldPath , String newPath , List < String > oldPlayer , YamlConfiguration yamlConfiguration ) {
for ( String Player : oldPlayer ) {
try {
String uuid = NameHistory . getPlayerUUID ( Player ) ;
if ( uuid = = null ) {
send . warning ( Main . plugin ,
" The UUID of the player §6 " + Player + " §ecould not be found. Please check the config.yml and / or if the player exists / if the player name is correct! " ) ;
2022-01-06 01:08:20 +00:00
convert ( " config.yml " , oldPath + " : " + Player , newPath + " : " + Player + " : UUID: Player UUID not found! " ) ;
2021-12-29 02:28:50 +00:00
yamlConfiguration . set ( newPath + Player + " .UUID " , " Player UUID not found! " ) ;
} else {
2022-01-06 01:08:20 +00:00
convert ( " config.yml " , oldPath + " : " + Player , newPath + " : " + Player + " : UUID: " + uuid ) ;
2021-12-29 02:28:50 +00:00
yamlConfiguration . set ( newPath + Player + " .UUID " , uuid ) ;
}
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
private static void renameLanguages ( ) {
String replace = Main . getPath ( ) . toString ( ) + " /languages/ " ;
Path messagesDEold = Paths . get ( Main . getPath ( ) + " /languages/de_DE_Messages.yml " ) ;
Path messagesDE = Paths . get ( Main . getPath ( ) + " /languages/german_messages.yml " ) ;
if ( Files . exists ( messagesDEold ) & & ! Files . isDirectory ( messagesDEold ) ) {
if ( Files . exists ( messagesDE ) & & ! Files . isDirectory ( messagesDE ) ) {
return ;
}
2022-01-06 01:08:20 +00:00
convert ( messagesDEold . toString ( ) . replace ( replace , " " ) , messagesDE . toString ( ) . replace ( replace , " " ) ) ;
2021-12-29 02:28:50 +00:00
try {
Files . move ( messagesDEold , messagesDE ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
Path messagesENold = Paths . get ( Main . getPath ( ) + " /languages/en_EN_Messages.yml " ) ;
Path messagesEN = Paths . get ( Main . getPath ( ) + " /languages/english_messages.yml " ) ;
if ( Files . exists ( messagesENold ) & & ! Files . isDirectory ( messagesENold ) ) {
if ( Files . exists ( messagesEN ) & & ! Files . isDirectory ( messagesEN ) ) {
return ;
}
2022-01-06 01:08:20 +00:00
convert ( messagesENold . toString ( ) . replace ( replace , " " ) , messagesEN . toString ( ) . replace ( replace , " " ) ) ;
2021-12-29 02:28:50 +00:00
try {
Files . move ( messagesENold , messagesEN ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
Path messagesNOold = Paths . get ( Main . getPath ( ) + " /languages/no_NO_Messages.yml " ) ;
Path messagesNO = Paths . get ( Main . getPath ( ) + " /languages/norwegian_messages.yml " ) ;
if ( Files . exists ( messagesNOold ) & & ! Files . isDirectory ( messagesNOold ) ) {
if ( Files . exists ( messagesNO ) & & ! Files . isDirectory ( messagesNO ) ) {
return ;
}
2022-01-06 01:08:20 +00:00
convert ( messagesNOold . toString ( ) . replace ( replace , " " ) , messagesNO . toString ( ) . replace ( replace , " " ) ) ;
2021-12-29 02:28:50 +00:00
try {
Files . move ( messagesNOold , messagesNO ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
}