Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update from latest commits of 1.12.2: Engine Pipe RF
  • Loading branch information
CalenXwX committed Apr 24, 2025
commit 061e77ead18adda47598ea45b70e6635b49b8b61
1 change: 1 addition & 0 deletions api/buildcraft/api/enums/EnumEngineType.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum EnumEngineType implements IStringSerializable, IEngineType {
IRON("energy", "iron"),
// CREATIVE("energy", "creative"),
CREATIVE("core", "creative"),
RF("energy", "rf"),
;

public final String unlocalizedTag;
Expand Down
43 changes: 43 additions & 0 deletions api/buildcraft/api/mj/IMjToRfStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package buildcraft.api.mj;

import buildcraft.api.BCModules;

public interface IMjToRfStatus {

public static IMjToRfStatus get() {
return MjToRfStatusHolder.STATUS;
}

MjRfConversion getConversion();

boolean isAutoconvertEnabled();
}

final class MjToRfStatusHolder implements IMjToRfStatus {

static final IMjToRfStatus STATUS = get0();

private static IMjToRfStatus get0() {
if (BCModules.LIB.isLoaded()) {
try {
return (IMjToRfStatus) Class.forName("buildcraft.lib.BCLibConfig$MjToRfStatus").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new Error(e);
}
} else {
return new MjToRfStatusHolder();
}
}

private final MjRfConversion defaultConversion = MjRfConversion.createDefault();

@Override
public MjRfConversion getConversion() {
return defaultConversion;
}

@Override
public boolean isAutoconvertEnabled() {
return false;
}
}
8 changes: 8 additions & 0 deletions api/buildcraft/api/mj/MjAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ private static String formatMjInternal(double val) {
return MJ_DISPLAY_FORMAT.format(val);
}

public static MjRfConversion getRfConversion() {
return IMjToRfStatus.get().getConversion();
}

public static boolean isRfAutoConversionEnabled() {
return IMjToRfStatus.get().isAutoconvertEnabled();
}

// ########################################
//
// Null based classes
Expand Down
119 changes: 119 additions & 0 deletions api/buildcraft/api/mj/MjCapabilityHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -26,12 +28,126 @@ public class MjCapabilityHelper implements ICapabilityProvider {
@Nullable
private final IMjPassiveProvider provider;

@Nullable
private final IEnergyStorage rfAutoConvert;

public MjCapabilityHelper(@Nonnull IMjConnector mj) {
this.connector = mj;
this.receiver = mj instanceof IMjReceiver ? (IMjReceiver) mj : null;
this.rsReceiver = mj instanceof IMjRedstoneReceiver ? (IMjRedstoneReceiver) mj : null;
this.readable = mj instanceof IMjReadable ? (IMjReadable) mj : null;
this.provider = mj instanceof IMjPassiveProvider ? (IMjPassiveProvider) mj : null;

if (MjAPI.isRfAutoConversionEnabled()) {
rfAutoConvert = new IEnergyStorage() {

@Override
public int getEnergyStored() {
IMjReadable read = readable;
if (read != null) {
long mjPerRf = MjAPI.getRfConversion().mjPerRf;
return (int) (read.getStored() / mjPerRf);
} else {
return 0;
}
}

@Override
public int getMaxEnergyStored() {
IMjReadable read = readable;
if (read != null) {
long mjPerRf = MjAPI.getRfConversion().mjPerRf;
return (int) (read.getCapacity() / mjPerRf);
} else {
return 0;
}
}

@Override
public boolean canReceive() {
return receiver != null && receiver.canReceive();
}

/** @return Amount of energy that was (or would have been, if simulated) accepted by the storage. */
@Override
public int receiveEnergy(int maxReceive, boolean simulate) {

if (maxReceive <= 0) {
return 0;
}

IMjReceiver recv = receiver;
if (recv == null || !recv.canReceive()) {
return 0;
}

long mjPerRf = MjAPI.getRfConversion().mjPerRf;
long maxReceiveMj = maxReceive * mjPerRf;
long excess = recv.receivePower(maxReceiveMj, true);

// Actual MJ that was accepted
long acceptedMj = maxReceiveMj - excess;

if (acceptedMj < mjPerRf) {
return 0;
}

// MJ that was accepted but cannot be converted back to RF
// (We need to actual accepted MJ to be some integer multiple of mjPerRf)
long excessMj = acceptedMj % mjPerRf;
// An MJ value that is an integer multiple of mjPerRf
long exactAcceptableMj = maxReceiveMj - excessMj;

if (exactAcceptableMj <= 0) {
return 0;
}

int rf = (int) (exactAcceptableMj / mjPerRf);
if (rf * mjPerRf != exactAcceptableMj) {
// Sanity check
throw new IllegalStateException(
"Programmer made a mistake?? mjPerRf=" + mjPerRf + ", rf=" + rf + ", exactAcceptableMJ="
+ exactAcceptableMj
);
}

long excess2 = recv.receivePower(exactAcceptableMj, true);

if (excess2 != 0) {
// Odd. This means we can't actually accept the exact amount
// not actually a crash
return 0;
}

if (!simulate) {
long excess3 = recv.receivePower(exactAcceptableMj, simulate);

if (excess3 != excess2) {
throw new IllegalStateException("Bad impl: " + recv.getClass() + " of receivePower");
}
}

return rf;
}

@Override
public boolean canExtract() {
return provider != null;
}

/** @return Amount of energy that was (or would have been, if simulated) extracted from the storage. */
@Override
public int extractEnergy(int maxExtract, boolean simulate) {
long mjPerRf = MjAPI.getRfConversion().mjPerRf;
// TODO!
// (Nothing in buildcraft supports this at the moment)
return 0;
}

};
} else {
rfAutoConvert = null;
}
}

// @Override
Expand Down Expand Up @@ -61,6 +177,9 @@ public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, Dire
// return MjAPI.CAP_PASSIVE_PROVIDER.cast(provider);
return provider == null ? LazyOptional.empty() : LazyOptional.of(() -> provider).cast();
}
if (capability == CapabilityEnergy.ENERGY) {
return provider == null ? LazyOptional.empty() : LazyOptional.of(() -> rfAutoConvert).cast();
}
return LazyOptional.empty();
}
}
46 changes: 46 additions & 0 deletions api/buildcraft/api/mj/MjRfConversion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package buildcraft.api.mj;

