Search in sources :

Example 1 with PartSpriteInfo

use of slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo in project TinkersConstruct by SlimeKnights.

the class ClientGeneratePartTexturesCommand method loadPartSprites.

/**
 * Loads all part sprites file
 */
private static List<PartSpriteInfo> loadPartSprites(ResourceManager manager) {
    ImmutableList.Builder<PartSpriteInfo> builder = ImmutableList.builder();
    // each namespace loads separately
    for (String namespace : manager.getNamespaces()) {
        ResourceLocation location = new ResourceLocation(namespace, GENERATOR_PART_TEXTURES);
        if (manager.hasResource(location)) {
            // if the namespace has the file, we will start building
            try {
                // start from the top most pack and work down, lets us break the loop as soon as we find a "replace"
                List<Resource> resources = manager.getResources(location);
                for (int r = resources.size() - 1; r >= 0; r--) {
                    Resource resource = resources.get(r);
                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
                        JsonObject object = GsonHelper.parse(reader);
                        List<PartSpriteInfo> parts = JsonHelper.parseList(object, "parts", (element, name) -> {
                            JsonObject part = GsonHelper.convertToJsonObject(element, name);
                            ResourceLocation path = JsonHelper.getResourceLocation(part, "path");
                            MaterialStatsId statId = new MaterialStatsId(JsonHelper.getResourceLocation(part, "statType"));
                            return new PartSpriteInfo(path, statId);
                        });
                        builder.addAll(parts);
                        // if we find replace, don't process lower files from this namespace
                        if (GsonHelper.getAsBoolean(object, "replace", false)) {
                            break;
                        }
                    } catch (IOException ex) {
                        log.error("Failed to load modifier models from {} for pack {}", location, resource.getSourceName(), ex);
                    }
                }
            } catch (IOException ex) {
                log.error("Failed to load modifier models from {}", location, ex);
            }
        }
    }
    return builder.build();
}
Also used : InputStreamReader(java.io.InputStreamReader) ImmutableList(com.google.common.collect.ImmutableList) Resource(net.minecraft.server.packs.resources.Resource) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) PartSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo) MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) ResourceLocation(net.minecraft.resources.ResourceLocation) BufferedReader(java.io.BufferedReader)

Example 2 with PartSpriteInfo

use of slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo in project TinkersConstruct by SlimeKnights.

the class GeneratorPartTextureJsonGenerator method run.

@Override
public void run(HashCache cache) throws IOException {
    JsonObject json = new JsonObject();
    json.addProperty("replace", false);
    JsonArray parts = new JsonArray();
    for (PartSpriteInfo spriteInfo : spriteProvider.getSprites()) {
        parts.add(GSON.toJsonTree(spriteInfo));
    }
    json.add("parts", parts);
    saveThing(cache, new ResourceLocation(modId, "generator_part_textures"), json);
}
Also used : JsonArray(com.google.gson.JsonArray) ResourceLocation(net.minecraft.resources.ResourceLocation) JsonObject(com.google.gson.JsonObject) PartSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo)

Example 3 with PartSpriteInfo

use of slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo in project TinkersConstruct by SlimeKnights.

the class MaterialPartTextureGenerator method run.

