2016-09-07 17:46:06 -07:00
|
|
|
package WayofTime.bloodmagic.tile.base;
|
|
|
|
|
2019-09-22 12:55:43 -07:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2019-09-23 09:51:22 -07:00
|
|
|
import net.minecraft.tileentity.ITickableTileEntity;
|
2016-09-07 17:46:06 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2016-09-07 17:46:06 -07:00
|
|
|
private int ticksExisted;
|
|
|
|
private boolean shouldTick = true;
|
|
|
|
|
|
|
|
@Override
|
2019-09-23 09:51:22 -07:00
|
|
|
public final void tick() {
|
2016-09-07 17:46:06 -07:00
|
|
|
if (shouldTick()) {
|
|
|
|
ticksExisted++;
|
|
|
|
onUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-09-22 12:55:43 -07:00
|
|
|
void deserializeBase(CompoundNBT tagCompound) {
|
2019-09-23 09:51:22 -07:00
|
|
|
this.ticksExisted = tagCompound.getInt("ticksExisted");
|
2016-09-07 17:46:06 -07:00
|
|
|
this.shouldTick = tagCompound.getBoolean("shouldTick");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-09-22 12:55:43 -07:00
|
|
|
CompoundNBT serializeBase(CompoundNBT tagCompound) {
|
2019-09-23 09:51:22 -07:00
|
|
|
tagCompound.putInt("ticksExisted", getTicksExisted());
|
|
|
|
tagCompound.putBoolean("shouldTick", shouldTick());
|
2016-09-07 17:46:06 -07:00
|
|
|
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() {
|
2016-09-07 17:46:06 -07:00
|
|
|
return ticksExisted;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public void resetLifetime() {
|
2016-09-07 17:46:06 -07:00
|
|
|
ticksExisted = 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean shouldTick() {
|
2016-09-07 17:46:06 -07:00
|
|
|
return shouldTick;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public void setShouldTick(boolean shouldTick) {
|
2016-09-07 17:46:06 -07:00
|
|
|
this.shouldTick = shouldTick;
|
|
|
|
}
|
|
|
|
}
|