Mekanism-tilera-Edition/common/mekanism/common/CommonWorldTickHandler.java
2013-10-18 07:33:27 -04:00

100 lines
2.5 KiB
Java

package mekanism.common;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import mekanism.api.Object3D;
import mekanism.common.tileentity.TileEntityDynamicTank;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType;
public class CommonWorldTickHandler implements ITickHandler
{
@Override
public void tickStart(EnumSet<TickType> type, Object... tickData) {}
@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData)
{
if(tickData[0] instanceof World)
{
ArrayList<Integer> idsToKill = new ArrayList<Integer>();
HashMap<Integer, HashSet<Object3D>> tilesToKill = new HashMap<Integer, HashSet<Object3D>>();
World world = (World)tickData[0];
if(!world.isRemote)
{
for(Map.Entry<Integer, DynamicTankCache> entry : Mekanism.dynamicInventories.entrySet())
{
int inventoryID = entry.getKey();
for(Object3D obj : entry.getValue().locations)
{
if(obj.dimensionId == world.provider.dimensionId)
{
TileEntityDynamicTank tileEntity = (TileEntityDynamicTank)obj.getTileEntity(world);
if(tileEntity == null || tileEntity.inventoryID != inventoryID)
{
if(!tilesToKill.containsKey(inventoryID))
{
tilesToKill.put(inventoryID, new HashSet<Object3D>());
}
tilesToKill.get(inventoryID).add(obj);
}
}
}
if(entry.getValue().locations.isEmpty())
{
idsToKill.add(inventoryID);
}
}
for(Map.Entry<Integer, HashSet<Object3D>> entry : tilesToKill.entrySet())
{
for(Object3D obj : entry.getValue())
{
Mekanism.dynamicInventories.get(entry.getKey()).locations.remove(obj);
}
}
for(int inventoryID : idsToKill)
{
for(Object3D obj : Mekanism.dynamicInventories.get(inventoryID).locations)
{
TileEntityDynamicTank dynamicTank = (TileEntityDynamicTank)obj.getTileEntity(world);
if(dynamicTank != null)
{
dynamicTank.cachedFluid = null;
dynamicTank.inventory = new ItemStack[2];
dynamicTank.inventoryID = -1;
}
}
Mekanism.dynamicInventories.remove(inventoryID);
}
}
}
}
@Override
public EnumSet<TickType> ticks()
{
return EnumSet.of(TickType.WORLD);
}
@Override
public String getLabel()
{
return "MekanismCommonWorld";
}
}