Skip to content

Commit

Permalink
Test add annotations to read the config without duplicating arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSerpskiStalker committed Sep 29, 2024
1 parent 5dc7250 commit fe9b8d5
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

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

public CfgCacheWorldGame(String nameConfigFile) throws IOException
public CfgCacheWorldGame(String nameConfigFile)
{
super(nameConfigFile);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,83 +1,114 @@
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.configs.cfgGameDebugger.DataCategories;
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.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

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

public CfgGameDebugger(String nameConfigFile) throws IOException
{
public CfgGameDebugger(String nameConfigFile) {
super(nameConfigFile);

this.nameConfig = this.constructPathToDirectory() + nameConfigFile;
Log.writeDataToLogFile(0, "nameConfigFile: " + nameConfigFile);

DataCategories.DebugMonitor.instance = new DataCategories.DebugMonitor("monitor");
DataCategories.DebugEvent.instance = new DataCategories.DebugEvent("event");

if (Files.exists(Paths.get(this.nameConfig)))
{
loadFromFile();
}
else
{
saveToFile();
if (Files.exists(Paths.get(this.nameConfig))) {
try {
loadFromFile();
} catch (FileNotFoundException e) {
Log.writeDataToLogFile(2, "File not found while loading: " + e.getMessage());
} catch (IOException e) {
Log.writeDataToLogFile(2, "IO Exception while loading: " + e.getMessage());
}
} else {
Log.writeDataToLogFile(0, "Config file does not exist. Creating a new one.");
try {
saveToFile();
} catch (IOException e) {
Log.writeDataToLogFile(2, "IO Exception while saving: " + e.getMessage());
}
}

CodeGenericUtils.printInitClassToLog(CfgGameDebugger.class);
}

public void saveToFile() throws IOException
{
public void saveToFile() throws IOException {
Path configPath = Paths.get(this.nameConfig).getParent();
if (Files.notExists(configPath)) {
Log.writeDataToLogFile(0, "Creating directory: " + configPath);
Files.createDirectories(configPath);
}

JsonObject jsonObject = new JsonObject();
JsonObject monitorObject = new JsonObject();

monitorObject.addProperty("monitorDebug", DataCategories.DebugMonitor.instance.getDebugMonitorOpt());
monitorObject.addProperty("debug_monitor_cache", DataCategories.DebugMonitor.instance.getDebugMonitorCache());
jsonObject.add("monitor", monitorObject);

JsonObject eventObject = new JsonObject();

eventObject.addProperty("drop_all_exp", DataCategories.DebugEvent.instance.getDebugDropAllExpOpt());
eventObject.addProperty("debug_on_block_break", DataCategories.DebugEvent.instance.getDebugOnBlockBreak());
eventObject.addProperty("debug_on_block_place", DataCategories.DebugEvent.instance.getDebugOnBlockPlace());
eventObject.addProperty("debug_on_entity_spawn", DataCategories.DebugEvent.instance.getDebugOnEntitySpawn());
eventObject.addProperty("debug_on_left_click", DataCategories.DebugEvent.instance.getDebugOnLeftClick());
eventObject.addProperty("debug_on_living_drops", DataCategories.DebugEvent.instance.getDebugOnLivingDrops());
eventObject.addProperty("debug_on_living_experience_drop", DataCategories.DebugEvent.instance.getDebugOnLivingExperienceDrop());
eventObject.addProperty("debug_on_task_manager", DataCategories.DebugEvent.instance.getDebugOnTaskManager());
eventObject.addProperty("debug_on_player_tick", DataCategories.DebugEvent.instance.getDebugOnPlayerTick());
eventObject.addProperty("debug_on_potential_spawn", DataCategories.DebugEvent.instance.getDebugOnPotentialSpawn());
eventObject.addProperty("debug_on_right_click", DataCategories.DebugEvent.instance.getDebugOnRightClick());

jsonObject.add("event", eventObject);

Gson gson = new GsonBuilder().setPrettyPrinting().create();

try (FileWriter file = new FileWriter(this.nameConfig))
{
try (FileWriter file = new FileWriter(this.nameConfig)) {
gson.toJson(jsonObject, file);
} catch (IOException e) {
throw new RuntimeException("Error writing to file: " + e.getMessage(), e);
}
}

public void loadFromFile() throws IOException
{
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");
DataCategories.DebugMonitor.instance.setDebugMonitorOpt(monitorObject.get("monitorDebug").getAsBoolean());
}

if (jsonObject.has("event"))
{
JsonObject eventObject = jsonObject.getAsJsonObject("event");
DataCategories.DebugEvent.instance.setDebugDropAllExpOpt(eventObject.get("drop_all_exp").getAsBoolean());
public void loadFromFile() throws IOException {
Log.writeDataToLogFile(0, "test");
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");
DataCategories.DebugMonitor.instance.setDebugMonitorCache(monitorObject.get("monitorDebug").getAsBoolean());
}

if (jsonObject.has("event")) {
JsonObject eventObject = jsonObject.getAsJsonObject("event");
DataCategories.DebugEvent.instance.setDebugOnBlockBreak(eventObject.get("debug_on_block_break").getAsBoolean());
DataCategories.DebugEvent.instance.setDebugOnBlockPlace(eventObject.get("debug_on_block_place").getAsBoolean());
DataCategories.DebugEvent.instance.setDebugOnEntitySpawn(eventObject.get("debug_on_entity_spawn").getAsBoolean());
DataCategories.DebugEvent.instance.setDebugOnLeftClick(eventObject.get("debug_on_left_click").getAsBoolean());
DataCategories.DebugEvent.instance.setDebugOnLivingDrops(eventObject.get("debug_on_living_drops").getAsBoolean());
DataCategories.DebugEvent.instance.setDebugOnLivingExperienceDrop(eventObject.get("debug_on_living_experience_drop").getAsBoolean());
DataCategories.DebugEvent.instance.setDebugOnTaskManager(eventObject.get("debug_on_task_manager").getAsBoolean());
DataCategories.DebugEvent.instance.setDebugOnPlayerTick(eventObject.get("debug_on_player_tick").getAsBoolean());
DataCategories.DebugEvent.instance.setDebugOnPotentialSpawn(eventObject.get("debug_on_potential_spawn").getAsBoolean());
DataCategories.DebugEvent.instance.setDebugOnRightClick(eventObject.get("debug_on_right_click").getAsBoolean());

}
} catch (FileNotFoundException e) {
Log.writeDataToLogFile(2, "File not found: " + e.getMessage());
throw e;
} catch (IOException e) {
Log.writeDataToLogFile(2, "IO Exception while loading: " + e.getMessage());
throw e;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

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

public CfgLogFile(String nameConfigFile) throws IOException
{
super(nameConfigFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

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

public CfgPlayer(String nameConfigFile) throws IOException
{
super(nameConfigFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

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

public CfgRenderNightConfig(String nameConfigFile) throws IOException
{
super(nameConfigFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

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

public CfgWindowTitle(String nameConfigFile) throws IOException
{
super(nameConfigFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

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

public CfgWorldGenerator(String nameConfigFile) throws IOException
{
super(nameConfigFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

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

public CfgWorldTime(String nameConfigFile) throws IOException
{
super(nameConfigFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

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

public CfgZombieDropItem(String nameConfigFile) throws IOException
{
super(nameConfigFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import org.imesense.dynamicspawncontrol.DynamicSpawnControl;
import org.imesense.dynamicspawncontrol.technical.customlibrary.Log;

import javax.annotation.Nonnull;
import java.io.*;
Expand All @@ -22,8 +23,12 @@ protected String constructPathToDirectory()
DynamicSpawnControl.STRUCT_FILES_DIRS.NAME_DIR_CONFIGS + File.separator;
}

public CustomConceptConfig(String nameConfigFile) throws IOException
public CustomConceptConfig(String nameConfigFile)
{
Log.writeDataToLogFile(0, "nameConfigFile: " + nameConfigFile);

this.nameConfig = this.constructPathToDirectory() + nameConfigFile;

Log.writeDataToLogFile(0, "this.nameConfig: " + this.nameConfig);
}
}
Loading

0 comments on commit fe9b8d5

Please sign in to comment.