From 64182deff32489ece2e5061304c0927b57359bde Mon Sep 17 00:00:00 2001 From: Syriarting <88539234+hellvet3@users.noreply.github.com> Date: Wed, 3 Apr 2024 22:37:25 +0100 Subject: [PATCH 01/10] DataGen - FabricBlockModelSupplier created. Created the FabricBlockModelSupplier for data generation, makes making the [modid:block/myblock.json] model data easier. --- .../datagen/v1/FabricBlockModelSupplier.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java new file mode 100644 index 0000000000..2b8e21e2e9 --- /dev/null +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java @@ -0,0 +1,63 @@ +package net.fabricmc.fabric.api.datagen.v1; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import net.minecraft.util.Identifier; +import java.util.HashMap; +import java.util.function.Supplier; + +/* Acceptor for the BlockStateModelGenerator's blockStateCollector (datagen) */ + +public class BlockModelSupplier implements Supplier { + protected final JsonObject jsonObject; + + public BlockModelSupplier() + { + this.jsonObject = new JsonObject(); + } + + // You're expected to manually add a parent type + // TODO possibly change to be a construction parameter? + public BlockModelSupplier addParentType(String type) + { + this.jsonObject.addProperty("parent", "minecraft:block/" + type); + return this; + } + + public BlockModelSupplier addParentType(String modID, String type) + { + this.jsonObject.addProperty("parent", modID + ":block/" + type); + return this; + } + + public BlockModelSupplier addTextureData(HashMap textureMap) + { + JsonObject textureData = new JsonObject(); + for (String key : textureMap.keySet()) { + Identifier identifier = textureMap.get(key); + textureData.addProperty(key, identifier.getNamespace() + ":" + identifier.getPath()); + } + + this.jsonObject.add("textures", textureData); + return this; + } + + public BlockModelSupplier simpleTextureData(Identifier texture) + { + JsonObject textureData = new JsonObject(); + textureData.addProperty("all", texture.getNamespace() + ":" + texture.getPath()); + + this.jsonObject.add("textures", textureData); + return this; + } + + @Override + public JsonElement get() { + if (this.jsonObject.get("parent") == null) + { + // Default to cube_all if no parent is added! + this.jsonObject.addProperty("parent", "minecraft:block/cube_all"); + } + return this.jsonObject; + } +} From c8ec716d6257cbb8a6c1cb4dd58887946fa526fc Mon Sep 17 00:00:00 2001 From: helvete <88539234+hellvet3@users.noreply.github.com> Date: Wed, 3 Apr 2024 23:00:01 +0100 Subject: [PATCH 02/10] Update FabricBlockModelSupplier.java --- .../datagen/v1/FabricBlockModelSupplier.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java index 2b8e21e2e9..1570612495 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java @@ -6,30 +6,34 @@ import java.util.HashMap; import java.util.function.Supplier; -/* Acceptor for the BlockStateModelGenerator's blockStateCollector (datagen) */ +/** + * Acceptable class for BlockStateModelGenerator's blockStateCollector + * @see BlockStateModelGenerator + */ public class BlockModelSupplier implements Supplier { protected final JsonObject jsonObject; - public BlockModelSupplier() + public BlockModelSupplier(String type) { this.jsonObject = new JsonObject(); - } - - // You're expected to manually add a parent type - // TODO possibly change to be a construction parameter? - public BlockModelSupplier addParentType(String type) - { - this.jsonObject.addProperty("parent", "minecraft:block/" + type); - return this; + this.jsonObject.addProperty("parent", "minecraft:block/" + type); } - public BlockModelSupplier addParentType(String modID, String type) + /** + * Have an acceptable parameter in case of custom model parents. + */ + public BlockModelSupplier(String modID, String type) { - this.jsonObject.addProperty("parent", modID + ":block/" + type); - return this; + this.jsonObject = new JsonObject(); + this.jsonObject.addProperty("parent", modID + ":block/" + type); } + /** + * Add textures. + * + * @param textureMap The {@link FabricDataGenerator} instance + */ public BlockModelSupplier addTextureData(HashMap textureMap) { JsonObject textureData = new JsonObject(); @@ -53,11 +57,6 @@ public BlockModelSupplier simpleTextureData(Identifier texture) @Override public JsonElement get() { - if (this.jsonObject.get("parent") == null) - { - // Default to cube_all if no parent is added! - this.jsonObject.addProperty("parent", "minecraft:block/cube_all"); - } return this.jsonObject; } } From 0a4462759202b41a6f93c72b33b07434bd8773ee Mon Sep 17 00:00:00 2001 From: helvete <88539234+hellvet3@users.noreply.github.com> Date: Wed, 3 Apr 2024 23:09:02 +0100 Subject: [PATCH 03/10] Update FabricBlockModelSupplier readability. Makes the FabricBlockModelSupplier more readable than initially, and also updates the constructor, enforcing a parent type to be specified to restrict possible errors. --- .../datagen/v1/FabricBlockModelSupplier.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java index 1570612495..a2d727f254 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java @@ -7,8 +7,8 @@ import java.util.function.Supplier; /** - * Acceptable class for BlockStateModelGenerator's blockStateCollector - * @see BlockStateModelGenerator + * Acceptable class for {@link BlockStateModelGenerator}'s blockStateCollector. + * makes generating more specific block models easier for modders. */ public class BlockModelSupplier implements Supplier { @@ -30,9 +30,9 @@ public BlockModelSupplier(String modID, String type) } /** - * Add textures. + * Add HashMap textures to the model's JsonObject. * - * @param textureMap The {@link FabricDataGenerator} instance + * @param textureMap HashMap containing the data for the textures. */ public BlockModelSupplier addTextureData(HashMap textureMap) { @@ -46,7 +46,12 @@ public BlockModelSupplier addTextureData(HashMap textureMap) return this; } - public BlockModelSupplier simpleTextureData(Identifier texture) + /** + * Add a sample texture as 'all' of the textures. Can only be used on the cube_all parent. + * + * @param texture {@link Identifier} for the location of the texture. + */ + public BlockModelSupplier simpleCubeAllTextures(Identifier texture) { JsonObject textureData = new JsonObject(); textureData.addProperty("all", texture.getNamespace() + ":" + texture.getPath()); @@ -55,6 +60,11 @@ public BlockModelSupplier simpleTextureData(Identifier texture) return this; } + /** + * Returns the {@link JsonObject} for the Model. + * + * @return the supplier's JsonObject + */ @Override public JsonElement get() { return this.jsonObject; From 12062185282f4fb8428a183b7e54e8be69d1b5d9 Mon Sep 17 00:00:00 2001 From: helvete <88539234+hellvet3@users.noreply.github.com> Date: Wed, 3 Apr 2024 23:35:49 +0100 Subject: [PATCH 04/10] Update FabricModelSupplier's in-code class. For some reason it wasn't referred to as FabricBlockModelSupplier but BlockModelSupplier in the class, so changed it to FabricBlockModelSupplier. --- .../api/datagen/v1/FabricBlockModelSupplier.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java index a2d727f254..7862b4e1c0 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java @@ -11,10 +11,10 @@ * makes generating more specific block models easier for modders. */ -public class BlockModelSupplier implements Supplier { +public class FabricBlockModelSupplier implements Supplier { protected final JsonObject jsonObject; - public BlockModelSupplier(String type) + public FabricBlockModelSupplier(String type) { this.jsonObject = new JsonObject(); this.jsonObject.addProperty("parent", "minecraft:block/" + type); @@ -23,7 +23,7 @@ public BlockModelSupplier(String type) /** * Have an acceptable parameter in case of custom model parents. */ - public BlockModelSupplier(String modID, String type) + public FabricBlockModelSupplier(String modID, String type) { this.jsonObject = new JsonObject(); this.jsonObject.addProperty("parent", modID + ":block/" + type); @@ -34,7 +34,7 @@ public BlockModelSupplier(String modID, String type) * * @param textureMap HashMap containing the data for the textures. */ - public BlockModelSupplier addTextureData(HashMap textureMap) + public FabricBlockModelSupplier addTextureData(HashMap textureMap) { JsonObject textureData = new JsonObject(); for (String key : textureMap.keySet()) { @@ -51,7 +51,7 @@ public BlockModelSupplier addTextureData(HashMap textureMap) * * @param texture {@link Identifier} for the location of the texture. */ - public BlockModelSupplier simpleCubeAllTextures(Identifier texture) + public FabricBlockModelSupplier simpleCubeAllTextures(Identifier texture) { JsonObject textureData = new JsonObject(); textureData.addProperty("all", texture.getNamespace() + ":" + texture.getPath()); From 1d4ada8b4d052b143141950ae9068cc707d8e709 Mon Sep 17 00:00:00 2001 From: helvete <88539234+hellvet3@users.noreply.github.com> Date: Wed, 3 Apr 2024 23:42:16 +0100 Subject: [PATCH 05/10] Another readability update Makes the code more readable (in general, as well as for people in-editor) and inline with the other fabric code. --- .../datagen/v1/FabricBlockModelSupplier.java | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java index 7862b4e1c0..739f22942c 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java @@ -7,33 +7,41 @@ import java.util.function.Supplier; /** - * Acceptable class for {@link BlockStateModelGenerator}'s blockStateCollector. + * Acceptable class for {@link net.minecraft.data.client.BlockStateModelGenerator}'s blockStateCollector. * makes generating more specific block models easier for modders. */ public class FabricBlockModelSupplier implements Supplier { protected final JsonObject jsonObject; + /** + * Constructor for vanilla parent types. + * + * @param type The parent type for the model. + */ public FabricBlockModelSupplier(String type) { this.jsonObject = new JsonObject(); - this.jsonObject.addProperty("parent", "minecraft:block/" + type); + this.jsonObject.addProperty("parent", "minecraft:block/" + type); } - /** - * Have an acceptable parameter in case of custom model parents. - */ - public FabricBlockModelSupplier(String modID, String type) + /** + * Have an acceptable parameter in case of custom model parents. + * + * @param modID The modID as the prefix to the parent type in the Identifier. + * @param type The parent type for the model. + */ + public FabricBlockModelSupplier(String modID, String type) { this.jsonObject = new JsonObject(); - this.jsonObject.addProperty("parent", modID + ":block/" + type); + this.jsonObject.addProperty("parent", modID + ":block/" + type); } - /** - * Add HashMap textures to the model's JsonObject. - * - * @param textureMap HashMap containing the data for the textures. - */ + /** + * Add HashMap textures to the model's JsonObject. + * + * @param textureMap {@link HashMap} containing the data for the textures. + */ public FabricBlockModelSupplier addTextureData(HashMap textureMap) { JsonObject textureData = new JsonObject(); @@ -46,11 +54,11 @@ public FabricBlockModelSupplier addTextureData(HashMap textu return this; } - /** - * Add a sample texture as 'all' of the textures. Can only be used on the cube_all parent. - * - * @param texture {@link Identifier} for the location of the texture. - */ + /** + * Add a sample texture as 'all' of the textures. Can only be used on the cube_all parent. + * + * @param texture {@link Identifier} for the location of the texture. + */ public FabricBlockModelSupplier simpleCubeAllTextures(Identifier texture) { JsonObject textureData = new JsonObject(); @@ -60,11 +68,11 @@ public FabricBlockModelSupplier simpleCubeAllTextures(Identifier texture) return this; } - /** - * Returns the {@link JsonObject} for the Model. + /** + * Returns the {@link JsonObject} for the Model. * - * @return the supplier's JsonObject - */ + * @return the supplier's {@link JsonObject} + */ @Override public JsonElement get() { return this.jsonObject; From 237794b16d88dfb91b5e82ef579ecc9ea8d01f3f Mon Sep 17 00:00:00 2001 From: helvete <88539234+hellvet3@users.noreply.github.com> Date: Thu, 4 Apr 2024 00:37:24 +0100 Subject: [PATCH 06/10] Update for loop to use .entrySet() instead of .keySet() --- .../fabric/api/datagen/v1/FabricBlockModelSupplier.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java index 739f22942c..67fe5f97a1 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java @@ -5,6 +5,7 @@ import net.minecraft.util.Identifier; import java.util.HashMap; import java.util.function.Supplier; +import java.util.Map; /** * Acceptable class for {@link net.minecraft.data.client.BlockStateModelGenerator}'s blockStateCollector. @@ -45,8 +46,9 @@ public FabricBlockModelSupplier(String modID, String type) public FabricBlockModelSupplier addTextureData(HashMap textureMap) { JsonObject textureData = new JsonObject(); - for (String key : textureMap.keySet()) { - Identifier identifier = textureMap.get(key); + for (Map.Entry entry : textureMap.entrySet()) { + String key = entry.getKey(); + Identifier identifier = entry.getValue(); textureData.addProperty(key, identifier.getNamespace() + ":" + identifier.getPath()); } From e326541d9ae13bdcb632e23d63ca830c4a71d8f3 Mon Sep 17 00:00:00 2001 From: helvete <88539234+hellvet3@users.noreply.github.com> Date: Thu, 4 Apr 2024 00:42:18 +0100 Subject: [PATCH 07/10] Update FabricBlockModelSupplier.java --- .../fabric/api/datagen/v1/FabricBlockModelSupplier.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java index 67fe5f97a1..9de2346db3 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java @@ -46,7 +46,7 @@ public FabricBlockModelSupplier(String modID, String type) public FabricBlockModelSupplier addTextureData(HashMap textureMap) { JsonObject textureData = new JsonObject(); - for (Map.Entry entry : textureMap.entrySet()) { + for (Map.Entry entry : textureMap.entrySet()) { String key = entry.getKey(); Identifier identifier = entry.getValue(); textureData.addProperty(key, identifier.getNamespace() + ":" + identifier.getPath()); From eaea3fe3d5fc59c4b3f544247dab5e434fc12341 Mon Sep 17 00:00:00 2001 From: Animetrical <88539234+Animetrical@users.noreply.github.com> Date: Thu, 4 Apr 2024 23:01:15 +0100 Subject: [PATCH 08/10] Added FabricTextureMap in order to make it easier to create without double bracket indentation, or outer variables. Also easier to work with due to the use of Tuples as variables for FabricTextureMap. FabricBlockModelSupplier now requires a FabricTextureMap parameter when .addTextureData() is called. A provided example of a cube_all block model has also been provided in DataGeneratorTestEntrypoint --- .../datagen/v1/FabricBlockModelSupplier.java | 7 +-- .../api/datagen/v1/FabricTextureMap.java | 53 +++++++++++++++++++ .../datagen/DataGeneratorTestEntrypoint.java | 25 ++++++++- 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java index 9de2346db3..26ab7417e8 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricBlockModelSupplier.java @@ -8,7 +8,7 @@ import java.util.Map; /** - * Acceptable class for {@link net.minecraft.data.client.BlockStateModelGenerator}'s blockStateCollector. + * Acceptable class for {@link net.minecraft.data.client.BlockStateModelGenerator}'s modelCollector. * makes generating more specific block models easier for modders. */ @@ -41,10 +41,11 @@ public FabricBlockModelSupplier(String modID, String type) /** * Add HashMap textures to the model's JsonObject. * - * @param textureMap {@link HashMap} containing the data for the textures. + * @param fabricTextureMap {@link FabricTextureMap} containing the data for the textures. */ - public FabricBlockModelSupplier addTextureData(HashMap textureMap) + public FabricBlockModelSupplier addTextureData(FabricTextureMap fabricTextureMap) { + HashMap textureMap = fabricTextureMap.get(); JsonObject textureData = new JsonObject(); for (Map.Entry entry : textureMap.entrySet()) { String key = entry.getKey(); diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java new file mode 100644 index 0000000000..9b2198dbc5 --- /dev/null +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java @@ -0,0 +1,53 @@ +package net.fabricmc.fabric.api.datagen.v1; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + +import net.minecraft.util.Identifier; + +/** + * Constructor class for making Maps for BlockModel textures. + * {@see FabricBlockModelSupplier} + */ + +public class FabricTextureMap { + private final HashMap map; + private final List bufferNames; + + /** + * Constructor for the FabricTextureMap. Sets the private variables for later accession. + * + * @param textureNames Different texture names. + */ + public FabricTextureMap(String... textureNames) { + this.map = new HashMap<>(); + this.bufferNames = Arrays.stream(textureNames).toList(); + } + + /** + * Link the texture names to the locations of their textures. + * + * @param textureLocations Identifiers for the location of each texture. + */ + public FabricTextureMap set(Identifier... textureLocations) { + List textureList = Arrays.stream(textureLocations).toList(); + if (textureList.size() != this.bufferNames.size()) { + throw new IllegalStateException("Provided texture locations should be same length as texture names!"); + } + for (int i = 0; i < bufferNames.size(); i++) { + map.put(bufferNames.get(i), textureList.get(i)); + } + + return this; + } + + /** + * Return the resulting {@link HashMap}. + * + * @return this HashMap. + */ + public HashMap get() { + return this.map; + } +} diff --git a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java index d42da15181..07b1664e4c 100644 --- a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java +++ b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.nio.file.Path; +import java.util.HashMap; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.function.BiConsumer; @@ -36,6 +37,11 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; + +import net.fabricmc.fabric.api.datagen.v1.FabricBlockModelSupplier; + +import net.fabricmc.fabric.api.datagen.v1.FabricTextureMap; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -294,11 +300,28 @@ private TestModelProvider(FabricDataOutput output) { @Override public void generateBlockStateModels(BlockStateModelGenerator blockStateModelGenerator) { - blockStateModelGenerator.registerSimpleCubeAll(SIMPLE_BLOCK); + // blockStateModelGenerator.registerSimpleCubeAll(SIMPLE_BLOCK); blockStateModelGenerator.registerSimpleCubeAll(BLOCK_WITHOUT_ITEM); blockStateModelGenerator.registerSimpleCubeAll(BLOCK_WITHOUT_LOOT_TABLE); blockStateModelGenerator.registerSimpleCubeAll(BLOCK_WITH_VANILLA_LOOT_TABLE); blockStateModelGenerator.registerSimpleCubeAll(BLOCK_THAT_DROPS_NOTHING); + + // registerSimpleCubeAll() -> FabricBlockModelSupplier::new + blockStateModelGenerator.modelCollector.accept(new Identifier(MOD_ID, "simple_block"), + () -> new FabricBlockModelSupplier("cube_all") + .addTextureData(new FabricTextureMap("all") + .set(new Identifier(MOD_ID, "block/simple_block"))) + .get()); + + /* In reality, for cube_all textures you'd want to try and do a .registerSimpleCubeAll() from + the blockStateModelGenerator. You could also do something like this if you needed to access + it using the FabricBlockModelSupplier: + + blockStateModelGenerator.modelCollector.accept(new Identifier(MOD_ID, "simple_block"), + () -> new FabricBlockModelSupplier("cube_all") + .simpleCubeAllTextures(new Identifier(MOD_ID, "block/simple_block")) + .get()); + */ } @Override From 1d30011f309825c355c12ee06868c79d51d121e4 Mon Sep 17 00:00:00 2001 From: Animetrical <88539234+Animetrical@users.noreply.github.com> Date: Thu, 4 Apr 2024 23:17:32 +0100 Subject: [PATCH 09/10] Makes IllegalStateExceptions more readable for the user. --- .../fabricmc/fabric/api/datagen/v1/FabricTextureMap.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java index 9b2198dbc5..28b2bdd6c0 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java @@ -32,8 +32,12 @@ public FabricTextureMap(String... textureNames) { */ public FabricTextureMap set(Identifier... textureLocations) { List textureList = Arrays.stream(textureLocations).toList(); - if (textureList.size() != this.bufferNames.size()) { - throw new IllegalStateException("Provided texture locations should be same length as texture names!"); + int difference = textureList.size() - this.bufferNames.size(); + if (difference > 0) { + throw new IllegalStateException(String.format("You need to provide %s more texture names to allocate to texture locations!", difference)); + } + if (difference < 0) { + throw new IllegalStateException(String.format("You need to provide %s more texture locations to link to texture names!", difference)); } for (int i = 0; i < bufferNames.size(); i++) { map.put(bufferNames.get(i), textureList.get(i)); From efdf561832410a072354c9ca698535af327f8f72 Mon Sep 17 00:00:00 2001 From: Animetrical <88539234+Animetrical@users.noreply.github.com> Date: Thu, 4 Apr 2024 23:36:32 +0100 Subject: [PATCH 10/10] Fix negative numbers being provided in the Errors for FabricTextureMap Add SimpleBlockStateSupplier for no-variant based blockstates (because that apparently isn't something built-in already??) --- .../api/datagen/v1/FabricTextureMap.java | 3 +- .../datagen/v1/SimpleBlockStateSupplier.java | 57 +++++++++++++++++++ .../datagen/DataGeneratorTestEntrypoint.java | 3 + 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/SimpleBlockStateSupplier.java diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java index 28b2bdd6c0..4f467b6ee2 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/FabricTextureMap.java @@ -37,8 +37,9 @@ public FabricTextureMap set(Identifier... textureLocations) { throw new IllegalStateException(String.format("You need to provide %s more texture names to allocate to texture locations!", difference)); } if (difference < 0) { - throw new IllegalStateException(String.format("You need to provide %s more texture locations to link to texture names!", difference)); + throw new IllegalStateException(String.format("You need to provide %s more texture locations to link to texture names!", -difference)); } + for (int i = 0; i < bufferNames.size(); i++) { map.put(bufferNames.get(i), textureList.get(i)); } diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/SimpleBlockStateSupplier.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/SimpleBlockStateSupplier.java new file mode 100644 index 0000000000..e5cab198e4 --- /dev/null +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/SimpleBlockStateSupplier.java @@ -0,0 +1,57 @@ +package net.fabricmc.fabric.api.datagen.v1; + +import com.google.gson.JsonElement; + +import com.google.gson.JsonObject; + +import net.minecraft.block.Block; +import net.minecraft.data.client.BlockStateSupplier; +import net.minecraft.util.Identifier; + +/** + * Basic constructor class for generating non-variant aligned blockstates, + * since there is no built-in class for non-variant blockstates. + */ +public class SimpleBlockStateSupplier implements BlockStateSupplier { + private final Block block; + private final JsonObject jsonObject; + + /** + * Constructor for the SimpleBlockStateSupplier. Generates the entire jsonObject. + * + * @param block Block to be applied to. + * @param modelLocation Location of the block's default model. + */ + public SimpleBlockStateSupplier(Block block, Identifier modelLocation) + { + this.block=block; + this.jsonObject=new JsonObject(); + + JsonObject variants = new JsonObject(); + JsonObject defaultVariant = new JsonObject(); + defaultVariant.addProperty("model", modelLocation.getNamespace() + ":" + modelLocation.getPath()); + variants.add("", defaultVariant); + + this.jsonObject.add("variants", variants); + } + + /** + * Returns the block this is being applied to. + * + * @return this block + */ + @Override + public Block getBlock() { + return this.block; + } + + /** + * Returns the resulting {@link JsonElement} from the class. + * + * @return this jsonObject + */ + @Override + public JsonElement get() { + return this.jsonObject; + } +} diff --git a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java index 07b1664e4c..c4d52603ef 100644 --- a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java +++ b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java @@ -42,6 +42,8 @@ import net.fabricmc.fabric.api.datagen.v1.FabricTextureMap; +import net.fabricmc.fabric.api.datagen.v1.SimpleBlockStateSupplier; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -307,6 +309,7 @@ public void generateBlockStateModels(BlockStateModelGenerator blockStateModelGen blockStateModelGenerator.registerSimpleCubeAll(BLOCK_THAT_DROPS_NOTHING); // registerSimpleCubeAll() -> FabricBlockModelSupplier::new + blockStateModelGenerator.blockStateCollector.accept(new SimpleBlockStateSupplier(SIMPLE_BLOCK, new Identifier(MOD_ID, "simple_block"))); blockStateModelGenerator.modelCollector.accept(new Identifier(MOD_ID, "simple_block"), () -> new FabricBlockModelSupplier("cube_all") .addTextureData(new FabricTextureMap("all")