9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00

改进默认atlas读取

This commit is contained in:
XiaoMoMi
2025-06-29 04:58:00 +08:00
parent e980d9776c
commit 4a2d4c76c7
5 changed files with 127 additions and 49 deletions

View File

@@ -0,0 +1,58 @@
{
"sources": [
{
"type": "minecraft:directory",
"prefix": "block/",
"source": "block"
},
{
"type": "minecraft:directory",
"prefix": "item/",
"source": "item"
},
{
"type": "minecraft:directory",
"prefix": "entity/conduit/",
"source": "entity/conduit"
},
{
"type": "minecraft:single",
"resource": "minecraft:entity/bell/bell_body"
},
{
"type": "minecraft:single",
"resource": "minecraft:entity/decorated_pot/decorated_pot_side"
},
{
"type": "minecraft:single",
"resource": "minecraft:entity/enchanting_table_book"
},
{
"type": "minecraft:paletted_permutations",
"palette_key": "minecraft:trims/color_palettes/trim_palette",
"permutations": {
"amethyst": "minecraft:trims/color_palettes/amethyst",
"copper": "minecraft:trims/color_palettes/copper",
"diamond": "minecraft:trims/color_palettes/diamond",
"diamond_darker": "minecraft:trims/color_palettes/diamond_darker",
"emerald": "minecraft:trims/color_palettes/emerald",
"gold": "minecraft:trims/color_palettes/gold",
"gold_darker": "minecraft:trims/color_palettes/gold_darker",
"iron": "minecraft:trims/color_palettes/iron",
"iron_darker": "minecraft:trims/color_palettes/iron_darker",
"lapis": "minecraft:trims/color_palettes/lapis",
"netherite": "minecraft:trims/color_palettes/netherite",
"netherite_darker": "minecraft:trims/color_palettes/netherite_darker",
"quartz": "minecraft:trims/color_palettes/quartz",
"redstone": "minecraft:trims/color_palettes/redstone",
"resin": "minecraft:trims/color_palettes/resin"
},
"textures": [
"minecraft:trims/items/helmet_trim",
"minecraft:trims/items/chestplate_trim",
"minecraft:trims/items/leggings_trim",
"minecraft:trims/items/boots_trim"
]
}
]
}

View File

@@ -69,7 +69,7 @@ items:
display-transform: NONE
billboard: FIXED
translation: 0,0.5,0
rotation: -90
rotation: 90
hitboxes:
- position: 0,0,0
type: interaction

View File

