Skip to content

Commit 934909f

Browse files
committed
Fixed the AttachedEjector component causing crashes when attempting to be serialized due to null colors
1 parent 3bba856 commit 934909f

File tree

6 files changed

+28
-10
lines changed

6 files changed

+28
-10
lines changed

src/api/java/mekanism/api/text/EnumColor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.mojang.serialization.Codec;
44
import io.netty.buffer.ByteBuf;
5+
import java.util.Optional;
56
import java.util.function.IntFunction;
67
import mekanism.api.IIncrementalEnum;
78
import mekanism.api.SupportsColorMap;
@@ -61,6 +62,12 @@ public enum EnumColor implements IIncrementalEnum<EnumColor>, SupportsColorMap,
6162
* @since 10.6.0
6263
*/
6364
public static final StreamCodec<ByteBuf, EnumColor> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, EnumColor::ordinal);
65+
/**
66+
* Stream codec for syncing optional colors by index.
67+
*
68+
* @since 10.6.0
69+
*/
70+
public static final StreamCodec<ByteBuf, Optional<EnumColor>> OPTIONAL_STREAM_CODEC = ByteBufCodecs.optional(STREAM_CODEC);
6471
/**
6572
* The color code that will be displayed
6673
*/

src/main/java/mekanism/common/attachments/component/AttachedEjector.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mojang.serialization.Codec;
44
import com.mojang.serialization.codecs.RecordCodecBuilder;
55
import io.netty.buffer.ByteBuf;
6+
import java.util.ArrayList;
67
import java.util.Collections;
78
import java.util.List;
89
import java.util.Optional;
@@ -12,22 +13,33 @@
1213
import mekanism.common.util.EnumUtils;
1314
import net.minecraft.network.codec.ByteBufCodecs;
1415
import net.minecraft.network.codec.StreamCodec;
16+
import net.minecraft.util.ExtraCodecs;
17+
import org.jetbrains.annotations.Nullable;
1518

