From bba6ac70390db9374ef8fc90005121db68823d76 Mon Sep 17 00:00:00 2001 From: Scribble Date: Thu, 15 Feb 2024 14:00:44 +0100 Subject: [PATCH] [VirtualInput] More documentation and tests - [Util] Added documentation - [Util] Refactored PointerNormalizer - [Gui] Disabled trajectories from InfoHud - [VirtualInput] Refactored MixinEntityRenderer --- .../com/minecrafttas/tasmod/gui/InfoHud.java | 24 +++--- .../playbackhooks/MixinEntityRenderer.java | 57 +++++++------ .../com/minecrafttas/tasmod/util/Ducks.java | 4 + .../minecrafttas/tasmod/util/FileThread.java | 9 +- .../tasmod/util/PointerNormalizer.java | 84 ++++++++++--------- .../minecrafttas/tasmod/util/Scheduler.java | 1 - .../tasmod/util/ShieldDownloader.java | 5 ++ .../tasmod/virtual/VirtualCameraAngle.java | 49 +++++++++-- .../tasmod/virtual/VirtualInput.java | 54 +++++++++--- .../tasmod/virtual/VirtualKeyboard.java | 2 +- .../tasmod/virtual/VirtualMouse.java | 31 ++++--- .../virtual/VirtualCameraAngleTest.java | 77 +++++++++++++++++ 12 files changed, 282 insertions(+), 115 deletions(-) create mode 100644 src/test/java/tasmod/virtual/VirtualCameraAngleTest.java diff --git a/src/main/java/com/minecrafttas/tasmod/gui/InfoHud.java b/src/main/java/com/minecrafttas/tasmod/gui/InfoHud.java index ae30b176..d3fc1ccd 100644 --- a/src/main/java/com/minecrafttas/tasmod/gui/InfoHud.java +++ b/src/main/java/com/minecrafttas/tasmod/gui/InfoHud.java @@ -350,18 +350,18 @@ public boolean checkInit() { // return String.format("Mouse Cursor: " + TASmodClient.virtual.getNextMouse().getPath().get(0).cursorX + " " + TASmodClient.virtual.getNextMouse().getPath().get(0).cursorY); // })); TODO Remove? - title = "trajectories"; - y += 14; - if (configuration.getProperty(title + "_x", "err").equals("err")) setDefaults(title, y); - lists.add(new InfoLabel(title, Integer.parseInt(configuration.getProperty(title + "_x")), Integer.parseInt(configuration.getProperty(title + "_y")), Boolean.parseBoolean(configuration.getProperty(title + "_visible")), Boolean.parseBoolean(configuration.getProperty(title + "_rect")), () -> { - if (Minecraft.getMinecraft().currentScreen == this) return "Trajectories"; - String message = "Invalid Item"; - Vec3d vec = TrajectoriesCalculator.calculate(); - if (vec != null) { - message = String.format("%.3f %.3f %.3f", vec.x, vec.y, vec.z); - } - return String.format("Trajectories: " + message); - })); +// title = "trajectories"; +// y += 14; +// if (configuration.getProperty(title + "_x", "err").equals("err")) setDefaults(title, y); +// lists.add(new InfoLabel(title, Integer.parseInt(configuration.getProperty(title + "_x")), Integer.parseInt(configuration.getProperty(title + "_y")), Boolean.parseBoolean(configuration.getProperty(title + "_visible")), Boolean.parseBoolean(configuration.getProperty(title + "_rect")), () -> { +// if (Minecraft.getMinecraft().currentScreen == this) return "Trajectories"; +// String message = "Invalid Item"; +// Vec3d vec = TrajectoriesCalculator.calculate(); +// if (vec != null) { +// message = String.format("%.3f %.3f %.3f", vec.x, vec.y, vec.z); +// } +// return String.format("Trajectories: " + message); +// })); title = "velocity"; y += 14; diff --git a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinEntityRenderer.java b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinEntityRenderer.java index f4352ec7..70909a6d 100644 --- a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinEntityRenderer.java +++ b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinEntityRenderer.java @@ -6,6 +6,7 @@ import com.minecrafttas.tasmod.util.Ducks.SubtickDuck; import com.minecrafttas.tasmod.virtual.VirtualInput; import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.renderer.EntityRenderer; import net.minecraft.util.math.MathHelper; import org.lwjgl.input.Mouse; @@ -16,11 +17,17 @@ import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; /** * Redirects the camera to use {@link VirtualInput.VirtualCameraAngleInput}.
- * Also conforms the camera to 20tps as + * To support handling the camera in TASes and to avoid desyncs via lag,
+ * it was decided to only update the camera every tick.
+ *
+ * To achieve this, some parts of the vanilla code were disabled, but get called every tick in {@link #runUpdate(float)} + * + * @author Scribble, Pancake */ @Mixin(EntityRenderer.class) public class MixinEntityRenderer implements SubtickDuck { @@ -63,8 +70,8 @@ public void playback_injectAtStartSection(float partialTicks, long nanoTime, Cal dX = 0; dY = 0; } else { - // Comment this out to disable interpolation, also comment out @SubscribeEvent - // in InterpolationEvents + // Comment this out to disable interpolation + // - Pancake if (this.mc.currentScreen == null) { InterpolationHandler.rotationYaw = ((float) ((double) InterpolationHandler.rotationYaw + (double) mc.mouseHelper.deltaX * f1 * 0.15D)); InterpolationHandler.rotationPitch = (float) ((double) InterpolationHandler.rotationPitch - (double) mc.mouseHelper.deltaY * f1 * 0.15D); @@ -73,47 +80,43 @@ public void playback_injectAtStartSection(float partialTicks, long nanoTime, Cal } } - @ModifyExpressionValue(method = "updateCameraAndRender", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;inGameHasFocus:Z", opcode = Opcodes.GETFIELD)) - public boolean playback_stopVanilla(boolean original){ - return original && TASmodClient.tickratechanger.ticksPerSecond == 0; + @Redirect(method = "updateCameraAndRender", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/EntityPlayerSP;turn(FF)V")) + public void playback_stopVanilla(EntityPlayerSP player, float deltaYaw, float deltaPitch){ + if(TASmodClient.tickratechanger.ticksPerSecond == 0){ + player.turn(deltaYaw, deltaPitch); + } } @Override public void runUpdate(float partialTicks) { - boolean flag = Display.isActive(); - if (flag && Minecraft.IS_RUNNING_ON_MAC && mc.inGameHasFocus && !Mouse.isInsideWindow()) { - Mouse.setGrabbed(false); - Mouse.setCursorPosition(Display.getWidth() / 2, Display.getHeight() / 2 - 20); - Mouse.setGrabbed(true); - } + boolean isDisplayActive = Display.isActive(); - if (mc.inGameHasFocus && flag) { - mc.getTutorial().handleMouse(mc.mouseHelper); - float f = mc.gameSettings.mouseSensitivity * 0.6F + 0.2F; - float f1 = f * f * f * 8.0F; - float f2 = (float) dX * f1; - float f3 = (float) dY * f1; - int i = 1; + if (mc.inGameHasFocus && isDisplayActive) { + float mouseSensitivity = mc.gameSettings.mouseSensitivity * 0.6F + 0.2F; + float cubedSensitivity = mouseSensitivity * mouseSensitivity * mouseSensitivity * 8.0F; + float yawDelta = (float) dX * cubedSensitivity; + float pitchDelta = (float) dY * cubedSensitivity; + int isInverted = 1; dX = 0; dY = 0; if (mc.gameSettings.invertMouse) { - i = -1; + isInverted = -1; } if (mc.gameSettings.smoothCamera) { - smoothCamYaw += f2; - smoothCamPitch += f3; - float f4 = partialTicks - smoothCamPartialTicks; + smoothCamYaw += yawDelta; + smoothCamPitch += pitchDelta; + float smoothCamTimerframe = partialTicks - smoothCamPartialTicks; smoothCamPartialTicks = partialTicks; - f2 = smoothCamFilterX * f4; - f3 = smoothCamFilterY * f4; - mc.player.turn(f2, f3 * (float) i); + yawDelta = smoothCamFilterX * smoothCamTimerframe; + pitchDelta = smoothCamFilterY * smoothCamTimerframe; + mc.player.turn(yawDelta, pitchDelta * (float) isInverted); } else { smoothCamYaw = 0.0F; smoothCamPitch = 0.0F; - mc.player.turn(f2, f3 * (float) i); + mc.player.turn(yawDelta, pitchDelta * (float) isInverted); } TASmodClient.virtual.CAMERA_ANGLE.updateCameraAngle(mc.player.rotationPitch, mc.player.rotationYaw); mc.player.rotationPitch = TASmodClient.virtual.CAMERA_ANGLE.getPitch(); diff --git a/src/main/java/com/minecrafttas/tasmod/util/Ducks.java b/src/main/java/com/minecrafttas/tasmod/util/Ducks.java index 5b785a6a..533affea 100644 --- a/src/main/java/com/minecrafttas/tasmod/util/Ducks.java +++ b/src/main/java/com/minecrafttas/tasmod/util/Ducks.java @@ -81,6 +81,10 @@ public static interface GuiScreenDuck { * Quacks the subtick */ public static interface SubtickDuck { + /** + * Custom updating method for EntityRenderer, updating the player rotation + * @param partialTicks The partial ticks from the vanilla Minecraft timer + */ void runUpdate(float partialTicks); } diff --git a/src/main/java/com/minecrafttas/tasmod/util/FileThread.java b/src/main/java/com/minecrafttas/tasmod/util/FileThread.java index cdf61425..759c5af1 100644 --- a/src/main/java/com/minecrafttas/tasmod/util/FileThread.java +++ b/src/main/java/com/minecrafttas/tasmod/util/FileThread.java @@ -9,12 +9,17 @@ import java.util.ArrayList; import java.util.List; +/** + * Thread for writing files to disc + * + * @author Pancake + */ public class FileThread extends Thread { - private PrintWriter stream; + private final PrintWriter stream; private boolean end = false; - private List output = new ArrayList(); + private final List output = new ArrayList<>(); public FileThread(File fileLocation, boolean append) throws FileNotFoundException { stream = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileLocation, append), StandardCharsets.UTF_8)); diff --git a/src/main/java/com/minecrafttas/tasmod/util/PointerNormalizer.java b/src/main/java/com/minecrafttas/tasmod/util/PointerNormalizer.java index e9136f0d..12a6d2a2 100644 --- a/src/main/java/com/minecrafttas/tasmod/util/PointerNormalizer.java +++ b/src/main/java/com/minecrafttas/tasmod/util/PointerNormalizer.java @@ -5,37 +5,41 @@ import net.minecraft.client.gui.GuiWorldSelection; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.gui.inventory.GuiContainer; -import net.minecraft.client.gui.inventory.GuiContainerCreative; /** - * Adjusts the pointer/cursor of the playback to different gui scalings. - * - * This was the work of many hours of trial and error. - * - * Out of despair I reached out to Darkmoon to help me with this problem... - * - * @author ScribbleLP, Darkmoon + * Normalizes the cursor to be independent of gui scalings.
+ * That way, a TAS recorded in e.g. Gui Scale "Large" can also be played back on Gui Scale "Small" * + * @author Scribble, Darkmoon */ public class PointerNormalizer { + /** + * Mathematically removes scaling from the x coordinate + * @param pointerX The current pointer coordinate + * @return The normalized x coordinate + */ public static int getNormalizedX(int pointerX) { Minecraft mc = Minecraft.getMinecraft(); ScaledResolution scaled = new ScaledResolution(mc); - int out = (int) (pointerX - (scaled.getScaledWidth() / 2D)); - return out; + return (int) (pointerX - (scaled.getScaledWidth() / 2D)); } + /** + * Mathematically removes scaling from the y coordinate + * @param pointerY The current pointer coordinate + * @return The normalized y coordinate + */ public static int getNormalizedY(int pointerY) { Minecraft mc = Minecraft.getMinecraft(); ScaledResolution scaled = new ScaledResolution(mc); int out = pointerY; - if (mc.currentScreen instanceof GuiContainer || mc.currentScreen instanceof GuiContainerCreative) { + if (mc.currentScreen instanceof GuiContainer) { out = (int) (pointerY - (scaled.getScaledHeight() / 2D)); } else if (mc.currentScreen instanceof GuiWorldSelection|| mc.currentScreen instanceof GuiMultiplayer) { - + // TODO Figure out what to do here } else { out = (int) (pointerY - (scaled.getScaledHeight() / 4 + 72 + -16)); } @@ -43,62 +47,62 @@ public static int getNormalizedY(int pointerY) { return out; } - public static int getCoordsX(int normalizedX) { + /** + * Reapplies gui scaling to the normalized pointer x coordinate + * @param normalizedX The normalized pointer coordinate + * @return The scaled coordinate + */ + public static int reapplyScalingX(int normalizedX) { Minecraft mc = Minecraft.getMinecraft(); ScaledResolution scaled = new ScaledResolution(mc); int out = (int) Math.round(normalizedX + (scaled.getScaledWidth() / 2D)); - return limiterX(out, scaled); + return clamp(out, 0, scaled.getScaledWidth()); } - public static int getCoordsY(int normalizedY) { + /** + * Reapplies gui scaling to the normalized pointer y coordinate + * @param normalizedY The normalized pointer coordinate + * @return The scaled coordinate + */ + public static int reapplyScalingY(int normalizedY) { Minecraft mc = Minecraft.getMinecraft(); ScaledResolution scaled = new ScaledResolution(mc); int out = normalizedY; - if (mc.currentScreen instanceof GuiContainer || mc.currentScreen instanceof GuiContainerCreative) { + if (mc.currentScreen instanceof GuiContainer) { out = (int) Math.round(normalizedY + (scaled.getScaledHeight() / 2D)); } else if (mc.currentScreen instanceof GuiWorldSelection || mc.currentScreen instanceof GuiMultiplayer) { - + // TODO Figure out what to do here } else { out = (int) (normalizedY + (scaled.getScaledHeight() / 4 + 72 + -16)); } - return limiterY(out, scaled); - } - - private static int limiterX(int out, ScaledResolution scaled) { - int width = scaled.getScaledWidth(); - if (out > width) { - out = width; - } else if (out < 0) - out = 0; - return out; - } - - private static int limiterY(int out, ScaledResolution scaled) { - int height = scaled.getScaledHeight(); - if (out > height) { - out = height; - } else if (out < 0) - out = 0; - return out; + return clamp(out, 0, scaled.getScaledHeight()); } - private static int gcd(int a, int b) { - return (b == 0) ? a : gcd(b, a % b); + private static int clamp(int value, int lower, int upper) { + if (value < lower) { + return lower; + } else { + return Math.min(value, upper); + } } public static void printAspectRatio() { int height = Minecraft.getMinecraft().displayHeight; int width = Minecraft.getMinecraft().displayWidth; - int gcd = gcd(width, height); + int gcd = greatestCommonDivisor(width, height); if (gcd == 0) { System.out.println(gcd); } else { System.out.println(width / gcd + ":" + height / gcd); } } - + + private static int greatestCommonDivisor(int a, int b) { + return (b == 0) ? a : greatestCommonDivisor(b, a % b); + } + /* * Here lies 10 hours of work for something I didn't even use. This code * normalizes the pointers coordinates and scales it depending on the screen diff --git a/src/main/java/com/minecrafttas/tasmod/util/Scheduler.java b/src/main/java/com/minecrafttas/tasmod/util/Scheduler.java index fd13454b..9e462b3a 100644 --- a/src/main/java/com/minecrafttas/tasmod/util/Scheduler.java +++ b/src/main/java/com/minecrafttas/tasmod/util/Scheduler.java @@ -7,7 +7,6 @@ * A simple scheduling interface * * @author Scribble - * */ public class Scheduler { diff --git a/src/main/java/com/minecrafttas/tasmod/util/ShieldDownloader.java b/src/main/java/com/minecrafttas/tasmod/util/ShieldDownloader.java index 4f514c1e..2588c4e1 100644 --- a/src/main/java/com/minecrafttas/tasmod/util/ShieldDownloader.java +++ b/src/main/java/com/minecrafttas/tasmod/util/ShieldDownloader.java @@ -30,6 +30,11 @@ import net.minecraft.entity.EntityLivingBase; import net.minecraft.util.ResourceLocation; +/** + * Downloads shield textures from + *
+ * Unlike {@link VirtualKeyboard} and {@link VirtualMouse}, no subticking functionality is implemented currently.
+ * Uses absolute camera angle coordinates (Like you see in the debug F3 menu) as values instead of deltas.
+ * This makes it independent of mouse sensitivity. + */ public class VirtualCameraAngle implements Serializable { - private Float pitch; - private Float yaw; - + /** + * Controls the up/down coordinate of the camera. Clamped between -90 and +90 + */ + private float pitch; + /** + * Controls the left/right coordinate of the camera. In this case the camera is clamped between -180 and +180 + */ + private float yaw; + /** + * Creates an empty camera angle with pitch and yaw = 0 + */ public VirtualCameraAngle() { - this(null, null); + this(0, 0); } - public VirtualCameraAngle(Float pitch, Float yaw) { + /** + * Creates a camera angle with initialized values + * @param pitch {@link VirtualCameraAngle#pitch} + * @param yaw {@link VirtualCameraAngle#yaw} + */ + public VirtualCameraAngle(float pitch, float yaw) { this.pitch = pitch; this.yaw = yaw; } - public void update(Float pitch, Float yaw) { + /** + * Updates the current camera angle + * @param pitch {@link VirtualCameraAngle#pitch} + * @param yaw {@link VirtualCameraAngle#yaw} + */ + public void update(float pitch, float yaw) { this.pitch = pitch; this.yaw = yaw; } - - public Float getPitch() { + + /** + * @return {@link VirtualCameraAngle#pitch} + */ + public float getPitch() { return pitch; } - public Float getYaw() { + /** + * @return {@link VirtualCameraAngle#yaw} + */ + public float getYaw() { return yaw; } diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput.java index fc8231cf..f625a84c 100644 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput.java +++ b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput.java @@ -15,6 +15,7 @@ import com.minecrafttas.tasmod.util.PointerNormalizer; import net.minecraft.client.gui.GuiScreen; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; /** * Main component for redirecting inputs.
@@ -378,8 +379,9 @@ public VirtualMouseInput(VirtualMouse preloadedMouse) { * @see VirtualInput#update(GuiScreen) * @param keycode The keycode of this event * @param keystate The keystate of this event - * @param character The character of this event - * @param repeatEventsEnabled If repeat events are enabled + * @param scrollwheel The scrollwheel direction of this event + * @param cursorX The x coordinate of the cursor + * @param cursorY The y coordinate of the cursor */ public void updateNextMouse(int keycode, boolean keystate, int scrollwheel, int cursorX, int cursorY) { keycode-=100; @@ -435,14 +437,14 @@ public int getEventMouseScrollWheel() { * @return The x coordinate of the cursor of {@link #currentMouseEvent} */ public int getEventCursorX() { - return PointerNormalizer.getCoordsX(currentMouseEvent.getCursorX()); + return PointerNormalizer.reapplyScalingX(currentMouseEvent.getCursorX()); } /** * @return The y coordinate of the cursor of {@link #currentMouseEvent} */ public int getEventCursorY() { - return PointerNormalizer.getCoordsY(currentMouseEvent.getCursorY()); + return PointerNormalizer.reapplyScalingY(currentMouseEvent.getCursorY()); } /** @@ -466,23 +468,51 @@ public boolean willKeyBeDown(int keycode) { } + /** + * Subclass of {@link VirtualInput} handling camera angle logic.
+ *
+ * Unlike {@link VirtualKeyboardInput} or {@link VirtualMouseInput} no subtick behaviour is implemented,
+ * making this a simple pitch and yaw storing class, allowing for redirection. + *
+ * In theory, subtick behaviour is possible, but only useful for interpolation,
+ * as the camera angle is only updated every tick (see {@link com.minecrafttas.tasmod.mixin.playbackhooks.MixinEntityRenderer}). + */ public class VirtualCameraAngleInput { - private final VirtualCameraAngle cameraAngle; + /** + * The current camera angle + */ + private final VirtualCameraAngle currentCameraAngle; + /** + * Constructor to preload the {@link #currentCameraAngle} with an existing camera angle + * @param preloadedCamera The new {@link #currentCameraAngle} + */ public VirtualCameraAngleInput(VirtualCameraAngle preloadedCamera) { - cameraAngle = preloadedCamera; + currentCameraAngle = preloadedCamera; } - + + /** + * Update the camera angle + * @see com.minecrafttas.tasmod.mixin.playbackhooks.MixinEntityRenderer#runUpdate(float); + * @param pitch Absolute rotationPitch of the player + * @param yaw Absolute rotationYaw of the player + */ public void updateCameraAngle(float pitch, float yaw) { - cameraAngle.update(pitch, yaw); + currentCameraAngle.update(pitch, yaw); } - + + /** + * @return {@link VirtualCameraAngle#pitch} + */ public float getPitch() { - return cameraAngle.getPitch(); + return currentCameraAngle.getPitch(); } - + + /** + * @return {@link VirtualCameraAngle#yaw} + */ public float getYaw() { - return cameraAngle.getYaw(); + return currentCameraAngle.getYaw(); } } } diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard.java index ae96365d..d12bea00 100644 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard.java +++ b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard.java @@ -252,7 +252,7 @@ public void addChar(char character, boolean repeatEventsEnabled) { } @Override - protected void clear(){ + public void clear(){ super.clear(); charList.clear(); } diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse.java index d2e7c552..8f24b1af 100644 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse.java +++ b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse.java @@ -6,6 +6,15 @@ import java.util.*; import java.util.stream.Collectors; +/** + * Stores the mouse specific values in a given timeframe
+ *
+ * Similar to {@link VirtualKeyboard}, but instead of a list of characters,
+ * it stores the state of the scroll wheel and the cursors x and y coordinate on screen. + * + * @author Scribble + * @see VirtualInput.VirtualMouseInput + */ public class VirtualMouse extends VirtualPeripheral implements Serializable { /** @@ -123,25 +132,25 @@ public void getVirtualEvents(VirtualMouse nextMouse, Queue re * the one from tick 16 * @param reference The queue to fill. Passed in by reference. */ - public void getDifference(VirtualMouse nextPeripheral, Queue reference) { + public void getDifference(VirtualMouse nextMouse, Queue reference) { /* * Checks if pressedKeys are the same... */ - if(pressedKeys.equals(nextPeripheral.pressedKeys)){ + if(pressedKeys.equals(nextMouse.pressedKeys)){ - /** + /* * ...but scrollWheel, cursorX or cursorY are different. * Without this, the scrollWheel would only work if a mouse button is pressed at the same time. */ - if(!equals(nextPeripheral)) { - reference.add(new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, nextPeripheral.scrollWheel, nextPeripheral.cursorX, nextPeripheral.cursorY)); + if(!equals(nextMouse)) { + reference.add(new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, nextMouse.scrollWheel, nextMouse.cursorX, nextMouse.cursorY)); } return; } - int scrollWheelCopy = nextPeripheral.scrollWheel; - int cursorXCopy = nextPeripheral.cursorX; - int cursorYCopy = nextPeripheral.cursorY; + int scrollWheelCopy = nextMouse.scrollWheel; + int cursorXCopy = nextMouse.cursorX; + int cursorYCopy = nextMouse.cursorY; /* Calculate symmetric difference of keycodes */ @@ -153,7 +162,7 @@ public void getDifference(VirtualMouse nextPeripheral, Queue RC <- unpressed */ for(int keycode : pressedKeys) { - if (!nextPeripheral.getPressedKeys().contains(keycode)) { + if (!nextMouse.getPressedKeys().contains(keycode)) { reference.add(new VirtualMouseEvent(keycode, false, scrollWheelCopy, cursorXCopy, cursorYCopy)); scrollWheelCopy = 0; cursorXCopy = 0; @@ -168,7 +177,7 @@ public void getDifference(VirtualMouse nextPeripheral, Queue ------------- MC <- pressed */ - for(int keycode : nextPeripheral.getPressedKeys()) { + for(int keycode : nextMouse.getPressedKeys()) { if (!this.pressedKeys.contains(keycode)) { reference.add(new VirtualMouseEvent(keycode, true, scrollWheelCopy, cursorXCopy, cursorYCopy)); } @@ -176,7 +185,7 @@ public void getDifference(VirtualMouse nextPeripheral, Queue } @Override - protected void clear() { + public void clear() { super.clear(); clearMouseData(); } diff --git a/src/test/java/tasmod/virtual/VirtualCameraAngleTest.java b/src/test/java/tasmod/virtual/VirtualCameraAngleTest.java new file mode 100644 index 00000000..43575098 --- /dev/null +++ b/src/test/java/tasmod/virtual/VirtualCameraAngleTest.java @@ -0,0 +1,77 @@ +package tasmod.virtual; + +import com.minecrafttas.tasmod.virtual.VirtualCameraAngle; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +public class VirtualCameraAngleTest { + + /** + * Test the empty constructor + */ + @Test + void testEmptyConstructor(){ + VirtualCameraAngle actual = new VirtualCameraAngle(); + assertEquals(0f, actual.getPitch()); + assertEquals(0f, actual.getYaw()); + } + + /** + * Test constructor with premade pitch and value + */ + @Test + void testConstructor(){ + float x = 1f; + float y = 2f; + + VirtualCameraAngle actual = new VirtualCameraAngle(x, y); + assertEquals(1f, actual.getPitch()); + assertEquals(2f, actual.getYaw()); + } + + /** + * Testing update function + */ + @Test + void testUpdate(){ + float x = 1f; + float y = 2f; + + VirtualCameraAngle actual = new VirtualCameraAngle(); + + actual.update(x, y); + + assertEquals(1f, actual.getPitch()); + assertEquals(2f, actual.getYaw()); + } + + /** + * Test cloning the camera angle + */ + @Test + void testClone(){ + float x = 1f; + float y = 2f; + + VirtualCameraAngle test = new VirtualCameraAngle(x, y); + + VirtualCameraAngle actual = test.clone(); + + assertEquals(1f, actual.getPitch()); + assertEquals(2f, actual.getYaw()); + } + + /** + * Test equals + */ + @Test + void testEquals(){ + float x = 1f; + float y = 2f; + + VirtualCameraAngle test = new VirtualCameraAngle(x, y); + VirtualCameraAngle test2 = new VirtualCameraAngle(x, y); + + assertEquals(test, test2); + } +}