Skip to content

Commit fe754de

Browse files
committed
Fix biomes and daysLoop
1 parent 162d99d commit fe754de

File tree

5 files changed

+57
-45
lines changed

5 files changed

+57
-45
lines changed

src/main/java/btw/lowercase/skyboxify/Skyboxify.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import lombok.Getter;
3737
import lombok.experimental.UtilityClass;
3838
import net.minecraft.client.multiplayer.ClientLevel;
39-
import net.minecraft.resources.ResourceLocation;
4039
import net.minecraft.resources.ResourceKey;
40+
import net.minecraft.resources.ResourceLocation;
4141
import net.minecraft.world.level.Level;
4242
import org.jetbrains.annotations.NotNull;
4343
import org.joml.Matrix4f;
@@ -203,9 +203,9 @@ private void parseSkyboxes(SkyboxResourceHelper skyboxResourceHelper, String sky
203203
"world",
204204
//? >=1.21.11 {
205205
/*resourceKey.identifier().toString()
206-
*///?} else {
206+
*///?} else {
207207
resourceKey.location().toString()
208-
//?}
208+
//?}
209209
);
210210
SkyboxManager.INSTANCE.addSkybox(Skybox.CODEC.decode(JsonOps.INSTANCE, skyJson).getOrThrow().getFirst());
211211
}

