Add features of #14

- Add support for local images
- Add support for referencing Minecraft textures
Big thanks to @Hendrix-Shen

- Code cleanup
This commit is contained in:
Martin Prokoph
2024-06-27 12:10:05 +02:00
parent a46d69cf8d
commit a85a09d554
3 changed files with 75 additions and 20 deletions

View File

@@ -27,7 +27,7 @@ public class PictureSignRenderer {
PictureSignType type = PictureSignType.getType(signBlockEntity, front);
String url = PictureURLUtils.getLink(signBlockEntity, front);
PictureInfo info = null;
if (!url.contains("://")) {
if (!url.contains("://") && !url.startsWith("file:") && !url.startsWith("rp:")) {
url = "https://" + url;
}
isSafeJsonUrl = false;
@@ -43,14 +43,15 @@ public class PictureSignRenderer {
info = PictureURLUtils.infoFromJson(url);
if (info == null) return;
url = info.url();
if (!url.contains("://")) {
url = "https://" + url;
}
}
if (!url.contains("://")) {
url = "https://" + url;
}
if (type == PictureSignType.PICTURE && !url.contains(".png") && !url.contains(".jpg") && !url.contains(".jpeg")) return;
if (type == PictureSignType.PICTURE && !url.contains(".png") && !url.contains(".jpg") && !url.contains(".jpeg") && !url.startsWith("rp:")) return;
if (type == PictureSignType.GIF && !url.contains(".gif")) return;
if (PictureSignConfig.safeMode) {
if (PictureSignConfig.safeMode && !url.startsWith("file:") && !url.startsWith("rp:")) {
isSafeUrl = false;
String finalUrl = url;
if (type == PictureSignType.PICTURE) {

View File

@@ -47,7 +47,7 @@ public class PictureDownloader {
PictureData data = this.cache.get(url);
if (data == null) {
// Download the picture if not in cache
this.downloadPicture(url);
this.loadPicture(url);
return null;
}
@@ -60,6 +60,11 @@ public class PictureDownloader {
}
// Download the image and save it in cache
private void loadPicture(String url) {
if (url.startsWith("file:")) loadLocalPicture(url);
else if (url.startsWith("rp:")) loadResourcePackTexture(url);
else downloadPicture(url);
}
private void downloadPicture(String url) {
if (PictureSignConfig.debug) PictureSignClient.LOGGER.info("Started downloading picture: " + url);
this.cache.put(url, new PictureData(url));
@@ -80,19 +85,7 @@ public class PictureDownloader {
out.close();
// Convert to png
BufferedImage bufferedImage = ImageIO.read(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
NativeImage nativeImage = NativeImage.read(inputStream);
NativeImageBackedTexture nativeImageBackedTexture = new NativeImageBackedTexture(nativeImage);
Identifier texture = MinecraftClient.getInstance().getTextureManager().registerDynamicTexture(MOD_ID+"/image",
nativeImageBackedTexture);
Identifier texture = convert2png(file);
// Cache the downloaded picture
synchronized (mutex) {
@@ -107,5 +100,62 @@ public class PictureDownloader {
}
});
}
private void loadLocalPicture(String url) {
String realPath = url.replace("file:", "");
if (PictureSignConfig.debug) PictureSignClient.LOGGER.info("Started loading local picture: " + url);
this.cache.put(url, new PictureData(url));
service.submit(() -> {
try {
File file = new File(realPath);
Identifier texture = convert2png(file);
synchronized (mutex) {
PictureData data = this.cache.get(url);
data.identifier = texture;
}
} catch (IOException error) {
PictureSignClient.LOGGER.error("Error loading local picture: " + error);
}
});
if (PictureSignConfig.debug) PictureSignClient.LOGGER.info("Finished loading local picture: " + url);
}
private void loadResourcePackTexture(String url) {
String realIdentifierPath = url.replace("rp:", "");
if (!realIdentifierPath.endsWith(".png")) realIdentifierPath += ".png";
if (PictureSignConfig.debug) PictureSignClient.LOGGER.info("Started loading resource pack picture: " + url);
Identifier id = Identifier.tryParse(realIdentifierPath);
if (id == null) {
PictureSignClient.LOGGER.error("Unable to locate resource texture: " + url);
return;
}
this.cache.put(url, new PictureData(url));
service.submit(() -> {
synchronized (mutex) {
PictureData data = this.cache.get(url);
data.identifier = id;
}
});
if (PictureSignConfig.debug) PictureSignClient.LOGGER.info("Finished loading resource pack picture: " + url);
}
private static Identifier convert2png(File file) throws IOException {
BufferedImage bufferedImage = ImageIO.read(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
NativeImage nativeImage = NativeImage.read(inputStream);
NativeImageBackedTexture nativeImageBackedTexture = new NativeImageBackedTexture(nativeImage);
return MinecraftClient.getInstance().getTextureManager().registerDynamicTexture("picturesign/image",
nativeImageBackedTexture);
}
}

View File

@@ -9,6 +9,10 @@
"Motschen",
"TeamMidnightDust"
],
"contributors": [
"Hendrix-Shen",
"Frost-ZX"
],
"contact": {
"homepage": "https://www.midnightdust.eu/",
"sources": "https://github.com/TeamMidnightDust/PictureSign",