Skip to content

Commit 625cf9b

Browse files
committed
Clean up lazy codec creating
1 parent 12015ef commit 625cf9b

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/main/java/aztech/modern_industrialization/util/MIExtraCodecs.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,7 @@ public class MIExtraCodecs {
4141
public static final Codec<Float> FLOAT_01 = Codec.floatRange(0, 1);
4242
public static final Codec<Long> NON_NEGATIVE_LONG = longRange(0, Long.MAX_VALUE);
4343
public static final Codec<Long> POSITIVE_LONG = longRange(1, Long.MAX_VALUE);
44-
public static final Codec<Lazy<BlockState>> LAZY_BLOCK_STATE = new Codec<>() {
45-
@Override
46-
public <T> DataResult<Pair<Lazy<BlockState>, T>> decode(DynamicOps<T> ops, T input) {
47-
return DataResult.success(Pair.of(Lazy.of(() -> BlockState.CODEC.decode(ops, input).getOrThrow().getFirst()), input));
48-
}
49-
50-
@Override
51-
public <T> DataResult<T> encode(Lazy<BlockState> input, DynamicOps<T> ops, T prefix) {
52-
return BlockState.CODEC.encode(input.get(), ops, prefix);
53-
}
54-
};
44+
public static final Codec<Lazy<BlockState>> LAZY_BLOCK_STATE = lazy(BlockState.CODEC);
5545

5646
private static <N extends Number & Comparable<N>> Function<N, DataResult<N>> checkRange(final N minInclusive, final N maxInclusive) {
5747
return value -> {
@@ -84,4 +74,21 @@ public static <T> MapCodec<T> optionalFieldAlwaysWrite(Codec<T> baseCodec, Strin
8474
return baseCodec.optionalFieldOf(field)
8575
.xmap(read -> read.orElse(defaultValue), Optional::of);
8676
}
77+
78+
/**
79+
* A codec that wraps another codec in a Lazy object.
80+
*/
81+
public static <B> Codec<Lazy<B>> lazy(Codec<B> baseCodec) {
82+
return new Codec<>() {
83+
@Override
84+
public <T> DataResult<Pair<Lazy<B>, T>> decode(DynamicOps<T> ops, T input) {
85+
return DataResult.success(Pair.of(Lazy.of(() -> baseCodec.decode(ops, input).getOrThrow().getFirst()), input));
86+
}
87+
88+
@Override
89+
public <T> DataResult<T> encode(Lazy<B> input, DynamicOps<T> ops, T prefix) {
90+
return baseCodec.encode(input.get(), ops, prefix);
91+
}
92+
};
93+
}
8794
}

0 commit comments

Comments
 (0)