diff --git a/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNode.java b/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNode.java index b818940a2..4961d0111 100644 --- a/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNode.java +++ b/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNode.java @@ -7,6 +7,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; +import resonantinduction.core.ResonantInduction; import resonantinduction.core.grid.Grid; import resonantinduction.core.grid.TickingGrid; import universalelectricity.api.vector.Vector3; @@ -68,7 +69,8 @@ public class MechanicalNode extends EnergyNode { prevAngularVelocity = angularVelocity; - angle += angularVelocity * deltaTime; + if (!ResonantInduction.proxy.isPaused()) + angle += angularVelocity * deltaTime; if (angle % (Math.PI * 2) != angle) { @@ -127,7 +129,7 @@ public class MechanicalNode extends EnergyNode /** * Set all current rotations */ - //adjacentMech.angle = Math.abs(angle) * (adjacentMech.angle >= 0 ? 1 : -1); + // adjacentMech.angle = Math.abs(angle) * (adjacentMech.angle >= 0 ? 1 : -1); } } } diff --git a/src/main/java/resonantinduction/core/CommonProxy.java b/src/main/java/resonantinduction/core/CommonProxy.java index 348ad8592..9b2eef6f5 100644 --- a/src/main/java/resonantinduction/core/CommonProxy.java +++ b/src/main/java/resonantinduction/core/CommonProxy.java @@ -14,12 +14,6 @@ import calclavia.lib.prefab.ProxyBase; */ public class CommonProxy extends ProxyBase { - @Override - public void postInit() - { - ThreadedGridTicker.INSTANCE.start(); - } - public boolean isPaused() { return false; diff --git a/src/main/java/resonantinduction/core/ResonantInduction.java b/src/main/java/resonantinduction/core/ResonantInduction.java index 821c6b292..db9aac1ee 100644 --- a/src/main/java/resonantinduction/core/ResonantInduction.java +++ b/src/main/java/resonantinduction/core/ResonantInduction.java @@ -11,6 +11,7 @@ import net.minecraftforge.fluids.BlockFluidFinite; import org.modstats.ModstatInfo; import org.modstats.Modstats; +import resonantinduction.core.grid.ThreadedGridTicker; import resonantinduction.core.handler.TextureHookHandler; import resonantinduction.core.prefab.part.PacketMultiPart; import resonantinduction.core.resource.BlockDust; @@ -35,6 +36,8 @@ import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.event.FMLServerStartingEvent; +import cpw.mods.fml.common.event.FMLServerStoppingEvent; import cpw.mods.fml.common.network.NetworkMod; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.GameRegistry; @@ -139,4 +142,18 @@ public class ResonantInduction ResourceGenerator.generateOreResources(); proxy.postInit(); } + + @EventHandler + public void serverStarting(FMLServerStartingEvent event) + { + if (!ThreadedGridTicker.INSTANCE.isAlive()) + ThreadedGridTicker.INSTANCE.start(); + ThreadedGridTicker.INSTANCE.pause = false; + } + + @EventHandler + public void onServerStopping(FMLServerStoppingEvent evt) + { + ThreadedGridTicker.INSTANCE.pause = true; + } } diff --git a/src/main/java/resonantinduction/core/grid/ThreadedGridTicker.java b/src/main/java/resonantinduction/core/grid/ThreadedGridTicker.java index 4aa3c0c15..0589f33d8 100644 --- a/src/main/java/resonantinduction/core/grid/ThreadedGridTicker.java +++ b/src/main/java/resonantinduction/core/grid/ThreadedGridTicker.java @@ -25,7 +25,9 @@ public class ThreadedGridTicker extends Thread /** For queuing Forge events to be invoked the next tick. */ private final Queue queuedEvents = new ConcurrentLinkedQueue(); - private ThreadedGridTicker() + public boolean pause = false; + + public ThreadedGridTicker() { setName("Universal Electricity"); setPriority(MIN_PRIORITY); @@ -56,49 +58,52 @@ public class ThreadedGridTicker extends Thread while (true) { - long current = System.currentTimeMillis(); - long delta = current - last; - - /** Tick all updaters. */ - synchronized (updaters) + if (!pause) { - Set removeUpdaters = Collections.newSetFromMap(new WeakHashMap()); + long current = System.currentTimeMillis(); + long delta = current - last; - Iterator updaterIt = new HashSet(updaters).iterator(); - - try + /** Tick all updaters. */ + synchronized (updaters) { - while (updaterIt.hasNext()) + Set removeUpdaters = Collections.newSetFromMap(new WeakHashMap()); + + Iterator updaterIt = new HashSet(updaters).iterator(); + + try { - IUpdate updater = updaterIt.next(); - - if (updater.canUpdate()) + while (updaterIt.hasNext()) { - updater.update(); + IUpdate updater = updaterIt.next(); + + if (updater.canUpdate()) + { + updater.update(); + } + + if (!updater.continueUpdate()) + { + removeUpdaters.add(updater); + } } - if (!updater.continueUpdate()) - { - removeUpdaters.add(updater); - } + updaters.removeAll(removeUpdaters); + } + catch (Exception e) + { + System.out.println("Universal Electricity Threaded Ticker: Failed while tcking updater. This is a bug! Clearing all tickers for self repair."); + updaters.clear(); + e.printStackTrace(); } - - updaters.removeAll(removeUpdaters); } - catch (Exception e) - { - System.out.println("Universal Electricity Threaded Ticker: Failed while tcking updater. This is a bug! Clearing all tickers for self repair."); - updaters.clear(); - e.printStackTrace(); - } - } - /** Perform all queued events */ - synchronized (queuedEvents) - { - while (!queuedEvents.isEmpty()) + /** Perform all queued events */ + synchronized (queuedEvents) { - MinecraftForge.EVENT_BUS.post(queuedEvents.poll()); + while (!queuedEvents.isEmpty()) + { + MinecraftForge.EVENT_BUS.post(queuedEvents.poll()); + } } }