Skip to content

Commit 162d99d

Browse files
committed
Some reorganizing/improvements
1 parent 3bff37d commit 162d99d

File tree

6 files changed

+141
-126
lines changed

6 files changed

+141
-126
lines changed

note.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Codec#comapFlatMap (comap/flat map): Deserialize into a DataResult instead.
2+
Codec#flatComapMap (flat comap/map): Serialize into a DataResult instead.
3+
Codec#flatXmap (flat comap/flat map): Two-way flatmapping with DataResults.

src/main/java/btw/lowercase/skyboxify/config/SkyboxifyConfig.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ public class SkyboxifyConfig extends Config {
4343

4444
public SkyboxifyConfig(final Path path) {
4545
super(Skyboxify.MOD_ID, path);
46-
this.processOptiFine.onValueChanged((oldVal, newVal) -> Minecraft.getInstance().reloadResourcePacks());
47-
this.processMCPatcher.onValueChanged((oldVal, newVal) -> Minecraft.getInstance().reloadResourcePacks());
48-
this.ignoreBrokenSkies.onValueChanged((oldVal, newVal) -> Minecraft.getInstance().reloadResourcePacks());
46+
final Minecraft minecraft = Minecraft.getInstance();
47+
this.processOptiFine.onValueChanged((oldVal, newVal) -> minecraft.reloadResourcePacks());
48+
this.processMCPatcher.onValueChanged((oldVal, newVal) -> minecraft.reloadResourcePacks());
49+
this.ignoreBrokenSkies.onValueChanged((oldVal, newVal) -> minecraft.reloadResourcePacks());
4950
}
5051

5152
@Override
5253
public Screen getConfigScreen(Screen parent) {
53-
return new SkyboxifyConfigScreen(Component.translatable("options.skyboxify.title"), this, parent);
54+
return new SkyboxifyConfigScreen(Component.translatable("options." + Skyboxify.MOD_ID + ".title"), this, parent);
5455
}
5556
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class Skybox {
4747
private final List<SkyLayer> layers;
4848
@Getter
4949
private final ResourceKey<@NotNull Level> worldResourceKey;
50-
private final Map<SkyLayer, Float> optiFineSkyLayerAlphaMap = new HashMap<>();
50+
private final Map<SkyLayer, Float> alphaMap = new HashMap<>();
5151
@Getter
5252
private boolean active = true;
5353

@@ -58,15 +58,17 @@ public Skybox(List<SkyLayer> layers, ResourceKey<@NotNull Level> worldResourceKe
5858

5959
public void tick(ClientLevel level) {
6060
this.active = true;
61-
if (level.dimension().equals(this.worldResourceKey) || (Skyboxify.getConfig().showOverworldForUnknownDimension.isEnabled() && this.worldResourceKey.equals(Level.OVERWORLD) && !level.dimension().equals(Level.NETHER) && !level.dimension().equals(Level.END))) {
62-
this.layers.forEach(layer -> optiFineSkyLayerAlphaMap.put(layer, layer.getPositionBrightness(level, this.getConditionAlphaFor(layer))));
61+
62+
final boolean allowOtherDimensions = Skyboxify.getConfig().showOverworldForUnknownDimension.isEnabled() && this.worldResourceKey.equals(Level.OVERWORLD) && !level.dimension().equals(Level.NETHER) && !level.dimension().equals(Level.END);
63+
if (level.dimension().equals(this.worldResourceKey) || allowOtherDimensions) {
64+
this.layers.forEach(layer -> alphaMap.put(layer, layer.getPositionBrightness(level, this.getConditionAlphaFor(layer))));
6365
} else {
64-
this.layers.forEach(layer -> optiFineSkyLayerAlphaMap.put(layer, -1.0F));
66+
this.layers.forEach(layer -> alphaMap.put(layer, -1.0F));
6567
this.active = false;
6668
}
6769
}
6870

6971
public float getConditionAlphaFor(SkyLayer skyLayer) {
70-
return this.optiFineSkyLayerAlphaMap.getOrDefault(skyLayer, -1.0F);
72+
return this.alphaMap.getOrDefault(skyLayer, -1.0F);
7173
}
7274
}

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

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,11 @@ private SkyboxParser() {
6262
output.addProperty("source", sourceResult.result().orElse(MissingTextureAtlasSprite.getLocation()).toString());
6363

6464
// Convert fade
65-
final JsonObject fade = new JsonObject();
66-
if (properties.containsKey("startFadeIn") && properties.containsKey("endFadeIn") && properties.containsKey("endFadeOut")) {
67-
final int startFadeIn = toTickTime(properties.getProperty("startFadeIn"));
68-
final int endFadeIn = toTickTime(properties.getProperty("endFadeIn"));
69-
final int endFadeOut = toTickTime(properties.getProperty("endFadeOut"));
70-
int startFadeOut;
71-
if (properties.containsKey("startFadeOut")) {
72-
startFadeOut = toTickTime(properties.getProperty("startFadeOut"));
73-
} else {
74-
startFadeOut = endFadeOut - (endFadeIn - startFadeIn);
75-
if (startFadeIn <= startFadeOut && endFadeIn >= startFadeOut) {
76-
startFadeOut = endFadeOut;
77-
}
78-
}
79-
80-
fade.addProperty("startFadeIn", CommonUtils.normalizeTickTime(startFadeIn));
81-
fade.addProperty("endFadeIn", CommonUtils.normalizeTickTime(endFadeIn));
82-
fade.addProperty("startFadeOut", CommonUtils.normalizeTickTime(startFadeOut));
83-
fade.addProperty("endFadeOut", CommonUtils.normalizeTickTime(endFadeOut));
84-
} else {
85-
fade.addProperty("alwaysOn", true);
86-
}
87-
88-
output.add("fade", fade);
65+
parseFade(properties, output);
8966

9067
// Speed
9168
if (properties.containsKey("speed")) {
92-
final float value = ParserCodecs.safeParseFloat(properties.getProperty("speed"), 1.0F);
69+
final float value = ParserCodecs.safeParseFloat(properties.getProperty("speed", null), 1.0F);
9370
if (value != Float.MIN_VALUE) {
9471
output.addProperty("speed", value);
9572
} else {
@@ -99,30 +76,17 @@ private SkyboxParser() {
9976

10077
// Heights
10178
if (properties.containsKey("heights")) {
102-
final List<Range> rangeEntries = ParserCodecs.getRangeEntriesCodec(true).orElse(List.of()).parse(JavaOps.INSTANCE, properties.getProperty("heights")).getOrThrow();
103-
if (!rangeEntries.isEmpty()) {
104-
final JsonArray heights = new JsonArray();
105-
rangeEntries.stream().map(range -> Range.CODEC.encode(range, JsonOps.INSTANCE, new JsonObject()).getOrThrow()).forEach(heights::add);
106-
output.add("heights", heights);
107-
}
79+
parseHeights(properties.getProperty("heights"), output);
10880
}
10981

11082
// Days Loop -> Loop
11183
if (properties.containsKey("days")) {
112-
final List<Range> rangeEntries = ParserCodecs.getRangeEntriesCodec(false).orElse(List.of()).parse(JavaOps.INSTANCE, properties.getProperty("days")).getOrThrow();
113-
if (!rangeEntries.isEmpty()) {
114-
final JsonArray ranges = new JsonArray();
115-
rangeEntries.stream().map(range -> Range.CODEC.encode(range, JsonOps.INSTANCE, new JsonObject()).getOrThrow()).forEach(ranges::add);
116-
final JsonObject loop = new JsonObject();
117-
loop.addProperty("days", properties.containsKey("daysLoop") ? ParserCodecs.safeParseInteger(properties.getProperty("daysLoop"), 8) : 8);
118-
loop.add("ranges", ranges);
119-
output.add("loop", loop);
120-
}
84+
parseDaysLoop(properties.getProperty("days"), properties.getProperty("daysLoop", null), output);
12185
}
12286

12387
// Blend
12488
if (properties.containsKey("blend")) {
125-
output.addProperty("blend", properties.getProperty("blend"));
89+
output.addProperty("blend", String.valueOf(properties.getProperty("blend")));
12690
}
12791

12892
// Rotation
@@ -142,18 +106,18 @@ private SkyboxParser() {
142106

143107
// Weather
144108
if (properties.containsKey("weather")) {
145-
output.addProperty("weather", properties.getProperty("weather"));
109+
output.addProperty("weather", String.valueOf(properties.getProperty("weather")));
146110
}
147111

148112
// Biomes
149113
if (properties.containsKey("biomes")) {
150-
output.addProperty("biomes", properties.getProperty("biomes"));
114+
output.addProperty("biomes", String.valueOf(properties.getProperty("biomes")));
151115
}
152116

153117
return output;
154118
}
155119

156-
public static int toTickTime(String time) {
120+
private static int toTickTime(String time) {
157121
final String[] parts = time.trim().split(":");
158122
if (parts.length == 2) {
159123
final int m = ParserCodecs.safeParseInteger(parts[1], -1);
@@ -171,4 +135,53 @@ public static int toTickTime(String time) {
171135
LOGGER.warn("Invalid time: \"{}\" in skybox.", time);
172136
return -1;
173137
}
138+
139+
private static void parseFade(Properties properties, JsonObject output) {
140+
final JsonObject fade = new JsonObject();
141+
if (properties.containsKey("startFadeIn") && properties.containsKey("endFadeIn") && properties.containsKey("endFadeOut")) {
142+
final int startFadeIn = toTickTime(properties.getProperty("startFadeIn"));
143+
final int endFadeIn = toTickTime(properties.getProperty("endFadeIn"));
144+
final int endFadeOut = toTickTime(properties.getProperty("endFadeOut"));
145+
int startFadeOut;
146+
if (properties.containsKey("startFadeOut")) {
147+
startFadeOut = toTickTime(properties.getProperty("startFadeOut"));
148+
} else {
149+
startFadeOut = endFadeOut - (endFadeIn - startFadeIn);
150+
if (startFadeIn <= startFadeOut && endFadeIn >= startFadeOut) {
151+
startFadeOut = endFadeOut;
152+
}
153+
}
154+
155+
fade.addProperty("startFadeIn", CommonUtils.normalizeTickTime(startFadeIn));
156+
fade.addProperty("endFadeIn", CommonUtils.normalizeTickTime(endFadeIn));
157+
fade.addProperty("startFadeOut", CommonUtils.normalizeTickTime(startFadeOut));
158+
fade.addProperty("endFadeOut", CommonUtils.normalizeTickTime(endFadeOut));
159+
} else {
160+
fade.addProperty("alwaysOn", true);
161+
}
162+
163+
output.add("fade", fade);
164+
}
165+
166+
private static void parseDaysLoop(String days, String daysLoop, JsonObject output) {
167+
final List<Range> rangeEntries = ParserCodecs.getRangeEntriesCodec(false).orElse(List.of()).parse(JavaOps.INSTANCE, days).getOrThrow();
168+
if (!rangeEntries.isEmpty()) {
169+
final JsonArray ranges = new JsonArray();
170+
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));
174+
loop.add("ranges", ranges);
175+
output.add("loop", loop);
176+
}
177+
}
178+
179+
private static void parseHeights(String input, JsonObject output) {
180+
final List<Range> rangeEntries = ParserCodecs.getRangeEntriesCodec(true).orElse(List.of()).parse(JavaOps.INSTANCE, input).getOrThrow();
181+
if (!rangeEntries.isEmpty()) {
182+
final JsonArray heights = new JsonArray();
183+
rangeEntries.stream().map(range -> Range.CODEC.encode(range, JsonOps.INSTANCE, new JsonObject()).getOrThrow()).forEach(heights::add);
184+
output.add("heights", heights);
185+
}
186+
}
174187
}

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

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -32,81 +32,81 @@
3232

3333
@UtilityClass
3434
public final class CommonUtils {
35-
public static int normalizeTickTime(int tickTime) {
36-
int result = tickTime % 24000;
37-
if (result < 0) {
38-
result += 24000;
39-
}
35+
public static int normalizeTickTime(int tickTime) {
36+
int result = tickTime % 24000;
37+
if (result < 0) {
38+
result += 24000;
39+
}
4040

41-
return result;
42-
}
41+
return result;
42+
}
4343

44-
public static boolean checkRanges(double value, List<Range> rangeEntries) {
45-
return rangeEntries.isEmpty() || rangeEntries.stream()
46-
.anyMatch(range -> com.google.common.collect.Range.closed(range.min(), range.max()).contains((float) value));
47-
}
44+
public static boolean checkRanges(double value, List<Range> rangeEntries) {
45+
return rangeEntries.isEmpty() || rangeEntries.stream()
46+
.anyMatch(range -> com.google.common.collect.Range.closed(range.min(), range.max()).contains((float) value));
47+
}
4848

49-
public static boolean isInTimeInterval(int currentTime, int startTime, int endTime) {
50-
if (currentTime < 0 || currentTime >= 24000) {
51-
return false; // Invalid time
52-
} else if (startTime <= endTime) {
53-
return currentTime >= startTime && currentTime <= endTime;
54-
} else {
55-
return currentTime >= startTime || currentTime <= endTime;
56-
}
57-
}
49+
public static boolean isInTimeInterval(int currentTime, int startTime, int endTime) {
50+
if (currentTime < 0 || currentTime >= 24000) {
51+
return false; // Invalid time
52+
} else if (startTime <= endTime) {
53+
return currentTime >= startTime && currentTime <= endTime;
54+
} else {
55+
return currentTime >= startTime || currentTime <= endTime;
56+
}
57+
}
5858

59-
public static float calculateFadeAlphaValue(float maxAlpha, float minAlpha, int currentTime, int startFadeIn, int endFadeIn, int startFadeOut, int endFadeOut) {
60-
if (isInTimeInterval(currentTime, endFadeIn, startFadeOut)) {
61-
return maxAlpha;
62-
} else if (isInTimeInterval(currentTime, startFadeIn, endFadeIn)) {
63-
final int fadeInDuration = calculateCyclicTimeDistance(startFadeIn, endFadeIn);
64-
final int timePassedSinceFadeInStart = calculateCyclicTimeDistance(startFadeIn, currentTime);
65-
return minAlpha + ((float) timePassedSinceFadeInStart / fadeInDuration) * (maxAlpha - minAlpha);
66-
} else if (isInTimeInterval(currentTime, startFadeOut, endFadeOut)) {
67-
final int fadeOutDuration = calculateCyclicTimeDistance(startFadeOut, endFadeOut);
68-
final int timePassedSinceFadeOutStart = calculateCyclicTimeDistance(startFadeOut, currentTime);
69-
return maxAlpha + ((float) timePassedSinceFadeOutStart / fadeOutDuration) * (minAlpha - maxAlpha);
70-
} else {
71-
return minAlpha;
72-
}
73-
}
59+
public static float calculateFadeAlphaValue(float maxAlpha, float minAlpha, int currentTime, int startFadeIn, int endFadeIn, int startFadeOut, int endFadeOut) {
60+
if (isInTimeInterval(currentTime, endFadeIn, startFadeOut)) {
61+
return maxAlpha;
62+
} else if (isInTimeInterval(currentTime, startFadeIn, endFadeIn)) {
63+
final int fadeInDuration = calculateCyclicTimeDistance(startFadeIn, endFadeIn);
64+
final int timePassedSinceFadeInStart = calculateCyclicTimeDistance(startFadeIn, currentTime);
65+
return minAlpha + ((float) timePassedSinceFadeInStart / fadeInDuration) * (maxAlpha - minAlpha);
66+
} else if (isInTimeInterval(currentTime, startFadeOut, endFadeOut)) {
67+
final int fadeOutDuration = calculateCyclicTimeDistance(startFadeOut, endFadeOut);
68+
final int timePassedSinceFadeOutStart = calculateCyclicTimeDistance(startFadeOut, currentTime);
69+
return maxAlpha + ((float) timePassedSinceFadeOutStart / fadeOutDuration) * (minAlpha - maxAlpha);
70+
} else {
71+
return minAlpha;
72+
}
73+
}
7474

75-
public static int calculateCyclicTimeDistance(int startTime, int endTime) {
76-
return (endTime - startTime + 24000) % 24000;
77-
}
75+
public static int calculateCyclicTimeDistance(int startTime, int endTime) {
76+
return (endTime - startTime + 24000) % 24000;
77+
}
7878

79-
public static float calculateConditionAlphaValue(float maxAlpha, float minAlpha, float lastAlpha, int duration, boolean in) {
80-
if (duration == 0) {
81-
return lastAlpha;
82-
} else if (in && maxAlpha == lastAlpha) {
83-
return maxAlpha;
84-
} else if (!in && lastAlpha == minAlpha) {
85-
return minAlpha;
86-
} else {
87-
float alphaChange = (maxAlpha - minAlpha) / duration;
88-
float result = in ? lastAlpha + alphaChange : lastAlpha - alphaChange;
89-
return Mth.clamp(result, minAlpha, maxAlpha);
90-
}
91-
}
79+
public static float calculateConditionAlphaValue(float maxAlpha, float minAlpha, float lastAlpha, int duration, boolean in) {
80+
if (duration == 0) {
81+
return lastAlpha;
82+
} else if (in && maxAlpha == lastAlpha) {
83+
return maxAlpha;
84+
} else if (!in && lastAlpha == minAlpha) {
85+
return minAlpha;
86+
} else {
87+
final float alphaChange = (maxAlpha - minAlpha) / duration;
88+
final float result = in ? lastAlpha + alphaChange : lastAlpha - alphaChange;
89+
return Mth.clamp(result, minAlpha, maxAlpha);
90+
}
91+
}
9292

93-
public static float getWeatherAlpha(List<Weather> weatherConditions, float rainStrength, float thunderStrength) {
94-
final float alpha = 1.0F - rainStrength;
95-
final float calculatedRainStrength = rainStrength - thunderStrength;
93+
public static float getWeatherAlpha(List<Weather> weatherConditions, float rainStrength, float thunderStrength) {
94+
final float alpha = 1.0F - rainStrength;
95+
final float calculatedRainStrength = rainStrength - thunderStrength;
9696

97-
float weatherAlpha = 0.0F;
98-
if (weatherConditions.contains(Weather.CLEAR)) {
99-
weatherAlpha += alpha;
100-
}
97+
float weatherAlpha = 0.0F;
98+
if (weatherConditions.contains(Weather.CLEAR)) {
99+
weatherAlpha += alpha;
100+
}
101101

102-
if (weatherConditions.contains(Weather.RAIN)) {
103-
weatherAlpha += calculatedRainStrength;
104-
}
102+
if (weatherConditions.contains(Weather.RAIN)) {
103+
weatherAlpha += calculatedRainStrength;
104+
}
105105

106-
if (weatherConditions.contains(Weather.THUNDER)) {
107-
weatherAlpha += thunderStrength;
108-
}
106+
if (weatherConditions.contains(Weather.THUNDER)) {
107+
weatherAlpha += thunderStrength;
108+
}
109109

110-
return Mth.clamp(weatherAlpha, 0.0F, 1.0F);
111-
}
110+
return Mth.clamp(weatherAlpha, 0.0F, 1.0F);
111+
}
112112
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static Codec<List<Range>> getRangeEntriesCodec(boolean allowNegative) {
127127
}
128128

129129
public static Codec<ResourceLocation> getSourceTextureCodec(final ResourceLocation propertiesLocation) {
130-
return Codec.STRING.flatXmap(input -> {
130+
return Codec.STRING.comapFlatMap(input -> {
131131
if (input == null) {
132132
return DataResult.success(propertiesLocation.withPath(propertiesLocation.getPath().replace(".properties", ".png")));
133133
} else if (input.startsWith("./")) {
@@ -146,7 +146,7 @@ public static Codec<ResourceLocation> getSourceTextureCodec(final ResourceLocati
146146
}
147147
}
148148
}
149-
}, ParserCodecs::emptyCodecResult);
149+
}, ParserCodecs::emptyCodecString);
150150
}
151151

152152
public static float safeParseFloat(String value, float defaultValue) {
@@ -157,11 +157,7 @@ public static int safeParseInteger(String value, int defaultValue) {
157157
return SAFE_INTEGER.orElse(defaultValue).parse(JavaOps.INSTANCE, value).getOrThrow();
158158
}
159159

160-
public static <T> String emptyCodecString(T in) {
160+
public static <T> String emptyCodecString(T output) {
161161
return "";
162162
}
163-
164-
public static <T> DataResult<String> emptyCodecResult(T in) {
165-
return DataResult.success("");
166-
}
167163
}

0 commit comments

Comments
 (0)