@Override
public void run(HashCache cache) throws IOException {
    runCallbacks(existingFileHelper, null);
    // ensure we have parts
    List<PartSpriteInfo> parts = partProvider.getSprites();
    if (parts.isEmpty()) {
        throw new IllegalStateException(partProvider.getName() + " has no parts, must have at least one part to generate");
    }
    // for each material list, generate sprites
    for (AbstractMaterialSpriteProvider materialProvider : materialProviders) {
        Collection<MaterialSpriteInfo> materials = materialProvider.getMaterials().values();
        if (materials.isEmpty()) {
            throw new IllegalStateException(materialProvider.getName() + " has no materials, must have at least one material to generate");
        }
        // want cross product of textures
        BiConsumer<ResourceLocation, NativeImage> saver = (path, image) -> saveImage(cache, path, image);
        Predicate<ResourceLocation> shouldGenerate = path -> !spriteReader.exists(path);
        for (MaterialSpriteInfo material : materials) {
            for (PartSpriteInfo part : parts) {
                if (material.supportStatType(part.getStatType())) {
                    generateSprite(spriteReader, material, part, shouldGenerate, saver);
                }
            }
        }
    }
    spriteReader.closeAll();
    partProvider.cleanCache();
    runCallbacks(null, null);
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) PartSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo) MaterialSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractMaterialSpriteProvider.MaterialSpriteInfo) DataGenSpriteReader(slimeknights.tconstruct.library.client.data.util.DataGenSpriteReader) Predicate(java.util.function.Predicate) Collection(java.util.Collection) HashCache(net.minecraft.data.HashCache) ResourceManager(net.minecraft.server.packs.resources.ResourceManager) AbstractSpriteReader(slimeknights.tconstruct.library.client.data.util.AbstractSpriteReader) IOException(java.io.IOException) ExistingFileHelper(net.minecraftforge.common.data.ExistingFileHelper) ArrayList(java.util.ArrayList) List(java.util.List) GenericTextureGenerator(slimeknights.tconstruct.library.client.data.GenericTextureGenerator) BiConsumer(java.util.function.BiConsumer) NativeImage(com.mojang.blaze3d.platform.NativeImage) DataGenerator(net.minecraft.data.DataGenerator) Nullable(javax.annotation.Nullable) NativeImage(com.mojang.blaze3d.platform.NativeImage) MaterialSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractMaterialSpriteProvider.MaterialSpriteInfo) ResourceLocation(net.minecraft.resources.ResourceLocation) PartSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo)

Example 4 with PartSpriteInfo

use of slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo in project TinkersConstruct by SlimeKnights.

the class ClientGeneratePartTexturesCommand method generateTextures.

/**
 * Generates all textures using the resource pack list
 */
