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