Compare commits

...

2 Commits

@ -57,7 +57,7 @@ processResources {
} }
tasks.withType(JavaCompile).configureEach { tasks.withType(JavaCompile).configureEach {
it.options.release = 17 it.options.release = 19
} }
java { java {

@ -6,6 +6,7 @@ import net.brokenmoon.redcontrol.blocks.CpuBlock;
import net.brokenmoon.redcontrol.blocks.DriveBlock; import net.brokenmoon.redcontrol.blocks.DriveBlock;
import net.brokenmoon.redcontrol.blocks.TerminalBlock; import net.brokenmoon.redcontrol.blocks.TerminalBlock;
import net.brokenmoon.redcontrol.blocks.TerminalEntity; import net.brokenmoon.redcontrol.blocks.TerminalEntity;
import net.brokenmoon.redcontrol.item.BasicFloppy;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings; import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
@ -43,7 +44,9 @@ public class RedControl implements ModInitializer {
public static final CpuBlock CPU = new CpuBlock(FabricBlockSettings.create().strength(4.0f)); public static final CpuBlock CPU = new CpuBlock(FabricBlockSettings.create().strength(4.0f));
public static final TerminalBlock TERMINAL = new TerminalBlock(FabricBlockSettings.create().strength(4.0f)); public static final TerminalBlock TERMINAL = new TerminalBlock(FabricBlockSettings.create().strength(4.0f));
public static final DriveBlock DRIVE = new DriveBlock(FabricBlockSettings.create().strength(4.0f)); public static final DriveBlock DRIVE = new DriveBlock(FabricBlockSettings.create().strength(4.0f));
//Items //Items
public static final BasicFloppy BASIC_FLOPPY = new BasicFloppy(new FabricItemSettings().maxCount(1));
//Block Entities //Block Entities
public static final BlockEntityType<CpuEntity> CPU_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, modloc("cpu_block_entity"), FabricBlockEntityTypeBuilder.create(CpuEntity::new, CPU).build()); public static final BlockEntityType<CpuEntity> CPU_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, modloc("cpu_block_entity"), FabricBlockEntityTypeBuilder.create(CpuEntity::new, CPU).build());
@ -88,6 +91,7 @@ public class RedControl implements ModInitializer {
Registry.register(Registries.ITEM, modloc("monitor"), new BlockItem(TERMINAL, new FabricItemSettings())); Registry.register(Registries.ITEM, modloc("monitor"), new BlockItem(TERMINAL, new FabricItemSettings()));
Registry.register(Registries.ITEM, modloc("disk_drive"), new BlockItem(DRIVE, new FabricItemSettings())); Registry.register(Registries.ITEM, modloc("disk_drive"), new BlockItem(DRIVE, new FabricItemSettings()));
Registry.register(Registries.ITEM, modloc("squeaky_hammer"), SQUEAKY_HAMMER); Registry.register(Registries.ITEM, modloc("squeaky_hammer"), SQUEAKY_HAMMER);
Registry.register(Registries.ITEM, modloc("basic_disk"), BASIC_FLOPPY);
//Packets //Packets
ServerPlayNetworking.registerGlobalReceiver(RedControlNetworking.CPU_START, ((server, player, handler, buf, responseSender) -> server.execute(() -> { ServerPlayNetworking.registerGlobalReceiver(RedControlNetworking.CPU_START, ((server, player, handler, buf, responseSender) -> server.execute(() -> {

@ -4,6 +4,10 @@ import com.simon816.j65el02.device.DiskDriver;
import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.RedControl;
import net.brokenmoon.redcontrol.api.DriveFactory; import net.brokenmoon.redcontrol.api.DriveFactory;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.net.URI; import java.net.URI;
@ -13,106 +17,137 @@ public class DriveEntity extends Peripheral{
private static final int SECTOR_SIZE = 0x80; private static final int SECTOR_SIZE = 0x80;
private final DiskDriver driver; private DiskDriver driver = null;
private final byte[] diskName = new byte[SECTOR_SIZE]; private ByteBuffer buffer = null;
private final byte[] diskSerial = new byte[SECTOR_SIZE];
private final ByteBuffer buffer;
private int sector; private int sector;
private int command; private int command;
private ItemStack disk = ItemStack.EMPTY;
public DriveEntity(BlockPos pos, BlockState state) { public DriveEntity(BlockPos pos, BlockState state) {
super(RedControl.DRIVE_BLOCK_ENTITY, pos, state, 2); super(RedControl.DRIVE_BLOCK_ENTITY, pos, state, 2);
this.driver = DriveFactory.INSTANCE.createDriver(URI.create("img://basic.img"),"Basic","BASIC");
this.buffer = ByteBuffer.allocateDirect(SECTOR_SIZE); this.buffer = ByteBuffer.allocateDirect(SECTOR_SIZE);
byte[] name = driver.getDriveName();
byte[] serial = driver.getDriveSerial();
System.arraycopy(name, 0, this.diskName, 0, name.length);
System.arraycopy(serial, 0, this.diskSerial, 0, serial.length);
} }
@Override @Override
public void write(int address, int data) { public void write(int address, int data) {
switch (address) { if(driver != null) {
case 0x80: // Sector number (lo) switch (address) {
this.sector = (this.sector & 0xff00) | data; case 0x80: // Sector number (lo)
break; this.sector = (this.sector & 0xff00) | data;
case 0x81: // Sector number (hi) break;
this.sector = (data << 8) | (this.sector & 0xff); case 0x81: // Sector number (hi)
break; this.sector = (data << 8) | (this.sector & 0xff);
case 0x82: // Disk command break;
this.command = data; case 0x82: // Disk command
break; this.command = data;
default: // Disk sector buffer break;
if (address >= 0 && address <= 0x7f) { default: // Disk sector buffer
this.buffer.put(address, (byte) data); if (address >= 0 && address <= 0x7f) {
} this.buffer.put(address, (byte) data);
}
}
} }
} }
@Override @Override
public int read(int address) { public int read(int address) {
switch (address) { if(driver != null) {
case 0x80: // Sector number (lo) switch (address) {
return this.sector & 0xff; case 0x80: // Sector number (lo)
case 0x81: // Sector number (hi) return this.sector & 0xff;
return (this.sector >> 8) & 0xff; case 0x81: // Sector number (hi)
case 0x82: // Disk command return (this.sector >> 8) & 0xff;
return this.command; case 0x82: // Disk command
default: // Disk sector buffer return this.command;
if (address >= 0 && address <= 0x7f) { default: // Disk sector buffer
return this.buffer.get(address); if (address >= 0 && address <= 0x7f) {
} return this.buffer.get(address);
return 0; }
return 0;
}
} }
return 0;
} }
@Override @Override
public void update() { public void update() {
try { if(driver != null) {
switch (this.command) { try {
case 0x01: // Read Disk Name switch (this.command) {
this.buffer.clear(); case 0x01: // Read Disk Name
this.buffer.put(this.diskName); this.buffer.clear();
this.command = 0; this.buffer.put(driver.getDriveName());
break; this.command = 0;
case 0x02: // Write Disk Name
this.buffer.position(0);
this.buffer.get(this.diskName);
this.driver.writeDiskName(this.diskName);
this.command = 0;
break;
case 0x03: // Read Disk Serial
this.buffer.clear();
this.buffer.put(this.diskSerial);
this.command = 0;
break;
case 0x04: // Read Disk Sector
if (this.sector >= 0x800) {
this.command = 0xff;
break; break;
} case 0x02: // Write Disk Name
this.driver.seek(this.sector << 7); this.buffer.position(0);
this.buffer.position(0); byte[] diskName = new byte[SECTOR_SIZE];
this.driver.read(this.buffer); this.buffer.get(diskName);
this.command = 0; this.driver.writeDiskName(diskName);
break; this.command = 0;
case 0x05: // Write Disk Sector
if (this.sector >= 0x800) {
this.command = 0xff;
break; break;
} case 0x03: // Read Disk Serial
this.driver.seek(this.sector << 7); this.buffer.clear();
this.buffer.position(0); this.buffer.put(driver.getDriveSerial());
this.driver.write(this.buffer); this.command = 0;
this.command = 0; break;
break; case 0x04: // Read Disk Sector
if (this.sector >= 0x800) {
this.command = 0xff;
break;
}
this.driver.seek(this.sector << 7);
this.buffer.position(0);
this.driver.read(this.buffer);
this.command = 0;
break;
case 0x05: // Write Disk Sector
if (this.sector >= 0x800) {
this.command = 0xff;
break;
}
this.driver.seek(this.sector << 7);
this.buffer.position(0);
this.driver.write(this.buffer);
this.command = 0;
break;
}
} catch (Exception e) {
this.command = 0xff;
//e.printStackTrace();
} }
} catch (Exception e) {
this.command = 0xff;
//e.printStackTrace();
} }
} }
public ActionResult onUse(PlayerEntity player, Hand hand) {
if(!world.isClient) {
if (disk == ItemStack.EMPTY) {
return loadDisk(player, hand);
} else if (player.getStackInHand(hand) == ItemStack.EMPTY) {
return ejectDisk(player, hand);
}
}
return ActionResult.PASS;
}
private ActionResult ejectDisk(PlayerEntity player, Hand hand) {
RedControl.LOGGER.info("Trying to eject!");
player.setStackInHand(hand, disk.copy());
disk = ItemStack.EMPTY;
this.driver = null;
this.buffer = null;
return ActionResult.SUCCESS;
}
private ActionResult loadDisk(PlayerEntity player, Hand hand) {
disk = player.getStackInHand(hand).copy();
player.setStackInHand(hand, ItemStack.EMPTY);
this.driver = DriveFactory.INSTANCE.createDriver(URI.create(String.valueOf(disk.getNbt().get("uri"))), String.valueOf(disk.getName()),disk.getNbt().getString("serial"));
this.buffer = ByteBuffer.allocateDirect(SECTOR_SIZE);
return ActionResult.SUCCESS;
}
} }

@ -3,10 +3,12 @@ package net.brokenmoon.redcontrol.blocks;
import com.mojang.serialization.MapCodec; import com.mojang.serialization.MapCodec;
import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.RedControl;
import net.brokenmoon.redcontrol.blockentities.DriveEntity; import net.brokenmoon.redcontrol.blockentities.DriveEntity;
import net.brokenmoon.redcontrol.item.FloppyDisk;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity; import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.hit.BlockHitResult;
@ -34,8 +36,12 @@ public class DriveBlock extends NetworkCarrier{
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (player.getStackInHand(hand).getItem() == RedControl.SQUEAKY_HAMMER) { if (player.getStackInHand(hand).getItem() == RedControl.SQUEAKY_HAMMER) {
return super.onUse(state,world,pos,player,hand,hit); return super.onUse(state,world,pos,player,hand,hit);
} else { } else if(player.getStackInHand(hand).getItem() instanceof FloppyDisk || player.getStackInHand(hand) == ItemStack.EMPTY){
return ActionResult.SUCCESS; if(world.getBlockEntity(pos) instanceof DriveEntity){
return ((DriveEntity)world.getBlockEntity(pos)).onUse(player, hand);
}
} }
return ActionResult.PASS;
} }
} }

@ -1,22 +1,19 @@
package net.brokenmoon.redcontrol.item; package net.brokenmoon.redcontrol.item;
import com.simon816.j65el02.device.DiskDriver;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.text.Text;
public class FloppyDisk extends Item { public class FloppyDisk extends Item {
public String uri; public String uri;
public String name; public String name;
public String serial;
public FloppyDisk(Settings settings) { public FloppyDisk(Settings settings) {
super(settings); super(settings);
} }
public FloppyDisk(Settings settings, String uri, String name, String serial){ @Override
this(settings); public Text getName() {
this.uri = uri; return Text.literal(name);
this.name = name;
this.serial = serial;
} }
} }

Loading…
Cancel
Save