From 5b964bf442448099a8f5c85c8c3cc581f9874867 Mon Sep 17 00:00:00 2001 From: worldwidepixel <58098422+worldwidepixel@users.noreply.github.com> Date: Wed, 14 Aug 2024 14:13:19 -0700 Subject: [PATCH] feat: Attempted nullchecks and a bit of refactoring --- .../dev/spiritstudios/snapper/Snapper.java | 4 +-- .../snapper/gui/PanoramaViewerScreen.java | 8 ++--- .../snapper/gui/ScreenshotViewerScreen.java | 6 ++-- .../gui/widget/ScreenshotListWidget.java | 36 +++++++++---------- .../snapper/mixin/TitleScreenMixin.java | 4 ++- ...reenshotIcon.java => ScreenshotImage.java} | 30 ++++++++-------- 6 files changed, 46 insertions(+), 42 deletions(-) rename src/client/java/dev/spiritstudios/snapper/util/{ScreenshotIcon.java => ScreenshotImage.java} (79%) diff --git a/src/client/java/dev/spiritstudios/snapper/Snapper.java b/src/client/java/dev/spiritstudios/snapper/Snapper.java index 85cb55f..15126dd 100644 --- a/src/client/java/dev/spiritstudios/snapper/Snapper.java +++ b/src/client/java/dev/spiritstudios/snapper/Snapper.java @@ -2,7 +2,7 @@ import dev.spiritstudios.snapper.gui.ScreenshotScreen; import dev.spiritstudios.snapper.gui.ScreenshotViewerScreen; -import dev.spiritstudios.snapper.util.ScreenshotIcon; +import dev.spiritstudios.snapper.util.ScreenshotImage; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; @@ -62,7 +62,7 @@ public void onInitializeClient() { } while (RECENT_SCREENSHOT_KEY.wasPressed()) { client.setScreen(new ScreenshotViewerScreen( - ScreenshotIcon.of(getLatestScreenshot()), + ScreenshotImage.of(getLatestScreenshot()), getLatestScreenshot(), null )); diff --git a/src/client/java/dev/spiritstudios/snapper/gui/PanoramaViewerScreen.java b/src/client/java/dev/spiritstudios/snapper/gui/PanoramaViewerScreen.java index 15afe4f..50c07b6 100644 --- a/src/client/java/dev/spiritstudios/snapper/gui/PanoramaViewerScreen.java +++ b/src/client/java/dev/spiritstudios/snapper/gui/PanoramaViewerScreen.java @@ -1,7 +1,7 @@ package dev.spiritstudios.snapper.gui; import dev.spiritstudios.snapper.Snapper; -import dev.spiritstudios.snapper.util.ScreenshotIcon; +import dev.spiritstudios.snapper.util.ScreenshotImage; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.CubeMapRenderer; import net.minecraft.client.gui.DrawContext; @@ -53,15 +53,15 @@ private void load() { if (panorama == null) return; panorama.parallelStream().map(face -> { - ScreenshotIcon icon = ScreenshotIcon.forPanoramaFace(client.getTextureManager(), face.getName()); + ScreenshotImage icon = ScreenshotImage.forPanoramaFace(client.getTextureManager(), face.getName()); this.loadIcon(icon, face.getName(), Path.of(face.getPath())); return icon; - }).toList().forEach(ScreenshotIcon::joinLoad); + }).toList().forEach(ScreenshotImage::joinLoad); this.loaded = true; } - private void loadIcon(ScreenshotIcon icon, String fileName, Path filePath) { + private void loadIcon(ScreenshotImage icon, String fileName, Path filePath) { if (filePath == null || !Files.isRegularFile(filePath)) { icon.destroy(); return; diff --git a/src/client/java/dev/spiritstudios/snapper/gui/ScreenshotViewerScreen.java b/src/client/java/dev/spiritstudios/snapper/gui/ScreenshotViewerScreen.java index 6559504..1cffc6e 100644 --- a/src/client/java/dev/spiritstudios/snapper/gui/ScreenshotViewerScreen.java +++ b/src/client/java/dev/spiritstudios/snapper/gui/ScreenshotViewerScreen.java @@ -3,7 +3,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import dev.spiritstudios.snapper.Snapper; import dev.spiritstudios.snapper.util.ScreenshotActions; -import dev.spiritstudios.snapper.util.ScreenshotIcon; +import dev.spiritstudios.snapper.util.ScreenshotImage; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; @@ -25,7 +25,7 @@ public class ScreenshotViewerScreen extends Screen { private final MinecraftClient client = MinecraftClient.getInstance(); - private final ScreenshotIcon icon; + private final ScreenshotImage icon; private Path iconPath; private final String title; private final int imageWidth; @@ -36,7 +36,7 @@ public class ScreenshotViewerScreen extends Screen { private static final Identifier MENU_DECOR_BACKGROUND_TEXTURE = Identifier.ofVanilla("textures/gui/menu_list_background.png"); private static final Identifier INWORLD_MENU_DECOR_BACKGROUND_TEXTURE = Identifier.ofVanilla("textures/gui/inworld_menu_list_background.png"); - public ScreenshotViewerScreen(ScreenshotIcon icon, File screenshot, Screen parent) { + public ScreenshotViewerScreen(ScreenshotImage icon, File screenshot, Screen parent) { super(Text.translatable("menu.snapper.viewermenu")); this.parent = parent; diff --git a/src/client/java/dev/spiritstudios/snapper/gui/widget/ScreenshotListWidget.java b/src/client/java/dev/spiritstudios/snapper/gui/widget/ScreenshotListWidget.java index e933790..0ced122 100644 --- a/src/client/java/dev/spiritstudios/snapper/gui/widget/ScreenshotListWidget.java +++ b/src/client/java/dev/spiritstudios/snapper/gui/widget/ScreenshotListWidget.java @@ -4,7 +4,7 @@ import dev.spiritstudios.snapper.Snapper; import dev.spiritstudios.snapper.gui.ScreenshotScreen; import dev.spiritstudios.snapper.gui.ScreenshotViewerScreen; -import dev.spiritstudios.snapper.util.ScreenshotIcon; +import dev.spiritstudios.snapper.util.ScreenshotImage; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.LoadingDisplay; @@ -157,7 +157,7 @@ public class ScreenshotEntry extends Entry implements AutoCloseable { public final long lastModified; private final MinecraftClient client; - public final ScreenshotIcon icon; + public final ScreenshotImage icon; public final String iconFileName; public Path iconPath; public final Screen screenParent; @@ -168,7 +168,7 @@ public class ScreenshotEntry extends Entry implements AutoCloseable { public ScreenshotEntry(File screenshot, MinecraftClient client, Screen parent) { this.client = client; this.screenParent = parent; - this.icon = ScreenshotIcon.forScreenshot(this.client.getTextureManager(), screenshot.getName()); + this.icon = ScreenshotImage.forScreenshot(this.client.getTextureManager(), screenshot.getName()); this.iconPath = Path.of(screenshot.getPath()); this.iconFileName = screenshot.getName(); this.lastModified = screenshot.lastModified(); @@ -213,21 +213,21 @@ public void render(DrawContext context, int index, int y, int x, int entryWidth, ); if (this.icon != null) { - RenderSystem.enableBlend(); - context.drawTexture( - this.icon.getTextureId(), - x, - y, - 32, - 32, - (icon.getHeight()) / 3.0F + 32, - 0, - icon.getHeight(), - icon.getHeight(), - icon.getWidth(), - icon.getHeight() - ); - RenderSystem.disableBlend(); + RenderSystem.enableBlend(); + context.drawTexture( + this.icon.getTextureId(), + x, + y, + 32, + 32, + (icon.getHeight()) / 3.0F + 32, + 0, + icon.getHeight(), + icon.getHeight(), + icon.getWidth(), + icon.getHeight() + ); + RenderSystem.disableBlend(); } if (this.client.options.getTouchscreen().getValue() || hovered) { diff --git a/src/client/java/dev/spiritstudios/snapper/mixin/TitleScreenMixin.java b/src/client/java/dev/spiritstudios/snapper/mixin/TitleScreenMixin.java index 9ad6501..6c42b4e 100644 --- a/src/client/java/dev/spiritstudios/snapper/mixin/TitleScreenMixin.java +++ b/src/client/java/dev/spiritstudios/snapper/mixin/TitleScreenMixin.java @@ -35,7 +35,9 @@ protected void init(CallbackInfo ci) { this.addDrawableChild( TextIconButtonWidget.builder( Text.translatable("button.snapper.screenshots"), - button -> this.client.setScreen(new ScreenshotScreen((TitleScreen) ((Object) this))), + button -> { + this.client.setScreen(new ScreenshotScreen((TitleScreen) ((Object) this))); + }, true ).width(20).texture(SNAPPER_BUTTON_ICON, 15, 15).build() ).setPosition(this.width / 2 - 124, y + spacingY); diff --git a/src/client/java/dev/spiritstudios/snapper/util/ScreenshotIcon.java b/src/client/java/dev/spiritstudios/snapper/util/ScreenshotImage.java similarity index 79% rename from src/client/java/dev/spiritstudios/snapper/util/ScreenshotIcon.java rename to src/client/java/dev/spiritstudios/snapper/util/ScreenshotImage.java index 57b49c6..9ba910d 100644 --- a/src/client/java/dev/spiritstudios/snapper/util/ScreenshotIcon.java +++ b/src/client/java/dev/spiritstudios/snapper/util/ScreenshotImage.java @@ -16,7 +16,7 @@ import java.nio.file.Path; import java.util.concurrent.CompletableFuture; -public class ScreenshotIcon implements AutoCloseable { +public class ScreenshotImage implements AutoCloseable { private static final Identifier UNKNOWN_SERVER_ID = Identifier.ofVanilla("textures/misc/unknown_server.png"); private final TextureManager textureManager; @@ -27,19 +27,19 @@ public class ScreenshotIcon implements AutoCloseable { private boolean closed; private static final MinecraftClient client = MinecraftClient.getInstance(); - private ScreenshotIcon(TextureManager textureManager, Identifier id) { + private ScreenshotImage(TextureManager textureManager, Identifier id) { this.textureManager = textureManager; this.id = id; } - private ScreenshotIcon(TextureManager textureManager, Identifier id, File screenshot) { + private ScreenshotImage(TextureManager textureManager, Identifier id, File screenshot) { this.textureManager = textureManager; this.id = id; this.loadIcon(screenshot.toPath()); } - public static ScreenshotIcon of(File screenshot) { - return new ScreenshotIcon( + public static ScreenshotImage of(File screenshot) { + return new ScreenshotImage( client.getTextureManager(), Identifier.ofVanilla( "screenshots/" + Util.replaceInvalidChars(screenshot.getName(), Identifier::isPathCharacterValid) + "/icon" @@ -62,8 +62,8 @@ private void loadIcon(Path path) { }); } - public static ScreenshotIcon forScreenshot(TextureManager textureManager, String screenshotName) { - return new ScreenshotIcon( + public static ScreenshotImage forScreenshot(TextureManager textureManager, String screenshotName) { + return new ScreenshotImage( textureManager, Identifier.ofVanilla( "screenshots/" + Util.replaceInvalidChars(screenshotName, Identifier::isPathCharacterValid) + "/icon" @@ -71,8 +71,8 @@ public static ScreenshotIcon forScreenshot(TextureManager textureManager, String ); } - public static ScreenshotIcon forPanoramaFace(TextureManager textureManager, String screenshotName) { - return new ScreenshotIcon( + public static ScreenshotImage forPanoramaFace(TextureManager textureManager, String screenshotName) { + return new ScreenshotImage( textureManager, Identifier.ofVanilla( "screenshots/panorama/" + Util.replaceInvalidChars(screenshotName, Identifier::isPathCharacterValid) @@ -82,12 +82,14 @@ public static ScreenshotIcon forPanoramaFace(TextureManager textureManager, Stri public void load(NativeImage image) { this.assertOpen(); - if (this.texture == null) this.texture = new NativeImageBackedTexture(image); - else { - this.texture.setImage(image); - this.texture.upload(); + if (image != null) { + if (this.texture == null) this.texture = new NativeImageBackedTexture(image); + else { + this.texture.setImage(image); + this.texture.upload(); + } + this.textureManager.registerTexture(this.id, this.texture); } - this.textureManager.registerTexture(this.id, this.texture); } public void joinLoad() {