Search in sources :

Example 1 with Operation

use of slimeknights.tconstruct.shared.network.GeneratePartTexturesPacket.Operation 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

ImmutableList (com.google.common.collect.ImmutableList)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 NativeImage (com.mojang.blaze3d.platform.NativeImage)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 List (java.util.List)1 Objects (java.util.Objects)1 BiConsumer (java.util.function.BiConsumer)1 Predicate (java.util.function.Predicate)1 Log4j2 (lombok.extern.log4j.Log4j2)1