Skip to content

Instantly share code, notes, and snippets.

@ClementGre
Last active November 25, 2020 01:09
Show Gist options
  • Save ClementGre/f1612dbc904c5e1823eff5172b37baa2 to your computer and use it in GitHub Desktop.
Save ClementGre/f1612dbc904c5e1823eff5172b37baa2 to your computer and use it in GitHub Desktop.
Convertisseur de ressource pack Minecraft en différentes résolutions. Il permet par exemple de convertir un pack 16×16 en 4×4 ou même 2×2 avec des paramètres personnalisés (voir constantes).
package fr.themsou.pixelpackmaker;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
public class Main {
public static final String packPath = "C:\\Users\\utilisateur\\Downloads\\assets";
public static final HashMap<String, Integer> folderSizes = new HashMap<>();
public static final int defaultPackSize = 16;
public static final int otherSize = 4; // /16
public static final int guiSize = 8; // /16
public static final int fontFinalWidth = 12; // /16
public static final int skyFinalWidth = 16; // /16
public static final int armorFinalWidth = 4; // /16
public static final int itemsFinalWidth = 4; // /16
public static final int blocksFinalWidth = 4; // /16
public static void main(String[] args){
folderSizes.put("font", 16 * fontFinalWidth);
folderSizes.put("sky", 6 * skyFinalWidth);
folderSizes.put("armor", 8 * armorFinalWidth);
folderSizes.put("items", itemsFinalWidth);
folderSizes.put("blocks", blocksFinalWidth);
System.out.println("--> SETTINGS :");
System.out.println();
System.out.println("- packSize = " + defaultPackSize);
System.out.println();
System.out.println("- packSize = " + otherSize);
System.out.println("- iconsSize = " + guiSize);
System.out.println();
for(Map.Entry<String, Integer> folderSize : folderSizes.entrySet()){
System.out.println("- " + folderSize.getKey() + "FinalImageWidth = " + folderSize.getValue());
}
System.out.println();
System.out.println("--> CONVERTING...");
System.out.println();
convertDir(new File(packPath));
System.out.println();
System.out.println("--> SUCCESSFULLY FINISHED!");
}
public static void convertDir(File dir){
System.out.println("- Converting directory " + dir.getName() + "...");
for(File subFile : dir.listFiles()){
if(subFile.isDirectory()){
convertDir(subFile);
}else{
if(folderSizes.containsKey(dir.getName())){
convertFile(subFile, true, folderSizes.get(dir.getName()));
}else if("colormap".equals(dir.getName())) {
}else if("gui".equals(dir.getName())) {
convertFile(subFile, false, guiSize);
}else{
convertFile(subFile, false, otherSize);
}
}
}
}
public static void convertFile(File file, boolean folder, int arg){
String[] splitedFileName = file.getName().split(Pattern.quote("."));
if(!splitedFileName[splitedFileName.length -1].equals("png")){
return;
}
try{
BufferedImage image = ImageIO.read(file);
int width;
int height;
if(folder){
width = arg;
height = (int) (image.getHeight() / ((double) image.getWidth()) * arg);
}else{
width = (int) (image.getWidth() / ((double) defaultPackSize) * arg);
height = (int) (image.getHeight() / ((double) defaultPackSize) * arg);
}
if(width == 0) width = 1;
if(height == 0) height = 1;
BufferedImage outputImage = new BufferedImage(width, height, image.getType());
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(image, 0, 0, outputImage.getWidth(), outputImage.getHeight(), null);
g2d.dispose();
ImageIO.write(outputImage, "png", file);
}catch(IOException ex){ ex.printStackTrace(); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment