You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.5 KiB
Java

package net.brokenmoon.redcontrol.blockentities;
import com.simon816.j65el02.device.RedBus;
import net.brokenmoon.redcontrol.RedControl;
import net.brokenmoon.redcontrol.api.*;
import net.brokenmoon.redcontrol.blocks.NetworkCarrier;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
public class CpuEntity extends Peripheral implements CpuControl {
public int i = 0;
public boolean notTicked = true;
public Emulator core = new Emulator(new RedBus());
private int defaultMonitorId = 1;
private int defaultDriveId = 2;
private CpuState state = CpuState.STOPPED;
@Override
public CpuState getCpuState() {return state;}
private int resetTimer = 40;
public CpuEntity(BlockPos pos, BlockState state) {
super(RedControl.CPU_BLOCK_ENTITY, pos, state, 0);
}
public static void tick(World world, BlockPos pos, BlockState state, CpuEntity be) {
if(!world.isClient) {
if(be.worldBus == null || !be.worldBus.getValid()){
((NetworkCarrier)state.getBlock()).generateBus((ServerWorld) world, pos);
}
if (be.notTicked) {
be.notTicked = false;
}
if(be.state == CpuState.RUNNING) {
for (int i = 0; i < 500; i++)
be.step();
} else{
if (be.state == CpuState.RESET && be.resetTimer > 0){
be.resetTimer--;
} else if(be.state == CpuState.RESET) {
be.core = new Emulator(be.getBus().getRedBus());
}
}
if (be.core.isWaitingOnInterrupt()) {
be.core.setWaitingOnInterrupt(false);
}
}
}
public int getDefaultDriveId() {
return this.defaultDriveId;
}
public void setDefaultDriveId(int id) {
this.defaultDriveId = id;
}
public int getDefaultMonitorId() {
return this.defaultMonitorId;
}
public void setDefaultMonitorId(int id) {
this.defaultMonitorId = id;
}
public void reset() {
this.core.reset(this.defaultDriveId, this.defaultMonitorId);
state = CpuState.RESET;
resetTimer = 40;
markDirty();
world.updateListeners(pos, this.getCachedState(), this.getCachedState(), Block.NOTIFY_LISTENERS);
}
public void stop() {
state = CpuState.STOPPED;
markDirty();
world.updateListeners(pos, this.getCachedState(), this.getCachedState(), Block.NOTIFY_LISTENERS);
}
public void start() {
state = CpuState.RUNNING;
markDirty();
world.updateListeners(pos, this.getCachedState(), this.getCachedState(), Block.NOTIFY_LISTENERS);
}
public void step() {
i++;
core.step();
}
@Override
public void write(int address, int data) {
}
@Override
public int read(int address) {
return 0;
}
@Override
public void update() {}
@Override
public void setBus(RCWorldBus bus){
this.bus = bus.getRedBus();
this.core.setBus(this.bus);
this.bus.setPeripheral(id, this);
this.worldBus = bus;
}
@Nullable
@Override
public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}
@Override
public NbtCompound toInitialChunkDataNbt() {
return createNbt();
}
@Override
protected void writeNbt(NbtCompound nbt) {
nbt.putString("state", this.state.toString());
nbt.putInt("resetTimer", this.resetTimer);
nbt.putInt("defaultMonitorId", this.defaultMonitorId);
nbt.putInt("defaultDriveId", this.defaultDriveId);
core.writeNbt(nbt);
super.writeNbt(nbt);
}
@Override
public void readNbt(NbtCompound nbt) {
this.state = CpuState.valueOf(nbt.getString("state"));
this.resetTimer = nbt.getInt("resetTimer");
this.defaultMonitorId = nbt.getInt("defaultMonitorId");
this.defaultDriveId = nbt.getInt("defaultDriveId");
core.readNbt(nbt);
super.readNbt(nbt);
}
}