Skip to content

Instantly share code, notes, and snippets.

@ccrama
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ccrama/759879cace268a99a548 to your computer and use it in GitHub Desktop.
Save ccrama/759879cace268a99a548 to your computer and use it in GitHub Desktop.
FaceLibraryMinecraft
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.bukkit.entity.Player;
public class FaceLibrary {
//Library by @ccrama, can be found at gist.github.com/ccrama/759879cace268a99a548
public void saveFace(Player p) {
String name = p.getName();
URL url = null;
if (new File(new Main().getDataFolder() + File.separator
+ "FaceLibrary" + File.separator + name + ".png").exists() == false) {
try {
url = new URL("http://skins.minecraft.net/MinecraftSkins/"
+ name + ".png");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
Image img = null;
try {
img = ImageIO.read(url);
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedImage dest = ((BufferedImage) img).getSubimage(8, 8, 8, 8);
File outputfile = new File(new Main().getDataFolder()
+ File.separator + "FaceLibrary" + File.separator + name
+ ".png");
try {
ImageIO.write(dest, "png", outputfile);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public BufferedImage getImage(Player p) {
String name = p.getName();
File imageFile = new File(new Main().getDataFolder() + File.separator
+ "FaceLibrary" + File.separator + name + ".png");
BufferedImage img = null;
try {
img = ImageIO.read(imageFile);
return img;
} catch (IOException e) {
System.out.println("Image for " + name + " is invalid!");
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment