Skip to content

Commit

Permalink
Refactor iterating over datapack registry entries to eliminate duplic…
Browse files Browse the repository at this point in the history
…ate code.
  • Loading branch information
leMaik committed Jul 6, 2024
1 parent 2cb9b7e commit 03994b7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 115 deletions.
47 changes: 47 additions & 0 deletions chunky/src/java/se/llbit/chunky/resources/DataPackUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package se.llbit.chunky.resources;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class DataPackUtil {
private DataPackUtil() {
}

public static void forEachDataRegistryEntry(LayeredResourcePacks resourcePacks, String registryPath, Consumer<DataRegistryEntry> consumer) {
for (LayeredResourcePacks.Entry data : resourcePacks.getAllEntries("data")) {
try (Stream<Path> namespaces = Files.list(data.getPath())) {
namespaces.forEach(ns -> {
String namespace = String.valueOf(ns.getFileName());
Path entriesPath = ns;
for (String part : registryPath.split("/")) {
entriesPath = entriesPath.resolve(part);
}
try (Stream<Path> entriesStream = Files.walk(entriesPath)) {
entriesStream
.filter(p -> Files.isRegularFile(p, LinkOption.NOFOLLOW_LINKS))
.forEach(file -> {
String name = file.getFileName().toString();
if (name.toLowerCase().endsWith(".json")) {
name = name.substring(0, name.length() - ".json".length());
}
consumer.accept(new DataRegistryEntry(namespace, name, file));
});
} catch (IOException ignored) {
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

public record DataRegistryEntry(String namespace, String name, Path path) {
public String getNamespacedName() {
return namespace + ":" + name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Stream;

public class ResourcePackBiomeLoader implements ResourcePackLoader.PackLoader {
public ResourcePackBiomeLoader() {
Expand All @@ -57,69 +53,36 @@ protected static class BiomeEffects {

@Override
public boolean load(LayeredResourcePacks resourcePacks) {
for (LayeredResourcePacks.Entry data : resourcePacks.getAllEntries("data")) {
try (Stream<Path> namespaces = Files.list(data.getPath())) {
namespaces.forEach(ns -> {
String namespace = String.valueOf(ns.getFileName());
DataPackUtil.forEachDataRegistryEntry(resourcePacks, "worldgen/biome", biome -> {
if (!Biomes.contains(biome.getNamespacedName())) {
try (Reader f = Files.newBufferedReader(biome.path())) {
BiomeJson json = GSON.fromJson(f, BiomeJson.class);

Path biomes = ns.resolve("worldgen").resolve("biome");
try (Stream<Path> biomeStream = Files.walk(biomes)) {
biomeStream
.filter(p -> Files.isRegularFile(p, LinkOption.NOFOLLOW_LINKS))
.forEach(biome -> {
if (biome.toString().endsWith(".json")) {
String biomeName = getBiomeName(biomes.relativize(biome));
String resourceLocation = namespace + ":" + biomeName;
BiomeBuilder builder = Biome.create(biome.getNamespacedName(), biome.name(), json.temperature, json.downfall);
Optional.ofNullable(json.effects.foliage_color).ifPresent(builder::foliageColor);
Optional.ofNullable(json.effects.grass_color).ifPresent(builder::grassColor);
Optional.ofNullable(json.effects.water_color).ifPresent(builder::waterColor);
Optional.ofNullable(json.effects.grass_color_modifier).ifPresent(modifier -> {
switch (modifier.toLowerCase()) {
case "none":
break;
case "dark_forest":
builder.darkForest();
break;
case "swamp":
builder.swamp();
break;
default:
Log.warnf("Unsupported biome `grass_modifier_color`: %s", modifier);
}
});
// TODO Custom fog colors

if (!Biomes.contains(resourceLocation)) {
try (Reader f = Files.newBufferedReader(biome)) {
BiomeJson json = GSON.fromJson(f, BiomeJson.class);

BiomeBuilder builder = Biome.create(resourceLocation, biomeName, json.temperature, json.downfall);
Optional.ofNullable(json.effects.foliage_color).ifPresent(builder::foliageColor);
Optional.ofNullable(json.effects.grass_color).ifPresent(builder::grassColor);
Optional.ofNullable(json.effects.water_color).ifPresent(builder::waterColor);
Optional.ofNullable(json.effects.grass_color_modifier).ifPresent(modifier -> {
switch (modifier.toLowerCase()) {
case "none":
break;
case "dark_forest":
builder.darkForest();
break;
case "swamp":
builder.swamp();
break;
default:
Log.warnf("Unsupported biome `grass_modifier_color`: %s", modifier);
}
});
// TODO Custom fog colors

Biomes.register(builder);
} catch (IOException ignored) {
}
}
}
});
} catch (IOException ignored) {
}
});
} catch (IOException e) {
throw new RuntimeException(e);
Biomes.register(builder);
} catch (IOException ignored) {
}
}
}

});
return false;
}

private static String getBiomeName(Path biome) {
ArrayList<String> path = new ArrayList<>();
biome.iterator().forEachRemaining(p -> path.add(String.valueOf(p)));

String out = String.join("/", path);
if (out.toLowerCase().endsWith(".json")) {
out = out.substring(0, out.length() - ".json".length());
}
return out;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.stream.Stream;

public class ResourcePackPaintingLoader implements ResourcePackLoader.PackLoader {

Expand All @@ -35,55 +31,23 @@ public Pair<String, String> getAsset() {

@Override
public boolean load(LayeredResourcePacks resourcePacks) {
for (LayeredResourcePacks.Entry data : resourcePacks.getAllEntries("data")) {
try (Stream<Path> namespaces = Files.list(data.getPath())) {
namespaces.forEach(ns -> {
String namespace = String.valueOf(ns.getFileName());
Path paintingVariants = ns.resolve("painting_variant");
try (Stream<Path> paintingVariantStream = Files.walk(paintingVariants)) {
paintingVariantStream
.filter(p -> Files.isRegularFile(p, LinkOption.NOFOLLOW_LINKS))
.forEach(paintingVariant -> {
if (paintingVariant.toString().endsWith(".json")) {
String paintingVariantName = getPaintingVariantName(paintingVariants.relativize(paintingVariant));
String resourceLocation = namespace + ":" + paintingVariantName;

if (!PaintingEntity.containsPainting(resourceLocation)) {
try (Reader f = Files.newBufferedReader(paintingVariant)) {
PaintingVariantJson json = GSON.fromJson(f, PaintingVariantJson.class);
Texture paintingTexture = new Texture();
Pair<String, String> asset = json.getAsset();
if (!ResourcePackLoader.loadResources(
ResourcePackTextureLoader.singletonLoader(json.asset_id, new SimpleTexture("assets/" + asset.thing1 + "/textures/painting/" + asset.thing2, paintingTexture)))
) {
Log.warnf("Failed to load painting texture: %s", json.asset_id);
}
PaintingEntity.registerPainting(resourceLocation, new PaintingEntity.Painting(paintingTexture, json.width, json.height));
} catch (IOException ignored) {
Log.warnf("Failed to load painting variant: %s", paintingVariantName);
}
}
}
});
} catch (IOException ignored) {
DataPackUtil.forEachDataRegistryEntry(resourcePacks, "painting_variant", paintingVariant -> {
if (!PaintingEntity.containsPainting(paintingVariant.getNamespacedName())) {
try (Reader f = Files.newBufferedReader(paintingVariant.path())) {
PaintingVariantJson json = GSON.fromJson(f, PaintingVariantJson.class);
Texture paintingTexture = new Texture();
Pair<String, String> asset = json.getAsset();
if (!ResourcePackLoader.loadResources(
ResourcePackTextureLoader.singletonLoader(json.asset_id, new SimpleTexture("assets/" + asset.thing1 + "/textures/painting/" + asset.thing2, paintingTexture)))
) {
Log.warnf("Failed to load painting texture: %s", json.asset_id);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
PaintingEntity.registerPainting(paintingVariant.getNamespacedName(), new PaintingEntity.Painting(paintingTexture, json.width, json.height));
} catch (IOException ignored) {
Log.warnf("Failed to load painting variant: %s", paintingVariant.getNamespacedName());
}
}
}

});
return false;
}

private static String getPaintingVariantName(Path paintingVariant) {
ArrayList<String> path = new ArrayList<>();
paintingVariant.iterator().forEachRemaining(p -> path.add(String.valueOf(p)));

String out = String.join("/", path);
if (out.toLowerCase().endsWith(".json")) {
out = out.substring(0, out.length() - ".json".length());
}
return out;
}
}

0 comments on commit 03994b7

Please sign in to comment.