mirror of
https://github.com/TeamMidnightDust/PictureSign.git
synced 2025-12-16 06:05:09 +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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user