Skip to content

Commit

Permalink
Fix screenshots not being saved when an error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
Deftu committed Jun 13, 2024
1 parent c775aeb commit 7573e9a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@

@Mixin({ScreenshotRecorder.class})
public class ScreenshotRecorderMixin {

@Inject(method = "method_1661", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/texture/NativeImage;writeTo(Ljava/io/File;)V", shift = At.Shift.AFTER))
private static void onFileWrite(NativeImage nativeImage, File file, Consumer<Text> consumer, CallbackInfo ci) {
try {
byte[] bytes = nativeImage.getBytes();
ScreenshotHandler.INSTANCE.handle(nativeImage, bytes, file);
} catch (Exception e) {
throw new RuntimeException("Couldn't read from screenshot.", e);
}
ScreenshotHandler.INSTANCE.capture(nativeImage, file);
}

@ModifyArg(method = "method_1661", at = @At(value = "INVOKE", target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V"))
private static Object acceptText(Object original) {
return ScreenshotHandler.INSTANCE.createText((Text) original);
}

}
27 changes: 18 additions & 9 deletions src/main/kotlin/dev/deftu/screencapper/ScreenshotHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ import javax.imageio.ImageIO
object ScreenshotHandler {
private var screenshot: Screenshot? = null

fun capture(image: NativeImage, file: File) {
try {
val bytes = image.bytes
handle(image, bytes, file)
} catch (t: Throwable) {
t.printStackTrace()
}
}

fun upload(): URL = upload(screenshot!!)
fun upload(screenshot: Screenshot): URL {
var screenshot = screenshot
Expand All @@ -39,15 +48,6 @@ object ScreenshotHandler {
return screenshot.url!!
}

fun handle(image: NativeImage, bytes: ByteArray, file: File) {
if (!ScreencapperConfig.toggle) return
var screenshot = Screenshot(image, bytes, file)
this.screenshot = screenshot
ScreenshotPreview.append(screenshot)
if (ScreencapperConfig.autoCopy) copy()
if (ScreencapperConfig.uploadToggle) UDesktop.setClipboardString(upload(screenshot).toString())
}

fun createText(original: Text): Text {
if (!ScreencapperConfig.toggle || screenshot == null) return original

Expand Down Expand Up @@ -91,6 +91,15 @@ object ScreenshotHandler {
}
}

private fun handle(image: NativeImage, bytes: ByteArray, file: File) {
if (!ScreencapperConfig.toggle) return
var screenshot = Screenshot(image, bytes, file)
this.screenshot = screenshot
ScreenshotPreview.append(screenshot)
if (ScreencapperConfig.autoCopy) copy()
if (ScreencapperConfig.uploadToggle) UDesktop.setClipboardString(upload(screenshot).toString())
}

internal fun delete() {
if (screenshot?.file?.delete() == true) {
Screencapper.sendMessage(TextHelper.createTranslatableText("${Screencapper.ID}.text.chat.delete.success")
Expand Down

0 comments on commit 7573e9a

Please sign in to comment.