From 9472dea734d88666c17cdef59a754f459b5b774e Mon Sep 17 00:00:00 2001 From: CovertJaguar Date: Tue, 6 Aug 2013 22:48:07 -0700 Subject: [PATCH] Revert "An attempt to improve PowerHandler cpu usage" This reverts commit e85d99b04729ea5351924dd9cea6f64faee47850. --- common/buildcraft/api/core/TickLimiter.java | 32 ------------------- common/buildcraft/api/power/PowerHandler.java | 27 ++++++++-------- .../core/TickHandlerTimeTracker.java | 5 +-- 3 files changed, 14 insertions(+), 50 deletions(-) delete mode 100644 common/buildcraft/api/core/TickLimiter.java diff --git a/common/buildcraft/api/core/TickLimiter.java b/common/buildcraft/api/core/TickLimiter.java deleted file mode 100644 index 41436183..00000000 --- a/common/buildcraft/api/core/TickLimiter.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) SpaceToad, 2011-2012 - * http://www.mod-buildcraft.com - * - * BuildCraft is distributed under the terms of the Minecraft Mod Public - * License 1.0, or MMPL. Please check the contents of the license located in - * http://www.mod-buildcraft.com/MMPL-1.0.txt - */ -package buildcraft.api.core; - -/** - * - * @author CovertJaguar - */ -public class TickLimiter { - - public static long worldTime; - private long lastTick = worldTime; - private long timeSinceLastTick = -1; - - public boolean canTick() { - if (lastTick == worldTime) - return false; - timeSinceLastTick = worldTime - lastTick; - lastTick = worldTime; - return true; - } - - public long timeSinceLastTick() { - return timeSinceLastTick > 0 ? timeSinceLastTick : 0; - } -} diff --git a/common/buildcraft/api/power/PowerHandler.java b/common/buildcraft/api/power/PowerHandler.java index 2e1ed194..763a0fe4 100644 --- a/common/buildcraft/api/power/PowerHandler.java +++ b/common/buildcraft/api/power/PowerHandler.java @@ -7,7 +7,7 @@ */ package buildcraft.api.power; -import buildcraft.api.core.TickLimiter; +import buildcraft.api.core.SafeTimeTracker; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.ForgeDirection; @@ -77,9 +77,9 @@ public final class PowerHandler { private float maxEnergyStored; private float activationEnergy; private float energyStored = 0; - private final TickLimiter doWorkLimiter = new TickLimiter(); - private final TickLimiter sourcesLimiter = new TickLimiter(); - private final TickLimiter perditionLimiter = new TickLimiter(); + private final SafeTimeTracker doWorkTracker = new SafeTimeTracker(); + private final SafeTimeTracker sourcesTracker = new SafeTimeTracker(); + private final SafeTimeTracker perditionTracker = new SafeTimeTracker(); public final int[] powerSources = new int[6]; public final IPowerReceptor receptor; private PerditionCalculator perdition; @@ -180,33 +180,32 @@ public final class PowerHandler { * design around this though if you are aware of the limitations. */ public void update() { - if (receptor.getWorld().isRemote) - return; applyPerdition(); applyWork(); validateEnergy(); } private void applyPerdition() { - if (energyStored > 0 && perditionLimiter.canTick()) { - float newEnergy = perdition.applyPerdition(this, energyStored, perditionLimiter.timeSinceLastTick()); - if (newEnergy < energyStored || newEnergy == 0) + if (perditionTracker.markTimeIfDelay(1) && energyStored > 0) { + float newEnergy = perdition.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay()); + if (newEnergy == 0 || newEnergy < energyStored) energyStored = newEnergy; else - energyStored = DEFUALT_PERDITION.applyPerdition(this, energyStored, perditionLimiter.timeSinceLastTick()); + energyStored = DEFUALT_PERDITION.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay()); validateEnergy(); } } private void applyWork() { - if (energyStored >= activationEnergy && doWorkLimiter.canTick()) - receptor.doWork(this); + if (energyStored >= activationEnergy) + if (doWorkTracker.markTimeIfDelay(1)) + receptor.doWork(this); } private void updateSources(ForgeDirection source) { - if (sourcesLimiter.canTick()) + if (sourcesTracker.markTimeIfDelay(1)) for (int i = 0; i < 6; ++i) { - powerSources[i] -= sourcesLimiter.timeSinceLastTick(); + powerSources[i] -= sourcesTracker.durationOfLastDelay(); if (powerSources[i] < 0) powerSources[i] = 0; } diff --git a/common/buildcraft/core/TickHandlerTimeTracker.java b/common/buildcraft/core/TickHandlerTimeTracker.java index 5410d0d0..6dac28ce 100644 --- a/common/buildcraft/core/TickHandlerTimeTracker.java +++ b/common/buildcraft/core/TickHandlerTimeTracker.java @@ -1,7 +1,6 @@ package buildcraft.core; import buildcraft.api.core.SafeTimeTracker; -import buildcraft.api.core.TickLimiter; import cpw.mods.fml.common.ITickHandler; import cpw.mods.fml.common.TickType; import java.util.EnumSet; @@ -11,9 +10,7 @@ public class TickHandlerTimeTracker implements ITickHandler { @Override public void tickStart(EnumSet type, Object... tickData) { - long worldTime = ((World) tickData[0]).getWorldTime(); - SafeTimeTracker.worldTime = worldTime; - TickLimiter.worldTime = worldTime; + SafeTimeTracker.worldTime = ((World) tickData[0]).getWorldTime(); } @Override