Skip to content

Commit

Permalink
Fix the issue where the error snack bar in screenshot source was not …
Browse files Browse the repository at this point in the history
…displaying correctly
  • Loading branch information
Zailer43 committed Sep 12, 2024
1 parent f3b1785 commit 07e6e2b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public void loadImageFromText(IImageLoaderFromText imageLoaderFromText, String v
}

public void interactiveImageLoad(IInteractiveImageLoader interactiveImageLoader) {
MinecraftClient.getInstance().execute(() -> {
this.active = true;
SnackBarManager.getInstance().remove(SnackBarManager.IMAGE_ID);
});
this.active = false;
interactiveImageLoader.execute(bufferedImage -> {
if (this.image != null) {
Expand All @@ -129,11 +133,6 @@ public void interactiveImageLoad(IInteractiveImageLoader interactiveImageLoader)
if (this.callback != null) {
this.callback.accept(this.image);
}

MinecraftClient.getInstance().execute(() -> {
this.active = true;
SnackBarManager.getInstance().remove(SnackBarManager.IMAGE_ID);
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import fzmm.zailer.me.client.gui.components.style.StyledContainers;
import fzmm.zailer.me.client.gui.components.snack_bar.BaseSnackBarComponent;
import fzmm.zailer.me.client.gui.components.snack_bar.ISnackBarComponent;
import fzmm.zailer.me.client.gui.components.snack_bar.ISnackBarScreen;
import fzmm.zailer.me.utils.FzmmUtils;
import fzmm.zailer.me.utils.SnackBarManager;
import io.wispforest.owo.ui.component.LabelComponent;
Expand All @@ -23,13 +22,15 @@
import net.minecraft.client.util.ScreenshotRecorder;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public class ScreenshotSource implements IInteractiveImageLoader {
Expand Down Expand Up @@ -102,45 +103,70 @@ private FlowLayout getHud() {
}

public void takeScreenshot() {
MinecraftClient client = MinecraftClient.getInstance();
try {
Framebuffer framebuffer = client.getFramebuffer();
byte[] byteArray;
try (var screenshot = ScreenshotRecorder.takeScreenshot(framebuffer)) {
byteArray = screenshot.getBytes();
byte[] byteArray = null;
Exception exception = null;

Framebuffer framebuffer = MinecraftClient.getInstance().getFramebuffer();
try (var screenshot = ScreenshotRecorder.takeScreenshot(framebuffer)) {
byteArray = screenshot.getBytes();
} catch (Exception e) {
exception = e;
}

byte[] finalByteArray = byteArray;
Exception finalException = exception;
CompletableFuture.supplyAsync(() -> {
if (finalException != null) {
throw new RuntimeException(finalException);
}

try {
BufferedImage screenshot = ImageIO.read(new ByteArrayInputStream(finalByteArray));
int width = screenshot.getWidth();
int height = screenshot.getHeight();
int smallerSide = Math.min(width, height);
int halfLongerSide = smallerSide / 2;

BufferedImage scaled = screenshot.getSubimage(width / 2 - halfLongerSide, height / 2 - halfLongerSide, smallerSide, smallerSide);
BufferedImage finalImage = this.removePadding(scaled);

screenshot.flush();
scaled.flush();

return finalImage;
} catch (IOException e) {
throw new RuntimeException(e);
}
BufferedImage screenshot = ImageIO.read(new ByteArrayInputStream(byteArray));
int width = screenshot.getWidth();
int height = screenshot.getHeight();
int smallerSide = Math.min(width, height);
int halfLongerSide = smallerSide / 2;

BufferedImage scaled = screenshot.getSubimage(width / 2 - halfLongerSide, height / 2 - halfLongerSide, smallerSide, smallerSide);
BufferedImage finalImage = this.removePadding(scaled);

screenshot.flush();
scaled.flush();

this.setImage(finalImage);
} catch (IOException e) {
FzmmClient.LOGGER.error("Unexpected error loading an image", e);

ISnackBarComponent toast = BaseSnackBarComponent.builder(SnackBarManager.IMAGE_ID)
.title(Text.translatable("fzmm.snack_bar.image.error.title"))
.details(Text.translatable("fzmm.snack_bar.image.error.details.unexpectedError"))
.backgroundColor(FzmmStyles.ALERT_ERROR_COLOR)
.closeButton()
.build();

if (MinecraftClient.getInstance().currentScreen instanceof ISnackBarScreen manager) {
manager.addSnackBar(toast);
}, Util.getMainWorkerExecutor()).whenComplete((image, throwable) -> {
instance = null;
ISnackBarComponent snackBar = null;

if (throwable != null || image == null) {
FzmmClient.LOGGER.error("[ScreenshotSource] Unexpected error while taking screenshot", throwable);
snackBar = BaseSnackBarComponent.builder(SnackBarManager.IMAGE_ID)
.title(Text.translatable("fzmm.snack_bar.image.error.title"))
.details(Text.translatable("fzmm.snack_bar.image.error.details.unexpectedError"))
.backgroundColor(FzmmStyles.ALERT_ERROR_COLOR)
.closeButton()
.build();

}
}

Hud.remove(HUD_CAPTURE_SCREENSHOT);
FzmmUtils.setScreen(this.previousScreen);
this.previousScreen = null;
instance = null;
ISnackBarComponent finalSnackBar = snackBar;
MinecraftClient client = MinecraftClient.getInstance();
client.execute(() -> {
SnackBarManager manager = SnackBarManager.getInstance();
Hud.remove(HUD_CAPTURE_SCREENSHOT);
if (finalSnackBar != null) {
manager.add(finalSnackBar);
}

FzmmUtils.setScreen(this.previousScreen);
this.previousScreen = null;
});

this.setImage(image);
});
}

private BufferedImage removePadding(BufferedImage image) {
Expand Down

0 comments on commit 07e6e2b

Please sign in to comment.