From 79eb7afa495be87997c7cd63d22ea9716a0992a7 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 1 Oct 2021 19:54:27 +0200 Subject: [PATCH] improve screenshot taking --- .../gui/rendering/util/ScreenshotTaker.kt | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/util/ScreenshotTaker.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/util/ScreenshotTaker.kt index cb2a7f289..4a342bfd0 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/util/ScreenshotTaker.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/util/ScreenshotTaker.kt @@ -35,21 +35,20 @@ class ScreenshotTaker( ) { fun takeScreenshot() { try { + val width = renderWindow.window.size.x + val height = renderWindow.window.size.y + val buffer = renderWindow.renderSystem.readPixels(Vec2i(0, 0), Vec2i(width, height), PixelTypes.RGBA) + val basePath = "${RunConfiguration.HOME_DIRECTORY}/screenshots/${renderWindow.connection.address.hostname}/${DATE_FORMATTER.format(System.currentTimeMillis())}" var path = "$basePath.png" var i = 1 while (File(path).exists()) { path = "${basePath}_${i++}.png" - if (i > 10) { - screenshotFail(StackOverflowError()) - return + if (i > MAX_FILES_CHECK) { + throw StackOverflowError("There are already > $MAX_FILES_CHECK screenshots with this date! Please try again!") } } - val width = renderWindow.window.size.x - val height = renderWindow.window.size.y - val buffer = renderWindow.renderSystem.readPixels(Vec2i(0, 0), Vec2i(width, height), PixelTypes.RGBA) - DefaultThreadPool += { try { val bufferedImage = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) @@ -88,20 +87,21 @@ class ScreenshotTaker( // }, )) } catch (exception: Exception) { - screenshotFail(exception) + exception.fail() } } } catch (exception: Exception) { - screenshotFail(exception) + exception.fail() } } - private fun screenshotFail(exception: Throwable?) { - exception?.printStackTrace() + private fun Throwable?.fail() { + this?.printStackTrace() renderWindow.sendDebugMessage("§cFailed to make a screenshot!") } companion object { private val DATE_FORMATTER = SimpleDateFormat("yyyy-MM-dd_HH.mm.ss") + private const val MAX_FILES_CHECK = 10 } }