BloodMagic/src/main/java/WayofTime/bloodmagic/tile/base/TileTicking.java

56 lines
1.4 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.tile.base;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ITickable;
/**
* Base class for tiles that tick. Allows disabling the ticking programmatically.
*/
// TODO - Move implementations that depend on existed ticks to new methods from here.
2017-08-15 21:30:48 -07:00
public abstract class TileTicking extends TileBase implements ITickable {
private int ticksExisted;
private boolean shouldTick = true;
@Override
2017-08-15 21:30:48 -07:00
public final void update() {
if (shouldTick()) {
ticksExisted++;
onUpdate();
}
}
@Override
2017-08-15 21:30:48 -07:00
void deserializeBase(NBTTagCompound tagCompound) {
this.ticksExisted = tagCompound.getInteger("ticksExisted");
this.shouldTick = tagCompound.getBoolean("shouldTick");
}
@Override
2017-08-15 21:30:48 -07:00
NBTTagCompound serializeBase(NBTTagCompound tagCompound) {
tagCompound.setInteger("ticksExisted", getTicksExisted());
tagCompound.setBoolean("shouldTick", shouldTick());
return tagCompound;
}
/**
* Called every tick that {@link #shouldTick()} is true.
*/
public abstract void onUpdate();
2017-08-15 21:30:48 -07:00
public int getTicksExisted() {
return ticksExisted;
}
2017-08-15 21:30:48 -07:00
public void resetLifetime() {
ticksExisted = 0;
}
2017-08-15 21:30:48 -07:00
public boolean shouldTick() {
return shouldTick;
}
2017-08-15 21:30:48 -07:00
public void setShouldTick(boolean shouldTick) {
this.shouldTick = shouldTick;
}
}