Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make user (un)loading async #214

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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