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.CompoundNBT;
2019-09-23 09:51:22 -07:00
import net.minecraft.tileentity.ITickableTileEntity;
/**
* Base class for tiles that tick. Allows disabling the ticking programmatically.
*/
// TODO - Move implementations that depend on existed ticks to new methods from here.
2019-09-23 09:51:22 -07:00
public abstract class TileTicking extends TileBase implements ITickableTileEntity {
private int ticksExisted;
private boolean shouldTick = true;
@Override
2019-09-23 09:51:22 -07:00
public final void tick() {
if (shouldTick()) {
ticksExisted++;
onUpdate();
}
}
@Override
void deserializeBase(CompoundNBT tagCompound) {
2019-09-23 09:51:22 -07:00
this.ticksExisted = tagCompound.getInt("ticksExisted");
this.shouldTick = tagCompound.getBoolean("shouldTick");
}
@Override
CompoundNBT serializeBase(CompoundNBT tagCompound) {
2019-09-23 09:51:22 -07:00
tagCompound.putInt("ticksExisted", getTicksExisted());
tagCompound.putBoolean("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;
}
}