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/energylobeingnotpresentandmeplacecrash #574

Merged
merged 6 commits into from
Nov 26, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.fml.util.thread.EffectiveSide;
import net.minecraftforge.forgespi.language.IModInfo;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand Down Expand Up @@ -49,6 +52,13 @@ public final class ConduitBundle implements INBTSerializable<CompoundTag> {

private int dataVersion = Integer.MIN_VALUE;

private static final boolean IS_NEO_ENV_AFTER_ON_LOAD_CHANGE;

static {

IModInfo forge = ModList.get().getModFileById("forge").getMods().get(0);
IS_NEO_ENV_AFTER_ON_LOAD_CHANGE = forge.getDisplayName().equals("NeoForge") && forge.getVersion().compareTo(new DefaultArtifactVersion("47.1.77")) >= 0;
}
public ConduitBundle(Runnable scheduleSync, BlockPos pos) {
this.scheduleSync = scheduleSync;
for (Direction value : Direction.values()) {
Expand Down Expand Up @@ -113,7 +123,11 @@ public RightClickAction addType(Level level, IConduitType<?> type, Player player
} else {
types.add(type);
nodes.put(type, node);
node.getExtendedConduitData().onCreated(type, level, pos, player);
if (types.size() != 1 || !IS_NEO_ENV_AFTER_ON_LOAD_CHANGE) {
//NeoForge contains a patch that calls onLoad after the conduit has been placed if it's the first one, so onCreated would be called twice. it's easier to detect here
//TODO, remove neocheck and just keep the size check with an explaining comment on why we have a types.size check at all
node.getExtendedConduitData().onCreated(type, level, pos, player);
}
}

scheduleSync.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public AE2InWorldConduitNodeHost createExtendedConduitData(Level level, BlockPos
@Override
public <K> Optional<LazyOptional<K>> proxyCapability(Capability<K> cap, AE2InWorldConduitNodeHost extendedConduitData, Level level, BlockPos pos, @Nullable Direction direction, Optional<NodeIdentifier.IOState> state) {
if (getCapability() == cap) {
return Optional.of(extendedConduitData.selfCap.cast());
return Optional.of(extendedConduitData.getSelfCap().cast());
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AE2InWorldConduitNodeHost implements IInWorldGridNodeHost, IExtende
@Nullable
private IManagedGridNode mainNode = null;

final LazyOptional<AE2InWorldConduitNodeHost> selfCap = LazyOptional.of(() -> this);
private LazyOptional<AE2InWorldConduitNodeHost> selfCap = LazyOptional.of(() -> this);

public AE2InWorldConduitNodeHost(AE2ConduitType type) {
this.type = type;
Expand Down Expand Up @@ -53,6 +53,13 @@ public IGridNode getGridNode(Direction dir) {
return mainNode.getNode();
}

public LazyOptional<AE2InWorldConduitNodeHost> getSelfCap() {
if (!selfCap.isPresent()) {
selfCap = LazyOptional.of(() -> this);
}
return selfCap;
}

@Override
public AECableType getCableConnectionType(Direction dir) {
if (type.isDense()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void tickGraph(IConduitType<?> type, List<NodeIdentifier<?>> loadedNodes,
super.tickGraph(type, loadedNodes, level, graph, isRedstoneActive);
for (NodeIdentifier<?> node : loadedNodes) {
EnergyExtendedData energyExtendedData = node.getExtendedConduitData().castTo(EnergyExtendedData.class);
IEnergyStorage energy = energyExtendedData.selfCap
IEnergyStorage energy = energyExtendedData.getSelfCap()
.resolve()
.orElseThrow();
if (energy.getEnergyStored() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public <K> Optional<LazyOptional<K>> proxyCapability(Capability<K> cap, EnergyEx
if (ForgeCapabilities.ENERGY == cap
&& state.map(NodeIdentifier.IOState::isExtract).orElse(true)
&& (direction == null || !level.getBlockState(pos.relative(direction)).is(ConduitTags.Blocks.ENERGY_CABLE))) {
return Optional.of(extendedConduitData.selfCap.cast());
return Optional.of(extendedConduitData.getSelfCap().cast());

}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class EnergyExtendedData implements IExtendedConduitData<EnergyExtendedDa
private int capacity = 500;
private int stored = 0;

final LazyOptional<IEnergyStorage> selfCap = LazyOptional.of( () -> new ConduitEnergyStorage(this));
private LazyOptional<IEnergyStorage> selfCap = LazyOptional.of( () -> new EnergyExtendedData.ConduitEnergyStorage(this));


@Override
Expand Down Expand Up @@ -82,6 +82,13 @@ public EnergySidedData compute(Direction direction) {
return energySidedData.computeIfAbsent(direction, dir -> new EnergySidedData());
}

LazyOptional<IEnergyStorage> getSelfCap() {
if (!selfCap.isPresent()) {
selfCap = LazyOptional.of(() -> new EnergyExtendedData.ConduitEnergyStorage(this));
}
return selfCap;
}

public static class EnergySidedData {
public int rotatingIndex = 0;

Expand Down
Loading