|
|
|
@ -4,6 +4,10 @@ import com.simon816.j65el02.device.DiskDriver;
|
|
|
|
|
import net.brokenmoon.redcontrol.RedControl;
|
|
|
|
|
import net.brokenmoon.redcontrol.api.DriveFactory;
|
|
|
|
|
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 java.net.URI;
|
|
|
|
@ -13,106 +17,137 @@ public class DriveEntity extends Peripheral{
|
|
|
|
|
|
|
|
|
|
private static final int SECTOR_SIZE = 0x80;
|
|
|
|
|
|
|
|
|
|
private final DiskDriver driver;
|
|
|
|
|
private final byte[] diskName = new byte[SECTOR_SIZE];
|
|
|
|
|
private final byte[] diskSerial = new byte[SECTOR_SIZE];
|
|
|
|
|
private final ByteBuffer buffer;
|
|
|
|
|
private DiskDriver driver = null;
|
|
|
|
|
private ByteBuffer buffer = null;
|
|
|
|
|
|
|
|
|
|
private int sector;
|
|
|
|
|
private int command;
|
|
|
|
|
|
|
|
|
|
private ItemStack disk = ItemStack.EMPTY;
|
|
|
|
|
|
|
|
|
|
public DriveEntity(BlockPos pos, BlockState state) {
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
|
public void write(int address, int data) {
|
|
|
|
|
switch (address) {
|
|
|
|
|
case 0x80: // Sector number (lo)
|
|
|
|
|
this.sector = (this.sector & 0xff00) | data;
|
|
|
|
|
break;
|
|
|
|
|
case 0x81: // Sector number (hi)
|
|
|
|
|
this.sector = (data << 8) | (this.sector & 0xff);
|
|
|
|
|
break;
|
|
|
|
|
case 0x82: // Disk command
|
|
|
|
|
this.command = data;
|
|
|
|
|
break;
|
|
|
|
|
default: // Disk sector buffer
|
|
|
|
|
if (address >= 0 && address <= 0x7f) {
|
|
|
|
|
this.buffer.put(address, (byte) data);
|
|
|
|
|
}
|
|
|
|
|
if(driver != null) {
|
|
|
|
|
switch (address) {
|
|
|
|
|
case 0x80: // Sector number (lo)
|
|
|
|
|
this.sector = (this.sector & 0xff00) | data;
|
|
|
|
|
break;
|
|
|
|
|
case 0x81: // Sector number (hi)
|
|
|
|
|
this.sector = (data << 8) | (this.sector & 0xff);
|
|
|
|
|
break;
|
|
|
|
|
case 0x82: // Disk command
|
|
|
|
|
this.command = data;
|
|
|
|
|
break;
|
|
|
|
|
default: // Disk sector buffer
|
|
|
|
|
if (address >= 0 && address <= 0x7f) {
|
|
|
|
|
this.buffer.put(address, (byte) data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int read(int address) {
|
|
|
|
|
switch (address) {
|
|
|
|
|
case 0x80: // Sector number (lo)
|
|
|
|
|
return this.sector & 0xff;
|
|
|
|
|
case 0x81: // Sector number (hi)
|
|
|
|
|
return (this.sector >> 8) & 0xff;
|
|
|
|
|
case 0x82: // Disk command
|
|
|
|
|
return this.command;
|
|
|
|
|
default: // Disk sector buffer
|
|
|
|
|
if (address >= 0 && address <= 0x7f) {
|
|
|
|
|
return this.buffer.get(address);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
if(driver != null) {
|
|
|
|
|
switch (address) {
|
|
|
|
|
case 0x80: // Sector number (lo)
|
|
|
|
|
return this.sector & 0xff;
|
|
|
|
|
case 0x81: // Sector number (hi)
|
|
|
|
|
return (this.sector >> 8) & 0xff;
|
|
|
|
|
case 0x82: // Disk command
|
|
|
|
|
return this.command;
|
|
|
|
|
default: // Disk sector buffer
|
|
|
|
|
if (address >= 0 && address <= 0x7f) {
|
|
|
|
|
return this.buffer.get(address);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void update() {
|
|
|
|
|
try {
|
|
|
|
|
switch (this.command) {
|
|
|
|
|
case 0x01: // Read Disk Name
|
|
|
|
|
this.buffer.clear();
|
|
|
|
|
this.buffer.put(this.diskName);
|
|
|
|
|
this.command = 0;
|
|
|
|
|
break;
|
|
|
|
|
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;
|
|
|
|
|
if(driver != null) {
|
|
|
|
|
try {
|
|
|
|
|
switch (this.command) {
|
|
|
|
|
case 0x01: // Read Disk Name
|
|
|
|
|
this.buffer.clear();
|
|
|
|
|
this.buffer.put(driver.getDriveName());
|
|
|
|
|
this.command = 0;
|
|
|
|
|
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;
|
|
|
|
|
case 0x02: // Write Disk Name
|
|
|
|
|
this.buffer.position(0);
|
|
|
|
|
byte[] diskName = new byte[SECTOR_SIZE];
|
|
|
|
|
this.buffer.get(diskName);
|
|
|
|
|
this.driver.writeDiskName(diskName);
|
|
|
|
|
this.command = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
this.driver.seek(this.sector << 7);
|
|
|
|
|
this.buffer.position(0);
|
|
|
|
|
this.driver.write(this.buffer);
|
|
|
|
|
this.command = 0;
|
|
|
|
|
break;
|
|
|
|
|
case 0x03: // Read Disk Serial
|
|
|
|
|
this.buffer.clear();
|
|
|
|
|
this.buffer.put(driver.getDriveSerial());
|
|
|
|
|
this.command = 0;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|