PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/hunternif/mc/atlas/client/BiomeTextureConfig.java

https://github.com/AntiqueAtlasTeam/AntiqueAtlas
Java | 97 lines | 81 code | 8 blank | 8 comment | 16 complexity | 2a78ecb8d44e695c9cc2c8ce68f5eb85 MD5 | raw file
Possible License(s): GPL-3.0
  1. package hunternif.mc.atlas.client;
  2. import com.google.gson.JsonArray;
  3. import com.google.gson.JsonElement;
  4. import com.google.gson.JsonObject;
  5. import hunternif.mc.atlas.AntiqueAtlasMod;
  6. import hunternif.mc.atlas.util.AbstractJSONConfig;
  7. import hunternif.mc.atlas.util.Log;
  8. import net.minecraft.util.ResourceLocation;
  9. import net.minecraft.world.biome.Biome;
  10. import net.minecraftforge.fml.common.registry.ForgeRegistries;
  11. import net.minecraftforge.fml.relauncher.Side;
  12. import net.minecraftforge.fml.relauncher.SideOnly;
  13. import java.io.File;
  14. import java.util.ArrayList;
  15. import java.util.Collections;
  16. import java.util.Comparator;
  17. import java.util.List;
  18. import java.util.Map.Entry;
  19. /**
  20. * Client-only config mapping biome IDs to texture sets.
  21. * <p>Must be loaded after {@link TextureSetConfig}!</p>
  22. * @author Hunternif
  23. */
  24. @SideOnly(Side.CLIENT)
  25. public class BiomeTextureConfig extends AbstractJSONConfig<BiomeTextureMap> {
  26. private static final int VERSION = 2;
  27. private final TextureSetMap textureSetMap;
  28. public BiomeTextureConfig(File file, TextureSetMap textureSetMap) {
  29. super(file);
  30. this.textureSetMap = textureSetMap;
  31. }
  32. @Override
  33. public int currentVersion() {
  34. return VERSION;
  35. }
  36. @Override
  37. protected void loadData(JsonObject json, BiomeTextureMap data, int version) {
  38. if (version == 0) {
  39. Log.warn("Too many biome textures changed since config version 0,"
  40. + " disregarding this config entirely");
  41. return;
  42. }
  43. if (version == 1) {
  44. Log.warn("Config version 1 no longer supported, config file will be reset"
  45. + " We now use resource location to identify biomes");
  46. return;
  47. }
  48. for (Entry<String, JsonElement> entry : json.entrySet()) {
  49. Biome biome = ForgeRegistries.BIOMES.getValue(new ResourceLocation(entry.getKey()));
  50. if (biome == null) {
  51. Log.warn("Unknown biome in texture map: %s", entry.getKey());
  52. continue;
  53. }
  54. if (entry.getValue().isJsonArray()) {
  55. // List of textures: (this should be gone as of VERSION > 1)
  56. JsonArray array = entry.getValue().getAsJsonArray();
  57. ResourceLocation[] textures = new ResourceLocation[array.size()];
  58. for (int i = 0; i < array.size(); i++) {
  59. String path = array.get(i).getAsString();
  60. textures[i] = new ResourceLocation(path);
  61. }
  62. data.setTexture(biome, new TextureSet(null, textures));
  63. Log.info("Registered %d custom texture(s) for biome %s",
  64. textures.length, biome.getRegistryName().toString());
  65. } else {
  66. // Texture set:
  67. String name = entry.getValue().getAsString();
  68. if (textureSetMap.isRegistered(name)) {
  69. data.setTexture(biome, textureSetMap.getByName(name));
  70. Log.info("Registered texture set %s for biome %s", name, biome.getRegistryName().toString());
  71. } else {
  72. Log.warn("Unknown texture set %s for biome %s", name, biome.getRegistryName().toString());
  73. }
  74. }
  75. }
  76. }
  77. @Override
  78. protected void saveData(JsonObject json, BiomeTextureMap data) {
  79. // Sort resource locations by name
  80. List<Biome> keys = new ArrayList<Biome>(data.biomeTextureMap.keySet());
  81. Collections.sort(keys, Comparator.comparing(Biome::getRegistryName));
  82. for(Biome key : keys) {
  83. int biomeID = Biome.getIdForBiome(key);
  84. if (biomeID >= 0 && (AntiqueAtlasMod.instance.jeidPresent || biomeID < 256)) {
  85. json.addProperty(key.getRegistryName().toString(), data.biomeTextureMap.get(key).name);
  86. }
  87. }
  88. }
  89. }