rise-of-blocks-mod/src/main/java/nl/kallestruik/riseofblocks/BlastResistanceOverrideConfig.java
Kalle Struik 129d754f67
All checks were successful
Build / build (push) Successful in 2m22s
Blast resistance overrides
2025-07-01 19:20:50 +02:00

60 lines
2.1 KiB
Java

package nl.kallestruik.riseofblocks;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.minecraft.resources.ResourceLocation;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import static nl.kallestruik.riseofblocks.RiseOfBlocks.LOGGER;
public class BlastResistanceOverrideConfig {
private static final File CONFIG_FILE = new File("config/riseofblocks-blastresistance.json");
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final Map<ResourceLocation, Float> BLAST_RESISTANCES = new HashMap<>();
public static void load() {
if (!CONFIG_FILE.exists()) {
writeDefaultConfig();
}
BLAST_RESISTANCES.clear();
try (FileReader reader = new FileReader(CONFIG_FILE)) {
Type type = new TypeToken<Map<String, Float>>() {
}.getType();
Map<String, Float> rawMap = GSON.fromJson(reader, type);
rawMap.forEach((id, value) -> BLAST_RESISTANCES.put(ResourceLocation.parse(id), value));
LOGGER.info("Loaded blast resistance override config for {} blocks", BLAST_RESISTANCES.size());
} catch (Exception e) {
LOGGER.error("Failed to load blast resistance override config", e);
}
}
private static void writeDefaultConfig() {
var defaultValues = new HashMap<String, Float>();
try {
CONFIG_FILE.getParentFile().mkdirs();
String json = GSON.toJson(defaultValues);
Files.write(CONFIG_FILE.toPath(), json.getBytes());
} catch (Exception e) {
LOGGER.error("Failed to write default blast resistance override config", e);
}
}
public static Float getResistance(ResourceLocation id) {
return BLAST_RESISTANCES.get(id);
}
public static boolean hasCustomResistance(ResourceLocation id) {
return BLAST_RESISTANCES.containsKey(id);
}
}