diff --git a/buildSrc/src/main/java/de/griefed/generation/blocks/TextureScaler.java b/buildSrc/src/main/java/de/griefed/generation/blocks/TextureScaler.java new file mode 100644 index 0000000..7e232ad --- /dev/null +++ b/buildSrc/src/main/java/de/griefed/generation/blocks/TextureScaler.java @@ -0,0 +1,24 @@ +package de.griefed.generation.blocks; + +import java.awt.*; +import java.awt.image.BufferedImage; + +public class TextureScaler { + public static BufferedImage getScaledInstance(BufferedImage sourceImage) { + Dimension imgSize = new Dimension(sourceImage.getWidth(), sourceImage.getHeight()); + double widthRatio; + Image scaledImage; + BufferedImage output; + int newWidth = 16; + int newHeight; + widthRatio = (double) newWidth / imgSize.width; + newHeight = (int) (sourceImage.getHeight() * widthRatio); + while (newHeight % 16 != 0) { + newHeight++; + } + scaledImage = sourceImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH); + output = new BufferedImage(scaledImage.getWidth(null), scaledImage.getHeight(null), BufferedImage.TYPE_INT_ARGB); + output.getGraphics().drawImage(scaledImage,0, 0, null); + return output; + } +} diff --git a/buildSrc/src/main/java/de/griefed/generation/generator/CodeGenerator.java b/buildSrc/src/main/java/de/griefed/generation/generator/CodeGenerator.java index ec1fdb6..63baad0 100644 --- a/buildSrc/src/main/java/de/griefed/generation/generator/CodeGenerator.java +++ b/buildSrc/src/main/java/de/griefed/generation/generator/CodeGenerator.java @@ -4,8 +4,11 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import de.griefed.generation.blocks.BlockDefinition; import de.griefed.generation.blocks.BlockDefinitionParser; +import de.griefed.generation.blocks.TextureScaler; import org.gradle.api.Project; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.io.*; import java.nio.file.Files; import java.nio.file.StandardCopyOption; @@ -212,6 +215,8 @@ public void createBlockFiles() throws IOException { File itemBlockModel; File blockTexture; File textureSource; + File textureMCMeta; + BufferedImage texture; for (BlockDefinition block : blockDefinitionParser.getBlocks()) { //blockstate blockstate = new File(blockstatesDir, blockstatesTemp.replace("BLOCKID", block.getId())); @@ -225,6 +230,21 @@ public void createBlockFiles() throws IOException { //block texture blockTexture = new File(blockTexturesDir, blockTextureTemp.replace("BLOCKID", block.getId())); textureSource = new File(blockDefinitionParser.getAssetsDirectory(), block.getId() + ".png"); + texture = ImageIO.read(textureSource); + if (texture.getWidth() > 16 && texture.getHeight() > 16) { + texture = TextureScaler.getScaledInstance(texture); + ImageIO.write(texture,"png",textureSource); + } + if (texture.getWidth() < texture.getHeight()) { + textureMCMeta = new File(blockTexture.getParent(), blockTexture.getName() + ".mcmeta"); + textureMCMeta.createNewFile(); + writeToFile(textureMCMeta, + """ + { + "animation": {} + } + """); + } Files.copy(textureSource.toPath(), blockTexture.toPath(), StandardCopyOption.REPLACE_EXISTING); } }