Skip to content

Commit

Permalink
fix: support fabric context update mutated stack
Browse files Browse the repository at this point in the history
  • Loading branch information
ZurrTum committed Nov 25, 2024
1 parent a18776d commit 48ea4a8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> 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;
}
Expand All @@ -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
Expand Down

0 comments on commit 48ea4a8

Please sign in to comment.