From c06a81538180d525466497076328416d02b6b148 Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Sun, 8 Dec 2024 11:55:06 +0100 Subject: [PATCH] fix: charging energy items not working on fabric --- CHANGELOG.md | 1 + .../energy/ItemBlockEnergyStorage.java | 4 ++ .../support/energy/ItemEnergyStorage.java | 4 ++ .../fabric/ModInitializerImpl.java | 12 +++-- .../support/energy/EnergyStorageAdapter.java | 46 +++++++++++++++++-- 5 files changed, 60 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc98875fa..0b27956aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Fixed potential crash when trying to build cable shapes. - Fixed storage disk upgrade recipes not showing properly in recipe viewers. - Protect against crashes from other mods when trying to build the cached Grid tooltip. +- Fixed charging energy items not working on Fabric. ## [2.0.0-milestone.4.10] - 2024-11-24 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 465bdd2c0..731711db6 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 @@ -830,20 +830,24 @@ 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..bf2a0cb46 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,28 +2,66 @@ 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 { private final EnergyStorage energyStorage; + @Nullable + private final ContainerItemContext containerItemContext; - public EnergyStorageAdapter(final EnergyStorage energyStorage) { + public EnergyStorageAdapter(final EnergyStorage energyStorage, + @Nullable final ContainerItemContext containerItemContext) { this.energyStorage = energyStorage; + this.containerItemContext = containerItemContext; + } + + public EnergyStorageAdapter(final EnergyStorage energyStorage) { + this(energyStorage, null); } public EnergyStorage getEnergyStorage() { return energyStorage; } + private void tryExchangeStack(final TransactionContext transaction) { + if (containerItemContext == null) { + return; + } + final ItemStack stack = extractStack(); + if (stack == null) { + return; + } + containerItemContext.exchange(ItemVariant.of(stack), 1, transaction); + } + + @Nullable + private ItemStack extractStack() { + if (energyStorage instanceof ItemBlockEnergyStorage itemBlockEnergyStorage) { + return itemBlockEnergyStorage.getStack(); + } else if (energyStorage instanceof ItemEnergyStorage itemEnergyStorage) { + return itemEnergyStorage.getStack(); + } + return null; + } + @Override public long insert(final long maxAmount, final TransactionContext transaction) { final long insertedSimulated = energyStorage.receive(maxAmount, Action.SIMULATE); if (insertedSimulated > 0) { updateSnapshots(transaction); } - return energyStorage.receive(maxAmount, Action.EXECUTE); + final long inserted = energyStorage.receive(maxAmount, Action.EXECUTE); + tryExchangeStack(transaction); + return inserted; } @Override @@ -32,7 +70,9 @@ public long extract(final long maxAmount, final TransactionContext transaction) if (extractedSimulated > 0) { updateSnapshots(transaction); } - return energyStorage.extract(maxAmount, Action.EXECUTE); + final long extracted = energyStorage.extract(maxAmount, Action.EXECUTE); + tryExchangeStack(transaction); + return extracted; } @Override