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();
}
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);
}
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);
}
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);
}
}
Aggregations