Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
10 903
src/client/java/dev/kodari/cinematicreplay/client/CinematicReplayClient.java
package dev.kodari.cinematicreplay.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.lwjgl.glfw.GLFW;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;
public final class CinematicReplayClient implements ClientModInitializer {
private static final int RECORDING_MAGIC = 0x43525031;
private static final int ROUTE_MAGIC = 0x43525431;
private static final int FORMAT_VERSION = 1;
private static final DateTimeFormatter FILE_TIME = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss-SSS");
private static final EquipmentSlot[] ARMOR_SLOTS = {
EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET
};
private static KeyMapping recordKey;
private static KeyMapping cameraKey;
private static KeyMapping waypointKey;
private static KeyMapping pathKey;
private static KeyMapping videoKey;
private static KeyMapping videoStopKey;
private static KeyMapping hudKey;
private static KeyMapping moveForwardKey;
private static KeyMapping moveBackKey;
private static KeyMapping moveLeftKey;
private static KeyMapping moveRightKey;
private static KeyMapping moveUpKey;
private static KeyMapping moveDownKey;
private static KeyMapping yawLeftKey;
private static KeyMapping yawRightKey;
private static KeyMapping pitchUpKey;
private static KeyMapping pitchDownKey;
private static KeyMapping speedUpKey;
private static KeyMapping speedDownKey;
private static KeyMapping fovIncreaseKey;
private static KeyMapping fovDecreaseKey;
private static KeyMapping pauseIncreaseKey;
private static KeyMapping pauseDecreaseKey;
private static DataOutputStream recordingOutput;
private static Path recordingPath;
private static long recordingStartNanos;
private static long recordingTicks;
private static final List<Waypoint> waypoints = new ArrayList<>();
private static Path routePath;
private static boolean detachedCamera;
private static boolean hideHud;
private static boolean routePlaying;
private static double cameraX;
private static double cameraY;
private static double cameraZ;
private static float cameraYaw;
private static float cameraPitch;
private static float cameraFov = 70.0f;
private static double cameraSpeed = 0.5;
private static long viewpointPauseMillis = 1000;
private static int routeFrom;
private static int routeTo;
private static long routeMoveStart;
private static long routeMoveDuration;
private static VideoCapture videoCapture;
@Override
public void onInitializeClient() {
recordKey = key("key.cinematicreplay.record", GLFW.GLFW_KEY_F6);
cameraKey = key("key.cinematicreplay.camera", GLFW.GLFW_KEY_F7);
waypointKey = key("key.cinematicreplay.waypoint", GLFW.GLFW_KEY_F8);
pathKey = key("key.cinematicreplay.path", GLFW.GLFW_KEY_F9);
videoKey = key("key.cinematicreplay.video", GLFW.GLFW_KEY_F10);
videoStopKey = key("key.cinematicreplay.video_stop", GLFW.GLFW_KEY_F11);
hudKey = key("key.cinematicreplay.hud", GLFW.GLFW_KEY_F12);
moveForwardKey = key("key.cinematicreplay.camera_forward", GLFW.GLFW_KEY_I);
moveBackKey = key("key.cinematicreplay.camera_back", GLFW.GLFW_KEY_K);
moveLeftKey = key("key.cinematicreplay.camera_left", GLFW.GLFW_KEY_J);
moveRightKey = key("key.cinematicreplay.camera_right", GLFW.GLFW_KEY_L);
moveUpKey = key("key.cinematicreplay.camera_up", GLFW.GLFW_KEY_U);
moveDownKey = key("key.cinematicreplay.camera_down", GLFW.GLFW_KEY_O);
yawLeftKey = key("key.cinematicreplay.camera_yaw_left", GLFW.GLFW_KEY_LEFT);
yawRightKey = key("key.cinematicreplay.camera_yaw_right", GLFW.GLFW_KEY_RIGHT);
pitchUpKey = key("key.cinematicreplay.camera_pitch_up", GLFW.GLFW_KEY_PAGE_UP);
pitchDownKey = key("key.cinematicreplay.camera_pitch_down", GLFW.GLFW_KEY_PAGE_DOWN);
speedUpKey = key("key.cinematicreplay.speed_up", GLFW.GLFW_KEY_KP_ADD);
speedDownKey = key("key.cinematicreplay.speed_down", GLFW.GLFW_KEY_KP_SUBTRACT);
fovIncreaseKey = key("key.cinematicreplay.fov_increase", GLFW.GLFW_KEY_RIGHT_BRACKET);
fovDecreaseKey = key("key.cinematicreplay.fov_decrease", GLFW.GLFW_KEY_LEFT_BRACKET);
pauseIncreaseKey = key("key.cinematicreplay.pause_increase", GLFW.GLFW_KEY_EQUAL);
pauseDecreaseKey = key("key.cinematicreplay.pause_decrease", GLFW.GLFW_KEY_MINUS);
ClientTickEvents.END_CLIENT_TICK.register(CinematicReplayClient::onClientTick);
}
private static KeyMapping key(String name, int keyCode) {
return KeyMappingHelper.registerKeyMapping(new KeyMapping(name, keyCode, KeyMapping.Category.MISC));
}
private static void onClientTick(Minecraft client) {
checkVideoCompletion(client);
if (recordKey.consumeClick()) {
if (recordingOutput == null) {
startRecording(client);
} else {
stopRecording(client);
}
}
if (cameraKey.consumeClick()) {
toggleCamera(client);
}
if (waypointKey.consumeClick()) {
saveWaypoint(client);
}
if (pathKey.consumeClick()) {
togglePath(client);
}
if (videoKey.consumeClick()) {
toggleVideo(client);
}
if (videoStopKey.consumeClick()) {
stopVideo(client);
}
if (hudKey.consumeClick()) {
hideHud = !hideHud;
message(client, "HUD " + (hideHud ? "hidden" : "shown") + ".");
}
if (recordingOutput != null && client.level != null) {
writePlayerSample(client.level);
}
if (detachedCamera) {
if (routePlaying) {
updateRoute();
} else {
updateManualCamera();
}
}
}
private static void startRecording(Minecraft client) {
try {
Path directory = replayDirectory(client);
Files.createDirectories(directory);
recordingPath = directory.resolve("players-" + timestamp() + ".crp");
recordingOutput = new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(recordingPath)));
recordingOutput.writeInt(RECORDING_MAGIC);
recordingOutput.writeInt(FORMAT_VERSION);
recordingStartNanos = System.nanoTime();
recordingTicks = 0;
message(client, "Player recording started: " + recordingPath.getFileName());
} catch (IOException exception) {
recordingOutput = null;
recordingPath = null;
message(client, "Could not start recording: " + exception.getMessage());
}
}
private static void writePlayerSample(ClientLevel level) {
try {
List<AbstractClientPlayer> players = level.players();
recordingOutput.writeLong(System.nanoTime() - recordingStartNanos);
recordingOutput.writeInt(players.size());
for (Player player : players) {
recordingOutput.writeLong(player.getUUID().getMostSignificantBits());
recordingOutput.writeLong(player.getUUID().getLeastSignificantBits());
recordingOutput.writeUTF(player.getName().getString());
recordingOutput.writeDouble(player.getX());
recordingOutput.writeDouble(player.getY());
recordingOutput.writeDouble(player.getZ());
recordingOutput.writeFloat(player.getYRot());
recordingOutput.writeFloat(player.getXRot());
recordingOutput.writeBoolean(player.isSprinting());
recordingOutput.writeBoolean(player.isCrouching());