From ab3ecf7ba347e79ce6894d7e729481a5e667fe71 Mon Sep 17 00:00:00 2001 From: iGabyTM Date: Tue, 11 Jul 2023 00:26:18 +0300 Subject: [PATCH] feat: make user (un)loading async Users are loaded and unloaded on PlayerJoin/Quit sync, which cause some overheat --- .../chatchat/data/impl/gson/GsonDatabase.java | 30 ++++++------------- .../chatchat/listener/PlayerListener.java | 5 ++-- .../helpch/chatchat/user/UsersHolderImpl.java | 7 +---- 3 files changed, 13 insertions(+), 29 deletions(-) diff --git a/plugin/src/main/java/at/helpch/chatchat/data/impl/gson/GsonDatabase.java b/plugin/src/main/java/at/helpch/chatchat/data/impl/gson/GsonDatabase.java index 408585ee..41b7db2c 100644 --- a/plugin/src/main/java/at/helpch/chatchat/data/impl/gson/GsonDatabase.java +++ b/plugin/src/main/java/at/helpch/chatchat/data/impl/gson/GsonDatabase.java @@ -11,11 +11,10 @@ import org.jetbrains.annotations.NotNull; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; import java.util.UUID; import java.util.logging.Level; @@ -43,6 +42,7 @@ public GsonDatabase(@NotNull final ChatChatPlugin plugin) { @Override public @NotNull ChatUser loadChatUser(@NotNull final UUID uuid) { final var userFile = new File(usersDirectory, uuid + ".json"); + if (!userFile.exists()) { final var user = new ChatUserImpl(uuid); final var channel = ChatChannel.defaultChannel(); @@ -54,11 +54,12 @@ public GsonDatabase(@NotNull final ChatChatPlugin plugin) { try(final var reader = new FileReader(userFile)) { return gson.fromJson(reader, ChatUser.class); } catch (final JsonParseException exception) { // Handles invalid JSON - plugin.getLogger().warning( - "Something went wrong while trying to load user " + uuid + "!" + System.lineSeparator() - + "Creating backup at " + uuid + "-backup.json." - ); - exception.printStackTrace(); + plugin.getLogger() + .log( + Level.WARNING, + String.format("Something went wrong while trying to load user %s!. Creating backup at %1$s-backup.json", uuid), + exception + ); final var backupFile = new File(usersDirectory, uuid + "-backup.json"); @@ -87,7 +88,7 @@ public GsonDatabase(@NotNull final ChatChatPlugin plugin) { // copy contents of userFile to backupFile try { - copyFileContents(userFile, backupFile); + Files.copy(userFile.toPath(), backupFile.toPath()); } catch (IOException ioException) { plugin.getLogger().log( Level.WARNING, @@ -148,17 +149,4 @@ public void saveChatUser(@NotNull final ChatUser chatUser) { } } - private static void copyFileContents( - @NotNull final File source, - @NotNull final File destination - ) throws IOException { - try (final var inStream = new FileInputStream(source); final var outStream = new FileOutputStream(destination)) { - final byte[] buffer = new byte[1024]; - - int length; - while ((length = inStream.read(buffer)) > 0){ - outStream.write(buffer, 0, length); - } - } - } } diff --git a/plugin/src/main/java/at/helpch/chatchat/listener/PlayerListener.java b/plugin/src/main/java/at/helpch/chatchat/listener/PlayerListener.java index 81a8abe5..74053302 100644 --- a/plugin/src/main/java/at/helpch/chatchat/listener/PlayerListener.java +++ b/plugin/src/main/java/at/helpch/chatchat/listener/PlayerListener.java @@ -2,6 +2,7 @@ import at.helpch.chatchat.ChatChatPlugin; import at.helpch.chatchat.api.user.ChatUser; +import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; @@ -18,7 +19,7 @@ public PlayerListener(@NotNull final ChatChatPlugin plugin) { @EventHandler private void onJoin(final PlayerJoinEvent event) { - plugin.usersHolder().getUser(event.getPlayer()); + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> plugin.usersHolder().getUser(event.getPlayer())); } @EventHandler @@ -32,6 +33,6 @@ private void onLeave(final PlayerQuitEvent event) { .filter(user -> user.lastMessagedUser().get().player().equals(event.getPlayer())) .forEach(user -> user.lastMessagedUser(null)); - plugin.usersHolder().removeUser(event.getPlayer()); + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> plugin.usersHolder().removeUser(event.getPlayer().getUniqueId())); } } diff --git a/plugin/src/main/java/at/helpch/chatchat/user/UsersHolderImpl.java b/plugin/src/main/java/at/helpch/chatchat/user/UsersHolderImpl.java index ffab3a83..9e55ca70 100644 --- a/plugin/src/main/java/at/helpch/chatchat/user/UsersHolderImpl.java +++ b/plugin/src/main/java/at/helpch/chatchat/user/UsersHolderImpl.java @@ -40,12 +40,7 @@ public UsersHolderImpl(@NotNull final ChatChatPlugin plugin) { } public void removeUser(@NotNull final UUID uuid) { - if (!users.containsKey(uuid)) { - return; - } - - final var user = users.get(uuid); - users.remove(uuid); + final var user = users.remove(uuid); if (!(user instanceof ChatUser)) { return;