Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 158
src/main/java/pl/kodari/mouserecorder/MouseRecorderClient.java
package pl.kodari.mouserecorder;
import java.util.ArrayList;
import java.util.List;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
public class MouseRecorderClient implements ClientModInitializer {
private static final int MAX_SAMPLES = 20 * 60 * 5;
private final List<Point> recording = new ArrayList<>();
private boolean recordingActive;
private boolean playing;
private boolean previousRecordKey;
private boolean previousPlayKey;
private int playIndex;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(this::tick);
}
private void tick(MinecraftClient client) {
long window = client.getWindow().getHandle();
boolean focused = GLFW.glfwGetWindowAttrib(window, GLFW.GLFW_FOCUSED) == GLFW.GLFW_TRUE;
boolean recordKey = focused && GLFW.glfwGetKey(window, GLFW.GLFW_KEY_F6) == GLFW.GLFW_PRESS;
boolean playKey = focused && GLFW.glfwGetKey(window, GLFW.GLFW_KEY_F7) == GLFW.GLFW_PRESS;
if (playKey && !previousPlayKey && playing) {
playing = false;
notify(client, "Odtwarzanie zatrzymane.");
} else if (!focused || client.currentScreen == null || client.mouse.isCursorLocked()) {
if (recordingActive) {
recordingActive = false;
notify(client, "Nagrywanie zatrzymane: otwórz ekran Minecrafta.");
}
} else {
if (recordKey && !previousRecordKey && !playing) {
if (recordingActive) {
recordingActive = false;
notify(client, "Nagrywanie zakończone: " + recording.size() + " próbek.");
} else {
recording.clear();
recordingActive = true;
notify(client, "Nagrywanie rozpoczęte. Naciśnij F6, aby zakończyć.");
}
}
if (playKey && !previousPlayKey) {
if (!recording.isEmpty()) {
recordingActive = false;
playing = true;
playIndex = 0;
notify(client, "Odtwarzanie rozpoczęte. Naciśnij F7, aby zatrzymać.");
} else {
notify(client, "Najpierw nagraj ruch za pomocą F6.");
}
}
if (recordingActive) {
double[] x = new double[1];
double[] y = new double[1];
GLFW.glfwGetCursorPos(window, x, y);
recording.add(new Point(x[0], y[0]));
if (recording.size() >= MAX_SAMPLES) {
recordingActive = false;
notify(client, "Osiągnięto limit nagrania: 5 minut.");
}
} else if (playing) {
Point point = recording.get(playIndex++);
GLFW.glfwSetCursorPos(window, point.x(), point.y());
if (playIndex >= recording.size()) {
playIndex = 0;
}
}
}
previousRecordKey = recordKey;
previousPlayKey = playKey;
}
private void notify(MinecraftClient client, String message) {
if (client.player != null) {
client.player.sendMessage(Text.literal(message), false);
}
}
private record Point(double x, double y) {}
}