2019-07-06 10:50:05 +00:00
package io.izzel.taboolib.module.db.local ;
import com.google.common.collect.Maps ;
import io.izzel.taboolib.TabooLib ;
2020-05-05 17:39:21 +00:00
import io.izzel.taboolib.common.loader.Startup ;
import io.izzel.taboolib.common.loader.StartupLoader ;
import io.izzel.taboolib.module.db.IHost ;
import io.izzel.taboolib.module.db.source.DBSource ;
import io.izzel.taboolib.module.db.sql.SQLTable ;
import io.izzel.taboolib.module.db.sql.query.Where ;
import io.izzel.taboolib.module.db.sqlite.SQLiteHost ;
2019-07-06 10:50:05 +00:00
import io.izzel.taboolib.module.inject.TSchedule ;
import io.izzel.taboolib.util.Files ;
import org.bukkit.Bukkit ;
import org.bukkit.OfflinePlayer ;
import org.bukkit.configuration.file.FileConfiguration ;
2020-05-05 17:39:21 +00:00
import javax.sql.DataSource ;
2019-07-06 10:50:05 +00:00
import java.io.File ;
2020-05-05 17:39:21 +00:00
import java.nio.charset.StandardCharsets ;
import java.util.Base64 ;
2019-07-06 10:50:05 +00:00
import java.util.Map ;
import java.util.UUID ;
/ * *
* @Author 坏 黑
* @Since 2019 - 07 - 06 17 : 43
* /
public class LocalPlayer {
2020-05-05 17:39:21 +00:00
static {
StartupLoader . register ( LocalPlayer . class ) ;
}
2020-05-01 03:56:00 +00:00
private static final Map < String , FileConfiguration > files = Maps . newConcurrentMap ( ) ;
2020-05-05 17:39:21 +00:00
private static IHost host ;
private static SQLTable table ;
private static DataSource dataSource ;
2019-07-06 10:50:05 +00:00
2020-05-05 17:39:21 +00:00
@Startup.Starting
public static void init ( ) {
host = new SQLiteHost ( new File ( LocalPlayer . getFolder ( ) , "v2/data.db" ) , TabooLib . getPlugin ( ) ) ;
table = new SQLTable ( "player_data" ) ;
try {
dataSource = DBSource . create ( host ) ;
table . executeUpdate ( "create table if not exists player_data (id integer not null primary key autoincrement, name text not null primary key, data text)" ) . dataSource ( dataSource ) . run ( ) ;
} catch ( Throwable t ) {
t . printStackTrace ( ) ;
}
2019-07-06 10:50:05 +00:00
}
2020-05-05 17:39:21 +00:00
public static synchronized boolean find0 ( OfflinePlayer player ) {
return table . select ( Where . equals ( "name" , LocalPlayer . toName ( player ) ) ) . find ( dataSource ) ;
}
public static synchronized FileConfiguration get0 ( OfflinePlayer player ) {
File file = toFile ( toName ( player ) ) ;
if ( file . exists ( ) ) {
2019-07-06 10:50:05 +00:00
try {
2020-05-05 17:39:21 +00:00
return SecuredFile . loadConfiguration ( file ) ;
} finally {
Files . copy ( file , Files . file ( getFolder ( ) , toName ( player ) + ".bak" ) ) ;
Files . deepDelete ( file ) ;
2019-07-06 10:50:05 +00:00
}
2020-05-05 17:39:21 +00:00
}
return table . select ( Where . equals ( "name" , LocalPlayer . toName ( player ) ) ) . to ( dataSource ) . resultNext ( r - > SecuredFile . loadConfiguration ( new String ( Base64 . getDecoder ( ) . decode ( r . getString ( "data" ) ) , StandardCharsets . UTF_8 ) ) ) . run ( new SecuredFile ( ) , SecuredFile . class ) ;
}
public static synchronized void set0 ( OfflinePlayer player , FileConfiguration data ) {
if ( find0 ( player ) ) {
table . update ( Where . equals ( "name" , LocalPlayer . toName ( player ) ) ) . set ( "data" , Base64 . getEncoder ( ) . encodeToString ( data . saveToString ( ) . getBytes ( StandardCharsets . UTF_8 ) ) ) . run ( dataSource ) ;
} else {
table . insert ( null , LocalPlayer . toName ( player ) , Base64 . getEncoder ( ) . encodeToString ( data . saveToString ( ) . getBytes ( StandardCharsets . UTF_8 ) ) ) . run ( dataSource ) ;
}
2019-07-06 10:50:05 +00:00
}
2020-05-05 17:39:21 +00:00
public static FileConfiguration get ( OfflinePlayer player ) {
return files . computeIfAbsent ( toName ( player ) , n - > get0 ( player ) ) ;
}
@TSchedule ( delay = 1200 , period = 1200 , async = true )
public static void saveFiles ( ) {
2019-07-06 10:50:05 +00:00
files . forEach ( ( name , file ) - > {
2020-05-05 17:39:21 +00:00
OfflinePlayer player = toPlayer ( name ) ;
if ( ! player . isOnline ( ) ) {
files . remove ( name ) ;
2019-07-06 10:50:05 +00:00
}
2020-05-05 17:39:21 +00:00
set0 ( player , file ) ;
2019-07-06 10:50:05 +00:00
} ) ;
}
public static File getFolder ( ) {
return Files . folder ( TabooLib . getConfig ( ) . getString ( "LOCAL-PLAYER" ) ) ;
}
2020-05-01 03:56:00 +00:00
public static File toFile ( String name ) {
2020-05-05 17:39:21 +00:00
return new File ( getFolder ( ) , name + ".yml" ) ;
2019-07-06 10:50:05 +00:00
}
2020-05-01 03:56:00 +00:00
public static String toName ( OfflinePlayer player ) {
2019-07-06 10:50:05 +00:00
return isUniqueIdMode ( ) ? player . getUniqueId ( ) . toString ( ) : player . getName ( ) ;
}
2020-05-01 03:56:00 +00:00
public static boolean isUniqueIdMode ( ) {
2019-07-06 10:50:05 +00:00
return TabooLib . getConfig ( ) . getBoolean ( "LOCAL-PLAYER-UUID" ) ;
}
2020-05-05 17:39:21 +00:00
public static OfflinePlayer toPlayer ( String name ) {
return isUniqueIdMode ( ) ? Bukkit . getOfflinePlayer ( UUID . fromString ( name ) ) : Bukkit . getOfflinePlayer ( name ) ;
2019-07-06 10:50:05 +00:00
}
}