public class MjRfConversion {

/** Maximum MJ per RF, or minimum of 5 RF to make 1 MJ. */
public static final long MAX_MJ_PER_RF = MjAPI.MJ / 5;

/** Minimum MJ per RF, or maximum of 10,000 RF to make 1 MJ */
public static final long MIN_MJ_PER_RF = MjAPI.MJ / 10_000;

/** Default MJ per RF. */
public static final long DEFAULT_MJ_PER_RF = MjAPI.MJ / 10;

/** micro MJ per 1 int rf. */
public final long mjPerRf;

/** Set to true if {@link #mjPerRf} has been set to the {@link #DEFAULT_MJ_PER_RF} because the passed in value was
* out-of-bounds. (This is used to differentiate between it being explicitly set to the default value). */
public final boolean usingDefaultValue;

private MjRfConversion(long mjPerRf) {
if (MIN_MJ_PER_RF <= mjPerRf && mjPerRf <= MAX_MJ_PER_RF) {
usingDefaultValue = false;
this.mjPerRf = mjPerRf;
} else {
usingDefaultValue = true;
this.mjPerRf = DEFAULT_MJ_PER_RF;
}
}

/** @param mjPerRf Micro Minecraft Joules per 1 RF */
public static MjRfConversion createRaw(long mjPerRf) {
return new MjRfConversion(mjPerRf);
}

/** @param configMjPerRf {@link MjAPI#MJ} per RF. This is rounded to the nearest 100 micro MJ */
public static MjRfConversion createParsed(double configMjPerRf) {
long value = Math.round(configMjPerRf * 10_000);
return new MjRfConversion(value * MjAPI.MJ / 10_000);
}

public static MjRfConversion createDefault() {
// -10 is always out of range
return new MjRfConversion(-10);
}
}
Loading