From a85a09d5542cceb312beca9927bc40a70230746b Mon Sep 17 00:00:00 2001 From: Martin Prokoph Date: Thu, 27 Jun 2024 12:10:05 +0200 Subject: [PATCH] Add features of #14 - Add support for local images - Add support for referencing Minecraft textures Big thanks to @Hendrix-Shen - Code cleanup --- .../render/PictureSignRenderer.java | 13 ++-- .../picturesign/util/PictureDownloader.java | 78 +++++++++++++++---- fabric/src/main/resources/fabric.mod.json | 4 + 3 files changed, 75 insertions(+), 20 deletions(-) diff --git a/common/src/main/java/eu/midnightdust/picturesign/render/PictureSignRenderer.java b/common/src/main/java/eu/midnightdust/picturesign/render/PictureSignRenderer.java index 16e4810..6749d13 100755 --- a/common/src/main/java/eu/midnightdust/picturesign/render/PictureSignRenderer.java +++ b/common/src/main/java/eu/midnightdust/picturesign/render/PictureSignRenderer.java @@ -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) { diff --git a/common/src/main/java/eu/midnightdust/picturesign/util/PictureDownloader.java b/common/src/main/java/eu/midnightdust/picturesign/util/PictureDownloader.java index 10a93ad..8c5d8c9 100755 --- a/common/src/main/java/eu/midnightdust/picturesign/util/PictureDownloader.java +++ b/common/src/main/java/eu/midnightdust/picturesign/util/PictureDownloader.java @@ -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); + } } diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index 248bb92..1b6f1ee 100755 --- a/fabric/src/main/resources/fabric.mod.json +++ b/fabric/src/main/resources/fabric.mod.json @@ -9,6 +9,10 @@ "Motschen", "TeamMidnightDust" ], + "contributors": [ + "Hendrix-Shen", + "Frost-ZX" + ], "contact": { "homepage": "https://www.midnightdust.eu/", "sources": "https://github.com/TeamMidnightDust/PictureSign",