Reorganized Code in CommonTickHandler

Reorganized code in CommonTickHandler. Should be a little more readable
now. This is in preparation for reimplementing Limbo decay.
This commit is contained in:
SenseiKiwi
2013-07-25 01:38:58 -04:00
parent 98162f2839
commit 9da6304dc9

View File

@@ -4,14 +4,11 @@ import java.util.ArrayList;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Random; import java.util.Random;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import StevenDimDoors.mod_pocketDim.helpers.dimHelper; import StevenDimDoors.mod_pocketDim.helpers.dimHelper;
import StevenDimDoors.mod_pocketDim.helpers.yCoordHelper; import StevenDimDoors.mod_pocketDim.helpers.yCoordHelper;
import StevenDimDoors.mod_pocketDim.ticking.MobObelisk; import StevenDimDoors.mod_pocketDim.ticking.MobObelisk;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.ITickHandler; import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType; import cpw.mods.fml.common.TickType;
@@ -19,16 +16,17 @@ import cpw.mods.fml.relauncher.Side;
public class CommonTickHandler implements ITickHandler public class CommonTickHandler implements ITickHandler
{ {
private Random rand = new Random(); private int tickCount = 0;
public int tickCount=0;
public int tickCount2=0;
private static DDProperties properties = null; private static DDProperties properties = null;
public static ArrayList<int[]> chunksToPopulate= new ArrayList<int[]>(); public static ArrayList<int[]> chunksToPopulate = new ArrayList<int[]>();
private static final Random rand = new Random();
public static final int MAX_MONOLITH_SPAWNING_CHANCE = 100; public static final int MAX_MONOLITH_SPAWNING_CHANCE = 100;
private static final int MAX_MONOLITH_SPAWN_Y = 245; private static final int MAX_MONOLITH_SPAWN_Y = 245;
private static final int CHUNK_SIZE = 16; private static final int CHUNK_SIZE = 16;
private static final int RIFT_REGENERATION_INTERVAL = 100; //Regenerate random rifts every 100 ticks
public CommonTickHandler() public CommonTickHandler()
{ {
if (properties == null) if (properties == null)
@@ -40,7 +38,7 @@ public class CommonTickHandler implements ITickHandler
{ {
if (type.equals(EnumSet.of(TickType.SERVER))) if (type.equals(EnumSet.of(TickType.SERVER)))
{ {
onTickInGame(); onServerTick();
} }
} }
@@ -51,9 +49,12 @@ public class CommonTickHandler implements ITickHandler
{ {
if(!CommonTickHandler.chunksToPopulate.isEmpty()) if(!CommonTickHandler.chunksToPopulate.isEmpty())
{ {
for(int[] chunkData : CommonTickHandler.chunksToPopulate) //TODO: This is bad. =/ We should not be passing around arrays of magic numbers.
//We should have an object that contains this information. ~SenseiKiwi
for (int[] chunkData : CommonTickHandler.chunksToPopulate)
{ {
if(chunkData[0]==properties.LimboDimensionID) if(chunkData[0] == properties.LimboDimensionID)
{ {
this.placeMonolithsInLimbo(chunkData[0], chunkData[1], chunkData[2]); this.placeMonolithsInLimbo(chunkData[0], chunkData[1], chunkData[2]);
} }
@@ -61,18 +62,20 @@ public class CommonTickHandler implements ITickHandler
{ {
this.placeMonolithsInPockets(chunkData[0], chunkData[1], chunkData[2]); this.placeMonolithsInPockets(chunkData[0], chunkData[1], chunkData[2]);
} }
} }
} }
CommonTickHandler.chunksToPopulate.clear(); CommonTickHandler.chunksToPopulate.clear();
} }
} }
public EnumSet ticks() @Override
public EnumSet<TickType> ticks()
{ {
return EnumSet.of(TickType.SERVER); return EnumSet.of(TickType.SERVER);
} }
@Override
public String getLabel() public String getLabel()
{ {
return null; return null;
@@ -82,24 +85,24 @@ public class CommonTickHandler implements ITickHandler
{ {
World worldObj = dimHelper.getWorld(worldID); World worldObj = dimHelper.getWorld(worldID);
DimData dimData = dimHelper.dimList.get(worldObj.provider.dimensionId); DimData dimData = dimHelper.dimList.get(worldObj.provider.dimensionId);
int sanity = 0; int sanity = 0;
int blockID = 0; int blockID = 0;
boolean didSpawn=false; boolean didSpawn=false;
if (dimData == null || if (dimData == null ||
dimData.dungeonGenerator == null || dimData.dungeonGenerator == null ||
dimData.dungeonGenerator.isOpen) dimData.dungeonGenerator.isOpen)
{ {
return; return;
} }
//The following initialization code is based on code from ChunkProviderGenerate. //The following initialization code is based on code from ChunkProviderGenerate.
//It makes our generation depend on the world seed. //It makes our generation depend on the world seed.
Random random = new Random(worldObj.getSeed()); Random random = new Random(worldObj.getSeed());
long factorA = random.nextLong() / 2L * 2L + 1L; long factorA = random.nextLong() / 2L * 2L + 1L;
long factorB = random.nextLong() / 2L * 2L + 1L; long factorB = random.nextLong() / 2L * 2L + 1L;
random.setSeed((long)chunkX * factorA + (long)chunkZ * factorB ^ worldObj.getSeed()); random.setSeed(chunkX * factorA + chunkZ * factorB ^ worldObj.getSeed());
int x, y, z; int x, y, z;
do do
{ {
@@ -108,12 +111,12 @@ public class CommonTickHandler implements ITickHandler
z = chunkZ * CHUNK_SIZE + random.nextInt(CHUNK_SIZE); z = chunkZ * CHUNK_SIZE + random.nextInt(CHUNK_SIZE);
y = MAX_MONOLITH_SPAWN_Y; y = MAX_MONOLITH_SPAWN_Y;
blockID = worldObj.getBlockId(x, y, z); blockID = worldObj.getBlockId(x, y, z);
while (blockID == 0 &&y>0) while (blockID == 0 &&y>0)
{ {
y--; y--;
blockID = worldObj.getBlockId(x, y, z); blockID = worldObj.getBlockId(x, y, z);
} }
while((blockID == mod_pocketDim.blockDimWall.blockID||blockID == mod_pocketDim.blockDimWallPerm.blockID)&&y>0) while((blockID == mod_pocketDim.blockDimWall.blockID||blockID == mod_pocketDim.blockDimWallPerm.blockID)&&y>0)
{ {
@@ -124,26 +127,26 @@ public class CommonTickHandler implements ITickHandler
{ {
y--; y--;
blockID = worldObj.getBlockId(x, y, z); blockID = worldObj.getBlockId(x, y, z);
} }
if(y > 0) if(y > 0)
{ {
int jumpSanity=0; int jumpSanity=0;
int jumpHeight=0; int jumpHeight=0;
do do
{ {
jumpHeight = y+random.nextInt(10); jumpHeight = y+random.nextInt(10);
jumpSanity++; jumpSanity++;
} }
while(!worldObj.isAirBlock(x,jumpHeight+6 , z)&&jumpSanity<20); while(!worldObj.isAirBlock(x,jumpHeight+6 , z)&&jumpSanity<20);
Entity mob = new MobObelisk(worldObj); Entity mob = new MobObelisk(worldObj);
mob.setLocationAndAngles(x, jumpHeight, z, 1, 1); mob.setLocationAndAngles(x, jumpHeight, z, 1, 1);
@@ -152,15 +155,15 @@ public class CommonTickHandler implements ITickHandler
} }
sanity++; sanity++;
} }
while (sanity<5&&!didSpawn); while (sanity<5&&!didSpawn);
} }
private void placeMonolithsInLimbo(int worldID, int var2, int var3) private void placeMonolithsInLimbo(int worldID, int var2, int var3)
{ {
World world = dimHelper.getWorld(worldID); World world = dimHelper.getWorld(worldID);
if (rand.nextInt(MAX_MONOLITH_SPAWNING_CHANCE) < properties.MonolithSpawningChance) if (rand.nextInt(MAX_MONOLITH_SPAWNING_CHANCE) < properties.MonolithSpawningChance)
{ {
int y =0; int y =0;
@@ -169,78 +172,91 @@ public class CommonTickHandler implements ITickHandler
int yTest; int yTest;
do do
{ {
x = var2*16 + rand.nextInt(16); x = var2*16 + rand.nextInt(16);
z = var3*16 + rand.nextInt(16); z = var3*16 + rand.nextInt(16);
while(world.getBlockId(x, y, z)==0&&y<255) while(world.getBlockId(x, y, z)==0&&y<255)
{ {
y++; y++;
} }
y = yCoordHelper.getFirstUncovered(world,x , y+2, z); y = yCoordHelper.getFirstUncovered(world,x , y+2, z);
yTest=yCoordHelper.getFirstUncovered(world,x , y+5, z); yTest=yCoordHelper.getFirstUncovered(world,x , y+5, z);
if(yTest>245) if(yTest>245)
{ {
return; return;
} }
int jumpSanity=0; int jumpSanity=0;
int jumpHeight=0; int jumpHeight=0;
do do
{ {
jumpHeight = y+rand.nextInt(25); jumpHeight = y+rand.nextInt(25);
jumpSanity++; jumpSanity++;
} }
while(!world.isAirBlock(x,jumpHeight+6 , z)&&jumpSanity<20); while(!world.isAirBlock(x,jumpHeight+6 , z)&&jumpSanity<20);
Entity mob = new MobObelisk(world); Entity mob = new MobObelisk(world);
mob.setLocationAndAngles(x, jumpHeight, z, 1, 1); mob.setLocationAndAngles(x, jumpHeight, z, 1, 1);
world.spawnEntityInWorld(mob); world.spawnEntityInWorld(mob);
} }
while (yTest > y); while (yTest > y);
} }
} }
private void onTickInGame() private void onServerTick()
{
tickCount++; //There is no need to reset the counter. Let it overflow.
if (tickCount % RIFT_REGENERATION_INTERVAL == 0)
{
regenerateRifts();
}
if (mod_pocketDim.teleTimer > 0)
{
mod_pocketDim.teleTimer--;
}
}
private void regenerateRifts()
{ {
try try
{ {
//Replace rifts that have been replaced (not permanently removed) by players //Replace rifts that have been replaced (not permanently removed) by players
if (tickCount > 100)
int i = 0;
while (i < 15 && FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
{ {
tickCount = 0; i++;
int i = 0; LinkData link;
while (i < 15 && FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) //actually gets the random rift based on the size of the list
link = (LinkData) dimHelper.instance.getRandomLinkData(true);
if(link!=null)
{ {
i++;
LinkData link;
//actually gets the random rift based on the size of the list if (dimHelper.getWorld(link.locDimID)!=null)
link = (LinkData) dimHelper.instance.getRandomLinkData(true);
if(link!=null)
{ {
World world = dimHelper.getWorld(link.locDimID);
if(dimHelper.getWorld(link.locDimID)!=null) int blocktoReplace = world.getBlockId(link.locXCoord, link.locYCoord, link.locZCoord);
if(!mod_pocketDim.blocksImmuneToRift.contains(blocktoReplace))//makes sure the rift doesn't replace a door or something
{ {
World world=dimHelper.getWorld(link.locDimID); if(dimHelper.instance.getLinkDataFromCoords(link.locXCoord, link.locYCoord, link.locZCoord, link.locDimID) != null)
int blocktoReplace = world.getBlockId(link.locXCoord, link.locYCoord, link.locZCoord);
if(!mod_pocketDim.blocksImmuneToRift.contains(blocktoReplace))//makes sure the rift doesn't replace a door or something
{ {
if(dimHelper.instance.getLinkDataFromCoords(link.locXCoord, link.locYCoord, link.locZCoord, link.locDimID) != null) dimHelper.getWorld(link.locDimID).setBlock(link.locXCoord, link.locYCoord, link.locZCoord, properties.RiftBlockID);
{ TileEntityRift.class.cast(dimHelper.getWorld(link.locDimID).getBlockTileEntity(link.locXCoord, link.locYCoord, link.locZCoord)).hasGrownRifts=true;
dimHelper.getWorld(link.locDimID).setBlock(link.locXCoord, link.locYCoord, link.locZCoord, properties.RiftBlockID);
TileEntityRift.class.cast(dimHelper.getWorld(link.locDimID).getBlockTileEntity(link.locXCoord, link.locYCoord, link.locZCoord)).hasGrownRifts=true;
}
} }
} }
} }
@@ -249,18 +265,8 @@ public class CommonTickHandler implements ITickHandler
} }
catch (Exception e) catch (Exception e)
{ {
System.out.println("An exception occurred in CommonTickHandler.onGameTick():"); System.out.println("An exception occurred in CommonTickHandler.onServerTick():");
e.printStackTrace(); e.printStackTrace();
} }
finally
{
tickCount++;
}
if (mod_pocketDim.teleTimer > 0)
{
mod_pocketDim.teleTimer--;
}
} }
} }