@@ -83,6 +83,7 @@ public abstract class AbstractPackManager implements PackManager {
private final BiConsumer<Path, Path> eventDispatcher;
private final Map<String, Pack> loadedPacks = new HashMap<>();
private final Map<String, ConfigParser> sectionParsers = new HashMap<>();
private final JsonObject vanillaAtlas;
private Map<Path, CachedConfigFile> cachedConfigFiles = Collections.emptyMap();
private Map<Path, CachedAssetFile> cachedAssetFiles = Collections.emptyMap();
protected BiConsumer<Path, Path> zipGenerator;
@@ -102,6 +103,11 @@ public abstract class AbstractPackManager implements PackManager {
this.plugin.logger().warn("Failed to create default configs folder", e);
}
this.initInternalData();
try (InputStream inputStream = plugin.resourceStream("internal/atlases/blocks.json")) {
this.vanillaAtlas = JsonParser.parseReader(new InputStreamReader(inputStream)).getAsJsonObject();
} catch (IOException e) {
throw new RuntimeException("Failed to read internal/atlases/blocks.json", e);
}
}
private void initInternalData() {
@@ -725,6 +731,61 @@ public abstract class AbstractPackManager implements PackManager {
}
}
private void processAtlas(JsonObject atlasJsonObject, BiConsumer<String, String> directory, Consumer<Key> unstitch, Consumer<Key> included) {
JsonArray sources = atlasJsonObject.getAsJsonArray("sources");
if (sources != null) {
for (JsonElement source : sources) {
if (!(source instanceof JsonObject sourceJson)) continue;
String type = Optional.ofNullable(sourceJson.get("type")).map(JsonElement::getAsString).orElse(null);
if (type == null) continue;
switch (type) {
case "directory", "minecraft:directory" -> {
JsonElement source0 = sourceJson.get("source");
JsonElement prefix = sourceJson.get("prefix");
if (prefix == null || source0 == null) continue;
directory.accept(prefix.getAsString(), source0.getAsString() + "/");
}
case "single", "minecraft:single" -> {
JsonElement resource = sourceJson.get("resource");
if (resource == null) continue;
included.accept(Key.of(resource.getAsString()));
}
case "unstitch", "minecraft:unstitch" -> {
JsonElement resource = sourceJson.get("resource");
if (resource == null) continue;
included.accept(Key.of(resource.getAsString()));
JsonArray regions = sourceJson.getAsJsonArray("regions");
if (regions != null) {
for (JsonElement region : regions) {
if (!(region instanceof JsonObject regionJson)) continue;
JsonElement sprite = regionJson.get("sprite");
if (sprite == null) continue;
unstitch.accept(Key.of(sprite.getAsString()));
}
}
}
case "filter", "minecraft:filter" -> {
// todo filter
}
case "paletted_permutations", "minecraft:paletted_permutations" -> {
JsonArray textures = sourceJson.getAsJsonArray("textures");
if (textures == null) continue;
JsonObject permutationsJson = sourceJson.getAsJsonObject("permutations");
if (permutationsJson == null) continue;
String separator = sourceJson.has("separator") ? sourceJson.get("separator").getAsString() : "_";
List<String> permutations = new ArrayList<>(permutationsJson.keySet());
for (JsonElement texture : textures) {
if (!(texture instanceof JsonPrimitive texturePath)) continue;
for (String permutation : permutations) {
included.accept(Key.of(texturePath.getAsString() + separator + permutation));
}
}
}
}
}
}
}
@SuppressWarnings("DuplicatedCode")
private void validateResourcePack(Path path) {
List<Path> rootPaths;
@@ -741,8 +802,7 @@ public abstract class AbstractPackManager implements PackManager {
Set<Key> texturesInAtlas = new HashSet<>();
Set<Key> unstitchTextures = new HashSet<>();
Map<String, String> directoryMapper = new HashMap<>();
directoryMapper.put("item/", "item/");
directoryMapper.put("block/", "block/");
processAtlas(this.vanillaAtlas, directoryMapper::put, unstitchTextures::add, texturesInAtlas::add);
for (Path rootPath : rootPaths) {
Path assetsPath = rootPath.resolve("assets");
@@ -760,47 +820,7 @@ public abstract class AbstractPackManager implements PackManager {
if (Files.exists(atlasesFile)) {
try {
JsonObject atlasJsonObject = GsonHelper.readJsonFile(atlasesFile).getAsJsonObject();
JsonArray sources = atlasJsonObject.getAsJsonArray("sources");
if (sources != null) {
for (JsonElement source : sources) {
if (!(source instanceof JsonObject sourceJson)) continue;
String type = Optional.ofNullable(sourceJson.get("type")).map(JsonElement::getAsString).orElse(null);
if (type == null) continue;
switch (type) {
case "directory", "minecraft:directory" -> {
JsonElement source0 = sourceJson.get("source");
JsonElement prefix = sourceJson.get("prefix");
if (prefix == null || source0 == null) continue;
directoryMapper.put(prefix.getAsString(), source0.getAsString() + "/");
}
case "single", "minecraft:single" -> {
JsonElement resource = sourceJson.get("resource");
if (resource == null) continue;
texturesInAtlas.add(Key.of(resource.getAsString()));
}
case "unstitch", "minecraft:unstitch" -> {
JsonElement resource = sourceJson.get("resource");
if (resource == null) continue;
texturesInAtlas.add(Key.of(resource.getAsString()));
JsonArray regions = sourceJson.getAsJsonArray("regions");
if (regions != null) {
for (JsonElement region : regions) {
if (!(region instanceof JsonObject regionJson)) continue;
JsonElement sprite = regionJson.get("sprite");
if (sprite == null) continue;
unstitchTextures.add(Key.of(sprite.getAsString()));
}
}
}
case "filter", "minecraft:filter" -> {
// todo filter
}
case "paletted_permutations", "minecraft:paletted_permutations" -> {
// todo paletted_permutations
}
}
}
}
processAtlas(atlasJsonObject, directoryMapper::put, unstitchTextures::add, texturesInAtlas::add);
} catch (IOException | JsonParseException e) {
TranslationManager.instance().log("warning.config.resource_pack.generation.malformatted_json", atlasesFile.toAbsolutePath().toString());
}

View File

@@ -93,9 +93,9 @@ public class MiscUtils {
if (split.length == 4) {
return new Quaternionf(Float.parseFloat(split[0]), Float.parseFloat(split[1]), Float.parseFloat(split[2]), Float.parseFloat(split[3]));
} else if (split.length == 3) {
return QuaternionUtils.toQuaternionf(Math.toRadians(Float.parseFloat(split[2])), Float.parseFloat(split[1]), Math.toRadians(-Float.parseFloat(split[0])));
return QuaternionUtils.toQuaternionf(Math.toRadians(-Float.parseFloat(split[2])), Math.toRadians(-Float.parseFloat(split[1])), Math.toRadians(-Float.parseFloat(split[0])));
} else if (split.length == 1) {
return QuaternionUtils.toQuaternionf(0, Math.toRadians(Float.parseFloat(split[0])), 0);
return QuaternionUtils.toQuaternionf(0, Math.toRadians(-Float.parseFloat(split[0])), 0);
} else {
throw new LocalizedResourceConfigException("warning.config.type.quaternionf", stringFormat, option);
}

View File

@@ -2,9 +2,9 @@ org.gradle.jvmargs=-Xmx1G
# Project settings
# Rule: [major update].[feature update].[bug fix]
project_version=0.0.58.4
config_version=39
lang_version=20
project_version=0.0.58.5
config_version=40
lang_version=21
project_group=net.momirealms
latest_supported_version=1.21.6