Made thread pause during main menu

This commit is contained in:
Calclavia 2014-03-05 22:50:21 +08:00
parent a7c0e8c502
commit 1cef920e62
4 changed files with 59 additions and 41 deletions

View file

@ -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);
}
}
}

View file

@ -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;

View file

@ -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;
}
}

View file

@ -25,7 +25,9 @@ public class ThreadedGridTicker extends Thread
/** For queuing Forge events to be invoked the next tick. */
private final Queue<Event> queuedEvents = new ConcurrentLinkedQueue<Event>();
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<IUpdate> removeUpdaters = Collections.newSetFromMap(new WeakHashMap<IUpdate, Boolean>());
long current = System.currentTimeMillis();
long delta = current - last;
Iterator<IUpdate> updaterIt = new HashSet<IUpdate>(updaters).iterator();
try
/** Tick all updaters. */
synchronized (updaters)
{
while (updaterIt.hasNext())
Set<IUpdate> removeUpdaters = Collections.newSetFromMap(new WeakHashMap<IUpdate, Boolean>());
Iterator<IUpdate> updaterIt = new HashSet<IUpdate>(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());
}
}
}