/src/main/java/hunternif/mc/atlas/client/BiomeTextureConfig.java
Java | 97 lines | 81 code | 8 blank | 8 comment | 16 complexity | 2a78ecb8d44e695c9cc2c8ce68f5eb85 MD5 | raw file
Possible License(s): GPL-3.0
- package hunternif.mc.atlas.client;
-
- import com.google.gson.JsonArray;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import hunternif.mc.atlas.AntiqueAtlasMod;
- import hunternif.mc.atlas.util.AbstractJSONConfig;
- import hunternif.mc.atlas.util.Log;
- import net.minecraft.util.ResourceLocation;
- import net.minecraft.world.biome.Biome;
- import net.minecraftforge.fml.common.registry.ForgeRegistries;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
-
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Map.Entry;
-
- /**
- * Client-only config mapping biome IDs to texture sets.
- * <p>Must be loaded after {@link TextureSetConfig}!</p>
- * @author Hunternif
- */
- @SideOnly(Side.CLIENT)
- public class BiomeTextureConfig extends AbstractJSONConfig<BiomeTextureMap> {
- private static final int VERSION = 2;
- private final TextureSetMap textureSetMap;
-
- public BiomeTextureConfig(File file, TextureSetMap textureSetMap) {
- super(file);
- this.textureSetMap = textureSetMap;
- }
-
- @Override
- public int currentVersion() {
- return VERSION;
- }
-
- @Override
- protected void loadData(JsonObject json, BiomeTextureMap data, int version) {
- if (version == 0) {
- Log.warn("Too many biome textures changed since config version 0,"
- + " disregarding this config entirely");
- return;
- }
- if (version == 1) {
- Log.warn("Config version 1 no longer supported, config file will be reset"
- + " We now use resource location to identify biomes");
- return;
- }
- for (Entry<String, JsonElement> entry : json.entrySet()) {
- Biome biome = ForgeRegistries.BIOMES.getValue(new ResourceLocation(entry.getKey()));
- if (biome == null) {
- Log.warn("Unknown biome in texture map: %s", entry.getKey());
- continue;
- }
- if (entry.getValue().isJsonArray()) {
- // List of textures: (this should be gone as of VERSION > 1)
- JsonArray array = entry.getValue().getAsJsonArray();
- ResourceLocation[] textures = new ResourceLocation[array.size()];
- for (int i = 0; i < array.size(); i++) {
- String path = array.get(i).getAsString();
- textures[i] = new ResourceLocation(path);
- }
- data.setTexture(biome, new TextureSet(null, textures));
- Log.info("Registered %d custom texture(s) for biome %s",
- textures.length, biome.getRegistryName().toString());
- } else {
- // Texture set:
- String name = entry.getValue().getAsString();
- if (textureSetMap.isRegistered(name)) {
- data.setTexture(biome, textureSetMap.getByName(name));
- Log.info("Registered texture set %s for biome %s", name, biome.getRegistryName().toString());
- } else {
- Log.warn("Unknown texture set %s for biome %s", name, biome.getRegistryName().toString());
- }
- }
- }
- }
-
- @Override
- protected void saveData(JsonObject json, BiomeTextureMap data) {
- // Sort resource locations by name
- List<Biome> keys = new ArrayList<Biome>(data.biomeTextureMap.keySet());
- Collections.sort(keys, Comparator.comparing(Biome::getRegistryName));
-
- for(Biome key : keys) {
- int biomeID = Biome.getIdForBiome(key);
- if (biomeID >= 0 && (AntiqueAtlasMod.instance.jeidPresent || biomeID < 256)) {
- json.addProperty(key.getRegistryName().toString(), data.biomeTextureMap.get(key).name);
- }
- }
- }
- }