Move chat storage

From "rooms" to "storage/rooms"
This commit is contained in:
Minecon724 2024-12-31 16:37:32 +01:00
parent 69cb2ef9af
commit 9e4125dd4e
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 18 additions and 16 deletions

View file

@ -19,7 +19,6 @@ import org.bukkit.plugin.Plugin;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
public class ChatManager { public class ChatManager {
private final Plugin plugin; private final Plugin plugin;
@ -75,7 +74,7 @@ public class ChatManager {
if (id.equals(defaultRoom)) { if (id.equals(defaultRoom)) {
chatRoom = new ChatRoom(defaultRoom, null, null); chatRoom = new ChatRoom(defaultRoom, null, null);
} else { } else {
chatRoom = ChatRoomLoader.load(plugin, id); chatRoom = ChatRoomLoader.load(id);
} }
roomIdMap.put(id, chatRoom); roomIdMap.put(id, chatRoom);
} }
@ -172,17 +171,17 @@ public class ChatManager {
throw new ChatRoomExistsException(); throw new ChatRoomExistsException();
ChatRoom chatRoom = new ChatRoom(id, password, owner); ChatRoom chatRoom = new ChatRoom(id, password, owner);
ChatRoomLoader.save(plugin, chatRoom); ChatRoomLoader.save(chatRoom);
return chatRoom; return chatRoom;
} }
void saveChatRoom(ChatRoom chatRoom) throws IOException { void saveChatRoom(ChatRoom chatRoom) throws IOException {
ChatRoomLoader.save(plugin, chatRoom); ChatRoomLoader.save(chatRoom);
} }
public void deleteChatRoom(ChatRoom chatRoom) { public void deleteChatRoom(ChatRoom chatRoom) {
roomIdMap.remove(chatRoom.id); roomIdMap.remove(chatRoom.id);
ChatRoomLoader.getFile(plugin, chatRoom.id).delete(); ChatRoomLoader.getFile(chatRoom.id).delete();
chatRoom.players.forEach(player -> setPlayerChatRoom(getById(defaultRoom), player)); chatRoom.players.forEach(player -> setPlayerChatRoom(getById(defaultRoom), player));
} }

View file

@ -13,23 +13,25 @@ import org.bukkit.plugin.Plugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID; import java.util.UUID;
public class ChatRoomLoader { 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 * Get the file of persistent storage of a chat room
* @return the file or null if ID is invalid * @return the file or null if ID is invalid
*/ */
static File getFile(Plugin plugin, String id) { static File getFile(String id) {
Path chatRoomsPath = Paths.get(plugin.getDataFolder().getPath(), "rooms");
chatRoomsPath.toFile().mkdirs();
if (validateId(id) != 0) if (validateId(id) != 0)
throw new RuntimeException("Invalid id: " + id); 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 * @param id the id of the chat room
* @return the chat room or null if no such chat room * @return the chat room or null if no such chat room
*/ */
static ChatRoom load(Plugin plugin, String id) { static ChatRoom load(String id) {
File chatRoomFile = getFile(plugin, id); File chatRoomFile = getFile(id);
if (!chatRoomFile.exists()) return null; if (!chatRoomFile.exists()) return null;
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(chatRoomFile); YamlConfiguration configuration = YamlConfiguration.loadConfiguration(chatRoomFile);
@ -89,14 +91,15 @@ public class ChatRoomLoader {
* *
* @throws IOException if saving failed * @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(); YamlConfiguration configuration = new YamlConfiguration();
configuration.set("password", chatRoom.password); configuration.set("password", chatRoom.password);
configuration.set("color", chatRoom.color.getName()); 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.msb", chatRoom.owner.getUniqueId().getMostSignificantBits());
configuration.set("owner.lsb", chatRoom.owner.getUniqueId().getLeastSignificantBits()); configuration.set("owner.lsb", chatRoom.owner.getUniqueId().getLeastSignificantBits());
File chatRoomFile = getFile(plugin, chatRoom.id); File chatRoomFile = getFile(chatRoom.id);
configuration.save(chatRoomFile); configuration.save(chatRoomFile);
} }
} }