-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for loading dynamic paintings from resource packs. (#1728)
* Add support for loading dynamic paintings from resource packs. * Refactor iterating over datapack registry entries to eliminate duplicate code.
- Loading branch information
Showing
5 changed files
with
198 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
chunky/src/java/se/llbit/chunky/resources/DataPackUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
chunky/src/java/se/llbit/chunky/resources/ResourcePackPaintingLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package se.llbit.chunky.resources; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import se.llbit.chunky.entity.PaintingEntity; | ||
import se.llbit.chunky.resources.texturepack.SimpleTexture; | ||
import se.llbit.log.Log; | ||
import se.llbit.util.Pair; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.nio.file.Files; | ||
|
||
public class ResourcePackPaintingLoader implements ResourcePackLoader.PackLoader { | ||
|
||
protected static final Gson GSON = new GsonBuilder() | ||
.disableJdkUnsafe() | ||
.setLenient() | ||
.create(); | ||
|
||
protected static class PaintingVariantJson { | ||
public String asset_id; | ||
public int width; | ||
public int height; | ||
|
||
public Pair<String, String> getAsset() { | ||
String[] parts = asset_id.split(":"); | ||
return new Pair<>(parts[0], parts[1]); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean load(LayeredResourcePacks resourcePacks) { | ||
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); | ||
} | ||
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; | ||
} | ||
} |