public static void generateTextures(Operation operation, String modId, String materialPath) {
    long time = System.nanoTime();
    ResourceManager manager = Minecraft.getInstance().getResourceManager();
    // the forge mod bus is annoying, but stuck using it due to the normal bus not existing at datagen time
    MaterialPartTextureGenerator.runCallbacks(null, manager);
    Player player = Minecraft.getInstance().player;
    // get the list of sprites
    List<PartSpriteInfo> partSprites = loadPartSprites(manager);
    if (partSprites.isEmpty()) {
        if (player != null) {
            player.displayClientMessage(NO_PARTS, false);
        }
        return;
    }
    // Predicate to check if a material ID is valid
    Predicate<ResourceLocation> validMaterialId = loc -> (modId.isEmpty() || modId.equals(loc.getNamespace())) && (materialPath.isEmpty() || materialPath.equals(loc.getPath()));
    // get all materials, filtered by the given parameters
    List<MaterialSpriteInfo> materialSprites = loadMaterialRenderInfoGenerators(manager, validMaterialId);
    if (materialSprites.isEmpty()) {
        if (player != null) {
            player.displayClientMessage(NO_MATERIALS, false);
        }
        return;
    }
    // prepare the output directory
    Path path = Minecraft.getInstance().getResourcePackDirectory().toPath().resolve(PACK_NAME);
    BiConsumer<ResourceLocation, NativeImage> saver = (outputPath, image) -> saveImage(path, outputPath, image);
    // create a pack.mcmeta so its a valid resource pack
    savePackMcmeta(path);
    // predicate for whether we should generate the texture
    AbstractSpriteReader spriteReader = new ResourceManagerSpriteReader(manager, MaterialPartTextureGenerator.FOLDER);
    // keep track of how many generated
    MutableInt generated = new MutableInt(0);
    Predicate<ResourceLocation> shouldGenerate;
    if (operation == Operation.ALL) {
        shouldGenerate = exists -> {
            generated.add(1);
            return true;
        };
    } else {
        shouldGenerate = loc -> {
            if (!spriteReader.exists(loc)) {
                generated.add(1);
                return true;
            }
            return false;
        };
    }
    // at this point in time we have all our materials, time to generate our sprites
    for (MaterialSpriteInfo material : materialSprites) {
        for (PartSpriteInfo part : partSprites) {
            if (material.supportStatType(part.getStatType())) {
                MaterialPartTextureGenerator.generateSprite(spriteReader, material, part, shouldGenerate, saver);
            }
        }
    }
    spriteReader.closeAll();
    // success message
    long deltaTime = System.nanoTime() - time;
    int count = generated.getValue();
    MaterialPartTextureGenerator.runCallbacks(null, null);
    log.info("Finished generating {} textures in {} ms", count, deltaTime / 1000000f);
    if (Minecraft.getInstance().player != null) {
        Minecraft.getInstance().player.displayClientMessage(new TranslatableComponent(SUCCESS_KEY, count, (deltaTime / 1000000) / 1000f, getOutputComponent(path.toFile())), false);
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) JsonObject(com.google.gson.JsonObject) Resource(net.minecraft.server.packs.resources.Resource) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ResourceManagerSpriteReader(slimeknights.tconstruct.library.client.data.util.ResourceManagerSpriteReader) PackType(net.minecraft.server.packs.PackType) Action(net.minecraft.network.chat.ClickEvent.Action) ImmutableList(com.google.common.collect.ImmutableList) Minecraft(net.minecraft.client.Minecraft) GsonHelper(net.minecraft.util.GsonHelper) BiConsumer(java.util.function.BiConsumer) NativeImage(com.mojang.blaze3d.platform.NativeImage) MaterialRenderInfoLoader(slimeknights.tconstruct.library.client.materials.MaterialRenderInfoLoader) Path(java.nio.file.Path) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) PartSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo) Component(net.minecraft.network.chat.Component) MaterialSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractMaterialSpriteProvider.MaterialSpriteInfo) JsonSyntaxException(com.google.gson.JsonSyntaxException) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) Predicate(java.util.function.Predicate) ResourceManager(net.minecraft.server.packs.resources.ResourceManager) AbstractSpriteReader(slimeknights.tconstruct.library.client.data.util.AbstractSpriteReader) Operation(slimeknights.tconstruct.shared.network.GeneratePartTexturesPacket.Operation) IOException(java.io.IOException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) MaterialGeneratorJson(slimeknights.tconstruct.library.client.materials.MaterialRenderInfoJson.MaterialGeneratorJson) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Player(net.minecraft.world.entity.player.Player) TConstruct(slimeknights.tconstruct.TConstruct) Objects(java.util.Objects) TextComponent(net.minecraft.network.chat.TextComponent) JsonHelper(slimeknights.mantle.util.JsonHelper) List(java.util.List) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) Paths(java.nio.file.Paths) MaterialRenderInfoJson(slimeknights.tconstruct.library.client.materials.MaterialRenderInfoJson) Log4j2(lombok.extern.log4j.Log4j2) MaterialPartTextureGenerator(slimeknights.tconstruct.library.client.data.material.MaterialPartTextureGenerator) BufferedReader(java.io.BufferedReader) ClickEvent(net.minecraft.network.chat.ClickEvent) InputStream(java.io.InputStream) Path(java.nio.file.Path) Player(net.minecraft.world.entity.player.Player) AbstractSpriteReader(slimeknights.tconstruct.library.client.data.util.AbstractSpriteReader) ResourceManager(net.minecraft.server.packs.resources.ResourceManager) ResourceManagerSpriteReader(slimeknights.tconstruct.library.client.data.util.ResourceManagerSpriteReader) PartSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo) NativeImage(com.mojang.blaze3d.platform.NativeImage) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) MaterialSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractMaterialSpriteProvider.MaterialSpriteInfo) ResourceLocation(net.minecraft.resources.ResourceLocation) MutableInt(org.apache.commons.lang3.mutable.MutableInt)

Aggregations

ResourceLocation (net.minecraft.resources.ResourceLocation)4 PartSpriteInfo (slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo)4 JsonObject (com.google.gson.JsonObject)3 IOException (java.io.IOException)3 ImmutableList (com.google.common.collect.ImmutableList)2 NativeImage (com.mojang.blaze3d.platform.NativeImage)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 List (java.util.List)2 BiConsumer (java.util.function.BiConsumer)2 Predicate (java.util.function.Predicate)2 Resource (net.minecraft.server.packs.resources.Resource)2 ResourceManager (net.minecraft.server.packs.resources.ResourceManager)2 MaterialSpriteInfo (slimeknights.tconstruct.library.client.data.material.AbstractMaterialSpriteProvider.MaterialSpriteInfo)2 AbstractSpriteReader (slimeknights.tconstruct.library.client.data.util.AbstractSpriteReader)2 JsonArray (com.google.gson.JsonArray)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 InputStream (java.io.InputStream)1