Skip to content

Commit

Permalink
feat: make user (un)loading async
Browse files Browse the repository at this point in the history
Users are loaded and unloaded on PlayerJoin/Quit sync, which cause some overheat
  • Loading branch information
iGabyTM committed Jul 10, 2023
1 parent 5672faf commit ab3ecf7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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");

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ab3ecf7

Please sign in to comment.