2013-08-26 18:49:32 -04:00
|
|
|
package mekanism.api.transmitters;
|
2013-07-06 02:43:59 +01:00
|
|
|
|
|
|
|
import java.util.EnumSet;
|
2013-11-30 12:29:49 -05:00
|
|
|
import java.util.HashMap;
|
2013-07-06 02:43:59 +01:00
|
|
|
import java.util.HashSet;
|
2013-09-09 02:04:56 -04:00
|
|
|
import java.util.Iterator;
|
2013-11-30 12:29:49 -05:00
|
|
|
import java.util.Map;
|
2013-07-06 02:43:59 +01:00
|
|
|
import java.util.Set;
|
2013-11-03 16:54:33 -05:00
|
|
|
|
2013-08-18 16:51:47 -04:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2013-08-22 03:34:36 +01:00
|
|
|
import net.minecraft.world.chunk.Chunk;
|
|
|
|
import net.minecraft.world.chunk.IChunkProvider;
|
2013-08-18 16:51:47 -04:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.ForgeSubscribe;
|
|
|
|
import net.minecraftforge.event.world.ChunkEvent;
|
2013-11-26 20:11:26 -05:00
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
2013-07-06 02:43:59 +01: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 01:18:43 +01:00
|
|
|
public class TransmitterNetworkRegistry implements ITickHandler
|
2013-07-06 02:43:59 +01:00
|
|
|
{
|
2013-08-04 01:18:43 +01:00
|
|
|
private static TransmitterNetworkRegistry INSTANCE = new TransmitterNetworkRegistry();
|
2013-08-18 16:51:47 -04:00
|
|
|
private static boolean loaderRegistered = false;
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-04 01:18:43 +01:00
|
|
|
private HashSet<ITransmitterNetwork> networks = new HashSet<ITransmitterNetwork>();
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-04 01:18:43 +01:00
|
|
|
public TransmitterNetworkRegistry()
|
2013-07-06 02:43:59 +01:00
|
|
|
{
|
|
|
|
TickRegistry.registerTickHandler(this, Side.SERVER);
|
|
|
|
}
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-18 16:51:47 -04:00
|
|
|
public static void initiate()
|
|
|
|
{
|
|
|
|
if(!loaderRegistered)
|
|
|
|
{
|
|
|
|
loaderRegistered = true;
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-22 03:34:36 +01:00
|
|
|
MinecraftForge.EVENT_BUS.register(new NetworkLoader());
|
2013-08-18 16:51:47 -04:00
|
|
|
}
|
|
|
|
}
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-04 01:18:43 +01:00
|
|
|
public static TransmitterNetworkRegistry getInstance()
|
2013-07-06 02:43:59 +01:00
|
|
|
{
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-04 01:18:43 +01:00
|
|
|
public void registerNetwork(ITransmitterNetwork network)
|
2013-07-06 02:43:59 +01:00
|
|
|
{
|
|
|
|
networks.add(network);
|
|
|
|
}
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-04 01:18:43 +01:00
|
|
|
public void removeNetwork(ITransmitterNetwork network)
|
2013-07-06 02:43:59 +01:00
|
|
|
{
|
2013-07-07 13:07:40 -04:00
|
|
|
if(networks.contains(network))
|
2013-07-06 02:43:59 +01:00
|
|
|
{
|
|
|
|
networks.remove(network);
|
|
|
|
}
|
|
|
|
}
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-07-31 16:44:53 -04:00
|
|
|
public void pruneEmptyNetworks()
|
|
|
|
{
|
2013-09-12 17:28:32 -04:00
|
|
|
HashSet<ITransmitterNetwork> copySet = new HashSet<ITransmitterNetwork>(networks);
|
|
|
|
|
|
|
|
for(ITransmitterNetwork e : copySet)
|
2013-07-28 03:32:41 +01:00
|
|
|
{
|
2013-08-04 01:18:43 +01:00
|
|
|
if(e.getSize() == 0)
|
2013-07-28 03:32:41 +01:00
|
|
|
{
|
|
|
|
removeNetwork(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-07-06 02:43:59 +01:00
|
|
|
@Override
|
2013-10-20 00:04:11 -04:00
|
|
|
public void tickStart(EnumSet<TickType> type, Object... tickData) {}
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-07-06 02:43:59 +01:00
|
|
|
@Override
|
|
|
|
public void tickEnd(EnumSet<TickType> type, Object... tickData)
|
|
|
|
{
|
2013-08-06 01:40:41 -04:00
|
|
|
Set<ITransmitterNetwork> iterNetworks = (Set<ITransmitterNetwork>)networks.clone();
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-04 01:18:43 +01:00
|
|
|
for(ITransmitterNetwork net : iterNetworks)
|
2013-07-06 02:43:59 +01:00
|
|
|
{
|
2013-07-28 03:32:41 +01:00
|
|
|
if(networks.contains(net))
|
|
|
|
{
|
|
|
|
net.tick();
|
|
|
|
}
|
2013-07-06 02:43:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumSet<TickType> ticks()
|
|
|
|
{
|
|
|
|
return EnumSet.of(TickType.SERVER);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getLabel()
|
|
|
|
{
|
2013-08-16 11:56:42 -04:00
|
|
|
return "MekanismNetworks";
|
2013-07-06 02:43:59 +01:00
|
|
|
}
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-07-07 13:07:40 -04:00
|
|
|
@Override
|
2013-09-09 02:04:56 -04:00
|
|
|
public String toString()
|
2013-07-07 13:07:40 -04:00
|
|
|
{
|
2013-08-04 01:18:43 +01:00
|
|
|
return "Network Registry:\n" + networks;
|
2013-07-06 02:43:59 +01:00
|
|
|
}
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-10-03 00:10:20 +01:00
|
|
|
public String[] toStrings()
|
|
|
|
{
|
|
|
|
String[] strings = new String[networks.size()];
|
|
|
|
int i = 0;
|
2013-12-06 07:57:26 -05:00
|
|
|
|
|
|
|
for(ITransmitterNetwork<?, ?> network : networks)
|
2013-10-03 00:10:20 +01:00
|
|
|
{
|
|
|
|
strings[i] = network.toString();
|
|
|
|
++i;
|
|
|
|
}
|
2013-12-06 07:57:26 -05:00
|
|
|
|
2013-10-03 00:10:20 +01:00
|
|
|
return strings;
|
|
|
|
}
|
|
|
|
|
2013-08-18 16:51:47 -04:00
|
|
|
public static class NetworkLoader
|
|
|
|
{
|
|
|
|
@ForgeSubscribe
|
|
|
|
public void onChunkLoad(ChunkEvent.Load event)
|
|
|
|
{
|
2013-11-26 20:11:26 -05:00
|
|
|
if(event.getChunk() != null && !event.world.isRemote)
|
2013-08-18 16:51:47 -04:00
|
|
|
{
|
2013-08-22 03:34:36 +01:00
|
|
|
int x = event.getChunk().xPosition;
|
|
|
|
int z = event.getChunk().zPosition;
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-22 03:34:36 +01:00
|
|
|
IChunkProvider cProvider = event.getChunk().worldObj.getChunkProvider();
|
|
|
|
Chunk[] neighbors = new Chunk[5];
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-22 03:34:36 +01:00
|
|
|
neighbors[0] = event.getChunk();
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-22 03:34:36 +01:00
|
|
|
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);
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-08-22 03:34:36 +01:00
|
|
|
for(Chunk c : neighbors)
|
|
|
|
{
|
|
|
|
refreshChunk(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-09 02:04:56 -04:00
|
|
|
|
2013-10-20 00:04:11 -04:00
|
|
|
public synchronized void refreshChunk(Chunk c)
|
2013-08-22 03:34:36 +01:00
|
|
|
{
|
2013-11-01 19:14:02 -04:00
|
|
|
try {
|
|
|
|
if(c != null)
|
|
|
|
{
|
2013-11-30 12:29:49 -05:00
|
|
|
Map copy = (Map)((HashMap)c.chunkTileEntityMap).clone();
|
|
|
|
|
|
|
|
for(Iterator iter = c.chunkTileEntityMap.values().iterator(); iter.hasNext();)
|
|
|
|
{
|
|
|
|
Object obj = iter.next();
|
|
|
|
|
2013-12-20 19:12:33 -05:00
|
|
|
if(obj instanceof IGridTransmitter)
|
2013-11-30 12:29:49 -05:00
|
|
|
{
|
2013-12-20 19:12:33 -05:00
|
|
|
((IGridTransmitter)obj).refreshTransmitterNetwork();
|
|
|
|
((IGridTransmitter)obj).chunkLoad();
|
2013-11-30 12:29:49 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-01 19:14:02 -04:00
|
|
|
}
|
|
|
|
} catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-08-18 16:51:47 -04:00
|
|
|
}
|
|
|
|
}
|
2013-07-06 02:43:59 +01:00
|
|
|
}
|