From 9e4125dd4ecbb077c435a421cd0ac8c67073ca86 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Tue, 31 Dec 2024 16:37:32 +0100 Subject: [PATCH] Move chat storage From "rooms" to "storage/rooms" --- .../java/eu/m724/tweaks/chat/ChatManager.java | 9 +++---- .../eu/m724/tweaks/chat/ChatRoomLoader.java | 25 +++++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/main/java/eu/m724/tweaks/chat/ChatManager.java b/src/main/java/eu/m724/tweaks/chat/ChatManager.java index 89b1ff1..04d1a84 100644 --- a/src/main/java/eu/m724/tweaks/chat/ChatManager.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatManager.java @@ -19,7 +19,6 @@ import org.bukkit.plugin.Plugin; import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.Objects; public class ChatManager { private final Plugin plugin; @@ -75,7 +74,7 @@ public class ChatManager { if (id.equals(defaultRoom)) { chatRoom = new ChatRoom(defaultRoom, null, null); } else { - chatRoom = ChatRoomLoader.load(plugin, id); + chatRoom = ChatRoomLoader.load(id); } roomIdMap.put(id, chatRoom); } @@ -172,17 +171,17 @@ public class ChatManager { throw new ChatRoomExistsException(); ChatRoom chatRoom = new ChatRoom(id, password, owner); - ChatRoomLoader.save(plugin, chatRoom); + ChatRoomLoader.save(chatRoom); return chatRoom; } void saveChatRoom(ChatRoom chatRoom) throws IOException { - ChatRoomLoader.save(plugin, chatRoom); + ChatRoomLoader.save(chatRoom); } public void deleteChatRoom(ChatRoom chatRoom) { roomIdMap.remove(chatRoom.id); - ChatRoomLoader.getFile(plugin, chatRoom.id).delete(); + ChatRoomLoader.getFile(chatRoom.id).delete(); chatRoom.players.forEach(player -> setPlayerChatRoom(getById(defaultRoom), player)); } diff --git a/src/main/java/eu/m724/tweaks/chat/ChatRoomLoader.java b/src/main/java/eu/m724/tweaks/chat/ChatRoomLoader.java index f2c73fa..a160311 100644 --- a/src/main/java/eu/m724/tweaks/chat/ChatRoomLoader.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatRoomLoader.java @@ -13,23 +13,25 @@ import org.bukkit.plugin.Plugin; import java.io.File; import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.UUID; public class ChatRoomLoader { + private static File chatRoomsDir; + + static void init(Plugin plugin) { + chatRoomsDir = new File(plugin.getDataFolder(), "storage/rooms"); + chatRoomsDir.mkdirs(); + } + /** * Get the file of persistent storage of a chat room * @return the file or null if ID is invalid */ - static File getFile(Plugin plugin, String id) { - Path chatRoomsPath = Paths.get(plugin.getDataFolder().getPath(), "rooms"); - chatRoomsPath.toFile().mkdirs(); - + static File getFile(String id) { if (validateId(id) != 0) throw new RuntimeException("Invalid id: " + id); - return Paths.get(chatRoomsPath.toFile().getPath(), id + ".yml").toFile(); + return new File(chatRoomsDir, id + ".yml"); } /** @@ -62,8 +64,8 @@ public class ChatRoomLoader { * @param id the id of the chat room * @return the chat room or null if no such chat room */ - static ChatRoom load(Plugin plugin, String id) { - File chatRoomFile = getFile(plugin, id); + static ChatRoom load(String id) { + File chatRoomFile = getFile(id); if (!chatRoomFile.exists()) return null; YamlConfiguration configuration = YamlConfiguration.loadConfiguration(chatRoomFile); @@ -89,14 +91,15 @@ public class ChatRoomLoader { * * @throws IOException if saving failed */ - static void save(Plugin plugin, ChatRoom chatRoom) throws IOException { + static void save(ChatRoom chatRoom) throws IOException { YamlConfiguration configuration = new YamlConfiguration(); configuration.set("password", chatRoom.password); configuration.set("color", chatRoom.color.getName()); + // TODO consider just making this str to make it easier configuration.set("owner.msb", chatRoom.owner.getUniqueId().getMostSignificantBits()); configuration.set("owner.lsb", chatRoom.owner.getUniqueId().getLeastSignificantBits()); - File chatRoomFile = getFile(plugin, chatRoom.id); + File chatRoomFile = getFile(chatRoom.id); configuration.save(chatRoomFile); } }