diff --git a/src/main/java/forestry/apiculture/worldgen/HiveDecorator.java b/src/main/java/forestry/apiculture/worldgen/HiveDecorator.java index 9036d05e4..3127f2cda 100755 --- a/src/main/java/forestry/apiculture/worldgen/HiveDecorator.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveDecorator.java @@ -17,14 +17,12 @@ import java.util.Random; import net.minecraft.block.Block; import net.minecraft.world.World; import net.minecraft.world.biome.BiomeGenBase; +import net.minecraft.world.chunk.IChunkProvider; import net.minecraftforge.common.util.EnumHelper; -import net.minecraftforge.event.terraingen.PopulateChunkEvent; import net.minecraftforge.event.terraingen.PopulateChunkEvent.Populate.EventType; import net.minecraftforge.event.terraingen.TerrainGen; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; - import forestry.api.core.EnumHumidity; import forestry.api.core.EnumTemperature; import forestry.core.config.Config; @@ -51,15 +49,15 @@ public class HiveDecorator { private HiveDecorator() { } - @SubscribeEvent - public void generate(PopulateChunkEvent.Post event) { - if (!TerrainGen.populate(event.chunkProvider, event.world, event.rand, event.chunkX, event.chunkZ, event.hasVillageGenerated, EVENT_TYPE)) { + public void decorateHives(IChunkProvider chunkProvider, World world, Random rand, int chunkX, int chunkZ, boolean hasVillageGenerated) { + if (!TerrainGen.populate(chunkProvider, world, rand, chunkX, chunkZ, hasVillageGenerated, EVENT_TYPE)) { return; } - decorateHives(event.world, event.rand, event.chunkX, event.chunkZ); + + decorateHives(world, rand, chunkX, chunkZ); } - private void decorateHives(World world, Random rand, int chunkX, int chunkZ) { + public void decorateHives(World world, Random rand, int chunkX, int chunkZ) { List hives = PluginApiculture.hiveRegistry.getHives(); Collections.shuffle(hives, rand); diff --git a/src/main/java/forestry/core/ForestryCore.java b/src/main/java/forestry/core/ForestryCore.java index d6bcac59c..9d20d965c 100755 --- a/src/main/java/forestry/core/ForestryCore.java +++ b/src/main/java/forestry/core/ForestryCore.java @@ -64,10 +64,6 @@ public class ForestryCore { } public void init(Object basemod) { - - // Register world generator - GameRegistry.registerWorldGenerator(new WorldGenerator(), 0); - // Set fuels and resources for the fermenter FuelManager.fermenterFuel.put(ForestryItem.fertilizerCompound.getItemStack(), new FermenterFuel(ForestryItem.fertilizerCompound.getItemStack(), GameMode.getGameMode().getIntegerSetting("fermenter.value.fertilizer"), GameMode.getGameMode().getIntegerSetting("fermenter.cycles.fertilizer"))); @@ -126,7 +122,12 @@ public class ForestryCore { public void postInit() { PluginManager.runPostInit(); - Proxies.common.registerTickHandlers(); + // Register world generator + WorldGenerator worldGenerator = new WorldGenerator(); + GameRegistry.registerWorldGenerator(worldGenerator, 0); + + // Register tick handlers + Proxies.common.registerTickHandlers(worldGenerator); // Handle IMC messages. processIMCMessages(FMLInterModComms.fetchRuntimeMessages(ForestryAPI.instance)); diff --git a/src/main/java/forestry/core/TickHandlerCoreServer.java b/src/main/java/forestry/core/TickHandlerCoreServer.java index dfa355c52..74a091310 100755 --- a/src/main/java/forestry/core/TickHandlerCoreServer.java +++ b/src/main/java/forestry/core/TickHandlerCoreServer.java @@ -10,7 +10,17 @@ ******************************************************************************/ package forestry.core; +import com.google.common.collect.LinkedListMultimap; + +import java.util.List; +import java.util.Random; + import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; + +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.world.ChunkDataEvent; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; @@ -18,12 +28,20 @@ import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.common.gameevent.TickEvent.WorldTickEvent; import forestry.core.config.Config; +import forestry.core.config.Defaults; import forestry.core.interfaces.IResupplyHandler; +import forestry.core.utils.ChunkCoords; import forestry.plugins.PluginManager; public class TickHandlerCoreServer { - public TickHandlerCoreServer() { + + private final WorldGenerator worldGenerator; + private final LinkedListMultimap chunkRegenList = LinkedListMultimap.create(); + + public TickHandlerCoreServer(WorldGenerator worldGenerator) { + this.worldGenerator = worldGenerator; FMLCommonHandler.instance().bus().register(this); + MinecraftForge.EVENT_BUS.register(this); } @SubscribeEvent @@ -37,5 +55,46 @@ public class TickHandlerCoreServer { handler.resupply(player); } } + + if (Config.doRetrogen) { + World world = event.world; + int dimensionID = world.provider.dimensionId; + List chunkList = chunkRegenList.get(dimensionID); + + if (chunkList.size() > 0) { + ChunkCoords coords = chunkList.get(0); + chunkList.remove(0); + + // This bit is from FML's GameRegistry.generateWorld where the seed is constructed. + long worldSeed = world.getSeed(); + Random random = new Random(worldSeed); + long xSeed = random.nextLong() >> 2 + 1L; + long zSeed = random.nextLong() >> 2 + 1L; + random.setSeed((xSeed * coords.xCoord + zSeed * coords.zCoord) ^ worldSeed); + + worldGenerator.retroGen(random, coords.xCoord, coords.zCoord, world); + } + } + } + + @SubscribeEvent + public void chunkSaveEventHandler(ChunkDataEvent.Save event) { + NBTTagCompound tag = new NBTTagCompound(); + if (Config.doRetrogen) { + tag.setBoolean("retrogen", true); + } + + event.getData().setTag(Defaults.MOD, tag); + } + + @SubscribeEvent + public void chunkLoadEventHandler(ChunkDataEvent.Load event) { + if (Config.doRetrogen) { + NBTTagCompound tag = (NBTTagCompound)event.getData().getTag(Defaults.MOD); + if (tag == null || !tag.hasKey("retrogen") || Config.forceRetrogen) { + ChunkCoords coords = new ChunkCoords(event.getChunk()); + chunkRegenList.put(coords.dimension, coords); + } + } } } diff --git a/src/main/java/forestry/core/WorldGenerator.java b/src/main/java/forestry/core/WorldGenerator.java index 0b8d11d6b..66b6dcc2b 100755 --- a/src/main/java/forestry/core/WorldGenerator.java +++ b/src/main/java/forestry/core/WorldGenerator.java @@ -15,7 +15,11 @@ import java.util.Random; import net.minecraft.world.World; import net.minecraft.world.chunk.IChunkProvider; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.terraingen.PopulateChunkEvent; + import cpw.mods.fml.common.IWorldGenerator; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; import forestry.core.config.Config; import forestry.core.config.ForestryBlock; @@ -29,8 +33,28 @@ public class WorldGenerator implements IWorldGenerator { private WorldGenMinableMeta copperGenerator; private WorldGenMinableMeta tinGenerator; + public WorldGenerator() { + MinecraftForge.EVENT_BUS.register(this); + } + @Override public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) { + generateWorld(random, chunkX, chunkZ, world); + } + + @SubscribeEvent + public void populateChunk(PopulateChunkEvent.Post event) { + // / PLUGIN WORLD GENERATION + PluginManager.populateChunk(event.chunkProvider, event.world, event.rand, event.chunkX, event.chunkZ, event.hasVillageGenerated); + } + + public void retroGen(Random random, int chunkX, int chunkZ, World world) { + generateWorld(random, chunkX, chunkZ, world); + PluginManager.populateChunkRetroGen(world, random, chunkX, chunkZ); + world.getChunkFromChunkCoords(chunkX, chunkZ).setChunkModified(); + } + + private void generateWorld(Random random, int chunkX, int chunkZ, World world) { if (apatiteGenerator == null) { apatiteGenerator = new WorldGenMinableMeta(ForestryBlock.resources, 0, 36); @@ -69,9 +93,6 @@ public class WorldGenerator implements IWorldGenerator { int randPosZ = chunkZ + random.nextInt(16); tinGenerator.generate(world, random, randPosX, randPosY, randPosZ); } - - // / PLUGIN WORLD GENERATION - PluginManager.generateSurface(world, random, chunkX, chunkZ); } } diff --git a/src/main/java/forestry/core/config/Config.java b/src/main/java/forestry/core/config/Config.java index 1ea5d2a79..9419ec6f2 100755 --- a/src/main/java/forestry/core/config/Config.java +++ b/src/main/java/forestry/core/config/Config.java @@ -62,6 +62,10 @@ public class Config { public static boolean generateBeehives = true; public static boolean generateBeehivesDebug = false; + // Retrogen + public static boolean doRetrogen = false; + public static boolean forceRetrogen = false; + // Performance public static boolean enableBackpackResupply = true; @@ -122,6 +126,26 @@ public class Config { particleFX.comment = "set to false to disable particle fx on slower machines"; enableParticleFX = Boolean.parseBoolean(particleFX.value); + // RetroGen + + String retroGenMessage = "Forestry will attempt worldGen in chunks that were created before the mod was added."; + Property retroGen = config.get("world.retrogen.normal", CATEGORY_COMMON, false); + retroGen.comment = "Set to true, " + retroGenMessage; + doRetrogen = Boolean.parseBoolean(retroGen.value); + + String forcedRetroGenMessage = "Forestry will attempt worldGen in all chunks for this game instance. Config option will be set to false after this run."; + Property forceRetroGen = config.get("world.retrogen.forced", CATEGORY_COMMON, false); + forceRetroGen.comment = "Set to true, " + forcedRetroGenMessage; + forceRetrogen = Boolean.parseBoolean(forceRetroGen.value); + + if (forceRetrogen) { + Proxies.log.info(forcedRetroGenMessage); + config.set("world.generate.retrogen.forced", CATEGORY_COMMON, false); + doRetrogen = true; + } else if (doRetrogen) { + Proxies.log.info(retroGenMessage); + } + Property genApatiteOre = config.get("world.generate.apatite", CATEGORY_COMMON, true); genApatiteOre.comment = "set to false to force forestry to skip generating own apatite ore blocks in the world"; generateApatiteOre = Boolean.parseBoolean(genApatiteOre.value); diff --git a/src/main/java/forestry/core/proxy/ClientProxyCommon.java b/src/main/java/forestry/core/proxy/ClientProxyCommon.java index e87f55154..45aec45bb 100755 --- a/src/main/java/forestry/core/proxy/ClientProxyCommon.java +++ b/src/main/java/forestry/core/proxy/ClientProxyCommon.java @@ -14,6 +14,7 @@ import com.mojang.authlib.GameProfile; import forestry.apiculture.render.TextureBiomefinder; import forestry.core.ForestryClient; import forestry.core.TickHandlerCoreClient; +import forestry.core.WorldGenerator; import forestry.core.config.Config; import forestry.core.render.SpriteSheet; import java.io.File; @@ -43,8 +44,8 @@ public class ClientProxyCommon extends ProxyCommon { } @Override - public void registerTickHandlers() { - super.registerTickHandlers(); + public void registerTickHandlers(WorldGenerator worldGenerator) { + super.registerTickHandlers(worldGenerator); new TickHandlerCoreClient(); } diff --git a/src/main/java/forestry/core/proxy/ProxyCommon.java b/src/main/java/forestry/core/proxy/ProxyCommon.java index e41783504..40eff4ed2 100755 --- a/src/main/java/forestry/core/proxy/ProxyCommon.java +++ b/src/main/java/forestry/core/proxy/ProxyCommon.java @@ -23,6 +23,7 @@ import cpw.mods.fml.common.versioning.VersionParser; import cpw.mods.fml.common.versioning.VersionRange; import forestry.Forestry; import forestry.core.TickHandlerCoreServer; +import forestry.core.WorldGenerator; import forestry.core.config.Defaults; import forestry.core.config.ForestryBlock; import forestry.core.config.ForestryItem; @@ -61,8 +62,8 @@ public class ProxyCommon { return Loader.instance().getMinecraftModContainer().getVersion(); } - public void registerTickHandlers() { - new TickHandlerCoreServer(); + public void registerTickHandlers(WorldGenerator worldGenerator) { + new TickHandlerCoreServer(worldGenerator); } public void registerBlock(Block block, Class itemClass) { diff --git a/src/main/java/forestry/core/utils/ChunkCoords.java b/src/main/java/forestry/core/utils/ChunkCoords.java new file mode 100644 index 000000000..d214b0dd5 --- /dev/null +++ b/src/main/java/forestry/core/utils/ChunkCoords.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.core.utils; + +import net.minecraft.world.chunk.Chunk; + +public class ChunkCoords +{ + public int dimension; + public int xCoord; + public int zCoord; + + public ChunkCoords(Chunk chunk) + { + this.dimension = chunk.worldObj.provider.dimensionId; + this.xCoord = chunk.xPosition; + this.zCoord = chunk.zPosition; + } +} diff --git a/src/main/java/forestry/plugins/ForestryPlugin.java b/src/main/java/forestry/plugins/ForestryPlugin.java index e5b8577f0..302370f82 100755 --- a/src/main/java/forestry/plugins/ForestryPlugin.java +++ b/src/main/java/forestry/plugins/ForestryPlugin.java @@ -22,6 +22,7 @@ import java.util.EnumSet; import java.util.Random; import net.minecraft.command.ICommand; import net.minecraft.world.World; +import net.minecraft.world.chunk.IChunkProvider; public abstract class ForestryPlugin { @@ -65,7 +66,10 @@ public abstract class ForestryPlugin { return null; } - public void generateSurface(World world, Random rand, int chunkX, int chunkZ) { + public void populateChunk(IChunkProvider chunkProvider, World world, Random rand, int chunkX, int chunkZ, boolean hasVillageGeneratedZ) { + } + + public void populateChunkRetroGen(World world, Random rand, int chunkX, int chunkZ) { } public IPacketHandler getPacketHandler() { diff --git a/src/main/java/forestry/plugins/PluginApiculture.java b/src/main/java/forestry/plugins/PluginApiculture.java index 994ac171b..70cd1073f 100644 --- a/src/main/java/forestry/plugins/PluginApiculture.java +++ b/src/main/java/forestry/plugins/PluginApiculture.java @@ -11,6 +11,7 @@ package forestry.plugins; import java.util.ArrayList; +import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; @@ -25,6 +26,8 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.potion.Potion; import net.minecraft.util.IIcon; import net.minecraft.util.WeightedRandomChestContent; +import net.minecraft.world.World; +import net.minecraft.world.chunk.IChunkProvider; import net.minecraftforge.client.event.TextureStitchEvent; import net.minecraftforge.common.BiomeDictionary; @@ -336,10 +339,6 @@ public class PluginApiculture extends ForestryPlugin { Proxies.render.registerVillagerSkin(Defaults.ID_VILLAGER_BEEKEEPER, Defaults.TEXTURE_SKIN_BEEKPEEPER); VillagerRegistry.instance().registerVillageTradeHandler(Defaults.ID_VILLAGER_BEEKEEPER, villageHandler); - // Register world gen - if (Config.generateBeehives) - MinecraftForge.EVENT_BUS.register(HiveDecorator.instance()); - proxy.initializeRendering(); } @@ -1243,6 +1242,18 @@ public class PluginApiculture extends ForestryPlugin { return null; } + @Override + public void populateChunk(IChunkProvider chunkProvider, World world, Random rand, int chunkX, int chunkZ, boolean hasVillageGenerated) { + if (Config.generateBeehives) + HiveDecorator.instance().decorateHives(chunkProvider, world, rand, chunkX, chunkZ, hasVillageGenerated); + } + + @Override + public void populateChunkRetroGen(World world, Random rand, int chunkX, int chunkZ) { + if (Config.generateBeehives) + HiveDecorator.instance().decorateHives(world, rand, chunkX, chunkZ); + } + @Override public boolean processIMCMessage(IMCMessage message) { if (message.key.equals("add-candle-lighting-id")) { diff --git a/src/main/java/forestry/plugins/PluginManager.java b/src/main/java/forestry/plugins/PluginManager.java index 463eb95d5..e521a671c 100755 --- a/src/main/java/forestry/plugins/PluginManager.java +++ b/src/main/java/forestry/plugins/PluginManager.java @@ -35,6 +35,8 @@ import net.minecraft.command.ICommand; import net.minecraft.server.MinecraftServer; import net.minecraft.util.StatCollector; import net.minecraft.world.World; +import net.minecraft.world.chunk.IChunkProvider; + import net.minecraftforge.common.config.Configuration; import net.minecraftforge.common.config.Property; @@ -288,10 +290,17 @@ public class PluginManager { } } - public static void generateSurface(World world, Random rand, int chunkX, int chunkZ) { + public static void populateChunk(IChunkProvider chunkProvider, World world, Random rand, int chunkX, int chunkZ, boolean hasVillageGenerated) { for (Module m : loadedModules) { ForestryPlugin plugin = m.instance; - plugin.generateSurface(world, rand, chunkX, chunkZ); + plugin.populateChunk(chunkProvider, world, rand, chunkX, chunkZ, hasVillageGenerated); + } + } + + public static void populateChunkRetroGen(World world, Random rand, int chunkX, int chunkZ) { + for (Module m : loadedModules) { + ForestryPlugin plugin = m.instance; + plugin.populateChunkRetroGen(world, rand, chunkX, chunkZ); } }