package mcjty.rftools.blocks.logic;
import mcjty.lib.entity.GenericTileEntity;
import mcjty.lib.network.Argument;
import mcjty.lib.varia.BlockTools;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import java.util.Map;
public class TimerTileEntity extends GenericTileEntity {
public static final String CMD_SETDELAY = "setDelay";
// For pulse detection.
private boolean prevIn = false;
private int delay = 1;
private int timer = 0;
private boolean redstoneOut = false;
public TimerTileEntity() {
}
public int getDelay() {
return delay;
}
public void setDelay(int delay) {
this.delay = delay;
timer = delay;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
@Override
protected void checkStateServer() {
super.checkStateServer();
int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
boolean newvalue = BlockTools.getRedstoneSignalIn(meta);
boolean pulse = newvalue && !prevIn;
prevIn = newvalue;
markDirty();
if (pulse) {
timer = delay;
}
boolean newout;
timer--;
if (timer <= 0) {
timer = delay;
newout = true;
} else {
newout = false;
}
if (newout != redstoneOut) {
redstoneOut = newout;
notifyBlockUpdate();
}
}
@Override
protected int updateMetaData(int meta) {
meta = super.updateMetaData(meta);
return BlockTools.setRedstoneSignalOut(meta, redstoneOut);
}
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);
redstoneOut = tagCompound.getBoolean("rs");
prevIn = tagCompound.getBoolean("prevIn");
timer = tagCompound.getInteger("timer");
}
@Override
public void readRestorableFromNBT(NBTTagCompound tagCompound) {
super.readRestorableFromNBT(tagCompound);
delay = tagCompound.getInteger("delay");
}
@Override
public void writeToNBT(NBTTagCompound tagCompound) {
super.writeToNBT(tagCompound);
tagCompound.setBoolean("rs", redstoneOut);
tagCompound.setBoolean("prevIn", prevIn);
tagCompound.setInteger("timer", timer);
}
@Override
public void writeRestorableToNBT(NBTTagCompound tagCompound) {
super.writeRestorableToNBT(tagCompound);
tagCompound.setInteger("delay", delay);
}
@Override
public boolean execute(EntityPlayerMP playerMP, String command, Map<String, Argument> args) {
boolean rc = super.execute(playerMP, command, args);
if (rc) {
return true;
}
if (CMD_SETDELAY.equals(command)) {
setDelay(args.get("delay").getInteger());
return true;
}
return false;
}
}