From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: AlphaKR93 Date: Thu, 11 Jan 2024 13:40:41 +0900 Subject: [PATCH] Save Json list asynchronously diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedPlayerList.java b/src/main/java/net/minecraft/server/dedicated/DedicatedPlayerList.java index 935dac757280731bfeb0a8f033cbe315ecac46da..038f370ac2cb768e14fe7605b32b2ac811c33b8f 100644 --- a/src/main/java/net/minecraft/server/dedicated/DedicatedPlayerList.java +++ b/src/main/java/net/minecraft/server/dedicated/DedicatedPlayerList.java @@ -60,20 +60,20 @@ public class DedicatedPlayerList extends PlayerList { } private void saveIpBanList() { - try { + //try { // Plazma - Build fix this.getIpBans().save(); - } catch (IOException var2) { + /*} catch (IOException var2) { // Plazma - Build fix LOGGER.warn("Failed to save ip banlist: ", (Throwable)var2); - } + }*/ // Plazma - Build fix } private void saveUserBanList() { - try { + //try { // Plazma - Build fix this.getBans().save(); - } catch (IOException var2) { + /*} catch (IOException var2) { // Plazma - Build fix LOGGER.warn("Failed to save user banlist: ", (Throwable)var2); - } + }*/ // Plazma - Build fix } diff --git a/src/main/java/net/minecraft/server/players/StoredUserList.java b/src/main/java/net/minecraft/server/players/StoredUserList.java index 34b7c5421da93f14050425b0fc16bbc27e5f3eba..a35340b9231bc3009ca9449fa6ea8f8ae6929146 100644 --- a/src/main/java/net/minecraft/server/players/StoredUserList.java +++ b/src/main/java/net/minecraft/server/players/StoredUserList.java @@ -61,11 +61,11 @@ public abstract class StoredUserList> { public void add(V entry) { this.map.put(this.getKeyForUser(entry.getUser()), entry); - try { + //try { // Plazma - Fix build this.save(); - } catch (IOException ioexception) { + /*} catch (IOException ioexception) { // Plazma - Fix build StoredUserList.LOGGER.warn("Could not save the list after adding a user.", ioexception); - } + }*/ // Plazma - Fix build } @@ -83,11 +83,11 @@ public abstract class StoredUserList> { public void remove(K key) { this.map.remove(this.getKeyForUser(key)); - try { + //try { // Plazma - Fix build this.save(); - } catch (IOException ioexception) { + /*} catch (IOException ioexception) { // Plazma - Fix build StoredUserList.LOGGER.warn("Could not save the list after removing a user.", ioexception); - } + }*/ // Plazma - Fix build } @@ -142,7 +142,10 @@ public abstract class StoredUserList> { return this.map.values(); } - public void save() throws IOException { + // Plazma start - Save Json list asynchronously + public void save()/* throws IOException*/ { + io.papermc.paper.util.MCUtil.scheduleAsyncTask(() -> { + this.removeExpired(); // Paper - remove expired values before saving JsonArray jsonarray = new JsonArray(); Stream stream = this.map.values().stream().map((jsonlistentry) -> { // CraftBukkit - decompile error @@ -154,27 +157,16 @@ public abstract class StoredUserList> { Objects.requireNonNull(jsonarray); stream.forEach(jsonarray::add); - BufferedWriter bufferedwriter = Files.newWriter(this.file, StandardCharsets.UTF_8); - try { + try (BufferedWriter bufferedwriter = Files.newWriter(this.file, StandardCharsets.UTF_8)) { StoredUserList.GSON.toJson(jsonarray, bufferedwriter); - } catch (Throwable throwable) { - if (bufferedwriter != null) { - try { - bufferedwriter.close(); - } catch (Throwable throwable1) { - throwable.addSuppressed(throwable1); - } - } - - throw throwable; - } - - if (bufferedwriter != null) { - bufferedwriter.close(); + } catch (IOException e) { + StoredUserList.LOGGER.warn("Failed to asynchronously save file " + this.file, e); } + }); } + // Plazma end public void load() throws IOException { if (this.file.exists()) {