2013-04-21 00:34:54 -04:00
|
|
|
package mekanism.common;
|
|
|
|
|
|
2013-04-28 15:23:08 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
2014-07-09 22:06:36 -04:00
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.Queue;
|
|
|
|
|
import java.util.Random;
|
2013-04-21 00:34:54 -04:00
|
|
|
|
2013-12-20 16:09:09 -05:00
|
|
|
import mekanism.api.Coord4D;
|
2014-08-01 13:04:31 -04:00
|
|
|
import mekanism.common.multiblock.MultiblockManager;
|
2014-07-09 22:06:36 -04:00
|
|
|
import net.minecraft.world.ChunkCoordIntPair;
|
2013-04-21 00:34:54 -04:00
|
|
|
import net.minecraft.world.World;
|
2014-04-20 21:15:44 +01:00
|
|
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
|
|
|
|
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
|
|
|
|
|
import cpw.mods.fml.common.gameevent.TickEvent.WorldTickEvent;
|
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
2013-04-21 00:34:54 -04:00
|
|
|
|
2014-04-20 21:15:44 +01:00
|
|
|
public class CommonWorldTickHandler
|
2013-04-21 00:34:54 -04:00
|
|
|
{
|
2014-07-09 22:06:36 -04:00
|
|
|
private static final long maximumDeltaTimeNanoSecs = 16000000; // 16 milliseconds
|
|
|
|
|
|
2014-08-01 13:04:31 -04:00
|
|
|
public boolean loaded;
|
|
|
|
|
|
2014-07-09 22:06:36 -04:00
|
|
|
private HashMap<Integer, Queue<ChunkCoordIntPair>> chunkRegenMap;
|
|
|
|
|
|
|
|
|
|
public void addRegenChunk(int dimensionId, ChunkCoordIntPair chunkCoord)
|
|
|
|
|
{
|
|
|
|
|
if(chunkRegenMap == null)
|
|
|
|
|
{
|
|
|
|
|
chunkRegenMap = new HashMap<Integer, Queue<ChunkCoordIntPair>>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!chunkRegenMap.containsKey(dimensionId))
|
|
|
|
|
{
|
|
|
|
|
LinkedList<ChunkCoordIntPair> list = new LinkedList<ChunkCoordIntPair>();
|
|
|
|
|
list.add(chunkCoord);
|
|
|
|
|
chunkRegenMap.put(dimensionId, list);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if(!chunkRegenMap.get(dimensionId).contains(chunkCoord))
|
|
|
|
|
{
|
|
|
|
|
chunkRegenMap.get(dimensionId).add(chunkCoord);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void resetRegenChunks()
|
|
|
|
|
{
|
|
|
|
|
if(chunkRegenMap != null)
|
|
|
|
|
{
|
|
|
|
|
chunkRegenMap.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-20 21:15:44 +01:00
|
|
|
@SubscribeEvent
|
|
|
|
|
public void onTick(WorldTickEvent event)
|
2013-04-21 00:34:54 -04:00
|
|
|
{
|
2014-08-01 13:04:31 -04:00
|
|
|
if(event.side == Side.SERVER)
|
2013-04-21 00:34:54 -04:00
|
|
|
{
|
2014-08-01 13:04:31 -04:00
|
|
|
if(event.phase == Phase.START)
|
|
|
|
|
{
|
|
|
|
|
tickStart(event.world);
|
|
|
|
|
}
|
|
|
|
|
else if(event.phase == Phase.END)
|
|
|
|
|
{
|
|
|
|
|
tickEnd(event.world);
|
|
|
|
|
}
|
2014-04-20 21:15:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-01 13:04:31 -04:00
|
|
|
|
|
|
|
|
public void tickStart(World world)
|
2014-04-20 21:15:44 +01:00
|
|
|
{
|
|
|
|
|
if(!world.isRemote)
|
|
|
|
|
{
|
2014-08-01 13:04:31 -04:00
|
|
|
if(!loaded)
|
2014-04-20 21:15:44 +01:00
|
|
|
{
|
2014-08-01 13:04:31 -04:00
|
|
|
loaded = true;
|
|
|
|
|
MultiblockManager.load(world);
|
2014-04-20 21:15:44 +01:00
|
|
|
}
|
2014-08-01 13:04:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2014-08-01 13:04:31 -04:00
|
|
|
public void tickEnd(World world)
|
|
|
|
|
{
|
|
|
|
|
if(!world.isRemote)
|
|
|
|
|
{
|
|
|
|
|
MultiblockManager.tick(world);
|
2014-07-09 22:06:36 -04:00
|
|
|
|
|
|
|
|
if(chunkRegenMap == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int dimensionId = world.provider.dimensionId;
|
|
|
|
|
|
2014-07-20 15:27:35 -04:00
|
|
|
//Credit to E. Beef
|
2014-07-09 22:06:36 -04:00
|
|
|
if(chunkRegenMap.containsKey(dimensionId))
|
|
|
|
|
{
|
|
|
|
|
Queue<ChunkCoordIntPair> chunksToGen = chunkRegenMap.get(dimensionId);
|
|
|
|
|
long startTime = System.nanoTime();
|
|
|
|
|
|
|
|
|
|
while(System.nanoTime() - startTime < maximumDeltaTimeNanoSecs && !chunksToGen.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
ChunkCoordIntPair nextChunk = chunksToGen.poll();
|
|
|
|
|
|
|
|
|
|
if(nextChunk == null)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Random fmlRandom = new Random(world.getSeed());
|
|
|
|
|
long xSeed = fmlRandom.nextLong() >> 2 + 1L;
|
|
|
|
|
long zSeed = fmlRandom.nextLong() >> 2 + 1L;
|
|
|
|
|
fmlRandom.setSeed((xSeed*nextChunk.chunkXPos + zSeed*nextChunk.chunkZPos) ^ world.getSeed());
|
|
|
|
|
|
|
|
|
|
Mekanism.genHandler.generate(fmlRandom, nextChunk.chunkXPos, nextChunk.chunkZPos, world, world.getChunkProvider(), world.getChunkProvider());
|
2014-07-20 15:27:35 -04:00
|
|
|
Mekanism.logger.info("[Mekanism] Regenerating ores at chunk " + nextChunk);
|
2014-07-09 22:06:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(chunksToGen.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
chunkRegenMap.remove(dimensionId);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-21 00:34:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|