Initialize disk on put in drive

main
Astoria 1 month ago
parent 6d86c3f6c3
commit 27543642a9

@ -3,6 +3,7 @@ package net.brokenmoon.redcontrol.blockentities;
import com.simon816.j65el02.device.DiskDriver; 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.brokenmoon.redcontrol.item.FloppyDisk;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -14,6 +15,7 @@ import net.minecraft.util.math.BlockPos;
import java.net.URI; import java.net.URI;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.UUID;
public class DriveEntity extends Peripheral{ public class DriveEntity extends Peripheral{
@ -128,7 +130,7 @@ public class DriveEntity extends Peripheral{
public ActionResult onUse(PlayerEntity player, Hand hand) { public ActionResult onUse(PlayerEntity player, Hand hand) {
if(!world.isClient) { if(!world.isClient) {
if (disk == ItemStack.EMPTY) { if (disk == ItemStack.EMPTY && player.getStackInHand(hand).getItem() instanceof FloppyDisk) {
return loadDisk(player, hand); return loadDisk(player, hand);
} else if (player.getStackInHand(hand) == ItemStack.EMPTY) { } else if (player.getStackInHand(hand) == ItemStack.EMPTY) {
return ejectDisk(player, hand); return ejectDisk(player, hand);
@ -138,7 +140,6 @@ public class DriveEntity extends Peripheral{
} }
private ActionResult ejectDisk(PlayerEntity player, Hand hand) { private ActionResult ejectDisk(PlayerEntity player, Hand hand) {
RedControl.LOGGER.info("Trying to eject!");
player.setStackInHand(hand, disk.copy()); player.setStackInHand(hand, disk.copy());
disk = ItemStack.EMPTY; disk = ItemStack.EMPTY;
this.driver = null; this.driver = null;
@ -149,7 +150,13 @@ public class DriveEntity extends Peripheral{
private ActionResult loadDisk(PlayerEntity player, Hand hand) { private ActionResult loadDisk(PlayerEntity player, Hand hand) {
disk = player.getStackInHand(hand).copy(); disk = player.getStackInHand(hand).copy();
player.setStackInHand(hand, ItemStack.EMPTY); player.setStackInHand(hand, ItemStack.EMPTY);
RedControl.LOGGER.info("Loading with " + disk.getNbt().get("uri")); if(!disk.hasNbt()){
NbtCompound newNbt = new NbtCompound();
String newUUID = UUID.randomUUID().toString();
newNbt.putString("uri", "save://" + newUUID + ".img");
newNbt.putString("serial", newUUID);
disk.setNbt(newNbt);
}
this.driver = DriveFactory.INSTANCE.createDriver(URI.create(disk.getNbt().getString("uri")), String.valueOf(disk.getName()), disk.getNbt().getString("serial")); this.driver = DriveFactory.INSTANCE.createDriver(URI.create(disk.getNbt().getString("uri")), String.valueOf(disk.getName()), disk.getNbt().getString("serial"));
this.buffer = ByteBuffer.allocateDirect(SECTOR_SIZE); this.buffer = ByteBuffer.allocateDirect(SECTOR_SIZE);
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
@ -158,13 +165,11 @@ public class DriveEntity extends Peripheral{
@Override @Override
protected void writeNbt(NbtCompound nbt) { protected void writeNbt(NbtCompound nbt) {
//SUPER DUPER NOT DONE TODO: FINISH THIS. //SUPER DUPER NOT DONE TODO: FINISH THIS.
disk.writeNbt(nbt);
super.writeNbt(nbt); super.writeNbt(nbt);
} }
@Override @Override
public void readNbt(NbtCompound nbt) { public void readNbt(NbtCompound nbt) {
disk = ItemStack.fromNbt(nbt);
super.readNbt(nbt); super.readNbt(nbt);
} }
} }

Loading…
Cancel
Save