@EventHandler
public void PlayerDeathBroadcast(PlayerDeathEvent event) {
String playerName = event.getEntity().getName();
String playerUUID = event.getEntity().getUniqueId().toString();
try {
BufferedImage skin = getSkin(playerUUID);
BufferedImage face = extractFace(skin);
saveImage(face, playerUUID);
String faceImageUrl = "http://lifestealsmp.org/images/" + playerUUID + ".png";
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle(playerName + " died!");
embedBuilder.setDescription("Rest in peace, " + playerName + ".");
embedBuilder.setImage(faceImageUrl);
embedBuilder.setColor(Color.RED); // Optional: setting a color for the embed side bar.
MessageEmbed embed = embedBuilder.build();
DiscordSRV.getPlugin().getMainTextChannel().sendMessage("Player death notification:").setEmbeds(embed).queue(
success -> System.out.println("Message with embed sent successfully!"),
error -> error.printStackTrace()
);
} catch (Exception e) {
e.printStackTrace();
}
}
public BufferedImage getSkin(String uuid) throws Exception {
URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream inputStream = connection.getInputStream();
java.util.Scanner scanner = new java.util.Scanner(inputStream, "UTF-8").useDelimiter("\\A");
String jsonText = scanner.hasNext() ? scanner.next() : "";
JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(jsonText).getAsJsonObject();
byte[] decodedBytes = Base64.getDecoder().decode(jsonObject.getAsJsonArray("properties").get(0).getAsJsonObject().get("value").getAsString());
String decodedString = new String(decodedBytes);
JsonObject skinData = parser.parse(decodedString).getAsJsonObject();
String directSkinUrl = skinData.get("textures").getAsJsonObject().get("SKIN").getAsJsonObject().get("url").getAsString();
URL imageUrl = new URL(directSkinUrl);
return ImageIO.read(imageUrl.openStream());
}
public BufferedImage extractFace(BufferedImage skin) {
// Extracting the 8x8 face portion from the skin.
BufferedImage face = skin.getSubimage(8, 8, 8, 8);
// Create a new 256x256 image for the scaled-up face.
BufferedImage scaledFace = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = scaledFace.createGraphics();
// Use high-quality rendering hints for scaling.
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// Draw the 8x8 face onto the new image, scaling it up to 256x256.
g.drawImage(face, 0, 0, 256, 256, null);
g.dispose();
return scaledFace;
}
public void saveImage(BufferedImage image, String uuid) throws Exception {
File outputfile = new File("/var/www/html/images/" + uuid + ".png");
ImageIO.write(image, "png", outputfile);
}