-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configuration Object Pattern (Test new configs)
- Loading branch information
1 parent
a7884cb
commit e3d9f92
Showing
12 changed files
with
114 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 90 additions & 89 deletions
179
...2.2/src/main/java/org/imesense/dynamicspawncontrol/technical/configs/CfgGameDebugger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.