src/main/java/btw/lowercase/skyboxify/skybox/SkyboxParser.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,13 @@ private static void parseFade(Properties properties, JsonObject output) {
166166
private static void parseDaysLoop(String days, String daysLoop, JsonObject output) {
167167
final List<Range> rangeEntries = ParserCodecs.getRangeEntriesCodec(false).orElse(List.of()).parse(JavaOps.INSTANCE, days).getOrThrow();
168168
if (!rangeEntries.isEmpty()) {
169+
final JsonObject loop = new JsonObject();
170+
loop.addProperty("days", daysLoop == null ? 8 : ParserCodecs.safeParseInteger(daysLoop, 8));
171+
169172
final JsonArray ranges = new JsonArray();
170173
rangeEntries.stream().map(range -> Range.CODEC.encode(range, JsonOps.INSTANCE, new JsonObject()).getOrThrow()).forEach(ranges::add);
171-
172-
final JsonObject loop = new JsonObject();
173-
loop.addProperty("days", ParserCodecs.safeParseInteger(daysLoop, 8));
174174
loop.add("ranges", ranges);
175+
175176
output.add("loop", loop);
176177
}
177178
}

src/main/java/btw/lowercase/skyboxify/skybox/components/Biomes.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@
3232
import java.util.List;
3333

3434
public record Biomes(ImmutableList<ResourceLocation> locations, boolean inclusion) {
35-
public static Biomes DEFAULT = new Biomes(ImmutableList.of(), true);
35+
public static Biomes DEFAULT = new Biomes(ImmutableList.of(), true);
3636

37-
public static Codec<Biomes> CODEC = ParserCodecs.TRIMMED_STRING.xmap(input -> {
38-
final boolean inclusion = input.startsWith("!");
39-
if (!inclusion) {
40-
input = input.substring(1);
41-
}
37+
public static Codec<Biomes> CODEC = ParserCodecs.TRIMMED_STRING.xmap(input -> {
38+
final boolean inclusion = !input.startsWith("!");
39+
if (!inclusion) {
40+
input = input.substring(1);
41+
}
4242

43-
final List<String> entries = ParserCodecs.SPLIT_SPACE_TRIMMED.parse(JavaOps.INSTANCE, input).getOrThrow();
44-
if (!entries.isEmpty()) {
45-
final ImmutableList.Builder<ResourceLocation> builder = new ImmutableList.Builder<>();
46-
builder.addAll(entries.stream().filter(ResourceLocation::isValidPath).map(ResourceLocation::parse).toList());
47-
return new Biomes(builder.build(), inclusion);
48-
} else {
49-
return Biomes.DEFAULT;
50-
}
51-
}, ParserCodecs::emptyCodecString);
43+
final List<String> entries = ParserCodecs.SPLIT_SPACE_TRIMMED.parse(JavaOps.INSTANCE, input).getOrThrow();
44+
if (!entries.isEmpty()) {
45+
final ImmutableList.Builder<ResourceLocation> builder = new ImmutableList.Builder<>();
46+
builder.addAll(entries.stream().filter(ResourceLocation::isValidPath).map(ResourceLocation::parse).toList());
47+
return new Biomes(builder.build(), inclusion);
48+
} else {
49+
return Biomes.DEFAULT;
50+
}
51+
}, ParserCodecs::emptyCodecString);
5252
}

src/main/java/btw/lowercase/skyboxify/utils/BlendFunction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
public record BlendFunction(int srcFactor, int dstFactor) {
3131
//? >=1.21.5 {
3232
public com.mojang.blaze3d.pipeline.BlendFunction toNative() {
33-
// TODO: Improve
3433
return new com.mojang.blaze3d.pipeline.BlendFunction(
3534
switch (this.srcFactor) {
3635
case GL11.GL_SRC_COLOR -> com.mojang.blaze3d.platform.SourceFactor.SRC_COLOR;

src/main/java/btw/lowercase/skyboxify/utils/ParserCodecs.java

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,20 @@ public final class ParserCodecs {
4646

4747
public static final Codec<String> TRIMMED_STRING = Codec.STRING.xmap(String::trim, ParserCodecs::emptyCodecString);
4848
public static final Codec<List<String>> SPLIT_SPACE_TRIMMED = TRIMMED_STRING.xmap(input -> Arrays.stream(input.split(" ")).map(String::trim).filter(s -> !s.isEmpty()).toList(), ParserCodecs::emptyCodecString);
49-
public static final Codec<Float> SAFE_FLOAT = TRIMMED_STRING.xmap(Float::parseFloat, String::valueOf);
50-
public static final Codec<Integer> SAFE_INTEGER = TRIMMED_STRING.xmap(Integer::parseInt, String::valueOf);
49+
public static final Codec<Float> SAFE_FLOAT = TRIMMED_STRING.comapFlatMap(input -> {
50+
try {
51+
return DataResult.success(Float.parseFloat(input));
52+
} catch (NumberFormatException exception) {
53+
return DataResult.error(exception::getMessage);
54+
}
55+
}, String::valueOf);
56+
public static final Codec<Integer> SAFE_INTEGER = TRIMMED_STRING.comapFlatMap(input -> {
57+
try {
58+
return DataResult.success(Integer.parseInt(input));
59+
} catch (NumberFormatException exception) {
60+
return DataResult.error(exception::getMessage);
61+
}
62+
}, String::valueOf);
5163

5264
public static final Codec<List<Weather>> WEATHER = SPLIT_SPACE_TRIMMED.xmap(input -> {
5365
if (!input.isEmpty()) {
@@ -71,23 +83,23 @@ public final class ParserCodecs {
7183
}, ParserCodecs::emptyCodecString);
7284

7385
private static final Codec<Range> RANGE_ENTRY = TRIMMED_STRING.xmap(input -> {
74-
if (input.contains("-")) {
75-
final String[] parts = input.split("-");
76-
if (parts.length == 2) {
77-
final int min = safeParseInteger(parts[0], -1);
78-
final int max = safeParseInteger(parts[1], -1);
79-
if (min >= 0 && max >= 0) {
80-
return new Range(min, max);
81-
}
82-
}
83-
} else {
84-
final int value = safeParseInteger(input, -1);
85-
if (value >= 0) {
86-
return new Range(value, value);
87-
}
88-
}
86+
if (input.contains("-")) {
87+
final String[] parts = input.split("-");
88+
if (parts.length == 2) {
89+
final int min = safeParseInteger(parts[0], -1);
90+
final int max = safeParseInteger(parts[1], -1);
91+
if (min >= 0 && max >= 0) {
92+
return new Range(min, max);
93+
}
94+
}
95+
} else {
96+
final int value = safeParseInteger(input, -1);
97+
if (value >= 0) {
98+
return new Range(value, value);
99+
}
100+
}
89101

90-
return null;
102+
return null;
91103
}, ParserCodecs::emptyCodecString);
92104

93105
private static final Codec<Range> NEGATIVE_RANGE_ENTRY = TRIMMED_STRING.xmap(input -> {
@@ -115,11 +127,11 @@ public final class ParserCodecs {
115127
public static Codec<List<Range>> getRangeEntriesCodec(boolean allowNegative) {
116128
return TRIMMED_STRING.xmap(input -> {
117129
final List<Range> entries = new ArrayList<>();
118-
for (String part : input.split(" ,")) {
119-
final Range range = (allowNegative ? NEGATIVE_RANGE_ENTRY : RANGE_ENTRY).orElse(null).parse(JavaOps.INSTANCE, part).getOrThrow();
120-
if (range != null) {
121-
entries.add(range);
122-
}
130+
for (String part : input.split("\\s*,\\s*|\\s+")) {
131+
final Range range = (allowNegative ? NEGATIVE_RANGE_ENTRY : RANGE_ENTRY).parse(JavaOps.INSTANCE, part).getOrThrow();
132+
if (range != null) {
133+
entries.add(range);
134+
}
123135
}
124136

125137
return entries;

0 commit comments

Comments
 (0)