2020import java .io .File ;
2121import java .io .IOException ;
2222import java .util .*;
23+ import java .util .concurrent .CompletableFuture ;
24+ import java .util .concurrent .TimeUnit ;
2325import java .util .stream .Collectors ;
2426
2527import static io .github .aplini .chat2qq .utils .Util .sendToGroup ;
@@ -38,32 +40,36 @@ public void onLoad() {
3840 plugin .saveDefaultConfig ();
3941 plugin .reloadConfig ();
4042
41- // 加载 temp.yml
42- temp = YamlConfiguration .loadConfiguration (tempFile );
43-
44- getLogger ().info ("[Chat2QQ] " );
45- getLogger ().info (temp .toString ());
46- getLogger ().info (Objects .requireNonNull (temp .get ("group_cache_all" )).toString ());
43+ readTempData ();
44+ }
4745
48- if (getConfig ().getBoolean ("aplini.player-cache.enabled" , true ) && temp .get ("group_cache_all" ) != null ){
46+ public static void readTempData () {
47+ temp = YamlConfiguration .loadConfiguration (tempFile );
48+ if (plugin .getConfig ().getBoolean ("aplini.player-cache.enabled" , true ) && temp .get ("group_cache_all" ) != null ){
4949 ConfigurationSection outerSection = temp .getConfigurationSection ("group_cache_all" );
5050 for (String groupIdStr : outerSection .getKeys (false )) {
5151 ConfigurationSection playerSection = outerSection .getConfigurationSection (groupIdStr );
5252 Map <Long , String > playerMap = playerSection .getKeys (false ).stream ().collect (Collectors .toMap (Long ::parseLong , playerSection ::getString ));
5353 group_cache_all .put (Long .parseLong (groupIdStr ), playerMap );
5454 }
55- getLogger ().info ("[Chat2QQ] 读取群成员缓存" );
56- getLogger ().info (group_cache_all .toString ());
5755 }
5856 }
5957
58+ public static boolean saveTempLock = false ;
6059 public static void saveTempData () {
61- temp .set ("group_cache_all" , group_cache_all );
62- try {
63- temp .save (tempFile );
64- } catch (IOException e ) {
65- throw new RuntimeException (e );
66- }
60+ if (saveTempLock ) return ;
61+ saveTempLock = true ;
62+ CompletableFuture .runAsync (() -> {
63+ try {
64+ TimeUnit .MILLISECONDS .sleep (3000 );
65+ temp .set ("group_cache_all" , group_cache_all );
66+ temp .save (tempFile );
67+ } catch (InterruptedException | IOException ex ) {
68+ throw new RuntimeException (ex );
69+ } finally {
70+ saveTempLock = false ;
71+ }
72+ });
6773 }
6874
6975 @ Override // 启用插件
0 commit comments