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.
166 lines
4.7 KiB
Java
166 lines
4.7 KiB
Java
package net.brokenmoon.redcontrol.blockentities;
|
|
|
|
import com.simon816.j65el02.device.RedBus;
|
|
import net.brokenmoon.redcontrol.RedControl;
|
|
import net.brokenmoon.redcontrol.api.Emulator;
|
|
import net.brokenmoon.redcontrol.api.RCWorldBus;
|
|
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.util.math.BlockPos;
|
|
import net.minecraft.world.World;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public class CpuEntity extends Peripheral{
|
|
public int i = 0;
|
|
|
|
public boolean notTicked = true;
|
|
public Emulator core = new Emulator(new RedBus());
|
|
|
|
private int defaultMonitorId = 1;
|
|
private int defaultDriveId = 2;
|
|
|
|
public boolean isRunning = false;
|
|
public boolean isResetting = false;
|
|
public boolean isReset = false;
|
|
private int resetTimer = 5;
|
|
|
|
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(be.worldBus == null || !be.worldBus.getValid()){
|
|
((NetworkCarrier)state.getBlock()).generateBus(world, pos);
|
|
}
|
|
if(!world.isClient) {
|
|
if (be.notTicked) {
|
|
be.notTicked = false;
|
|
}
|
|
if(be.isRunning) {
|
|
for (int i = 0; i < 1000; i++)
|
|
be.step();
|
|
} else{
|
|
if (be.isResetting && !be.isReset && be.resetTimer > 0){
|
|
be.resetTimer--;
|
|
} else {
|
|
be.core = new Emulator(be.getBus().getRedBus());
|
|
}
|
|
}
|
|
if (be.core.isWaitingOnInterrupt()) {
|
|
be.core.setWaitingOnInterrupt();
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
isRunning = false;
|
|
isResetting = true;
|
|
isReset = false;
|
|
resetTimer = 5;
|
|
markDirty();
|
|
world.updateListeners(pos, this.getCachedState(), this.getCachedState(), Block.NOTIFY_LISTENERS);
|
|
}
|
|
|
|
public void stop() {
|
|
isRunning = false;
|
|
isResetting = false;
|
|
isReset = false;
|
|
markDirty();
|
|
world.updateListeners(pos, this.getCachedState(), this.getCachedState(), Block.NOTIFY_LISTENERS);
|
|
}
|
|
|
|
public void start() {
|
|
isRunning = true;
|
|
isResetting = false;
|
|
isReset = false;
|
|
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.putBoolean("isRunning", this.isRunning);
|
|
nbt.putBoolean("isResetting", this.isResetting);
|
|
nbt.putBoolean("isReset", this.isReset);
|
|
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.isRunning = nbt.getBoolean("isRunning");
|
|
this.isResetting = nbt.getBoolean("isResetting");
|
|
this.isReset = nbt.getBoolean("isReset");
|
|
this.resetTimer = nbt.getInt("resetTimer");
|
|
this.defaultMonitorId = nbt.getInt("defaultMonitorId");
|
|
this.defaultDriveId = nbt.getInt("defaultDriveId");
|
|
core.readNbt(nbt);
|
|
super.readNbt(nbt);
|
|
}
|
|
|
|
}
|