2013-08-04 02:18:43 +02:00
|
|
|
package mekanism.api;
|
2013-07-06 03:43:59 +02:00
|
|
|
|
|
|
|
import java.util.EnumSet;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2013-08-22 04:34:36 +02:00
|
|
|
import net.minecraft.world.chunk.Chunk;
|
|
|
|
import net.minecraft.world.chunk.IChunkProvider;
|
2013-08-18 22:51:47 +02:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.ForgeSubscribe;
|
|
|
|
import net.minecraftforge.event.world.ChunkEvent;
|
2013-07-06 03:43:59 +02:00
|
|
|
import cpw.mods.fml.common.ITickHandler;
|
|
|
|
import cpw.mods.fml.common.TickType;
|
|
|
|
import cpw.mods.fml.common.registry.TickRegistry;
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
|
2013-08-04 02:18:43 +02:00
|
|
|
public class TransmitterNetworkRegistry implements ITickHandler
|
2013-07-06 03:43:59 +02:00
|
|
|
{
|
2013-08-04 02:18:43 +02:00
|
|
|
private static TransmitterNetworkRegistry INSTANCE = new TransmitterNetworkRegistry();
|
2013-08-18 22:51:47 +02:00
|
|
|
private static boolean loaderRegistered = false;
|
|
|
|
|
2013-08-04 02:18:43 +02:00
|
|
|
private HashSet<ITransmitterNetwork> networks = new HashSet<ITransmitterNetwork>();
|
2013-07-07 19:07:40 +02:00
|
|
|
|
2013-08-04 02:18:43 +02:00
|
|
|
public TransmitterNetworkRegistry()
|
2013-07-06 03:43:59 +02:00
|
|
|
{
|
|
|
|
TickRegistry.registerTickHandler(this, Side.SERVER);
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
public static void initiate()
|
|
|
|
{
|
|
|
|
if(!loaderRegistered)
|
|
|
|
{
|
|
|
|
loaderRegistered = true;
|
|
|
|
|
2013-08-22 04:34:36 +02:00
|
|
|
MinecraftForge.EVENT_BUS.register(new NetworkLoader());
|
2013-08-18 22:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:18:43 +02:00
|
|
|
public static TransmitterNetworkRegistry getInstance()
|
2013-07-06 03:43:59 +02:00
|
|
|
{
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
2013-08-04 02:18:43 +02:00
|
|
|
|
|
|
|
public void registerNetwork(ITransmitterNetwork network)
|
2013-07-06 03:43:59 +02:00
|
|
|
{
|
|
|
|
networks.add(network);
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:18:43 +02:00
|
|
|
public void removeNetwork(ITransmitterNetwork network)
|
2013-07-06 03:43:59 +02:00
|
|
|
{
|
2013-07-07 19:07:40 +02:00
|
|
|
if(networks.contains(network))
|
2013-07-06 03:43:59 +02:00
|
|
|
{
|
|
|
|
networks.remove(network);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 22:44:53 +02:00
|
|
|
public void pruneEmptyNetworks()
|
|
|
|
{
|
2013-08-04 02:18:43 +02:00
|
|
|
for(ITransmitterNetwork e : networks)
|
2013-07-28 04:32:41 +02:00
|
|
|
{
|
2013-08-04 02:18:43 +02:00
|
|
|
if(e.getSize() == 0)
|
2013-07-28 04:32:41 +02:00
|
|
|
{
|
|
|
|
removeNetwork(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-06 03:43:59 +02:00
|
|
|
@Override
|
|
|
|
public void tickStart(EnumSet<TickType> type, Object... tickData)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-08-04 02:18:43 +02:00
|
|
|
|
2013-07-06 03:43:59 +02:00
|
|
|
@Override
|
|
|
|
public void tickEnd(EnumSet<TickType> type, Object... tickData)
|
|
|
|
{
|
2013-08-06 07:40:41 +02:00
|
|
|
Set<ITransmitterNetwork> iterNetworks = (Set<ITransmitterNetwork>)networks.clone();
|
|
|
|
|
2013-08-04 02:18:43 +02:00
|
|
|
for(ITransmitterNetwork net : iterNetworks)
|
2013-07-06 03:43:59 +02:00
|
|
|
{
|
2013-07-28 04:32:41 +02:00
|
|
|
if(networks.contains(net))
|
|
|
|
{
|
|
|
|
net.tick();
|
|
|
|
}
|
2013-07-06 03:43:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumSet<TickType> ticks()
|
|
|
|
{
|
|
|
|
return EnumSet.of(TickType.SERVER);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getLabel()
|
|
|
|
{
|
2013-08-16 17:56:42 +02:00
|
|
|
return "MekanismNetworks";
|
2013-07-06 03:43:59 +02:00
|
|
|
}
|
|
|
|
|
2013-07-07 19:07:40 +02:00
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
2013-08-04 02:18:43 +02:00
|
|
|
return "Network Registry:\n" + networks;
|
2013-07-06 03:43:59 +02:00
|
|
|
}
|
2013-08-18 22:51:47 +02:00
|
|
|
|
|
|
|
public static class NetworkLoader
|
|
|
|
{
|
|
|
|
@ForgeSubscribe
|
|
|
|
public void onChunkLoad(ChunkEvent.Load event)
|
|
|
|
{
|
|
|
|
if(event.getChunk() != null)
|
|
|
|
{
|
2013-08-22 04:34:36 +02:00
|
|
|
int x = event.getChunk().xPosition;
|
|
|
|
int z = event.getChunk().zPosition;
|
|
|
|
IChunkProvider cProvider = event.getChunk().worldObj.getChunkProvider();
|
|
|
|
Chunk[] neighbors = new Chunk[5];
|
|
|
|
|
|
|
|
neighbors[0] = event.getChunk();
|
|
|
|
if(cProvider.chunkExists(x + 1, z)) neighbors[1] = cProvider.provideChunk(x + 1, z);
|
|
|
|
if(cProvider.chunkExists(x - 1, z)) neighbors[2] = cProvider.provideChunk(x - 1, z);
|
|
|
|
if(cProvider.chunkExists(x, z + 1)) neighbors[3] = cProvider.provideChunk(x, z + 1);
|
|
|
|
if(cProvider.chunkExists(x, z - 1)) neighbors[4] = cProvider.provideChunk(x, z - 1);
|
|
|
|
for(Chunk c : neighbors)
|
|
|
|
{
|
|
|
|
refreshChunk(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void refreshChunk(Chunk c)
|
|
|
|
{
|
|
|
|
if(c != null)
|
|
|
|
{
|
|
|
|
for(Object obj : c.chunkTileEntityMap.values())
|
2013-08-18 22:51:47 +02:00
|
|
|
{
|
|
|
|
if(obj instanceof TileEntity)
|
|
|
|
{
|
|
|
|
TileEntity tileEntity = (TileEntity)obj;
|
|
|
|
|
|
|
|
if(tileEntity instanceof ITransmitter)
|
|
|
|
{
|
|
|
|
((ITransmitter)tileEntity).refreshNetwork();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-06 03:43:59 +02:00
|
|
|
}
|