Added config for biomes

This commit is contained in:
Aleksey 2020-09-22 17:10:04 +03:00
parent 41e0d1e42a
commit d382a965e9
10 changed files with 322 additions and 267 deletions

View file

@ -1,37 +0,0 @@
package ru.betterend.config;
import com.google.gson.JsonObject;
public class ClientConfig extends Config {
private static ClientConfig instance;
public static ClientConfig get() {
if (instance == null) {
instance = new ClientConfig();
}
return instance;
}
private ClientConfig() {
JsonObject config = ConfigWriter.load();
if (config.size() > 0) {
KEEPER.fromJson(config);
} else {
ConfigWriter.save(KEEPER.toJson());
}
}
public void reloadFromDisk() {
JsonObject config = ConfigWriter.load();
if (config.size() > 0) {
KEEPER.fromJson(config);
}
}
@Override
public void saveChanges() {
ConfigWriter.save(KEEPER.toJson());
}
}

View file

@ -5,29 +5,29 @@ import ru.betterend.config.ConfigKeeper.*;
public abstract class Config { public abstract class Config {
protected final static ConfigKeeper KEEPER = ConfigKeeper.getInstance(); protected final ConfigKeeper configKeeper = new ConfigKeeper();
public abstract void saveChanges(); public abstract void saveChanges();
public <E extends Entry<?>> E getEntry(String key) { public <E extends Entry<?>> E getEntry(String key) {
return KEEPER.getEntry(key); return this.configKeeper.getEntry(key);
} }
public <T> T getDefault(String key) { public <T> T getDefault(String key) {
Entry<T> entry = KEEPER.getEntry(key); Entry<T> entry = configKeeper.getEntry(key);
return entry != null ? entry.getDefault() : null; return entry != null ? entry.getDefault() : null;
} }
public String getString(String key) { public String getString(String key) {
String str = KEEPER.getValue(key); String str = configKeeper.getValue(key);
return str != null ? str : ""; return str != null ? str : "";
} }
public boolean setString(String key, String value) { public boolean setString(String key, String value) {
try { try {
StringEntry entry = KEEPER.getEntry(key); StringEntry entry = configKeeper.getEntry(key);
entry.setValue(value); entry.setValue(value);
KEEPER.set(key, entry); this.configKeeper.set(key, entry);
return true; return true;
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
@ -38,15 +38,15 @@ public abstract class Config {
} }
public int getInt(String key) { public int getInt(String key) {
Integer val = KEEPER.getValue(key); Integer val = configKeeper.getValue(key);
return val != null ? val : 0; return val != null ? val : 0;
} }
public boolean setInt(String key, int value) { public boolean setInt(String key, int value) {
try { try {
IntegerEntry entry = KEEPER.getEntry(key); IntegerEntry entry = configKeeper.getEntry(key);
entry.setValue(value); entry.setValue(value);
KEEPER.set(key, entry); this.configKeeper.set(key, entry);
return true; return true;
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
@ -58,9 +58,9 @@ public abstract class Config {
public <T extends Comparable<T>> boolean setRanged(String key, T value) { public <T extends Comparable<T>> boolean setRanged(String key, T value) {
try { try {
RangeEntry<T> entry = KEEPER.getEntry(key); RangeEntry<T> entry = configKeeper.getEntry(key);
entry.setValue(value); entry.setValue(value);
KEEPER.set(key, entry); this.configKeeper.set(key, entry);
return true; return true;
} catch (NullPointerException | ClassCastException ex) { } catch (NullPointerException | ClassCastException ex) {
@ -71,15 +71,15 @@ public abstract class Config {
} }
public float getFloat(String key) { public float getFloat(String key) {
Float val = KEEPER.getValue(key); Float val = configKeeper.getValue(key);
return val != null ? val : 0.0F; return val != null ? val : 0.0F;
} }
public boolean setFloat(String key, float value) { public boolean setFloat(String key, float value) {
try { try {
FloatEntry entry = KEEPER.getEntry(key); FloatEntry entry = configKeeper.getEntry(key);
entry.setValue(value); entry.setValue(value);
KEEPER.set(key, entry); this.configKeeper.set(key, entry);
return true; return true;
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
@ -90,15 +90,15 @@ public abstract class Config {
} }
public boolean getBoolean(String key) { public boolean getBoolean(String key) {
Boolean val = KEEPER.getValue(key); Boolean val = configKeeper.getValue(key);
return val != null ? val : false; return val != null ? val : false;
} }
public boolean setBoolean(String key, boolean value) { public boolean setBoolean(String key, boolean value) {
try { try {
BooleanEntry entry = KEEPER.getEntry(key); BooleanEntry entry = configKeeper.getEntry(key);
entry.setValue(value); entry.setValue(value);
KEEPER.set(key, entry); this.configKeeper.set(key, entry);
return true; return true;
} catch (NullPointerException ex) { } catch (NullPointerException ex) {

View file

@ -13,48 +13,9 @@ import ru.betterend.BetterEnd;
public final class ConfigKeeper { public final class ConfigKeeper {
private static ConfigKeeper instance;
public static ConfigKeeper getInstance() {
if (instance == null) {
instance = new ConfigKeeper();
}
return instance;
}
private Map<String, Entry<?>> configEntries = new HashMap<>(); private Map<String, Entry<?>> configEntries = new HashMap<>();
private ConfigKeeper() {} public JsonElement toJson(JsonObject jsonObject) {
@SuppressWarnings("unchecked")
public <E extends Entry<?>> E getEntry(String key) {
Entry<?> entry = this.configEntries.get(key);
if (entry == null) {
BetterEnd.LOGGER.warning(String.format("Entry '%s' doesn't exists.", key));
return null;
}
return (E) entry;
}
public <T> T getValue(String key) {
Entry<T> entry = this.getEntry(key);
if (entry == null) {
BetterEnd.LOGGER.warning(String.format("Empty value will be returned.", key));
return null;
}
return entry.getValue();
}
public void set(String key, Entry<?> entry) {
configEntries.put(key, entry);
}
public <T extends Entry<?>> void registerEntry(String key, T entry) {
configEntries.put(key, entry);
}
public JsonElement toJson() {
JsonObject jsonObject = new JsonObject();
for (String param : configEntries.keySet()) { for (String param : configEntries.keySet()) {
jsonObject.addProperty(param, configEntries.get(param).asString()); jsonObject.addProperty(param, configEntries.get(param).asString());
} }
@ -71,6 +32,33 @@ public final class ConfigKeeper {
} }
} }
@SuppressWarnings("unchecked")
public <E extends Entry<?>> E getEntry(String key) {
Entry<?> entry = this.configEntries.get(key);
if (entry == null) {
BetterEnd.LOGGER.warning(String.format("Entry '%s' doesn't exists.", key));
return null;
}
return (E) entry;
}
public <T> T getValue(String key) {
Entry<T> entry = this.getEntry(key);
if (entry == null) {
BetterEnd.LOGGER.warning("Empty value will be returned.");
return null;
}
return entry.getValue();
}
public void set(String key, Entry<?> entry) {
configEntries.put(key, entry);
}
public <T extends Entry<?>> void registerEntry(String key, T entry) {
configEntries.put(key, entry);
}
public static class BooleanEntry extends Entry<Boolean> { public static class BooleanEntry extends Entry<Boolean> {
public BooleanEntry(Boolean defaultValue, Consumer<Boolean> consumer, Supplier<Boolean> supplier) { public BooleanEntry(Boolean defaultValue, Consumer<Boolean> consumer, Supplier<Boolean> supplier) {
@ -94,12 +82,12 @@ public final class ConfigKeeper {
@Override @Override
public String asString() { public String asString() {
return getValue() ? "true" : "false"; return this.getValue() ? "true" : "false";
} }
@Override @Override
public void fromString(String value) { public void fromString(String value) {
setValue(value.equals("true") ? true : false); this.setValue(value.equals("true") ? true : false);
} }
} }
@ -132,7 +120,7 @@ public final class ConfigKeeper {
@Override @Override
public void fromString(String value) { public void fromString(String value) {
setValue(Float.valueOf(value)); this.setValue(Float.valueOf(value));
} }
} }
@ -155,7 +143,7 @@ public final class ConfigKeeper {
@Override @Override
public void fromString(String value) { public void fromString(String value) {
setValue(Float.valueOf(value)); this.setValue(Float.valueOf(value));
} }
@Override @Override
@ -193,7 +181,7 @@ public final class ConfigKeeper {
@Override @Override
public void fromString(String value) { public void fromString(String value) {
setValue(Integer.valueOf(value)); this.setValue(Integer.valueOf(value));
} }
} }
@ -216,7 +204,7 @@ public final class ConfigKeeper {
@Override @Override
public void fromString(String value) { public void fromString(String value) {
setValue(Integer.valueOf(value)); this.setValue(Integer.valueOf(value));
} }
@Override @Override
@ -254,7 +242,7 @@ public final class ConfigKeeper {
@Override @Override
public void fromString(String value) { public void fromString(String value) {
setValue(value); this.setValue(value);
} }
} }

View file

@ -13,24 +13,51 @@ import ru.betterend.util.JsonFactory;
public class ConfigWriter { public class ConfigWriter {
private final static FabricLoader fabricLoader = FabricLoader.getInstance(); private final static Path GAME_CONFIG_DIR = FabricLoader.getInstance().getConfigDir();
private final static Path GAME_CONFIG_DIR = fabricLoader.getConfigDir(); public final static File MOD_CONFIG_DIR = new File(GAME_CONFIG_DIR.toFile(), BetterEnd.MOD_ID);
private final static File MOD_CONFIG_DIR = new File(GAME_CONFIG_DIR.toFile(), BetterEnd.MOD_ID); private final static File MAIN_CONFIG_FILE = new File(MOD_CONFIG_DIR, "settings.json");
private final static File CONFIG_FILE = new File(MOD_CONFIG_DIR, "settings.json");
private static JsonObject configObject; private static JsonObject mainConfig;
private ConfigWriter() {} private JsonObject configObject;
private File configFile;
public static JsonObject load() { public JsonObject loadConfig(File configFile) {
this.configFile = configFile;
if (configObject == null) { if (configObject == null) {
configObject = JsonFactory.getJsonObject(CONFIG_FILE); configObject = load(configFile);
} }
return configObject; return configObject;
} }
public void saveConfig() {
if (configFile == null || configObject == null) {
return;
}
save(configFile, configObject);
}
public static JsonObject load() {
if (mainConfig == null) {
mainConfig = load(MAIN_CONFIG_FILE);
}
return mainConfig;
}
public static JsonObject load(File configFile) {
return JsonFactory.getJsonObject(configFile);
}
public static void save() {
save(MAIN_CONFIG_FILE, mainConfig);
}
public static void save(JsonElement config) { public static void save(JsonElement config) {
JsonFactory.storeJson(CONFIG_FILE, config); save(MAIN_CONFIG_FILE, config);
}
public static void save(File configFile, JsonElement config) {
JsonFactory.storeJson(configFile, config);
} }
} }

View file

@ -0,0 +1,34 @@
package ru.betterend.config;
import com.google.gson.JsonObject;
public class MainConfig extends Config {
private static MainConfig instance;
public static MainConfig getInstance() {
if (instance == null) {
instance = new MainConfig();
}
return instance;
}
private MainConfig() {
//TODO: Need to register config params in the Keeper
JsonObject config = ConfigWriter.load();
if (config.size() > 0) {
this.configKeeper.fromJson(config);
} else {
this.configKeeper.toJson(config);
ConfigWriter.save();
}
}
@Override
public void saveChanges() {
this.configKeeper.toJson(ConfigWriter.load());
ConfigWriter.save();
}
}

View file

@ -1,4 +1,4 @@
package ru.betterend; package ru.betterend.util;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;

View file

@ -1,4 +1,4 @@
package ru.betterend; package ru.betterend.util;
import java.util.Random; import java.util.Random;

View file

@ -0,0 +1,43 @@
package ru.betterend.world.biome;
import java.io.File;
import java.nio.file.Path;
import com.google.gson.JsonObject;
import net.minecraft.util.Identifier;
import ru.betterend.config.Config;
import ru.betterend.config.ConfigWriter;
public class BiomeConfig extends Config {
private final static Path BIOME_CONFIG_DIR = ConfigWriter.MOD_CONFIG_DIR.toPath().resolve("biomes");
private EndBiome biome;
private ConfigWriter configWriter;
private File configFile;
public BiomeConfig(EndBiome biome) {
this.biome = biome;
Identifier biomeId = biome.getID();
this.configFile = new File(BIOME_CONFIG_DIR.toFile(), biomeId.getPath());
this.configWriter = new ConfigWriter();
this.registerEntries();
JsonObject config = configWriter.loadConfig(configFile);
if (config.size() > 0) {
this.configKeeper.fromJson(config);
} else {
this.configKeeper.toJson(config);
this.configWriter.saveConfig();
}
}
private void registerEntries() {
//TODO: Need to register config params in the Keeper
}
@Override
public void saveChanges() {
this.configWriter.saveConfig();
}
}

View file

@ -26,7 +26,7 @@ import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.ConfiguredStructureFeature; import net.minecraft.world.gen.feature.ConfiguredStructureFeature;
import net.minecraft.world.gen.surfacebuilder.ConfiguredSurfaceBuilders; import net.minecraft.world.gen.surfacebuilder.ConfiguredSurfaceBuilders;
import ru.betterend.BetterEnd; import ru.betterend.BetterEnd;
import ru.betterend.MHelper; import ru.betterend.util.MHelper;
public class BiomeDefinition public class BiomeDefinition
{ {

View file

@ -4,8 +4,8 @@ import java.util.HashMap;
import net.minecraft.util.math.ChunkPos; import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.gen.ChunkRandom; import net.minecraft.world.gen.ChunkRandom;
import ru.betterend.MHelper;
import ru.betterend.noise.OpenSimplexNoise; import ru.betterend.noise.OpenSimplexNoise;
import ru.betterend.util.MHelper;
import ru.betterend.world.biome.EndBiome; import ru.betterend.world.biome.EndBiome;
public class BiomeMap public class BiomeMap