mirror of
https://github.com/TeamMidnightDust/PictureSign.git
synced 2025-12-13 04:45:10 +01:00
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:
@@ -27,7 +27,7 @@ public class PictureSignRenderer {
|
|||||||
PictureSignType type = PictureSignType.getType(signBlockEntity, front);
|
PictureSignType type = PictureSignType.getType(signBlockEntity, front);
|
||||||
String url = PictureURLUtils.getLink(signBlockEntity, front);
|
String url = PictureURLUtils.getLink(signBlockEntity, front);
|
||||||
PictureInfo info = null;
|
PictureInfo info = null;
|
||||||
if (!url.contains("://")) {
|
if (!url.contains("://") && !url.startsWith("file:") && !url.startsWith("rp:")) {
|
||||||
url = "https://" + url;
|
url = "https://" + url;
|
||||||
}
|
}
|
||||||
isSafeJsonUrl = false;
|
isSafeJsonUrl = false;
|
||||||
@@ -43,14 +43,15 @@ public class PictureSignRenderer {
|
|||||||
info = PictureURLUtils.infoFromJson(url);
|
info = PictureURLUtils.infoFromJson(url);
|
||||||
if (info == null) return;
|
if (info == null) return;
|
||||||
url = info.url();
|
url = info.url();
|
||||||
|
|
||||||
|
if (!url.contains("://")) {
|
||||||
|
url = "https://" + url;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!url.contains("://")) {
|
if (type == PictureSignType.PICTURE && !url.contains(".png") && !url.contains(".jpg") && !url.contains(".jpeg") && !url.startsWith("rp:")) return;
|
||||||
url = "https://" + url;
|
|
||||||
}
|
|
||||||
if (type == PictureSignType.PICTURE && !url.contains(".png") && !url.contains(".jpg") && !url.contains(".jpeg")) return;
|
|
||||||
if (type == PictureSignType.GIF && !url.contains(".gif")) return;
|
if (type == PictureSignType.GIF && !url.contains(".gif")) return;
|
||||||
if (PictureSignConfig.safeMode) {
|
if (PictureSignConfig.safeMode && !url.startsWith("file:") && !url.startsWith("rp:")) {
|
||||||
isSafeUrl = false;
|
isSafeUrl = false;
|
||||||
String finalUrl = url;
|
String finalUrl = url;
|
||||||
if (type == PictureSignType.PICTURE) {
|
if (type == PictureSignType.PICTURE) {
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ public class PictureDownloader {
|
|||||||
PictureData data = this.cache.get(url);
|
PictureData data = this.cache.get(url);
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
// Download the picture if not in cache
|
// Download the picture if not in cache
|
||||||
this.downloadPicture(url);
|
this.loadPicture(url);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,6 +60,11 @@ public class PictureDownloader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Download the image and save it in cache
|
// 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) {
|
private void downloadPicture(String url) {
|
||||||
if (PictureSignConfig.debug) PictureSignClient.LOGGER.info("Started downloading picture: " + url);
|
if (PictureSignConfig.debug) PictureSignClient.LOGGER.info("Started downloading picture: " + url);
|
||||||
this.cache.put(url, new PictureData(url));
|
this.cache.put(url, new PictureData(url));
|
||||||
@@ -80,19 +85,7 @@ public class PictureDownloader {
|
|||||||
|
|
||||||
out.close();
|
out.close();
|
||||||
|
|
||||||
// Convert to png
|
Identifier texture = convert2png(file);
|
||||||
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);
|
|
||||||
|
|
||||||
// Cache the downloaded picture
|
// Cache the downloaded picture
|
||||||
synchronized (mutex) {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,10 @@
|
|||||||
"Motschen",
|
"Motschen",
|
||||||
"TeamMidnightDust"
|
"TeamMidnightDust"
|
||||||
],
|
],
|
||||||
|
"contributors": [
|
||||||
|
"Hendrix-Shen",
|
||||||
|
"Frost-ZX"
|
||||||
|
],
|
||||||
"contact": {
|
"contact": {
|
||||||
"homepage": "https://www.midnightdust.eu/",
|
"homepage": "https://www.midnightdust.eu/",
|
||||||
"sources": "https://github.com/TeamMidnightDust/PictureSign",
|
"sources": "https://github.com/TeamMidnightDust/PictureSign",
|
||||||
|
|||||||
Reference in New Issue
Block a user