Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: charging energy items not working on fabric #741

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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 @@ -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()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> 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
Expand All @@ -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
Expand Down
Loading