Skip to content

Commit

Permalink
Rewrite Vistas pano capture to use vanilla pano capture and revert il…
Browse files Browse the repository at this point in the history
…l-advised change to main-menu detection.

- Fix accidentally overriding in-game music on dedicated servers
- Using vanilla (simpler) panoramic screenshot capture
  • Loading branch information
gniftygnome committed Feb 26, 2024
1 parent d5bf485 commit 6adf94c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 229 deletions.
168 changes: 0 additions & 168 deletions src/main/java/com/terraformersmc/vistas/mixin/GameRendererMixin.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.RunArgs;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.resource.ReloadableResourceManagerImpl;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.sound.MusicSound;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
Expand All @@ -23,7 +23,7 @@

@Environment(EnvType.CLIENT)
@Mixin(MinecraftClient.class)
public abstract class MinecraftClientMixin implements MinecraftClientAccess {
public class MinecraftClientMixin implements MinecraftClientAccess {
@Unique
private PanoramaResourceReloader panoramaResourceReloader;

Expand All @@ -32,8 +32,8 @@ public abstract class MinecraftClientMixin implements MinecraftClientAccess {
private ReloadableResourceManagerImpl resourceManager;

@Shadow
@Nullable
private IntegratedServer server;
@Nullable
public ClientPlayerEntity player;

@Inject(
method = "<init>",
Expand All @@ -51,7 +51,7 @@ public abstract class MinecraftClientMixin implements MinecraftClientAccess {

@Inject(method = "getMusicType", at = @At("HEAD"), cancellable = true)
private void vistas$getMusicType(CallbackInfoReturnable<MusicSound> ci) {
if (this.server == null) {
if (this.player == null) {
ci.setReturnValue(VistasTitle.CURRENT.getValue().getMusicSound());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.terraformersmc.vistas.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.terraformersmc.vistas.resource.PanoramicScreenshots;
import net.minecraft.client.util.ScreenshotRecorder;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.io.File;

@Mixin(ScreenshotRecorder.class)
public class ScreenshotRecorderMixin {
@WrapOperation(method = "saveScreenshotInner",
at = @At(value = "NEW", target = "java/io/File", ordinal = 0)
)
@SuppressWarnings("unused")
private static File vistas$panoramaPathOverride(File path, String file, Operation<File> original) {
if (path.toString().contains(PanoramicScreenshots.PANORAMAS_PATH)) {
return path;
}

return original.call(path, file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,49 @@
*/
package com.terraformersmc.vistas.resource;

import com.google.common.collect.ImmutableList;
import com.mojang.datafixers.util.Pair;
import com.terraformersmc.vistas.Vistas;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.util.Util;
import net.minecraft.util.math.RotationAxis;
import org.joml.Quaternionf;
import net.minecraft.text.Text;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

//TODO: rewrite; i dont know what im doing!
public class PanoramicScreenshots {
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
public static final String PANORAMAS_PATH = "screenshots/panoramas";

public static final List<Quaternionf> ROTATIONS = ImmutableList.of(RotationAxis.POSITIVE_Y.rotationDegrees(0), RotationAxis.POSITIVE_Y.rotationDegrees(90), RotationAxis.POSITIVE_Y.rotationDegrees(180), RotationAxis.POSITIVE_Y.rotationDegrees(270), RotationAxis.POSITIVE_X.rotationDegrees(-90), RotationAxis.POSITIVE_X.rotationDegrees(90));

public static final List<Float> PITCHES = ImmutableList.of(0.0F, 0.0F, 0.0F, 0.0F, 90.0F, -90.0F);
public static final List<Float> YAWS = ImmutableList.of(0.0F, 90.0F, 180.0F, -90.0F, 0.0F, 0.0F);

public static double time = 0.0D;
public static double timeSinceLastKeyPress = -1.0D;
public static boolean needsScreenshot = false;
public static int onShot = -1;

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public static Optional<Pair<Float, Float>> startingRotation = Optional.empty();
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public static Optional<Path> currentScreenshotPath = Optional.empty();
public static int cooldown = 0;

public static void registerKeyBinding() {
KeyBinding screenshotKey = new KeyBinding("key.vistas.panoramic_screenshot", 'H', "key.categories.misc");
KeyBindingHelper.registerKeyBinding(screenshotKey);
ClientTickEvents.START_CLIENT_TICK.register(client -> {
if (client.currentScreen == null && screenshotKey.isPressed() && timeSinceLastKeyPress <= 0.0D) {
needsScreenshot = true;
onShot++;
timeSinceLastKeyPress = 5.0D;
if (cooldown > 0) {
// 100 client tick cooldown between panoramas.
--cooldown;
return;
}
});
}
if (client.currentScreen == null && screenshotKey.isPressed()) {
cooldown = 100;

// Capture the largest square view.
int size = Math.min(client.getWindow().getFramebufferWidth(), client.getWindow().getFramebufferHeight());

public static void saveScreenshot(NativeImage screenshot, Path folder, int i) {
Util.getIoWorkerExecutor().execute(() -> {
try (screenshot) {
int width = screenshot.getWidth();
int height = screenshot.getHeight();
int x = 0;
int y = 0;
if (width > height) {
x = (width - height) / 2;
//noinspection SuspiciousNameCombination
width = height;
} else {
y = (height - width) / 2;
//noinspection SuspiciousNameCombination
height = width;
}
NativeImage saved = new NativeImage(width, height, false);
screenshot.resizeSubRectTo(x, y, width, height, saved);
saved.writeTo(folder.resolve("panorama_" + i + ".png"));
} catch (IOException exception) {
Vistas.LOGGER.warn("Couldn't save screenshot", exception);
Text result = client.takePanorama(getPanoramicScreenshotFolder().toFile(), size, size);
Vistas.LOGGER.info("Panorama capture with result: {}", result.getString());
client.getMessageHandler().onGameMessage(result, false);
}
});
}

public static Path getPanoramicScreenshotFolder() {
if (currentScreenshotPath.isPresent()) {
return currentScreenshotPath.get();
}
File rootFile = FabricLoader.getInstance().getGameDir().resolve("screenshots/panoramas/").toFile();
File rootFile = FabricLoader.getInstance().getGameDir().resolve(PANORAMAS_PATH).toFile();
if (!rootFile.exists()) {
//noinspection ResultOfMethodCallIgnored
rootFile.mkdirs();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/vistas.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"package": "com.terraformersmc.vistas.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"GameRendererMixin",
"LogoDrawerMixin",
"MinecraftClientMixin",
"ScreenshotRecorderMixin",
"SplashTextRendererMixin",
"SplashTextResourceSupplierMixin",
"TitleScreenMixin"
Expand Down

0 comments on commit 6adf94c

Please sign in to comment.