Skip to content

Commit

Permalink
feat: Attempted nullchecks and a bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
worldwidepixel committed Aug 14, 2024
1 parent a1a5cd8 commit 5b964bf
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/client/java/dev/spiritstudios/snapper/Snapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,7 +62,7 @@ public void onInitializeClient() {
}
while (RECENT_SCREENSHOT_KEY.wasPressed()) {
client.setScreen(new ScreenshotViewerScreen(
ScreenshotIcon.of(getLatestScreenshot()),
ScreenshotImage.of(getLatestScreenshot()),
getLatestScreenshot(),
null
));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"
Expand All @@ -62,17 +62,17 @@ 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"
)
);
}

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)
Expand All @@ -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() {
Expand Down

0 comments on commit 5b964bf

Please sign in to comment.