Сохраняет и загружает положение драггинга в\из кфг package felon.gang.config; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import felon.gang.Felon; import felon.gang.functions.settings.Setting; import felon.gang.functions.settings.impl.*; import felon.gang.ui.styles.Style; import felon.gang.utils.client.IMinecraft; import felon.gang.utils.drag.DragManager; import felon.gang.utils.drag.Dragging; import lombok.Getter; import net.minecraft.client.Minecraft; import java.io.File; import java.util.Map; import java.util.function.Consumer; @Getter public class Config implements IMinecraft { private final File file; private final String name; public static String currentConfig = "autocfg"; Felon instance = Felon.getInstance(); public Config(String name) { this.name = name; this.file = new File(new File(Minecraft.getInstance().gameDir, "\\felon\\configs"), name + ".cfg"); } public void loadConfig(JsonObject jsonObject) { if (jsonObject == null) { return; } if (jsonObject.has("functions")) { loadFunctionSettings(jsonObject.getAsJsonObject("functions")); } if (jsonObject.has("styles")) { loadStyleSettings(jsonObject.getAsJsonObject("styles")); } if (jsonObject.has("draggables")) { loadDraggables(jsonObject.getAsJsonArray("draggables")); } } private void loadDraggables(JsonArray draggablesArray) { for (JsonElement element : draggablesArray) { JsonObject draggableObject = element.getAsJsonObject(); String name = draggableObject.get("name").getAsString(); Dragging dragging = DragManager.draggables.get(name); if (dragging != null) { dragging.setX(draggableObject.get("x").getAsInt()); dragging.setY(draggableObject.get("y").getAsInt()); } } } private void loadStyleSettings(JsonObject stylesObject) { for (Map.Entry<String, JsonElement> entry : stylesObject.entrySet()) { String styleName = entry.getKey(); JsonObject styleObject = entry.getValue().getAsJsonObject(); Style style = findStyleByName(styleName); if (style != null && styleObject.has("selected")) { boolean isSelected = styleObject.get("selected").getAsBoolean(); if (isSelected) { instance.getStyleManager().setCurrentStyle(style); } } } } private Style findStyleByName(String styleName) { for (Style style : instance.getStyleManager().getStyleList()) { if (style.getStyleName().equalsIgnoreCase(styleName)) { return style; } } return null; } private void loadFunctionSettings(JsonObject functionsObject) { instance.getFunctionRegistry().getFunctions().forEach(f -> { JsonObject moduleObject = functionsObject.getAsJsonObject(f.getName().toLowerCase()); if (moduleObject == null) { return; } f.setState(false, true); loadSettingFromJson(moduleObject, "bind", value -> f.setBind(value.getAsInt())); loadSettingFromJson(moduleObject, "state", value -> f.setState(value.getAsBoolean(), true)); f.getSettings().forEach(setting -> loadIndividualSetting(moduleObject, setting)); }); } private void loadIndividualSetting(JsonObject moduleObject, Setting<?> setting) { JsonElement settingElement = moduleObject.get(setting.getName()); if (settingElement == null || settingElement.isJsonNull()) { return; } if (setting instanceof SliderSetting) { ((SliderSetting) setting).set(settingElement.getAsFloat()); } else if (setting instanceof BooleanSetting) { ((BooleanSetting) setting).set(settingElement.getAsBoolean()); } else if (setting instanceof ColorSetting) { ((ColorSetting) setting).set(settingElement.getAsInt()); } else if (setting instanceof ModeSetting) { ((ModeSetting) setting).set(settingElement.getAsString()); } else if (setting instanceof BindSetting) { ((BindSetting) setting).set(settingElement.getAsInt()); } else if (setting instanceof StringSetting) { ((StringSetting) setting).set(settingElement.getAsString()); } else if (setting instanceof ModeListSetting) { loadModeListSetting((ModeListSetting) setting, moduleObject); } } private void loadModeListSetting(ModeListSetting setting, JsonObject moduleObject) { JsonObject elements = moduleObject.getAsJsonObject(setting.getName()); setting.get().forEach(option -> { JsonElement optionElement = elements.get(option.getName()); if (optionElement != null && !optionElement.isJsonNull()) { option.set(optionElement.getAsBoolean()); } }); } private void loadSettingFromJson(JsonObject jsonObject, String key, Consumer<JsonElement> consumer) { JsonElement element = jsonObject.get(key); if (element != null && !element.isJsonNull()) { consumer.accept(element); } } public JsonElement saveConfig() { JsonObject functionsObject = new JsonObject(); JsonObject stylesObject = new JsonObject(); JsonArray draggablesArray = new JsonArray(); saveFunctionSettings(functionsObject); saveStyleSettings(stylesObject); saveDraggables(draggablesArray); JsonObject newObject = new JsonObject(); newObject.add("functions", functionsObject); newObject.add("styles", stylesObject); newObject.add("draggables", draggablesArray); return newObject; } private void saveFunctionSettings(JsonObject functionsObject) { instance.getFunctionRegistry().getFunctions().forEach(module -> { JsonObject moduleObject = new JsonObject(); moduleObject.addProperty("bind", module.getBind()); moduleObject.addProperty("state", module.isState()); module.getSettings().forEach(setting -> saveIndividualSetting(moduleObject, setting)); functionsObject.add(module.getName().toLowerCase(), moduleObject); }); } private void saveIndividualSetting(JsonObject moduleObject, Setting<?> setting) { if (setting instanceof BooleanSetting) { moduleObject.addProperty(setting.getName(), ((BooleanSetting) setting).get()); } else if (setting instanceof SliderSetting) { moduleObject.addProperty(setting.getName(), ((SliderSetting) setting).get()); } else if (setting instanceof ModeSetting) { moduleObject.addProperty(setting.getName(), ((ModeSetting) setting).get()); } else if (setting instanceof ColorSetting) { moduleObject.addProperty(setting.getName(), ((ColorSetting) setting).get()); } else if (setting instanceof BindSetting) { moduleObject.addProperty(setting.getName(), ((BindSetting) setting).get()); } else if (setting instanceof StringSetting) { moduleObject.addProperty(setting.getName(), ((StringSetting) setting).get()); } else if (setting instanceof ModeListSetting) { saveModeListSetting(moduleObject, (ModeListSetting) setting); } } private void saveModeListSetting(JsonObject moduleObject, ModeListSetting setting) { JsonObject elements = new JsonObject(); setting.get().forEach(option -> elements.addProperty(option.getName(), option.get())); moduleObject.add(setting.getName(), elements); } private void saveStyleSettings(JsonObject stylesObject) { for (Style style : instance.getStyleManager().getStyleList()) { JsonObject styleObject = new JsonObject(); styleObject.addProperty("selected", instance.getStyleManager().getCurrentStyle() == style); stylesObject.add(style.getStyleName(), styleObject); } } private void saveDraggables(JsonArray draggablesArray) { for (Map.Entry<String, Dragging> entry : DragManager.draggables.entrySet()) { JsonObject draggableObject = new JsonObject(); draggableObject.addProperty("name", entry.getKey()); draggableObject.addProperty("x", entry.getValue().getX()); draggableObject.addProperty("y", entry.getValue().getY()); draggablesArray.add(draggableObject); } } } package felon.gang.config; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import net.minecraft.client.Minecraft; import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; public class ConfigStorage { public final Logger logger = Logger.getLogger(ConfigStorage.class.getName()); public final File CONFIG_DIR = new File(Minecraft.getInstance().gameDir, "\\felon\\configs"); public final File AUTOCFG_DIR = new File(CONFIG_DIR, "autocfg.cfg"); public final JsonParser jsonParser = new JsonParser(); public void init() throws IOException { setupFolder(); } public void writeDefaultConfig() { String json = """ { "functions": { }, "styles": { }, "draggables": [ ] } """; try (FileWriter writer = new FileWriter(AUTOCFG_DIR)) { writer.write(json); writer.flush(); logger.log(Level.INFO, "Created default system configuration!"); } catch (IOException e) { logger.log(Level.INFO, "Failed to create default system configuration file", e); } } public void setupFolder() { if (!CONFIG_DIR.exists()) { CONFIG_DIR.mkdirs(); } else if (AUTOCFG_DIR.exists()) { loadConfiguration("autocfg"); logger.log(Level.INFO, "Load system configuration..."); } else { logger.log(Level.INFO, "Creating system configuration..."); try { AUTOCFG_DIR.createNewFile(); writeDefaultConfig(); logger.log(Level.INFO, "Created!"); } catch (IOException e) { logger.log(Level.INFO, "Failed to create system configuration file", e); } } } public boolean isEmpty() { return getConfigs().isEmpty(); } public List<Config> getConfigs() { List<Config> configs = new ArrayList<>(); File[] configFiles = CONFIG_DIR.listFiles(); if (configFiles != null) { for (File configFile : configFiles) { if (configFile.isFile() && configFile.getName().endsWith(".cfg")) { String configName = configFile.getName().replace(".cfg", ""); Config config = findConfig(configName); if (config != null) { configs.add(config); } } } } return configs; } public void loadConfiguration(String configuration) { Config config = findConfig(configuration); Config.currentConfig = configuration; try { FileReader reader = new FileReader(config.getFile()); JsonParser parser = new JsonParser(); JsonObject object = (JsonObject) parser.parse(reader); config.loadConfig(object); } catch (FileNotFoundException e) { logger.log(Level.WARNING, "Not Found Exception", e); } catch (NullPointerException pointerException) { logger.log(Level.INFO, "Fatal error in Config!", pointerException); } } public void saveConfiguration(String configuration) { Config config = new Config(configuration); String contentPrettyPrint = new GsonBuilder().setPrettyPrinting().create().toJson(config.saveConfig()); try { FileWriter writer = new FileWriter(config.getFile()); writer.write(contentPrettyPrint); writer.close(); } catch (IOException e) { logger.log(Level.WARNING, "File not found!", e); } catch (NullPointerException e) { logger.log(Level.INFO, "Fatal Error in Config!", e); } } public static void saveConfiguration_static(String configuration) { Config config = new Config(configuration); String contentPrettyPrint = new GsonBuilder().setPrettyPrinting().create().toJson(config.saveConfig()); try { FileWriter writer = new FileWriter(config.getFile()); writer.write(contentPrettyPrint); writer.close(); } catch (IOException e) { System.out.println("File not found! " + e); } catch (NullPointerException e) { System.out.println("Fatal Error in Config! " + e); } } public Config findConfig(String configName) { if (configName == null) return null; if (new File(CONFIG_DIR, configName + ".cfg").exists()) return new Config(configName); return null; } } package felon.gang.utils.drag; import java.util.LinkedHashMap; import java.util.logging.Logger; public class DragManager { public static final Logger logger = Logger.getLogger(DragManager.class.getName()); public static LinkedHashMap<String, Dragging> draggables = new LinkedHashMap<>(); }