Skip to content

Commit

Permalink
feat: Backport Reborn Energy support
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Jul 3, 2024
1 parent 5359279 commit e181741
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fabric/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ dependencies {
modCompileOnly "com.terraformersmc:modmenu:$modmenu_version"

modCompileOnly("de.siphalor:amecsapi-1.20:1.5.2+mc1.20-pre1")

modCompileOnly modApi('teamreborn:energy:3.0.0')
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ public void onInitialize() {

return null;
});

Balm.initializeIfLoaded("team_reborn_energy", "net.blay09.mods.balm.fabric.compat.energy.RebornEnergy");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.blay09.mods.balm.fabric.compat.energy;

import net.blay09.mods.balm.api.energy.BalmEnergyStorageProvider;
import team.reborn.energy.api.EnergyStorage;

public class RebornEnergy {
public RebornEnergy() {
EnergyStorage.SIDED.registerFallback((world, pos, state, blockEntity, direction) -> {
if (blockEntity instanceof BalmEnergyStorageProvider energyStorageProvider) {
final var energyStorage = energyStorageProvider.getEnergyStorage(direction);
if (energyStorage != null) {
return new RebornEnergyStorage(energyStorage);
}
}

return null;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.blay09.mods.balm.fabric.compat.energy;

import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant;
import team.reborn.energy.api.EnergyStorage;

public class RebornEnergyStorage extends SnapshotParticipant<Long> implements EnergyStorage {

private final net.blay09.mods.balm.api.energy.EnergyStorage energyStorage;

public RebornEnergyStorage(net.blay09.mods.balm.api.energy.EnergyStorage energyStorage) {
this.energyStorage = energyStorage;
}

@Override
public long insert(long maxAmount, TransactionContext transaction) {
StoragePreconditions.notNegative(maxAmount);
updateSnapshots(transaction);
return energyStorage.fill((int) maxAmount, false);
}

@Override
public long extract(long maxAmount, TransactionContext transaction) {
StoragePreconditions.notNegative(maxAmount);
updateSnapshots(transaction);
return energyStorage.drain((int) maxAmount, false);
}

@Override
public long getAmount() {
return energyStorage.getEnergy();
}

@Override
public long getCapacity() {
return energyStorage.getCapacity();
}

@Override
protected Long createSnapshot() {
return (long) energyStorage.getEnergy();
}

@Override
protected void readSnapshot(Long snapshot) {
energyStorage.setEnergy(snapshot.intValue());
}
}

0 comments on commit e181741

Please sign in to comment.