Skip to content

Commit

Permalink
Merge pull request #2 from HelpChat/main
Browse files Browse the repository at this point in the history
  • Loading branch information
BlitzOffline authored Nov 15, 2023
2 parents cc4e0df + b3026eb commit 022d26a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 deletions.
5 changes: 5 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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"
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"
Expand Down
2 changes: 2 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 022d26a

Please sign in to comment.