19+
//TODO - 1.20.5: Try to make the version be what is attached to things that support ejectors so that if nothing is set it doesn't end up in the component patch
1620
@NothingNullByDefault
17-
public record AttachedEjector(List<EnumColor> inputColors, boolean strictInput, Optional<EnumColor> outputColor) {
21+
public record AttachedEjector(List<Optional<EnumColor>> inputColors, boolean strictInput, Optional<EnumColor> outputColor) {
1822

1923
public static final Codec<AttachedEjector> CODEC = RecordCodecBuilder.create(instance -> instance.group(
20-
EnumColor.CODEC.listOf(EnumUtils.SIDES.length, EnumUtils.SIDES.length).fieldOf(NBTConstants.INPUT_COLOR).forGetter(AttachedEjector::inputColors),
24+
ExtraCodecs.optionalEmptyMap(EnumColor.CODEC).listOf(EnumUtils.SIDES.length, EnumUtils.SIDES.length).fieldOf(NBTConstants.INPUT_COLOR).forGetter(AttachedEjector::inputColors),
2125
Codec.BOOL.fieldOf(NBTConstants.STRICT_INPUT).forGetter(AttachedEjector::strictInput),
2226
EnumColor.CODEC.optionalFieldOf(NBTConstants.QIO_META_TYPES).forGetter(AttachedEjector::outputColor)
2327
).apply(instance, AttachedEjector::new));
2428
public static final StreamCodec<ByteBuf, AttachedEjector> STREAM_CODEC = StreamCodec.composite(
25-
EnumColor.STREAM_CODEC.apply(ByteBufCodecs.list(EnumUtils.SIDES.length)), AttachedEjector::inputColors,
29+
EnumColor.OPTIONAL_STREAM_CODEC.apply(ByteBufCodecs.list(EnumUtils.SIDES.length)), AttachedEjector::inputColors,
2630
ByteBufCodecs.BOOL, AttachedEjector::strictInput,
27-
ByteBufCodecs.optional(EnumColor.STREAM_CODEC), AttachedEjector::outputColor,
31+
EnumColor.OPTIONAL_STREAM_CODEC, AttachedEjector::outputColor,
2832
AttachedEjector::new
2933
);
3034

35+
public static AttachedEjector create(EnumColor[] inputColors, boolean strictInput, @Nullable EnumColor outputColor) {
36+
List<Optional<EnumColor>> inputs = new ArrayList<>(inputColors.length);
37+
for (EnumColor inputColor : inputColors) {
38+
inputs.add(Optional.ofNullable(inputColor));
39+
}
40+
return new AttachedEjector(inputs, strictInput, Optional.ofNullable(outputColor));
41+
}
42+
3143
public AttachedEjector {
3244
if (inputColors.size() != EnumUtils.SIDES.length) {
3345
throw new IllegalArgumentException("Expected there to be an input color for each side");

src/main/java/mekanism/common/attachments/component/AttachedSideConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.jetbrains.annotations.NotNull;
2222
import org.jetbrains.annotations.Nullable;
2323

24+
//TODO - 1.20.5: Should we set the defaults for things via the default attached components so that then if it is equal to the default on break it doesn't get applied?
2425
@NothingNullByDefault
2526
public record AttachedSideConfig(Map<TransmissionType, LightConfigInfo> configInfo) {
2627

src/main/java/mekanism/common/content/transporter/SorterFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected static <FILTER extends SorterFilter<FILTER>> StreamCodec<ByteBuf, FILT
4444
ByteBufCodecs.BOOL, filter -> filter.sizeMode,
4545
ByteBufCodecs.VAR_INT, filter -> filter.min,
4646
ByteBufCodecs.VAR_INT, filter -> filter.max,
47-
ByteBufCodecs.optional(EnumColor.STREAM_CODEC), filter -> Optional.ofNullable(filter.color),
47+
EnumColor.OPTIONAL_STREAM_CODEC, filter -> Optional.ofNullable(filter.color),
4848
(filter, allowDefault, sizeMode, min, max, color) -> {
4949
filter.allowDefault = allowDefault;
5050
filter.color = color.orElse(null);

src/main/java/mekanism/common/content/transporter/TransporterStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class TransporterStack {
4141

4242
//Make sure to call updateForPos before calling this method
4343
public static StreamCodec<RegistryFriendlyByteBuf, TransporterStack> STREAM_CODEC = NeoForgeStreamCodecs.composite(
44-
ByteBufCodecs.optional(EnumColor.STREAM_CODEC), stack -> Optional.ofNullable(stack.color),
44+
EnumColor.OPTIONAL_STREAM_CODEC, stack -> Optional.ofNullable(stack.color),
4545
ByteBufCodecs.VAR_INT, stack -> stack.progress,
4646
BlockPos.STREAM_CODEC, stack -> stack.originalLocation,
4747
Path.STREAM_CODEC, TransporterStack::getPathType,

src/main/java/mekanism/common/tile/component/TileComponentEjector.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.IdentityHashMap;
1010
import java.util.List;
1111
import java.util.Map;
12-
import java.util.Optional;
1312
import java.util.Set;
1413
import java.util.function.Consumer;
1514
import java.util.function.Function;
@@ -406,7 +405,7 @@ public void applyImplicitComponents(@NotNull BlockEntity.DataComponentInput inpu
406405
AttachedEjector ejector = input.get(MekanismDataComponents.EJECTOR);
407406
if (ejector != null) {
408407
for (int i = 0; i < inputColors.length; i++) {
409-
inputColors[i] = ejector.inputColors().get(i);
408+
inputColors[i] = ejector.inputColors().get(i).orElse(null);
410409
}
411410
strictInput = ejector.strictInput();
412411
outputColor = ejector.outputColor().orElse(null);
@@ -415,8 +414,7 @@ public void applyImplicitComponents(@NotNull BlockEntity.DataComponentInput inpu
415414

416415
@Override
417416
public void collectImplicitComponents(DataComponentMap.Builder builder) {
418-
//Note: We have to use Arrays#asList instead of List#of as our inputColors can contain null elements
419-
builder.set(MekanismDataComponents.EJECTOR, new AttachedEjector(Arrays.asList(inputColors), strictInput, Optional.ofNullable(outputColor)));
417+
builder.set(MekanismDataComponents.EJECTOR, AttachedEjector.create(inputColors, strictInput, outputColor));
420418
}
421419

422420
@Override

0 commit comments

Comments
 (0)