Almost good, world is null

main
Astoria 7 months ago
parent 520c9fe3c2
commit 30d9aaeffb

@ -1,28 +1,63 @@
package net.brokenmoon.redcontrol.api; package net.brokenmoon.redcontrol.api;
import com.simon816.j65el02.Bus;
import com.simon816.j65el02.device.Device;
import com.simon816.j65el02.device.RedBus; import com.simon816.j65el02.device.RedBus;
import net.brokenmoon.redcontrol.blockentities.Peripheral; import net.brokenmoon.redcontrol.blockentities.Peripheral;
import net.brokenmoon.redcontrol.blocks.NetworkCarrier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.Arrays; public class RCBus {
public class RCBus extends Bus { boolean isValid = false;
boolean isValid = true; private RedBus redBus;
Peripheral[] peripherals = new Peripheral[256]; Peripheral[] peripherals = new Peripheral[256];
public RCBus(RedBus redBus) { public RCBus(RedBus redBus) {
super(redBus); this.redBus = redBus;
} }
public void write(int address, int data) { public void write(int address, int data, World world, BlockPos pos) {
if(isValid) {
Peripheral peripheral = peripherals[address]; Peripheral peripheral = peripherals[address];
peripheral.getBus().write(address, data); peripheral.getBus().getRedBus().write(address, data);
} else {
generateBus(world, pos);
write(address, data, world, pos);
}
} }
public int read(int address, boolean cpuAccess) { public int read(int address, boolean cpuAccess, World world, BlockPos pos) {
if(isValid) {
Peripheral peripheral = peripherals[address]; Peripheral peripheral = peripherals[address];
return peripheral.getBus().getRedBus().read(address, cpuAccess) & 0xff; return peripheral.getBus().getRedBus().read(address, cpuAccess) & 0xff;
} else {
generateBus(world, pos);
return read(address, cpuAccess, world, pos);
}
}
public void generateBus(World world, BlockPos pos){
((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBus(world, pos);
}
public RedBus getRedBus() {
return redBus;
}
public void setRedBus(RedBus redBus) {
this.redBus = redBus;
}
public void update() {
this.redBus.updatePeripheral();
}
public Peripheral[] getPeripherals(){
return peripherals;
}
public void setPeripherals(Peripheral[] peripherals){
this.peripherals = peripherals;
} }
} }

@ -0,0 +1,20 @@
package net.brokenmoon.redcontrol.api;
import com.simon816.j65el02.Cpu;
public class RCCpu extends Cpu {
private RCBus rcbus;
public RCCpu() {
}
public RCBus getRCBus() {
return rcbus;
}
public void setRCBus(RCBus rcbus) {
this.rcbus = rcbus;
}
}

@ -5,6 +5,8 @@ import com.simon816.j65el02.Cpu;
import com.simon816.j65el02.device.*; import com.simon816.j65el02.device.*;
import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.RedControl;
import net.brokenmoon.redcontrol.api.RCBus; import net.brokenmoon.redcontrol.api.RCBus;
import net.brokenmoon.redcontrol.api.RCCpu;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -32,7 +34,8 @@ public class CpuEntity extends Peripheral implements Runnable{
private boolean running = false; private boolean running = false;
private Semaphore waiInterrupt = new Semaphore(2); private Semaphore waiInterrupt = new Semaphore(2);
private final Cpu cpu; private final RCCpu cpu;
private RCBus rcbus;
private Path bootloader; private Path bootloader;
{ {
@ -42,25 +45,31 @@ public class CpuEntity extends Peripheral implements Runnable{
private int defaultMonitorId = 1; private int defaultMonitorId = 1;
private int defaultDriveId = 2; private int defaultDriveId = 2;
public CpuEntity(BlockPos pos, BlockState state) { public CpuEntity(BlockPos pos, BlockState state) {
super(RedControl.CPU_BLOCK_ENTITY, pos, state); super(RedControl.CPU_BLOCK_ENTITY, pos, state, 0);
RedControl.LOGGER.info("Making CpuEntity"); RedControl.LOGGER.info("Making CpuEntity");
this.cpu = new Cpu(); this.cpu = new RCCpu();
this.cpu.setBus(this.getBus()); this.setRCBus(new RCBus(new RedBus()));
this.cpu.setRCBus(this.getRCBus());
this.cpu.setBus(new Bus(this.getRCBus().getRedBus()));
Memory ram = new Memory(0x0000, 0x2000); Memory ram = new Memory(0x0000, 0x2000);
try { try {
ram.loadFromFile(bootloader, 0x400, 0x100); ram.loadFromFile(bootloader, 0x400, 0x100);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
this.getBus().addDevice(ram); cpu.getBus().addDevice(ram);
reset(); reset();
} }
public RCBus getBus() { public RCBus getRCBus(){
return this.getBus(); return this.rcbus;
}
public void setRCBus(RCBus bus){
this.rcbus = bus;
} }
public void setPeripheral(int id, RedBus.Peripheral peripheral) { public void setPeripheral(int id, RedBus.Peripheral peripheral) {
this.getBus().getRedBus().setPeripheral(id, peripheral); this.getBus().getRedBus().setPeripheral(id, peripheral);
} }
@ -110,14 +119,14 @@ public class CpuEntity extends Peripheral implements Runnable{
public void reset() { public void reset() {
stop(); stop();
this.cpu.reset(); this.cpu.reset();
this.getBus().write(0, this.defaultDriveId); this.getRCBus().write(0, this.defaultDriveId, this.getWorld(), this.getPos());
this.getBus().write(1, this.defaultMonitorId); this.getRCBus().write(1, this.defaultMonitorId, this.getWorld(), this.getPos());
} }
public void step() { public void step() {
this.waiInterrupt.acquireUninterruptibly(); this.waiInterrupt.acquireUninterruptibly();
this.cpu.step(); this.cpu.step();
this.getBus().update(); this.getRCBus().update();
if (this.cpu.isStopped()) { if (this.cpu.isStopped()) {
this.stop(); this.stop();
return; return;

@ -7,6 +7,6 @@ import net.minecraft.util.math.BlockPos;
public class MonitorEntity extends Peripheral { public class MonitorEntity extends Peripheral {
public MonitorEntity(BlockPos pos, BlockState state) { public MonitorEntity(BlockPos pos, BlockState state) {
super(RedControl.MONITOR_BLOCK_ENTITY, pos, state); super(RedControl.MONITOR_BLOCK_ENTITY, pos, state, 1);
} }
} }

@ -11,8 +11,12 @@ import net.minecraft.util.math.BlockPos;
public abstract class Peripheral extends BlockEntity { public abstract class Peripheral extends BlockEntity {
private RCBus bus; private RCBus bus;
public Peripheral(BlockEntityType<?> type, BlockPos pos, BlockState state) {
private int id;
public Peripheral(BlockEntityType<?> type, BlockPos pos, BlockState state, int id) {
super(type, pos, state); super(type, pos, state);
this.id = id;
} }
public RCBus getBus() { public RCBus getBus() {
@ -22,4 +26,12 @@ public abstract class Peripheral extends BlockEntity {
public void setBus(RCBus bus) { public void setBus(RCBus bus) {
this.bus = bus; this.bus = bus;
} }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
} }

@ -1,10 +1,12 @@
package net.brokenmoon.redcontrol.blocks; package net.brokenmoon.redcontrol.blocks;
import com.simon816.j65el02.device.RedBus;
import net.brokenmoon.redcontrol.api.RCBus; import net.brokenmoon.redcontrol.api.RCBus;
import net.brokenmoon.redcontrol.blockentities.Peripheral; import net.brokenmoon.redcontrol.blockentities.Peripheral;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -39,4 +41,14 @@ public abstract class NetworkCarrier extends Block implements BlockEntityProvide
public void invalidateBus(World world, BlockPos pos){ public void invalidateBus(World world, BlockPos pos){
} }
public void generateBus(World world, BlockPos pos){
Block worldBlock = world.getBlockState(pos).getBlock();
if(world.getBlockEntity(pos) instanceof Peripheral){
Peripheral entityBlock = (Peripheral) world.getBlockEntity(pos);
entityBlock.getBus().setPeripherals(new Peripheral[255]);
entityBlock.getBus().getPeripherals()[entityBlock.getId()] = entityBlock;
}
}
} }

Loading…
Cancel
Save