mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-26 23:03:18 +00:00
112 lines
3.4 KiB
Java
112 lines
3.4 KiB
Java
/*******************************************************************************
|
|
* Copyright (c) 2011-2014 SirSengir.
|
|
* All rights reserved. This program and the accompanying materials
|
|
* are made available under the terms of the GNU Lesser Public License v3
|
|
* which accompanies this distribution, and is available at
|
|
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
*
|
|
* Various Contributors including, but not limited to:
|
|
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
|
******************************************************************************/
|
|
package forestry.farming.tiles;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import forestry.api.multiblock.IFarmComponent;
|
|
import forestry.api.multiblock.IFarmController;
|
|
import forestry.energy.EnergyHelper;
|
|
import forestry.energy.EnergyManager;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.util.EnumFacing;
|
|
import net.minecraftforge.common.capabilities.Capability;
|
|
|
|
public class TileFarmGearbox extends TileFarm implements IFarmComponent.Active {
|
|
|
|
private static final int WORK_CYCLES = 4;
|
|
private static final int ENERGY_PER_OPERATION = WORK_CYCLES * 50;
|
|
|
|
private final EnergyManager energyManager;
|
|
|
|
private int activationDelay = 0;
|
|
private int previousDelays = 0;
|
|
private int workCounter;
|
|
|
|
public TileFarmGearbox() {
|
|
energyManager = new EnergyManager(200, 10000);
|
|
}
|
|
|
|
/* SAVING & LOADING */
|
|
@Override
|
|
public void readFromNBT(NBTTagCompound nbttagcompound) {
|
|
super.readFromNBT(nbttagcompound);
|
|
energyManager.readFromNBT(nbttagcompound);
|
|
|
|
activationDelay = nbttagcompound.getInteger("ActivationDelay");
|
|
previousDelays = nbttagcompound.getInteger("PrevDelays");
|
|
}
|
|
|
|
|
|
@Override
|
|
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) {
|
|
nbttagcompound = super.writeToNBT(nbttagcompound);
|
|
energyManager.writeToNBT(nbttagcompound);
|
|
|
|
nbttagcompound.setInteger("ActivationDelay", activationDelay);
|
|
nbttagcompound.setInteger("PrevDelays", previousDelays);
|
|
return nbttagcompound;
|
|
}
|
|
|
|
@Override
|
|
public void updateServer(int tickCount) {
|
|
if (energyManager.getEnergyStored() <= 0) {
|
|
return;
|
|
}
|
|
|
|
if (activationDelay > 0) {
|
|
activationDelay--;
|
|
return;
|
|
}
|
|
|
|
// Hard limit to 4 cycles / second.
|
|
if (workCounter < WORK_CYCLES && EnergyHelper.consumeEnergyToDoWork(energyManager, WORK_CYCLES, ENERGY_PER_OPERATION)) {
|
|
workCounter++;
|
|
}
|
|
|
|
if (workCounter >= WORK_CYCLES && tickCount % 5 == 0) {
|
|
IFarmController farmController = getMultiblockLogic().getController();
|
|
if (farmController.doWork()) {
|
|
workCounter = 0;
|
|
previousDelays = 0;
|
|
} else {
|
|
// If the central TE doesn't have work, we add to the activation delay to throttle the CPU usage.
|
|
activationDelay = 10 * previousDelays < 120 ? 10 * previousDelays : 120;
|
|
previousDelays++; // First delay is free!
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void updateClient(int tickCount) {
|
|
|
|
}
|
|
|
|
public EnergyManager getEnergyManager() {
|
|
return energyManager;
|
|
}
|
|
|
|
@Override
|
|
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
|
|
return energyManager.hasCapability(capability) || super.hasCapability(capability, facing);
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
|
|
T energyCapability = energyManager.getCapability(capability);
|
|
if (energyCapability != null) {
|
|
return energyCapability;
|
|
}
|
|
return super.getCapability(capability, facing);
|
|
}
|
|
}
|