diff --git a/README.md b/README.md index 124d3b8..dd0a8db 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Dostępnę Releasy: https://github.com/LordTricker/LT-Rynek/releases | `/ltr remove ` | Usuwa przedmiot z profilu | | `/ltr list` | Wyświetla przedmioty w aktywnym profilu | | `/ltr pomoc` | Lista dostępnych komend | -| `/ltr config save` | Zapisuje konfigurację | +| `/ltr config save` | Ręcznie zapisuje konfigurację (zapis następuje też automatycznie) | | `/ltr config reload` | Przeładowuje dane z konfiguracji | ## 🔧 Instalacja diff --git a/src/client/java/pl/lordtricker/ltrynek/client/LtrynekClient.java b/src/client/java/pl/lordtricker/ltrynek/client/LtrynekClient.java index 78274f4..6510688 100644 --- a/src/client/java/pl/lordtricker/ltrynek/client/LtrynekClient.java +++ b/src/client/java/pl/lordtricker/ltrynek/client/LtrynekClient.java @@ -2,6 +2,7 @@ import pl.lordtricker.ltrynek.client.command.ClientCommandRegistration; import pl.lordtricker.ltrynek.client.config.ConfigLoader; +import pl.lordtricker.ltrynek.client.config.ConfigSaver; import pl.lordtricker.ltrynek.client.config.PriceEntry; import pl.lordtricker.ltrynek.client.config.ServerEntry; import pl.lordtricker.ltrynek.client.config.ServersConfig; @@ -30,27 +31,29 @@ public void onInitializeClient() { } ClientPriceListManager.setActiveProfile(serversConfig.defaultProfile); - ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { - String address = getServerAddress(); - ServerEntry entry = findServerEntry(address); - if (entry != null) { - ClientPriceListManager.setActiveProfile(entry.profileName); - if (client.player != null) { - String welcomeMsg = Messages.format("player.join", Map.of("profile", entry.profileName)); - client.player.sendMessage(ColorUtils.translateColorCodes(welcomeMsg), false); - } - } else { - String def = serversConfig.defaultProfile; - ClientPriceListManager.setActiveProfile(def); - if (client.player != null) { - String welcomeMsg = Messages.format("player.join", Map.of("profile", def)); - client.player.sendMessage(ColorUtils.translateColorCodes(welcomeMsg), false); - } - } - }); + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { + String address = getServerAddress(); + ServerEntry entry = findServerEntry(address); + if (entry != null) { + ClientPriceListManager.setActiveProfile(entry.profileName); + if (client.player != null) { + String welcomeMsg = Messages.format("player.join", Map.of("profile", entry.profileName)); + client.player.sendMessage(ColorUtils.translateColorCodes(welcomeMsg), false); + } + } else { + String def = serversConfig.defaultProfile; + ClientPriceListManager.setActiveProfile(def); + if (client.player != null) { + String welcomeMsg = Messages.format("player.join", Map.of("profile", def)); + client.player.sendMessage(ColorUtils.translateColorCodes(welcomeMsg), false); + } + } + }); - ClientCommandRegistration.registerCommands(); - } + ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> ConfigSaver.save()); + + ClientCommandRegistration.registerCommands(); + } private String getServerAddress() { if (MinecraftClient.getInstance().getCurrentServerEntry() != null) { diff --git a/src/client/java/pl/lordtricker/ltrynek/client/command/ClientCommandRegistration.java b/src/client/java/pl/lordtricker/ltrynek/client/command/ClientCommandRegistration.java index ff2e5b7..8c1cda4 100644 --- a/src/client/java/pl/lordtricker/ltrynek/client/command/ClientCommandRegistration.java +++ b/src/client/java/pl/lordtricker/ltrynek/client/command/ClientCommandRegistration.java @@ -4,6 +4,7 @@ import pl.lordtricker.ltrynek.client.LtrynekClient; import pl.lordtricker.ltrynek.client.Messages; import pl.lordtricker.ltrynek.client.config.ConfigLoader; +import pl.lordtricker.ltrynek.client.config.ConfigSaver; import pl.lordtricker.ltrynek.client.config.PriceEntry; import pl.lordtricker.ltrynek.client.config.ServerEntry; import pl.lordtricker.ltrynek.client.price.ClientPriceListManager; @@ -48,6 +49,8 @@ private static void registerLtrynekCommand( .executes(ctx -> { String profile = StringArgumentType.getString(ctx, "profile"); ClientPriceListManager.setActiveProfile(profile); + LtrynekClient.serversConfig.defaultProfile = profile; + ConfigSaver.save(); String msg = Messages.format("command.defaultprofile.success", Map.of("profile", profile)); ctx.getSource().sendFeedback(ColorUtils.translateColorCodes(msg)); @@ -115,6 +118,7 @@ private static void registerLtrynekCommand( "profile", activeProfile )); ctx.getSource().sendFeedback(ColorUtils.translateColorCodes(msg)); + ConfigSaver.save(); } catch (NumberFormatException e) { ctx.getSource().sendError(Text.literal("Invalid price format: " + maxPriceStr)); } @@ -137,6 +141,7 @@ private static void registerLtrynekCommand( "profile", activeProfile )); ctx.getSource().sendFeedback(ColorUtils.translateColorCodes(msg)); + ConfigSaver.save(); return 1; }) ) @@ -227,9 +232,7 @@ private static void registerLtrynekCommand( .then(ClientCommandManager.literal("config") .then(ClientCommandManager.literal("save") .executes(ctx -> { - syncMemoryToConfig(); - - ConfigLoader.saveConfig(LtrynekClient.serversConfig); + ConfigSaver.save(); String msg = Messages.get("command.config.save.success"); ctx.getSource().sendFeedback(ColorUtils.translateColorCodes(msg)); @@ -253,36 +256,6 @@ private static void registerLtrynekCommand( ); } - /** - * Synchronizuje dane z pamięci (ClientPriceListManager) do obiektu LtrynekClient.serversConfig, - * tak aby /ltrynek config save mógł zapisać te zmiany do pliku. - */ - private static void syncMemoryToConfig() { - for (ServerEntry entry : LtrynekClient.serversConfig.servers) { - entry.prices.clear(); - } - Map> allProfiles = ClientPriceListManager.getAllProfiles(); - for (Map.Entry> profEntry : allProfiles.entrySet()) { - String profileName = profEntry.getKey(); - Map items = profEntry.getValue(); - - ServerEntry se = findServerEntryByProfile(profileName); - if (se == null) { - continue; - } - - for (Map.Entry itemEntry : items.entrySet()) { - String itemName = itemEntry.getKey(); - double maxPrice = itemEntry.getValue(); - - PriceEntry pe = new PriceEntry(); - pe.name = itemName; - pe.maxPrice = maxPrice; - se.prices.add(pe); - } - } - } - /** * Czyści i na nowo inicjalizuje ClientPriceListManager z LtrynekClient.serversConfig. */ @@ -296,15 +269,4 @@ private static void reinitProfilesFromConfig() { ClientPriceListManager.setActiveProfile(LtrynekClient.serversConfig.defaultProfile); } - /** - * Znajduje ServerEntry w LtrynekClient.serversConfig dla danego profileName. - */ - private static ServerEntry findServerEntryByProfile(String profileName) { - for (ServerEntry entry : LtrynekClient.serversConfig.servers) { - if (entry.profileName.equals(profileName)) { - return entry; - } - } - return null; - } } diff --git a/src/client/java/pl/lordtricker/ltrynek/client/config/ConfigSaver.java b/src/client/java/pl/lordtricker/ltrynek/client/config/ConfigSaver.java new file mode 100644 index 0000000..6d131e4 --- /dev/null +++ b/src/client/java/pl/lordtricker/ltrynek/client/config/ConfigSaver.java @@ -0,0 +1,64 @@ +package pl.lordtricker.ltrynek.client.config; + +import pl.lordtricker.ltrynek.client.LtrynekClient; +import pl.lordtricker.ltrynek.client.price.ClientPriceListManager; + +import java.util.Map; + +/** + * Utility class that synchronizes in-memory price list data with the config file + * and persists it to disk. + */ +public class ConfigSaver { + /** + * Synchronizes data from ClientPriceListManager to LtrynekClient.serversConfig + * and writes the result to the config file. + */ + public static void save() { + syncMemoryToConfig(); + ConfigLoader.saveConfig(LtrynekClient.serversConfig); + } + + /** + * Synchronizes data from memory (ClientPriceListManager) to the serversConfig object, + * so that it can be saved to the config file. + */ + public static void syncMemoryToConfig() { + for (ServerEntry entry : LtrynekClient.serversConfig.servers) { + entry.prices.clear(); + } + Map> allProfiles = ClientPriceListManager.getAllProfiles(); + for (Map.Entry> profEntry : allProfiles.entrySet()) { + String profileName = profEntry.getKey(); + Map items = profEntry.getValue(); + + ServerEntry se = findServerEntryByProfile(profileName); + if (se == null) { + continue; + } + + for (Map.Entry itemEntry : items.entrySet()) { + String itemName = itemEntry.getKey(); + double maxPrice = itemEntry.getValue(); + + PriceEntry pe = new PriceEntry(); + pe.name = itemName; + pe.maxPrice = maxPrice; + se.prices.add(pe); + } + } + } + + /** + * Finds the ServerEntry in serversConfig for the given profile name. + */ + private static ServerEntry findServerEntryByProfile(String profileName) { + for (ServerEntry entry : LtrynekClient.serversConfig.servers) { + if (entry.profileName.equals(profileName)) { + return entry; + } + } + return null; + } +} +