Skip to content

Commit

Permalink
2024/9/25 更新 修复了对 资源路径/图片文本/图片子路径/脚本语句 中的{extra}的解析的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
haishen668 committed Sep 25, 2024
1 parent 3303bd7 commit c262f9b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author haishen668
*/
public class CustomImage {
public class CustomImage implements Cloneable, Serializable{
// 图片的唯一标识符
public final String id;

Expand Down Expand Up @@ -74,6 +71,32 @@ public static BufferedImage downloadImage(String link) {
}
}


// 深度克隆方法
@Override
public CustomImage clone() {
return this.deepClone();
}

public CustomImage deepClone() {
try {
// Serialize the object to a byte array
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
ObjectOutputStream outStream = new ObjectOutputStream(byteOutStream);
outStream.writeObject(this);
outStream.flush();

// Deserialize the byte array back into an object
ByteArrayInputStream byteInStream = new ByteArrayInputStream(byteOutStream.toByteArray());
ObjectInputStream inStream = new ObjectInputStream(byteInStream);
return (CustomImage) inStream.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}


// 调整图片大小
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, 16);
Expand All @@ -86,17 +109,20 @@ public static BufferedImage resizeImage(BufferedImage originalImage, int targetW
}

// 渲染图片
public BufferedImage renderImage(OfflinePlayer player) throws Exception {
public BufferedImage renderImage(OfflinePlayer player, String extra) throws Exception {
BufferedImage srcImg;

// 解析图片路径,包括处理PlaceholderAPI和BotBind
String path;
if (player == null) {
path = this.source;
} else {
path = PlaceholderAPI.setPlaceholders(player, this.source);
// 解析图片路径,包括处理PlaceholderAPI和BotBind和{extra}
String path = this.source;
// 是否拥有{extra}
boolean hasExtra = !extra.isEmpty() && !extra.equals("") && extra != null;

if (hasExtra){
path = path.replaceAll("\\{extra}",extra);
}

if (player != null) {
path = PlaceholderAPI.setPlaceholders(player, path);
path = path.replace("{qq}", BotBind.getBindQQ(player.getName()).toLowerCase());
}

Expand All @@ -123,6 +149,12 @@ public BufferedImage renderImage(OfflinePlayer player) throws Exception {
for (SubImage subImage : this.subImages) {
BufferedImage image = null;
String subImgPath = subImage.path;
// 添加子图片的路径中{extra}的解析
if (hasExtra){
subImgPath = subImgPath.replaceAll("\\{extra}",extra);
}


subImgPath = PlaceholderAPI.setPlaceholders(player, subImgPath);

// 处理子图片路径
Expand Down Expand Up @@ -167,6 +199,12 @@ public BufferedImage renderImage(OfflinePlayer player) throws Exception {

for (CustomText customText : this.customTexts) {
String text = customText.text;

// 添加图片中文本的{extra}的解析
if (hasExtra){
text = text.replaceAll("\\{extra}",extra);
}

try {
text = PlaceholderAPI.setPlaceholders(player, text);
if (player != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ltd.dreamcraft.xinxincustommessage.objects.CustomText;
import ltd.dreamcraft.xinxincustommessage.objects.SubImage;
import me.clip.placeholderapi.PlaceholderAPI;
import org.apache.commons.lang3.SerializationUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -53,22 +54,9 @@ public static List<String> getMsg(List<String> message, OfflinePlayer offlinePla
response.add("无法找到图片ID: " + imageID);
continue;
}

// 替换文本和路径中的{extra}拓展自定义占位符
customImage.source = customImage.source.replaceAll("\\{extra}",extra);
for (CustomText customText : customImage.customTexts) {
customText.text = customText.text.replaceAll("\\{extra}",extra);
}
for (SubImage subImage : customImage.subImages) {
subImage.path = subImage.path.replaceAll("\\{extra}",extra);
}

//渲染图片
BufferedImage image = customImage.renderImage(offlinePlayer);
//发送信息
// System.out.println(bufferedImgToMsg(image));
// return Collections.singletonList(bufferedImgToMsg(image));
//TODO 计数图片信息数量
BufferedImage image = customImage.renderImage(offlinePlayer,extra);
// 计数图片信息数量
DataManager.invokeCountsMap.put("images", DataManager.invokeCountsMap.get("images") + 1);
response.add(bufferedImgToMsg(image));
} catch (Exception e) {
Expand Down

0 comments on commit c262f9b

Please sign in to comment.