Make sure item device state is flushed to item stack when computer block is dropped.

This commit is contained in:
Florian Nücke
2021-01-04 13:51:42 +01:00
parent 39996207fa
commit e43eb1e436
4 changed files with 28 additions and 13 deletions

View File

@@ -143,12 +143,16 @@ public final class ComputerBlock extends HorizontalBlock {
@Override
public void onBlockHarvested(final World world, final BlockPos pos, final BlockState state, final PlayerEntity player) {
final TileEntity tileEntity = world.getTileEntity(pos);
if (tileEntity instanceof ComputerTileEntity) {
if (!world.isRemote() && tileEntity instanceof ComputerTileEntity) {
final ComputerTileEntity computer = (ComputerTileEntity) tileEntity;
if (!world.isRemote() && player.isCreative() && !computer.isEmpty()) {
final ItemStack stack = new ItemStack(Items.COMPUTER_ITEM.get());
computer.exportToItemStack(stack);
spawnAsEntity(world, pos, stack);
if (!computer.isEmpty()) {
computer.exportDeviceDataToItemStacks();
if (player.isCreative()) {
final ItemStack stack = new ItemStack(Items.COMPUTER_ITEM.get());
computer.exportToItemStack(stack);
spawnAsEntity(world, pos, stack);
}
}
}

View File

@@ -35,15 +35,11 @@ public class ItemHandlerDeviceBusElement extends AbstractGroupingItemDeviceBusEl
}
}
public void handleBeforeItemRemoved(final int slot, final ItemStack stack) {
if (!stack.isEmpty()) {
exportDeviceDataToItemStack(slot, stack);
public void exportDeviceDataToItemStack(final int slot, final ItemStack stack) {
if (stack.isEmpty()) {
return;
}
}
///////////////////////////////////////////////////////////////////
private void exportDeviceDataToItemStack(final int slot, final ItemStack stack) {
final CompoundNBT exportedNbt = new CompoundNBT();
for (final ItemDeviceInfo info : groups.get(slot)) {
ItemDeviceUtils.getItemDeviceDataKey(info.provider).ifPresent(key -> {
@@ -58,6 +54,8 @@ public class ItemHandlerDeviceBusElement extends AbstractGroupingItemDeviceBusEl
ItemDeviceUtils.setItemDeviceData(stack, exportedNbt);
}
///////////////////////////////////////////////////////////////////
private void importDeviceDataFromItemStack(final ItemStack stack, final HashSet<ItemDeviceInfo> devices) {
ItemDeviceUtils.getItemDeviceData(stack).ifPresent(exportedNbt -> {
for (final ItemDeviceInfo info : devices) {

View File

@@ -36,6 +36,12 @@ public class DeviceItemStackHandler extends ItemStackHandler {
return busElement;
}
public void exportDeviceDataToItemStacks() {
for (int slot = 0; slot < getSlots(); slot++) {
busElement.exportDeviceDataToItemStack(slot, getStackInSlot(slot));
}
}
@Override
public CompoundNBT serializeNBT() {
final CompoundNBT nbt = super.serializeNBT();
@@ -56,7 +62,7 @@ public class DeviceItemStackHandler extends ItemStackHandler {
@Override
public ItemStack extractItem(final int slot, final int amount, final boolean simulate) {
if (!simulate && amount > 0) {
busElement.handleBeforeItemRemoved(slot, getStackInSlot(slot));
busElement.exportDeviceDataToItemStack(slot, getStackInSlot(slot));
}
return super.extractItem(slot, amount, simulate);

View File

@@ -469,6 +469,13 @@ public final class ComputerTileEntity extends AbstractTileEntity implements ITic
return true;
}
public void exportDeviceDataToItemStacks() {
memoryItemHandler.exportDeviceDataToItemStacks();
hardDriveItemHandler.exportDeviceDataToItemStacks();
flashMemoryItemHandler.exportDeviceDataToItemStacks();
cardItemHandler.exportDeviceDataToItemStacks();
}
public void exportToItemStack(final ItemStack stack) {
final CompoundNBT items = new CompoundNBT();
items.put(MEMORY_TAG_NAME, memoryItemHandler.serializeNBT());