-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add NeoForge storage adapters #369
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
Open
AdolfoCarneiro
wants to merge
3
commits into
Indemnity83:mc/26.1
Choose a base branch
from
AdolfoCarneiro:pr/neoforge-storage-adapters
base: mc/26.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,10 @@ | |
| build/ | ||
| out/ | ||
| classes/ | ||
| /common/bin/ | ||
| /fabric/bin/ | ||
| /neoforge/bin/ | ||
| /neoforge/runs/ | ||
|
|
||
| # Eclipse | ||
| *.launch | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
neoforge/src/main/java/com/logistics/neoforge/energy/NeoForgeEnergyStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package com.logistics.neoforge.energy; | ||
|
|
||
| import com.logistics.core.lib.energy.IEnergyStorage; | ||
| import net.neoforged.neoforge.transfer.energy.EnergyHandler; | ||
| import net.neoforged.neoforge.transfer.transaction.Transaction; | ||
| import net.neoforged.neoforge.transfer.transaction.TransactionContext; | ||
| import net.neoforged.neoforge.transfer.transaction.SnapshotJournal; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| public final class NeoForgeEnergyStorage implements IEnergyStorage { | ||
| private final EnergyHandler handler; | ||
|
|
||
| private NeoForgeEnergyStorage(EnergyHandler handler) { | ||
| this.handler = handler; | ||
| } | ||
|
|
||
| @Nullable | ||
| public static IEnergyStorage wrap(@Nullable EnergyHandler handler) { | ||
| return handler == null ? null : new NeoForgeEnergyStorage(handler); | ||
| } | ||
|
|
||
| @Nullable | ||
| public static EnergyHandler asNeoForge(@Nullable IEnergyStorage storage) { | ||
| return storage == null ? null : new CommonEnergyHandler(storage); | ||
| } | ||
|
|
||
| @Override | ||
| public long insert(long maxAmount, boolean simulate) { | ||
| try (Transaction tx = openTransaction()) { | ||
| int inserted = handler.insert(clampToInt(maxAmount), tx); | ||
| if (!simulate) { | ||
| tx.commit(); | ||
| } | ||
| return inserted; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public long extract(long maxAmount, boolean simulate) { | ||
| try (Transaction tx = openTransaction()) { | ||
| int extracted = handler.extract(clampToInt(maxAmount), tx); | ||
| if (!simulate) { | ||
| tx.commit(); | ||
| } | ||
| return extracted; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public long getAmount() { | ||
| return handler.getAmountAsLong(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getCapacity() { | ||
| return handler.getCapacityAsLong(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canInsert() { | ||
| if (handler instanceof CommonEnergyHandler common) { | ||
| return common.canInsert(); | ||
| } | ||
| try (Transaction tx = openTransaction()) { | ||
| return handler.insert(1, tx) > 0; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canExtract() { | ||
| if (handler instanceof CommonEnergyHandler common) { | ||
| return common.canExtract(); | ||
| } | ||
| try (Transaction tx = openTransaction()) { | ||
| return handler.extract(1, tx) > 0; | ||
| } | ||
| } | ||
|
|
||
| private static Transaction openTransaction() { | ||
| TransactionContext current = Transaction.getCurrentOpenedTransaction(); | ||
| return current == null ? Transaction.openRoot() : Transaction.open(current); | ||
| } | ||
|
|
||
| private static int clampToInt(long amount) { | ||
| return (int) Math.max(0, Math.min(amount, Integer.MAX_VALUE)); | ||
| } | ||
|
|
||
| private static final class CommonEnergyHandler extends SnapshotJournal<Long> implements EnergyHandler { | ||
| private final IEnergyStorage storage; | ||
| private long pendingDelta; | ||
|
|
||
| private CommonEnergyHandler(IEnergyStorage storage) { | ||
| this.storage = storage; | ||
| } | ||
|
|
||
| @Override | ||
| public long getAmountAsLong() { | ||
| return Math.max(0, storage.getAmount() + pendingDelta); | ||
| } | ||
|
|
||
| @Override | ||
| public long getCapacityAsLong() { | ||
| return storage.getCapacity(); | ||
| } | ||
|
|
||
| private boolean canInsert() { | ||
| return storage.canInsert(); | ||
| } | ||
|
|
||
| private boolean canExtract() { | ||
| return storage.canExtract(); | ||
| } | ||
|
|
||
| @Override | ||
| public int insert(int amount, net.neoforged.neoforge.transfer.transaction.TransactionContext transaction) { | ||
| updateSnapshots(transaction); | ||
| long effectiveFree = Math.max(0, getCapacityAsLong() - getAmountAsLong()); | ||
| long inserted = Math.min(amount, effectiveFree); | ||
| if (inserted > 0) { | ||
| pendingDelta += inserted; | ||
| } | ||
| return clampToInt(inserted); | ||
| } | ||
|
|
||
| @Override | ||
| public int extract(int amount, net.neoforged.neoforge.transfer.transaction.TransactionContext transaction) { | ||
| updateSnapshots(transaction); | ||
| long extracted = Math.min(getAmountAsLong(), storage.extract(amount, true)); | ||
| if (extracted > 0) { | ||
| pendingDelta -= extracted; | ||
| } | ||
| return clampToInt(extracted); | ||
| } | ||
|
|
||
| @Override | ||
| protected Long createSnapshot() { | ||
| return pendingDelta; | ||
| } | ||
|
|
||
| @Override | ||
| protected void revertToSnapshot(Long snapshot) { | ||
| pendingDelta = snapshot; | ||
| } | ||
|
|
||
| @Override | ||
| protected void onRootCommit(Long originalState) { | ||
| long delta = pendingDelta - originalState; | ||
| if (delta > 0) { | ||
| storage.insert(delta, false); | ||
| } else if (delta < 0) { | ||
| storage.extract(-delta, false); | ||
| } | ||
| pendingDelta = 0; | ||
| } | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
neoforge/src/main/java/com/logistics/neoforge/fluids/NeoForgeFluidKey.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package com.logistics.neoforge.fluids; | ||
|
|
||
| import com.logistics.core.lib.fluids.IFluidKey; | ||
| import net.minecraft.core.component.DataComponentPatch; | ||
| import net.minecraft.world.level.material.Fluid; | ||
| import net.neoforged.neoforge.fluids.FluidStack; | ||
| import net.neoforged.neoforge.transfer.fluid.FluidResource; | ||
|
|
||
| public final class NeoForgeFluidKey implements IFluidKey { | ||
| private final FluidResource resource; | ||
|
|
||
| public NeoForgeFluidKey(FluidResource resource) { | ||
| if (resource == null) { | ||
| throw new NullPointerException("resource must not be null"); | ||
| } | ||
| this.resource = resource; | ||
| } | ||
|
|
||
| public static NeoForgeFluidKey of(FluidStack stack) { | ||
| return new NeoForgeFluidKey(FluidResource.of(stack)); | ||
| } | ||
|
|
||
| public static NeoForgeFluidKey of(FluidResource resource) { | ||
| return new NeoForgeFluidKey(resource); | ||
| } | ||
|
|
||
| public FluidResource resource() { | ||
| return resource; | ||
| } | ||
|
|
||
| @Override | ||
| public Fluid getFluid() { | ||
| return resource.getFluid(); | ||
| } | ||
|
|
||
| @Override | ||
| public DataComponentPatch getComponents() { | ||
| return resource.getComponentsPatch(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isBlank() { | ||
| return resource.isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o instanceof NeoForgeFluidKey other) return resource.equals(other.resource); | ||
| if (o instanceof IFluidKey other) { | ||
| return getFluid() == other.getFluid() && getComponents().equals(other.getComponents()); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return 31 * getFluid().hashCode() + getComponents().hashCode(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.