VitezKolya Posted September 4, 2014 Posted September 4, 2014 I'm having a strange problem with my ore generator. In my ConfigurationHandler it sets the dimensionList to the defaults in Ore, but some how OreGenerator loses the list(Well it's a array of strings not a list). I've used this code \/ to check whether or not dimensionList has my array of dimension ids. If I place it right before the dimensionList null check it prints true. LogHelper.info(JATM.OGLinstance.getOreList().get(0).dimensionList == null); Any help would be greatly appreciated. Here is the relevant code and links to my github repository. OreGenerator.java package com.vitezkolya.jatm.worldgen; import com.vitezkolya.jatm.JATM; import com.vitezkolya.jatm.reference.Reference; import com.vitezkolya.jatm.reference.Settings; import com.vitezkolya.jatm.utility.LogHelper; import com.vitezkolya.jatm.utility.Ore; import cpw.mods.fml.common.IWorldGenerator; import cpw.mods.fml.common.registry.GameRegistry; import net.minecraft.block.Block; import net.minecraft.init.Blocks; import net.minecraft.world.World; import net.minecraft.world.chunk.IChunkProvider; import net.minecraft.world.gen.feature.WorldGenMinable; import java.util.ArrayList; import java.util.Random; public class OreGenerator implements IWorldGenerator { //private String dimReplaceBlock; private ArrayList<String> dimList; /** * Generate some world * * @param random the chunk specific {@link java.util.Random}. * @param chunkX the chunk X coordinate of this chunk. * @param chunkZ the chunk Z coordinate of this chunk. * @param world : additionalData[0] The minecraft {@link net.minecraft.world.World} we're generating for. * @param chunkGenerator : additionalData[1] The {@link net.minecraft.world.chunk.IChunkProvider} that is generating. * @param chunkProvider : additionalData[2] {@link net.minecraft.world.chunk.IChunkProvider} that is requesting the world generation. */ @Override public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) { ArrayList<Ore> oreList = JATM.OGLinstance.getOreList(); if (!oreList.isEmpty()) { for (int index = 0; index < oreList.size(); index++) { Ore ore = oreList.get(index); if(ore.enabled == true) { if (ore.dimensionList != null) { LogHelper.info(ore.block.getLocalizedName() + "\'s dimension list is not null"); for (int dimListIndex = 0; dimListIndex < ore.dimensionList.length; dimListIndex++) { String dimension = ore.dimensionList[dimListIndex]; String dimReplaceBlock = ""; // -15:endstone,-1,0,1, 5~25, 26, 32~50:Netherrak if (checkDimSpecChars(dimension)) { if (dimension.contains(":")) { String[] dimSplit; dimSplit = splitDimensionData(dimension); dimension = dimSplit[0]; dimReplaceBlock = dimSplit[1]; } if (dimension.contains("~")) { if (isValidDim(dimension, world)) { dimReplaceBlock = defaultMinecraftDims(world); if (dimReplaceBlock != "") { addOreSpawn(ore.block, GameRegistry.findBlock(Reference.MOD_ID, dimReplaceBlock), world, random, chunkX * 16, chunkZ * 16, ore.veinSize - random.nextInt(6), ore.veinSize, ore.veinChance, ore.minHeight, ore.maxHeight); } else { addOreSpawn(ore.block, world, random, chunkX * 16, chunkZ * 16, ore.veinSize - random.nextInt(6), ore.veinSize, ore.veinChance, ore.minHeight, ore.maxHeight); } } } else { if (isValidDim(dimension, world)) { if (dimReplaceBlock != "") { addOreSpawn(ore.block, GameRegistry.findBlock(Reference.MOD_ID, dimReplaceBlock), world, random, chunkX * 16, chunkZ * 16, ore.veinSize - random.nextInt(6), ore.veinSize, ore.veinChance, ore.minHeight, ore.maxHeight); } else { addOreSpawn(ore.block, world, random, chunkX * 16, chunkZ * 16, ore.veinSize - random.nextInt(6), ore.veinSize, ore.veinChance, ore.minHeight, ore.maxHeight); } } } } else { if (isValidDim(dimension, world)) { dimReplaceBlock = defaultMinecraftDims(world); // Spawn ore addOreSpawn(ore.block, world, random, chunkX * 16, chunkZ * 16, ore.veinSize - random.nextInt(6), ore.veinSize, ore.veinChance, ore.minHeight, ore.maxHeight); } } } } } } } } private boolean isValidDim(String dimension, World world) { if(checkDimSpecChars(dimension)) { String[] dimSplit = dimension.split("~"); int smallerDimID = Integer.parseInt(dimSplit[0]); int largerDimID = Integer.parseInt(dimSplit[1]); for(int dimId = smallerDimID; smallerDimID <= largerDimID; smallerDimID++) { if(world.provider.dimensionId == smallerDimID) { return true; } } } else { if(world.provider.dimensionId == Integer.parseInt(dimension)) { return true; } } return false; } private String defaultMinecraftDims(World world) { switch (world.provider.dimensionId) { case -1: return "netherrack"; case 0: return "stone"; case 1: return "end_stone"; default: return "stone"; } } private boolean checkDimSpecChars(String data) { if(data.contains(":")) { return true; } else if(data.contains("~")) { return true; } return false; } /** * Splits dimension data * @param data dimensionID:replaceBlock * @return {dimensionID, replaceBlock} */ private String[] splitDimensionData(String data) { return data.split(":"); } /** * * This method adds our block to the world. * It randomizes the coordinates, and does that as many times, as defined in spawnChance. * Then it gives all the params to WorldGenMinable, which handles the replacing of the ores for us. * * @param block The block you want to spawn * @param world The world * @param random The Random * @param blockXPos the blockXpos of a chunk * @param blockZPos the blockZpos of a chunk * @param minVeinSize min vein * @param maxVeinSize max vein * @param chancesToSpawn the chance to spawn. Usually around 2 * @param minY lowest point to spawn * @param maxY highest point to spawn */ public void addOreSpawn(Block block, World world, Random random, int blockXPos, int blockZPos, int minVeinSize, int maxVeinSize, int chancesToSpawn, int minY, int maxY ) { addOreSpawn(block, Blocks.stone, world, random, blockXPos, blockZPos, minVeinSize, maxVeinSize, chancesToSpawn, minY, maxY); } public void addOreSpawn(Block block, Block Replaceable, World world, Random random, int blockXPos, int blockZPos, int minVeinSize, int maxVeinSize, int chancesToSpawn, int minY, int maxY) { if(minVeinSize <= 0) { minVeinSize = 1; } if(maxVeinSize <= 0) { maxVeinSize = 1; } LogHelper.info("Spawning ore"); WorldGenMinable minable = new WorldGenMinable(block, (minVeinSize + random.nextInt(maxVeinSize - minVeinSize)), Replaceable); for(int i = 0; i < chancesToSpawn; i++) { int posX = blockXPos + random.nextInt(16); int posY = minY + random.nextInt(maxY - minY); int posZ = blockZPos + random.nextInt(16); minable.generate(world, random, posX, posY, posZ); } } } OreGenerationListHandler.java package com.vitezkolya.jatm.handler; import com.vitezkolya.jatm.utility.LogHelper; import com.vitezkolya.jatm.utility.Ore; import net.minecraft.block.Block; import java.util.ArrayList; public class OreGenerationListHandler { private ArrayList<Ore> oreList = new ArrayList<Ore>(); /** * Add an ore block to the ore generation list * * @param block Ore block to be generated * @param defaultVeinCount Default value for vein count * @param defaultVeinSize Default value for vein size * @param defaultMinHeight Default value for min height * @param defaultMaxHeight Default value for max height * @param defaultVeinChance Default value for vein spawn chance * @param defaultDimensionList Default value list for list of dimensions the ore to spawn in */ public void addOre(Block block, int defaultVeinCount, int defaultVeinSize, int defaultMinHeight, int defaultMaxHeight, int defaultVeinChance, String[] defaultDimensionList) { oreList.add(new Ore(block, defaultVeinCount, defaultVeinSize, defaultMinHeight, defaultMaxHeight, defaultVeinChance, defaultDimensionList)); LogHelper.info(("Added: " + block.getLocalizedName())); } /** * Add ore to generation list and use hard coded default values. * * @param block Ore block to be generated */ public void addOre(Block block) { addOre(block, 5, 7, 4, 75, 50, new String[] {"-1", "0", "1"}); } /** * Returns the list of ores to be generated * * @return oreList - ArrayList of Ore classes */ public ArrayList<Ore> getOreList() { return oreList; } public void setOreList(ArrayList<Ore> oreList) { this.oreList = oreList; } } Ore.java package com.vitezkolya.jatm.utility; import net.minecraft.block.Block; public class Ore { public Block block; public int veinCount; public int veinCountDefault; public int veinSize; public int veinSizeDefault; public int minHeight; public int minHeightDefault; public int maxHeight; public int maxHeightDefault; public int veinChance; public int veinChanceDefault; public boolean enabled; public boolean enabledDefault; public String[] dimensionList; public String[] dimensionListDefault; public Ore(Block block, int veinCount, int veinSize, int minHeight, int maxHeight, int veinChance, String[] dimensionList) { this.block = block; this.veinCountDefault = veinCount; this.veinSizeDefault = veinSize; this.minHeightDefault = minHeight; this.maxHeightDefault = maxHeight; this.veinChanceDefault = veinChance; this.enabledDefault = true; this.dimensionListDefault = dimensionList; } public Ore(Block block) { this.block = block; this.veinCountDefault = 5; this.veinSizeDefault = 7; this.minHeightDefault = 4; this.maxHeightDefault = 75; this.veinChanceDefault = 50; this.enabledDefault = true; String[] dimensionList = {"-1", "0", "1"}; this.dimensionListDefault = dimensionList; } } ConfigurationHandler.java package com.vitezkolya.jatm.handler; import com.vitezkolya.jatm.JATM; import com.vitezkolya.jatm.reference.Messages; import com.vitezkolya.jatm.reference.Reference; import com.vitezkolya.jatm.utility.LogHelper; import com.vitezkolya.jatm.utility.Ore; import cpw.mods.fml.client.event.ConfigChangedEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import net.minecraft.util.StatCollector; import net.minecraftforge.common.config.Configuration; import java.io.File; import java.util.ArrayList; public class ConfigurationHandler { public static Configuration configuration; public static boolean configValue = false; public static ArrayList oreList; public static void init(File configFile) { // Create the configuration object from the given configuration file if (configuration == null) { configuration = new Configuration(configFile); loadConfiguration(); } } private static void loadConfiguration() { // Read in properties from configuration file configValue = configuration.get(Configuration.CATEGORY_GENERAL, "configValue", true, "This is an example config value").getBoolean(true); ArrayList<Ore> oreList = JATM.OGLinstance.getOreList(); if (!oreList.isEmpty()) { for (int index = 0; index < oreList.size(); index++) { Ore ore = oreList.get(index); LogHelper.info(("Config add: " + ore.block.getLocalizedName())); ore.enabled = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + ore.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.GENERATION_ORE_ENABLE, ore.enabledDefault, Messages.Configuration.GENERATION_ORE_ENABLE_COMMENT).getBoolean(ore.enabledDefault); ore.minHeight = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + ore.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_MIN_HEIGHT, ore.minHeightDefault, Messages.Configuration.ORE_MIN_HEIGHT_COMMENT, 0, 255).getInt(ore.minHeightDefault); ore.maxHeight = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + ore.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_MAX_HEIGHT, ore.maxHeightDefault, Messages.Configuration.ORE_MAX_HEIGHT_COMMENT, 0, 255).getInt(ore.maxHeightDefault); ore.veinSize = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + ore.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_VEIN_SIZE, ore.veinSizeDefault, Messages.Configuration.ORE_VEIN_SIZE_COMMENT, 0, 255).getInt(ore.veinSizeDefault); ore.veinChance = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + ore.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_VEIN_CHANCE, ore.veinChanceDefault, Messages.Configuration.ORE_VEIN_CHANCE_COMMENT, 0, 255).getInt(ore.veinChanceDefault); ore.veinCount = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + ore.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_VEIN_COUNT, ore.veinCountDefault, Messages.Configuration.ORE_VEIN_COUNT_COMMENT, 0, 255).getInt(ore.veinCountDefault); ore.dimensionListDefault = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + ore.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_DIMENSION_LIST, ore.dimensionListDefault, Messages.Configuration.ORE_DIMENSION_LIST_COMMENT).getStringList(); // Save the config to the ore oreList.set(index,ore); } // Save the configs to the ore list JATM.OGLinstance.setOreList(oreList); } if (configuration.hasChanged()) { configuration.save(); } } @SubscribeEvent public void onConfigurationChangedEvent(ConfigChangedEvent.OnConfigChangedEvent event) { if (event.modID.equalsIgnoreCase(Reference.MOD_ID)) { // Resync configs loadConfiguration(); } } } Quote
sequituri Posted September 4, 2014 Posted September 4, 2014 You have no constructor for your Ore class that sets the dimensionList property of the object. So, it is always null. The only similar property set is dimensionListDefault (in both constructors). You are obviously having problems because you named your parameters the names of some of your class properties. The only way to set the class property when you do that is with this syntax: this.dimensionList = dimensionList; To avoid such confusion, a) always use "this." or b) don't make your parameter names the same as your property (member) names. Quote -S- (if I helped, please click Thank and applaud) http://6upnqa.dm2301.livefilestore.com/y2mtf-vG7Tqq1TiiVpIm53KWj7294NDPoHfSHHb4PzZiMAUfRCfK0UY0MwOu7Q3zTBNVTKqWjr2-xgBfFRpQT5p-QivtvknPpoABMNUw9br9WuZcBFkjePhnAbW500gVm-P/sequiturian.png[/img]
VitezKolya Posted September 4, 2014 Author Posted September 4, 2014 Thanks for your reply but I don't think what the problem is. I could be wrong though. I've updated the Ore class to make it a bit easier for others to understand what the Ore class is used for. The ore class's variables that don't have default in them are not supposed to be initialized. They hold temp data that only loads from the config files or from the default values. Originally the code from the syncConfig method was in the ConfigurationHandler class. This is where the dimensionList variable is initialized by dimensionListDefault and others. Some how it's not staying initialized when it gets to the OreGenerator class. Even though I call it through the same instance as I used to add the temporary ore with(JATM.OGLinstance). package com.vitezkolya.jatm.utility; import com.vitezkolya.jatm.reference.Messages; import net.minecraft.block.Block; import net.minecraftforge.common.config.Configuration; public class Ore { public Block block; public int veinCount; public int veinCountDefault; public int veinSize; public int veinSizeDefault; public int minHeight; public int minHeightDefault; public int maxHeight; public int maxHeightDefault; public int veinChance; public int veinChanceDefault; public boolean enabled; public boolean enabledDefault; public String[] dimensionList; public String[] dimensionListDefault; public Ore(Block block, int veinCount, int veinSize, int minHeight, int maxHeight, int veinChance, String[] dimensionList) { this.block = block; this.veinCountDefault = veinCount; this.veinSizeDefault = veinSize; this.minHeightDefault = minHeight; this.maxHeightDefault = maxHeight; this.veinChanceDefault = veinChance; this.enabledDefault = true; this.dimensionListDefault = dimensionList; } public Ore(Block block) { this.block = block; this.veinCountDefault = 5; this.veinSizeDefault = 7; this.minHeightDefault = 4; this.maxHeightDefault = 75; this.veinChanceDefault = 50; this.enabledDefault = true; String[] dimensionList = {"-1", "0", "1"}; this.dimensionListDefault = dimensionList; } public void syncConfig(Configuration configuration) { LogHelper.info(("Config add: " + this.block.getLocalizedName())); this.enabled = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + this.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.GENERATION_ORE_ENABLE, this.enabledDefault, Messages.Configuration.GENERATION_ORE_ENABLE_COMMENT).getBoolean(this.enabledDefault); this.minHeight = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + this.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_MIN_HEIGHT, this.minHeightDefault, Messages.Configuration.ORE_MIN_HEIGHT_COMMENT, 0, 255).getInt(this.minHeightDefault); this.maxHeight = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + this.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_MAX_HEIGHT, this.maxHeightDefault, Messages.Configuration.ORE_MAX_HEIGHT_COMMENT, 0, 255).getInt(this.maxHeightDefault); this.veinSize = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + this.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_VEIN_SIZE, this.veinSizeDefault, Messages.Configuration.ORE_VEIN_SIZE_COMMENT, 0, 255).getInt(this.veinSizeDefault); this.veinChance = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + this.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_VEIN_CHANCE, this.veinChanceDefault, Messages.Configuration.ORE_VEIN_CHANCE_COMMENT, 0, 255).getInt(this.veinChanceDefault); this.veinCount = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + this.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_VEIN_COUNT, this.veinCountDefault, Messages.Configuration.ORE_VEIN_COUNT_COMMENT, 0, 255).getInt(this.veinCountDefault); this.dimensionListDefault = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + this.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_DIMENSION_LIST, this.dimensionListDefault, Messages.Configuration.ORE_DIMENSION_LIST_COMMENT).getStringList(); } } Quote
VitezKolya Posted September 4, 2014 Author Posted September 4, 2014 I just saw something I should have noticed before. this.dimensionListDefault = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + this.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_DIMENSION_LIST, this.dimensionListDefault, Messages.Configuration.ORE_DIMENSION_LIST_COMMENT).getStringList(); Just loading the default value to the default value. It should be this. this.dimensionList = configuration.get(Messages.Configuration.CATEGORY_GENERATION_ORE + "." + this.block.getLocalizedName().toLowerCase().replace(' ', '_'), Messages.Configuration.ORE_DIMENSION_LIST, this.dimensionListDefault, Messages.Configuration.ORE_DIMENSION_LIST_COMMENT).getStringList(); After I fixed a few other problems. My oreGenerater now works qute nicely. Quote
sequituri Posted September 5, 2014 Posted September 5, 2014 You're welcome. Quote -S- (if I helped, please click Thank and applaud) http://6upnqa.dm2301.livefilestore.com/y2mtf-vG7Tqq1TiiVpIm53KWj7294NDPoHfSHHb4PzZiMAUfRCfK0UY0MwOu7Q3zTBNVTKqWjr2-xgBfFRpQT5p-QivtvknPpoABMNUw9br9WuZcBFkjePhnAbW500gVm-P/sequiturian.png[/img]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.