From a02ae18079be68c2b9c2268fffa02c4dde698be1 Mon Sep 17 00:00:00 2001 From: agnor99 <36055315+agnor99@users.noreply.github.com> Date: Sat, 25 Nov 2023 23:10:49 +0100 Subject: [PATCH 1/5] fixes me placement crash by only calling onCreated once, fix Energy LO invalidation being wrong --- .../conduits/common/blockentity/ConduitBundle.java | 10 +++++++++- .../conduits/common/types/EnergyConduitTicker.java | 2 +- .../conduits/common/types/EnergyConduitType.java | 2 +- .../conduits/common/types/EnergyExtendedData.java | 12 +++++++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java b/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java index b0ce2429a0..7764be0452 100644 --- a/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java +++ b/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java @@ -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; @@ -113,7 +116,12 @@ public RightClickAction addType(Level level, IConduitType type, Player player } else { types.add(type); nodes.put(type, node); - node.getExtendedConduitData().onCreated(type, level, pos, player); + IModInfo forge = ModList.get().getModFileById("forge").getMods().get(0); + if (types.size() != 1 || !(forge.getDisplayName().equals("NeoForge") && forge.getVersion().compareTo(new DefaultArtifactVersion("47.1.77")) >= 0)) { + //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(); diff --git a/src/conduits/java/com/enderio/conduits/common/types/EnergyConduitTicker.java b/src/conduits/java/com/enderio/conduits/common/types/EnergyConduitTicker.java index 09a8038c69..750d217ad3 100644 --- a/src/conduits/java/com/enderio/conduits/common/types/EnergyConduitTicker.java +++ b/src/conduits/java/com/enderio/conduits/common/types/EnergyConduitTicker.java @@ -31,7 +31,7 @@ public void tickGraph(IConduitType type, List> 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) { diff --git a/src/conduits/java/com/enderio/conduits/common/types/EnergyConduitType.java b/src/conduits/java/com/enderio/conduits/common/types/EnergyConduitType.java index 4f49f8d293..454a0eec12 100644 --- a/src/conduits/java/com/enderio/conduits/common/types/EnergyConduitType.java +++ b/src/conduits/java/com/enderio/conduits/common/types/EnergyConduitType.java @@ -46,7 +46,7 @@ public Optional> proxyCapability(Capability 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(); diff --git a/src/conduits/java/com/enderio/conduits/common/types/EnergyExtendedData.java b/src/conduits/java/com/enderio/conduits/common/types/EnergyExtendedData.java index 5bef02120c..6b7531f213 100644 --- a/src/conduits/java/com/enderio/conduits/common/types/EnergyExtendedData.java +++ b/src/conduits/java/com/enderio/conduits/common/types/EnergyExtendedData.java @@ -23,7 +23,7 @@ public class EnergyExtendedData implements IExtendedConduitData selfCap = LazyOptional.of( () -> new ConduitEnergyStorage(this)); + private LazyOptional selfCap = LazyOptional.of( () -> new EnergyExtendedData.ConduitEnergyStorage(this)); @Override @@ -82,6 +82,16 @@ public EnergySidedData compute(Direction direction) { return energySidedData.computeIfAbsent(direction, dir -> new EnergySidedData()); } + public void createCap() { + selfCap = LazyOptional.of( () -> new EnergyExtendedData.ConduitEnergyStorage(this)); + } + + LazyOptional getSelfCap() { + if (!selfCap.isPresent()) + selfCap = LazyOptional.of( () -> new EnergyExtendedData.ConduitEnergyStorage(this)); + return selfCap; + } + public static class EnergySidedData { public int rotatingIndex = 0; From 57ed713beed9bc91360fd3599f1807cf6e4a5c07 Mon Sep 17 00:00:00 2001 From: agnor99 <36055315+agnor99@users.noreply.github.com> Date: Sat, 25 Nov 2023 23:13:24 +0100 Subject: [PATCH 2/5] also add lo recreation to ae2conduits --- .../conduits/common/integrations/ae2/AE2ConduitType.java | 2 +- .../integrations/ae2/AE2InWorldConduitNodeHost.java | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2ConduitType.java b/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2ConduitType.java index e8f906cfcd..2ad6f047a9 100644 --- a/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2ConduitType.java +++ b/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2ConduitType.java @@ -55,7 +55,7 @@ public AE2InWorldConduitNodeHost createExtendedConduitData(Level level, BlockPos @Override public Optional> proxyCapability(Capability cap, AE2InWorldConduitNodeHost extendedConduitData, Level level, BlockPos pos, @Nullable Direction direction, Optional state) { if (getCapability() == cap) { - return Optional.of(extendedConduitData.selfCap.cast()); + return Optional.of(extendedConduitData.getSelfCap().cast()); } return Optional.empty(); } diff --git a/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2InWorldConduitNodeHost.java b/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2InWorldConduitNodeHost.java index 9f80dacde8..a7c823a7e4 100644 --- a/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2InWorldConduitNodeHost.java +++ b/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2InWorldConduitNodeHost.java @@ -24,7 +24,7 @@ public class AE2InWorldConduitNodeHost implements IInWorldGridNodeHost, IExtende @Nullable private IManagedGridNode mainNode = null; - final LazyOptional selfCap = LazyOptional.of(() -> this); + private LazyOptional selfCap = LazyOptional.of(() -> this); public AE2InWorldConduitNodeHost(AE2ConduitType type) { this.type = type; @@ -53,6 +53,12 @@ public IGridNode getGridNode(Direction dir) { return mainNode.getNode(); } + public LazyOptional getSelfCap() { + if (!selfCap.isPresent()) + selfCap = LazyOptional.of(() -> this); + return selfCap; + } + @Override public AECableType getCableConnectionType(Direction dir) { if (type.isDense()) { From 6698116715bc0136428705e9b5cf631fc198036c Mon Sep 17 00:00:00 2001 From: agnor99 <36055315+agnor99@users.noreply.github.com> Date: Sun, 26 Nov 2023 01:06:21 +0100 Subject: [PATCH 3/5] cache neoenv check --- .../conduits/common/blockentity/ConduitBundle.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java b/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java index 7764be0452..dec7b61e0e 100644 --- a/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java +++ b/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java @@ -52,6 +52,13 @@ public final class ConduitBundle implements INBTSerializable { private int dataVersion = Integer.MIN_VALUE; + private static final boolean isNeoEnvAfterOnLoadChange; + + static { + + IModInfo forge = ModList.get().getModFileById("forge").getMods().get(0); + isNeoEnvAfterOnLoadChange = 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()) { @@ -116,8 +123,7 @@ public RightClickAction addType(Level level, IConduitType type, Player player } else { types.add(type); nodes.put(type, node); - IModInfo forge = ModList.get().getModFileById("forge").getMods().get(0); - if (types.size() != 1 || !(forge.getDisplayName().equals("NeoForge") && forge.getVersion().compareTo(new DefaultArtifactVersion("47.1.77")) >= 0)) { + if (types.size() != 1 || !isNeoEnvAfterOnLoadChange) { //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); From 43c1d6ecb32af98493d8f681bf40beed2a5dfbd3 Mon Sep 17 00:00:00 2001 From: agnor99 <36055315+agnor99@users.noreply.github.com> Date: Sun, 26 Nov 2023 01:09:56 +0100 Subject: [PATCH 4/5] rename constant --- .../enderio/conduits/common/blockentity/ConduitBundle.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java b/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java index dec7b61e0e..c012468084 100644 --- a/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java +++ b/src/conduits/java/com/enderio/conduits/common/blockentity/ConduitBundle.java @@ -52,12 +52,12 @@ public final class ConduitBundle implements INBTSerializable { private int dataVersion = Integer.MIN_VALUE; - private static final boolean isNeoEnvAfterOnLoadChange; + private static final boolean IS_NEO_ENV_AFTER_ON_LOAD_CHANGE; static { IModInfo forge = ModList.get().getModFileById("forge").getMods().get(0); - isNeoEnvAfterOnLoadChange = forge.getDisplayName().equals("NeoForge") && forge.getVersion().compareTo(new DefaultArtifactVersion("47.1.77")) >= 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; @@ -123,7 +123,7 @@ public RightClickAction addType(Level level, IConduitType type, Player player } else { types.add(type); nodes.put(type, node); - if (types.size() != 1 || !isNeoEnvAfterOnLoadChange) { + 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); From abd5903d32e4145c03263d8f41e042b5bdde2dbc Mon Sep 17 00:00:00 2001 From: agnor99 <36055315+agnor99@users.noreply.github.com> Date: Sun, 26 Nov 2023 01:11:52 +0100 Subject: [PATCH 5/5] fix code formatting --- .../integrations/ae2/AE2InWorldConduitNodeHost.java | 3 ++- .../conduits/common/types/EnergyExtendedData.java | 9 +++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2InWorldConduitNodeHost.java b/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2InWorldConduitNodeHost.java index a7c823a7e4..37d56491fc 100644 --- a/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2InWorldConduitNodeHost.java +++ b/src/conduits/java/com/enderio/conduits/common/integrations/ae2/AE2InWorldConduitNodeHost.java @@ -54,8 +54,9 @@ public IGridNode getGridNode(Direction dir) { } public LazyOptional getSelfCap() { - if (!selfCap.isPresent()) + if (!selfCap.isPresent()) { selfCap = LazyOptional.of(() -> this); + } return selfCap; } diff --git a/src/conduits/java/com/enderio/conduits/common/types/EnergyExtendedData.java b/src/conduits/java/com/enderio/conduits/common/types/EnergyExtendedData.java index 6b7531f213..40a525d44d 100644 --- a/src/conduits/java/com/enderio/conduits/common/types/EnergyExtendedData.java +++ b/src/conduits/java/com/enderio/conduits/common/types/EnergyExtendedData.java @@ -82,13 +82,10 @@ public EnergySidedData compute(Direction direction) { return energySidedData.computeIfAbsent(direction, dir -> new EnergySidedData()); } - public void createCap() { - selfCap = LazyOptional.of( () -> new EnergyExtendedData.ConduitEnergyStorage(this)); - } - LazyOptional getSelfCap() { - if (!selfCap.isPresent()) - selfCap = LazyOptional.of( () -> new EnergyExtendedData.ConduitEnergyStorage(this)); + if (!selfCap.isPresent()) { + selfCap = LazyOptional.of(() -> new EnergyExtendedData.ConduitEnergyStorage(this)); + } return selfCap; }