From ab3ecf7ba347e79ce6894d7e729481a5e667fe71 Mon Sep 17 00:00:00 2001 From: iGabyTM Date: Tue, 11 Jul 2023 00:26:18 +0300 Subject: [PATCH 1/2] 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; From ba78f779b239e4b4afd94bce710787922cd7c16c Mon Sep 17 00:00:00 2001 From: Kqliber Date: Sun, 5 Nov 2023 18:27:28 +0000 Subject: [PATCH 2/2] 1.20.2 support (updated adventure) --- api/build.gradle.kts | 5 +++++ gradle/libs.versions.toml | 4 ++-- plugin/build.gradle.kts | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/api/build.gradle.kts b/api/build.gradle.kts index 2f09cfa8..601cc03b 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -6,6 +6,11 @@ plugins { id("com.github.johnrengelman.shadow") version "7.1.2" } +repositories { + // adventure snapshot repo + maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") +} + dependencies { api(libs.adventure.bukkit) api(libs.adventure.minimessage) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0a9e8df8..cae8064f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,7 +4,7 @@ spigot = "1.20.1-R0.1-SNAPSHOT" # Adventure minimessage = "4.14.0" -adventure-platform = "4.3.0" +adventure-platform = "4.3.2-SNAPSHOT" # Other configurate = "4.1.2" @@ -12,7 +12,7 @@ cmds = "2.0.0-SNAPSHOT" papi = "2.11.1" towny = "0.98.1.3" discordsrv = "1.25.1" -supervanish = "6.2.6-4" +supervanish = "6.2.17" bstats = "3.0.0" essentials = "2.19.4" griefprevention = "16.18.1" diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index 56642ddf..a6b1d025 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -12,6 +12,8 @@ version = "${rootProject.version}-${System.getenv("BUILD_NUMBER")}" repositories { papi() triumphSnapshots() + // adventure snapshot repo + maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") // towny maven("https://repo.glaremasters.me/repository/towny/") // dsrv + dependencies