Skip to content

Commit

Permalink
Configuration Object Pattern (Test new configs)
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSerpskiStalker committed Sep 28, 2024
1 parent a7884cb commit e3d9f92
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 395 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,4 @@ public CfgCacheWorldGame(String nameConfigFile) throws IOException

CodeGenericUtils.printInitClassToLog(CfgCacheWorldGame.class);
}

@Override
public void init() throws IOException
{

}

@Override
public void loadConfigFromFile() throws IOException {

}

@Override
public boolean getConfigValue(String key) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,138 +1,139 @@
package org.imesense.dynamicspawncontrol.technical.configs;

import com.google.gson.*;
import com.google.gson.annotations.JsonAdapter;
import org.imesense.dynamicspawncontrol.debug.CodeGenericUtils;
import org.imesense.dynamicspawncontrol.technical.customlibrary.Log;

import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public final class CfgGameDebugger extends CustomConceptConfig
{
public static CfgGameDebugger instance;

private final Map<String, Map<String, Boolean>> configParams = new HashMap<>();
private final DebugMonitor monitor;
private final DebugEvent event;

@Override
public boolean getConfigValue(String key) {
return false;
}

public CfgGameDebugger(String nameConfigFile) throws IOException
public static final class DebugMonitor
{
super(nameConfigFile);
final String setCategory;

CodeGenericUtils.printInitClassToLog(CfgGameDebugger.class);
private static boolean monitorDebug = false;

this.init();
public DebugMonitor(@Nonnull final String category)
{
this.setCategory = category;
}

this.loadConfigFromFile();
}
public static Boolean getDebugMonitorOpt()
{
return monitorDebug;
}

@Override
public void init() throws IOException
{
addConfigParamIfAbsent("debug_monitor", "monitor_debug", false); // Debug info cache
addConfigParamIfAbsent("debug_event", "generic_maps_debug_event_effects", false); // Debug EventEffects
addConfigParamIfAbsent("debug_event", "generic_maps_debug_drop_all_items", false); // Debug DropAllItems
addConfigParamIfAbsent("debug_event", "generic_maps_debug_zombie_summon_aid", false); // Debug ZombieSummonAid
addConfigParamIfAbsent("debug_event", "generic_maps_debug_event_left_mouse_click", false); // Debug EventLeftMouseClick
addConfigParamIfAbsent("debug_event", "generic_maps_debug_main_override_spawn", false); // Debug MainOverrideSpawn
addConfigParamIfAbsent("debug_event", "generic_maps_debug_event_right_mouse_click", false); // Debug EventRightMouseClick
addConfigParamIfAbsent("debug_event", "generic_maps_debug_event_block_break", false); // Debug EventBlockBreak
addConfigParamIfAbsent("debug_event", "generic_maps_debug_event_block_place", false); // Debug EventBlockPlace
addConfigParamIfAbsent("debug_event", "generic_maps_debug_spawn_conditions", false); // Debug SpawnConditions
addConfigParamIfAbsent("debug_event", "generic_maps_debug_drop_all_experience", false); // Debug DropAllExperience

saveConfig();
public static void setDebugMonitorOpt(Boolean monitorDebug_)
{
monitorDebug = monitorDebug_;
}
}

private void addConfigParamIfAbsent(String category, String key, boolean defaultValue) {
Map<String, Boolean> categoryParams = configParams.computeIfAbsent(category, k -> new HashMap<>());
public static final class DebugEvent
{
final String setCategory;
private boolean drop_all_experience = false;

categoryParams.putIfAbsent(key, defaultValue);
}
public DebugEvent(@Nonnull final String category)
{
this.setCategory = category;
}

public boolean isMonitorDebugEnabled() {
return getConfigValue("debug_monitor", "monitor_debug");
}
public Boolean getDebugDropAllExpOpt()
{
return this.drop_all_experience;
}

private void addConfigParam(String category, String key, boolean defaultValue) {
configParams.computeIfAbsent(category, k -> new HashMap<>()).put(key, defaultValue);
public void setDebugDropAllExpOpt(Boolean drop_all_experience)
{
this.drop_all_experience = drop_all_experience;
}
}

@Override
public void loadConfigFromFile() throws IOException {
File configFile = new File(this.nameConfig);
public CfgGameDebugger(String nameConfigFile) throws IOException
{
super(nameConfigFile);

Log.writeDataToLogFile(1, "Param file: " + configFile);
this.nameConfig = this.constructPathToDirectory() + nameConfigFile;
this.monitor = new DebugMonitor("monitor");
this.event = new DebugEvent("event");

if (!configFile.exists()) {
return;
if (Files.exists(Paths.get(this.nameConfig)))
{
loadFromFile();
}
else
{
saveToFile();
}

FileReader reader = new FileReader(configFile);
JsonElement jsonElement = new JsonParser().parse(reader);
JsonObject jsonObject = jsonElement.getAsJsonObject();

String jsonString = jsonElement.toString();
String configFilePath = configFile.getAbsolutePath();
CodeGenericUtils.printInitClassToLog(CfgGameDebugger.class);
}

Log.writeDataToLogFile(1, String.format("File: %s, JSON Element: %s, JSON Object: %s, Config File Path: %s",
configFilePath, jsonString, jsonObject, configFilePath));
public void saveToFile() throws IOException
{
JsonObject jsonObject = new JsonObject();

for (Map.Entry<String, Map<String, Boolean>> categoryEntry : configParams.entrySet()) {
String category = categoryEntry.getKey();
JsonObject categoryObject = jsonObject.getAsJsonObject(category);
JsonObject monitorObject = new JsonObject();
monitorObject.addProperty("monitorDebug", monitor.getDebugMonitorOpt());
jsonObject.add("monitor", monitorObject);

Log.writeDataToLogFile(1, "Param 0: " + category + " " + categoryObject);
JsonObject eventObject = new JsonObject();
eventObject.addProperty("drop_all_exp", event.getDebugDropAllExpOpt());
jsonObject.add("event", eventObject);

if (categoryObject != null) {
for (Map.Entry<String, Boolean> paramEntry : categoryEntry.getValue().entrySet()) {
String key = paramEntry.getKey();
JsonElement element = categoryObject.get(key);
Gson gson = new GsonBuilder().setPrettyPrinting().create();

Log.writeDataToLogFile(1, "Param 1: " + key + " " + element);
try (FileWriter file = new FileWriter(this.nameConfig))
{
gson.toJson(jsonObject, file);
}
}

if (element != null && element.isJsonPrimitive()) {
configParams.get(category).put(key, element.getAsBoolean());
public void loadFromFile() throws IOException
{
try (FileReader reader = new FileReader(this.nameConfig))
{
JsonElement jsonElement = new JsonParser().parse(reader);
JsonObject jsonObject = jsonElement.getAsJsonObject();

if (jsonObject.has("monitor"))
{
JsonObject monitorObject = jsonObject.getAsJsonObject("monitor");
this.monitor.setDebugMonitorOpt(monitorObject.get("monitorDebug").getAsBoolean());
}

Log.writeDataToLogFile(1, "Param 2: " + configParams.get(category).put(key, element.getAsBoolean()));
}
}
if (jsonObject.has("event"))
{
JsonObject eventObject = jsonObject.getAsJsonObject("event");
this.event.setDebugDropAllExpOpt(eventObject.get("drop_all_exp").getAsBoolean());
}
}
}

public boolean getConfigValue(String category, String key) {
Map<String, Boolean> categoryParams = configParams.get(category);
if (categoryParams != null) {
return categoryParams.getOrDefault(key, false);
}
return false;
// Геттеры для доступа к внутренним объектам
public DebugMonitor getMonitor()
{
return monitor;
}

private void saveConfig() throws IOException {
JsonObject jsonObject = new JsonObject();

for (Map.Entry<String, Map<String, Boolean>> categoryEntry : configParams.entrySet()) {
String category = categoryEntry.getKey();
JsonObject categoryObject = new JsonObject();

for (Map.Entry<String, Boolean> paramEntry : categoryEntry.getValue().entrySet()) {
categoryObject.addProperty(paramEntry.getKey(), paramEntry.getValue());
}

jsonObject.add(category, categoryObject);
}

Gson gson = new GsonBuilder().setPrettyPrinting().create();
try (FileWriter fileWriter = new FileWriter(this.nameConfig)) {
gson.toJson(jsonObject, fileWriter);
}
public DebugEvent getEvent()
{
return event;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,4 @@ public CfgLogFile(String nameConfigFile) throws IOException

CodeGenericUtils.printInitClassToLog(CfgLogFile.class);
}

@Override
public void init() throws IOException
{

}

@Override
public void loadConfigFromFile() throws IOException {

}

@Override
public boolean getConfigValue(String key) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,4 @@ public CfgPlayer(String nameConfigFile) throws IOException

CodeGenericUtils.printInitClassToLog(CfgPlayer.class);
}

@Override
public void init() throws IOException
{

}

@Override
public void loadConfigFromFile() throws IOException {

}

@Override
public boolean getConfigValue(String key) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,4 @@ public CfgRenderNightConfig(String nameConfigFile) throws IOException

CodeGenericUtils.printInitClassToLog(CfgRenderNightConfig.class);
}

@Override
public void init() throws IOException
{

}

@Override
public void loadConfigFromFile() throws IOException {

}

@Override
public boolean getConfigValue(String key) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,4 @@ public CfgWindowTitle(String nameConfigFile) throws IOException

CodeGenericUtils.printInitClassToLog(CfgWindowTitle.class);
}

@Override
public void init() throws IOException
{

}

@Override
public void loadConfigFromFile() throws IOException {

}

@Override
public boolean getConfigValue(String key) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,4 @@ public CfgWorldGenerator(String nameConfigFile) throws IOException

CodeGenericUtils.printInitClassToLog(CfgWorldGenerator.class);
}

@Override
public void init() throws IOException
{

}

@Override
public void loadConfigFromFile() throws IOException {

}

@Override
public boolean getConfigValue(String key) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,4 @@ public CfgWorldTime(String nameConfigFile) throws IOException

CodeGenericUtils.printInitClassToLog(CfgWorldTime.class);
}

@Override
public void init() throws IOException
{

}

@Override
public void loadConfigFromFile() throws IOException {

}

@Override
public boolean getConfigValue(String key) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,4 @@ public CfgZombieDropItem(String nameConfigFile) throws IOException

CodeGenericUtils.printInitClassToLog(CfgZombieDropItem.class);
}

@Override
public void init() throws IOException
{

}

@Override
public void loadConfigFromFile() throws IOException {

}

@Override
public boolean getConfigValue(String key) {
return false;
}
}
Loading

0 comments on commit e3d9f92

Please sign in to comment.