diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/energy/ItemBlockEnergyStorage.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/energy/ItemBlockEnergyStorage.java index e529731f1..3b0630551 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/energy/ItemBlockEnergyStorage.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/energy/ItemBlockEnergyStorage.java @@ -29,6 +29,10 @@ public ItemBlockEnergyStorage(final EnergyStorage energyStorage, } } + public ItemStack getStack() { + return stack; + } + @Override protected void onStoredChanged(final long stored) { final CustomData customData = stack.get(DataComponents.BLOCK_ENTITY_DATA); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/energy/ItemEnergyStorage.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/energy/ItemEnergyStorage.java index 38954f500..88533be05 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/energy/ItemEnergyStorage.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/energy/ItemEnergyStorage.java @@ -19,6 +19,10 @@ public ItemEnergyStorage(final ItemStack stack, final EnergyStorage delegate) { } } + public ItemStack getStack() { + return stack; + } + @Override protected void onStoredChanged(final long stored) { stack.set(DataComponents.INSTANCE.getEnergy(), stored); diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java index 90339f198..7bf291e14 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java @@ -828,20 +828,22 @@ private void registerEnergyBlockEntityProviders() { private void registerEnergyItemProviders() { EnergyStorage.ITEM.registerForItems( - (stack, context) -> new EnergyStorageAdapter(Items.INSTANCE.getWirelessGrid().createEnergyStorage(stack)), + (stack, context) -> + new EnergyStorageAdapter(Items.INSTANCE.getWirelessGrid().createEnergyStorage(stack), context), Items.INSTANCE.getWirelessGrid() ); Items.INSTANCE.getControllers().forEach(controller -> EnergyStorage.ITEM.registerForItems( - (stack, context) -> new EnergyStorageAdapter(controller.get().createEnergyStorage(stack)), + (stack, context) -> new EnergyStorageAdapter(controller.get().createEnergyStorage(stack), context), controller.get() )); EnergyStorage.ITEM.registerForItems( - (stack, context) -> new EnergyStorageAdapter(PortableGridBlockItem.createEnergyStorage(stack)), + (stack, context) -> new EnergyStorageAdapter(PortableGridBlockItem.createEnergyStorage(stack), context), Items.INSTANCE.getPortableGrid() ); EnergyStorage.ITEM.registerForItems( (stack, context) -> - new EnergyStorageAdapter(Items.INSTANCE.getWirelessAutocraftingMonitor().createEnergyStorage(stack)), + new EnergyStorageAdapter(Items.INSTANCE.getWirelessAutocraftingMonitor().createEnergyStorage(stack), + context), Items.INSTANCE.getWirelessAutocraftingMonitor() ); } diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/support/energy/EnergyStorageAdapter.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/support/energy/EnergyStorageAdapter.java index c0215c518..c35e482cb 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/support/energy/EnergyStorageAdapter.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/support/energy/EnergyStorageAdapter.java @@ -2,13 +2,27 @@ import com.refinedmods.refinedstorage.api.core.Action; import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage.common.support.energy.ItemBlockEnergyStorage; +import com.refinedmods.refinedstorage.common.support.energy.ItemEnergyStorage; +import javax.annotation.Nullable; + +import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; import net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant; +import net.minecraft.world.item.ItemStack; public class EnergyStorageAdapter extends SnapshotParticipant implements team.reborn.energy.api.EnergyStorage { + @Nullable + private ContainerItemContext ctx; private final EnergyStorage energyStorage; + public EnergyStorageAdapter(final EnergyStorage energyStorage, final ContainerItemContext ctx) { + this.energyStorage = energyStorage; + this.ctx = ctx; + } + public EnergyStorageAdapter(final EnergyStorage energyStorage) { this.energyStorage = energyStorage; } @@ -17,22 +31,40 @@ public EnergyStorage getEnergyStorage() { return energyStorage; } + public void exchangeStack(final TransactionContext transaction) { + if (ctx != null) { + ItemStack itemStack = null; + if (energyStorage instanceof ItemBlockEnergyStorage itemBlockEnergyStorage) { + itemStack = itemBlockEnergyStorage.getStack(); + } else if (energyStorage instanceof ItemEnergyStorage itemEnergyStorage) { + itemStack = itemEnergyStorage.getStack(); + } + if (itemStack != null) { + ctx.exchange(ItemVariant.of(itemStack), 1, transaction); + } + } + } + @Override public long insert(final long maxAmount, final TransactionContext transaction) { - final long insertedSimulated = energyStorage.receive(maxAmount, Action.SIMULATE); - if (insertedSimulated > 0) { + long inserted = energyStorage.receive(maxAmount, Action.SIMULATE); + if (inserted > 0) { updateSnapshots(transaction); } - return energyStorage.receive(maxAmount, Action.EXECUTE); + inserted = energyStorage.receive(maxAmount, Action.EXECUTE); + exchangeStack(transaction); + return inserted; } @Override public long extract(final long maxAmount, final TransactionContext transaction) { - final long extractedSimulated = energyStorage.extract(maxAmount, Action.SIMULATE); - if (extractedSimulated > 0) { + long extracted = energyStorage.extract(maxAmount, Action.SIMULATE); + if (extracted > 0) { updateSnapshots(transaction); } - return energyStorage.extract(maxAmount, Action.EXECUTE); + extracted = energyStorage.extract(maxAmount, Action.EXECUTE); + exchangeStack(transaction); + return extracted; } @Override