PageRenderTime 74ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/common/buildcraft/energy/BCEnergyConfig.java

https://github.com/2xsaiko/BuildCraft
Java | 153 lines | 122 code | 29 blank | 2 comment | 8 complexity | 34c806461a68e90b97cf85c4ae3f77e4 MD5 | raw file
Possible License(s): LGPL-2.0
  1. package buildcraft.energy;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Set;
  8. import gnu.trove.set.TIntSet;
  9. import gnu.trove.set.hash.TIntHashSet;
  10. import org.apache.logging.log4j.Level;
  11. import net.minecraft.util.ResourceLocation;
  12. import net.minecraftforge.common.config.Property;
  13. import net.minecraftforge.fml.common.registry.ForgeRegistries;
  14. import buildcraft.api.core.BCLog;
  15. import buildcraft.lib.config.EnumRestartRequirement;
  16. import buildcraft.core.BCCoreConfig;
  17. public class BCEnergyConfig {
  18. public static boolean enableOilGeneration;
  19. public static final TIntSet excludedDimensions = new TIntHashSet();
  20. public static final Set<ResourceLocation> excessiveBiomes = new HashSet<>();
  21. public static final Set<ResourceLocation> surfaceDepositBiomes = new HashSet<>();
  22. public static final Set<ResourceLocation> excludedBiomes = new HashSet<>();
  23. private static Property propEnableOilGeneration;
  24. private static Property propExcessiveBiomes;
  25. private static Property propSurfaceDepositBiomes;
  26. private static Property propExcludedBiomes;
  27. private static Property propExcludedDimensions;
  28. public static void preInit() {
  29. EnumRestartRequirement world = EnumRestartRequirement.WORLD;
  30. EnumRestartRequirement game = EnumRestartRequirement.GAME;
  31. propEnableOilGeneration = BCCoreConfig.config.get("worldgen", "enableOilGen", true);
  32. propEnableOilGeneration.setComment("Should any oil sprouts or lakes generate, at all?");
  33. game.setTo(propEnableOilGeneration);
  34. String[] _excessive = { //
  35. BCEnergy.MODID + ":oil_desert", //
  36. BCEnergy.MODID + ":oil_ocean", //
  37. };
  38. propExcessiveBiomes = BCCoreConfig.config.get("worldgen", "excessiveBiomes", _excessive);
  39. propExcessiveBiomes.setComment("Biome registry names (e.g. 'minecraft:ocean','minecraft:plains')"
  40. + " of biomes that should have GREATLY increased oil generation rates.");
  41. world.setTo(propExcessiveBiomes);
  42. String[] _surface = {};
  43. propSurfaceDepositBiomes = BCCoreConfig.config.get("worldgen", "surfaceDepositBiomes", _surface);
  44. propSurfaceDepositBiomes.setComment("Biome registry names (e.g. 'minecraft:ocean','minecraft:hills')"
  45. + " of biomes that should have slightly increased oil generation rates.");
  46. world.setTo(propSurfaceDepositBiomes);
  47. String[] _excluded = { //
  48. "minecraft:hell", //
  49. "minecraft:sky",//
  50. };
  51. propExcludedBiomes = BCCoreConfig.config.get("worldgen", "excludedBiomes", _excluded);
  52. propExcludedBiomes.setComment("Biome registry names (e.g. 'minecraft:hell','minecraft:jungle')"
  53. + " of biomes that should never generate oil.");
  54. world.setTo(propExcludedBiomes);
  55. int[] _dims = { -1, 1 };
  56. propExcludedDimensions = BCCoreConfig.config.get("worldgen", "excludedDimensions", _dims);
  57. propExcludedDimensions.setComment("Dimension ID's (e.g. '-1' for the nether,'1' for the end)"
  58. + " of dimensions that should never generate oil.");
  59. world.setTo(propExcludedDimensions);
  60. reloadConfig(EnumRestartRequirement.GAME);
  61. BCCoreConfig.addReloadListener(BCEnergyConfig::reloadConfig);
  62. }
  63. public static void reloadConfig(EnumRestartRequirement restarted) {
  64. if (EnumRestartRequirement.WORLD.hasBeenRestarted(restarted)) {
  65. addBiomeNames(propExcludedBiomes, excessiveBiomes);
  66. addBiomeNames(propExcessiveBiomes, excessiveBiomes);
  67. addBiomeNames(propSurfaceDepositBiomes, surfaceDepositBiomes);
  68. excludedDimensions.clear();
  69. excludedDimensions.addAll(propExcludedDimensions.getIntList());
  70. if (EnumRestartRequirement.GAME.hasBeenRestarted(restarted)) {
  71. enableOilGeneration = propEnableOilGeneration.getBoolean();
  72. } else {
  73. validateBiomeNames();
  74. }
  75. }
  76. }
  77. private static void addBiomeNames(Property prop, Set<ResourceLocation> set) {
  78. set.clear();
  79. for (String s : prop.getStringList()) {
  80. set.add(new ResourceLocation(s));
  81. }
  82. }
  83. /** Called in post-init, after all biomes should have been registered. In 1.12 this should be called after the
  84. * registry event for biomes has been fired. */
  85. public static void validateBiomeNames() {
  86. Set<ResourceLocation> invalids = new HashSet<>();
  87. addInvalidBiomeNames(excessiveBiomes, invalids);
  88. addInvalidBiomeNames(excludedBiomes, invalids);
  89. addInvalidBiomeNames(surfaceDepositBiomes, invalids);
  90. if (invalids.isEmpty()) {
  91. return;
  92. }
  93. List<ResourceLocation> invalidList = new ArrayList<>();
  94. invalidList.addAll(invalids);
  95. Collections.sort(invalidList, Comparator.comparing(ResourceLocation::toString));
  96. List<ResourceLocation> allValid = new ArrayList<>();
  97. allValid.addAll(ForgeRegistries.BIOMES.getKeys());
  98. Collections.sort(allValid, Comparator.comparing(ResourceLocation::toString));
  99. BCLog.logger.warn("****************************************************");
  100. BCLog.logger.warn("*");
  101. BCLog.logger.warn("* Unknown biome name detected in buildcraft config!");
  102. BCLog.logger.warn("* (Config file = " + BCCoreConfig.config.getConfigFile().getAbsolutePath() + ")");
  103. BCLog.logger.warn("*");
  104. BCLog.logger.warn("* Unknown biomes: ");
  105. printList(Level.WARN, invalidList);
  106. BCLog.logger.warn("*");
  107. BCLog.logger.info("* All possible known names: ");
  108. printList(Level.INFO, allValid);
  109. BCLog.logger.info("*");
  110. BCLog.logger.warn("****************************************************");
  111. }
  112. private static void printList(Level level, List<ResourceLocation> list) {
  113. for (ResourceLocation location : list) {
  114. BCLog.logger.log(level, "* - " + location);
  115. }
  116. }
  117. private static void addInvalidBiomeNames(Set<ResourceLocation> toTest, Set<ResourceLocation> invalidDest) {
  118. for (ResourceLocation test : toTest) {
  119. if (!ForgeRegistries.BIOMES.containsKey(test)) {
  120. invalidDest.add(test);
  121. }
  122. }
  123. }
  124. }