From 6183761f68f5f93c8f75da4e3e3ce78ebd678bc7 Mon Sep 17 00:00:00 2001 From: Ben Spiers Date: Sat, 6 Jul 2013 02:43:59 +0100 Subject: [PATCH] Add an EnergyNetworkRegistry to keep track of Energy Networks and act as a singleton tick handler. --- common/mekanism/common/EnergyNetwork.java | 43 +++-------- .../common/EnergyNetworkRegistry.java | 75 +++++++++++++++++++ 2 files changed, 86 insertions(+), 32 deletions(-) create mode 100644 common/mekanism/common/EnergyNetworkRegistry.java diff --git a/common/mekanism/common/EnergyNetwork.java b/common/mekanism/common/EnergyNetwork.java index d2b5dbd4d..945977711 100644 --- a/common/mekanism/common/EnergyNetwork.java +++ b/common/mekanism/common/EnergyNetwork.java @@ -13,11 +13,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -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; - import mekanism.api.IStrictEnergyAcceptor; import mekanism.api.Object3D; import net.minecraft.tileentity.TileEntity; @@ -27,7 +22,7 @@ import net.minecraftforge.event.ForgeSubscribe; import net.minecraftforge.event.world.ChunkEvent; import buildcraft.api.power.IPowerReceptor; -public class EnergyNetwork implements ITickHandler +public class EnergyNetwork { public Set cables = new HashSet(); @@ -40,7 +35,7 @@ public class EnergyNetwork implements ITickHandler public EnergyNetwork(IUniversalCable... varCables) { cables.addAll(Arrays.asList(varCables)); - TickRegistry.registerTickHandler(this, Side.SERVER); + EnergyNetworkRegistry.getInstance().registerNetwork(this); } public double getEnergyNeeded(ArrayList ignored) @@ -200,11 +195,14 @@ public class EnergyNetwork implements ITickHandler public void merge(EnergyNetwork network) { + EnergyNetworkRegistry registry = EnergyNetworkRegistry.getInstance(); if(network != null && network != this) { EnergyNetwork newNetwork = new EnergyNetwork(); newNetwork.cables.addAll(cables); + registry.removeNetwork(this); newNetwork.cables.addAll(network.cables); + registry.removeNetwork(network); newNetwork.refresh(); } } @@ -248,7 +246,6 @@ public class EnergyNetwork implements ITickHandler } } EnergyNetwork newNetwork = new EnergyNetwork(); - if (finder != null) { for(Object3D node : finder.iterated) { TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj); @@ -260,10 +257,11 @@ public class EnergyNetwork implements ITickHandler newNetwork.cables.add((IUniversalCable)nodeTile); } } - }} + } newNetwork.refresh(); } } + EnergyNetworkRegistry.getInstance().removeNetwork(this); } } @@ -346,33 +344,14 @@ public class EnergyNetwork implements ITickHandler return "[EnergyNetwork] " + cables.size() + " cables, " + possibleAcceptors.size() + " acceptors."; } - public double getPower() - { - return joulesTransmitted * 20; - } - - @Override - public void tickStart(EnumSet type, Object... tickData) - { - return; - } - - @Override - public void tickEnd(EnumSet type, Object... tickData) + public void clearJoulesTransmitted() { joulesLastTick = joulesTransmitted; joulesTransmitted = 0; } - - @Override - public EnumSet ticks() + + public double getPower() { - return EnumSet.of(TickType.SERVER); - } - - @Override - public String getLabel() - { - return toString(); + return joulesTransmitted * 20; } } \ No newline at end of file diff --git a/common/mekanism/common/EnergyNetworkRegistry.java b/common/mekanism/common/EnergyNetworkRegistry.java new file mode 100644 index 000000000..3c674428b --- /dev/null +++ b/common/mekanism/common/EnergyNetworkRegistry.java @@ -0,0 +1,75 @@ +package mekanism.common; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import cpw.mods.fml.common.FMLLog; +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; + + +public class EnergyNetworkRegistry implements ITickHandler +{ + + public EnergyNetworkRegistry() + { + TickRegistry.registerTickHandler(this, Side.SERVER); + } + static private EnergyNetworkRegistry INSTANCE = new EnergyNetworkRegistry(); + + static public EnergyNetworkRegistry getInstance() + { + return INSTANCE; + } + + private Set networks = new HashSet(); + + public void registerNetwork(EnergyNetwork network) + { + networks.add(network); + } + + public void removeNetwork(EnergyNetwork network) + { + if (networks.contains(network)) + { + networks.remove(network); + } + } + + @Override + public void tickStart(EnumSet type, Object... tickData) + { + return; + } + + @Override + public void tickEnd(EnumSet type, Object... tickData) + { + for (EnergyNetwork net : networks) + { + net.clearJoulesTransmitted(); + } + } + + @Override + public EnumSet ticks() + { + return EnumSet.of(TickType.SERVER); + } + + @Override + public String getLabel() + { + return "Mekanism Energy Networks"; + } + + public String toString() { + return networks.toString(); + } +}