From 27543642a92c41435d28f823348c811e628eeb80 Mon Sep 17 00:00:00 2001 From: Astoria Date: Sat, 13 Apr 2024 13:53:39 -0500 Subject: [PATCH] Initialize disk on put in drive --- .../redcontrol/blockentities/DriveEntity.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/DriveEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/DriveEntity.java index 1721cb3..717c7d2 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/DriveEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/DriveEntity.java @@ -3,6 +3,7 @@ package net.brokenmoon.redcontrol.blockentities; import com.simon816.j65el02.device.DiskDriver; import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.api.DriveFactory; +import net.brokenmoon.redcontrol.item.FloppyDisk; import net.minecraft.block.BlockState; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; @@ -14,6 +15,7 @@ import net.minecraft.util.math.BlockPos; import java.net.URI; import java.nio.ByteBuffer; +import java.util.UUID; public class DriveEntity extends Peripheral{ @@ -128,7 +130,7 @@ public class DriveEntity extends Peripheral{ public ActionResult onUse(PlayerEntity player, Hand hand) { if(!world.isClient) { - if (disk == ItemStack.EMPTY) { + if (disk == ItemStack.EMPTY && player.getStackInHand(hand).getItem() instanceof FloppyDisk) { return loadDisk(player, hand); } else if (player.getStackInHand(hand) == ItemStack.EMPTY) { return ejectDisk(player, hand); @@ -138,7 +140,6 @@ public class DriveEntity extends Peripheral{ } private ActionResult ejectDisk(PlayerEntity player, Hand hand) { - RedControl.LOGGER.info("Trying to eject!"); player.setStackInHand(hand, disk.copy()); disk = ItemStack.EMPTY; this.driver = null; @@ -149,7 +150,13 @@ public class DriveEntity extends Peripheral{ private ActionResult loadDisk(PlayerEntity player, Hand hand) { disk = player.getStackInHand(hand).copy(); 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.buffer = ByteBuffer.allocateDirect(SECTOR_SIZE); return ActionResult.SUCCESS; @@ -158,13 +165,11 @@ public class DriveEntity extends Peripheral{ @Override protected void writeNbt(NbtCompound nbt) { //SUPER DUPER NOT DONE TODO: FINISH THIS. - disk.writeNbt(nbt); super.writeNbt(nbt); } @Override public void readNbt(NbtCompound nbt) { - disk = ItemStack.fromNbt(nbt); super.readNbt(nbt); } }