From 9b615c7a50a873146e0a78808f64187e7f28381b Mon Sep 17 00:00:00 2001 From: Scribble Date: Thu, 8 Feb 2024 11:48:54 +0100 Subject: [PATCH] [VirtualInput] Removing old VirtualInput --- .../com/minecrafttas/tasmod/TASmodClient.java | 6 +- .../playbackhooks/MixinEntityRenderer.java | 3 +- .../mixin/playbackhooks/MixinGuiChat.java | 3 +- ...uiClickableScrolledSelectionListProxy.java | 3 +- .../playbackhooks/MixinGuiContainer.java | 6 +- .../mixin/playbackhooks/MixinGuiScreen.java | 12 +- .../mixin/playbackhooks/MixinMinecraft.java | 48 +- .../playback/PlaybackControllerClient.java | 8 +- .../tasmod/playback/PlaybackSerialiser.java | 146 ++- .../tasmod/virtual/VirtualCameraAngle.java | 34 +- .../tasmod/virtual/VirtualCameraAngle2.java | 50 - .../tasmod/virtual/VirtualInput.java | 946 ++++++++---------- .../tasmod/virtual/VirtualInput2.java | 486 --------- .../tasmod/virtual/VirtualKey.java | 244 +++-- .../tasmod/virtual/VirtualKey2.java | 189 ---- .../tasmod/virtual/VirtualKeyboard.java | 648 ++++++------ .../tasmod/virtual/VirtualKeyboard2.java | 323 ------ .../tasmod/virtual/VirtualMouse.java | 508 ++++------ .../tasmod/virtual/VirtualMouse2.java | 254 ----- .../tasmod/virtual/VirtualPeripheral.java | 9 +- .../virtual/{ => event}/VirtualEvent.java | 2 +- .../{ => event}/VirtualKeyboardEvent.java | 4 +- .../{ => event}/VirtualMouseEvent.java | 4 +- .../java/tasmod/virtual/VirtualInputTest.java | 49 +- .../tasmod/virtual/VirtualKeyboardTest.java | 148 +-- .../java/tasmod/virtual/VirtualMouseTest.java | 180 ++-- 26 files changed, 1444 insertions(+), 2869 deletions(-) delete mode 100644 src/main/java/com/minecrafttas/tasmod/virtual/VirtualCameraAngle2.java delete mode 100644 src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput2.java delete mode 100644 src/main/java/com/minecrafttas/tasmod/virtual/VirtualKey2.java delete mode 100644 src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard2.java delete mode 100644 src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse2.java rename src/main/java/com/minecrafttas/tasmod/virtual/{ => event}/VirtualEvent.java (93%) rename src/main/java/com/minecrafttas/tasmod/virtual/{ => event}/VirtualKeyboardEvent.java (88%) rename src/main/java/com/minecrafttas/tasmod/virtual/{ => event}/VirtualMouseEvent.java (89%) diff --git a/src/main/java/com/minecrafttas/tasmod/TASmodClient.java b/src/main/java/com/minecrafttas/tasmod/TASmodClient.java index 279b608b..6e398c21 100644 --- a/src/main/java/com/minecrafttas/tasmod/TASmodClient.java +++ b/src/main/java/com/minecrafttas/tasmod/TASmodClient.java @@ -36,7 +36,7 @@ import com.minecrafttas.tasmod.util.LoggerMarkers; import com.minecrafttas.tasmod.util.Scheduler; import com.minecrafttas.tasmod.util.ShieldDownloader; -import com.minecrafttas.tasmod.virtual.VirtualInput2; +import com.minecrafttas.tasmod.virtual.VirtualInput; import com.minecrafttas.tasmod.virtual.VirtualKeybindings; import net.fabricmc.api.ClientModInitializer; @@ -52,7 +52,7 @@ public class TASmodClient implements ClientModInitializer, EventClientInit, EventPlayerJoinedClientSide, EventOpenGui{ - public static VirtualInput2 virtual; + public static VirtualInput virtual; public static TickSyncClient ticksyncClient; @@ -126,7 +126,7 @@ public void onInitializeClient() { } else { config.reset(ConfigOptions.FileToOpen); } - virtual=new VirtualInput2(LOGGER); //TODO Move fileOnStart to PlaybackController + virtual=new VirtualInput(LOGGER); //TODO Move fileOnStart to PlaybackController // Initialize InfoHud hud = new InfoHud(); 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 c556a640..f4352ec7 100644 --- a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinEntityRenderer.java +++ b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinEntityRenderer.java @@ -4,6 +4,7 @@ import com.minecrafttas.tasmod.TASmodClient; import com.minecrafttas.tasmod.handlers.InterpolationHandler; import com.minecrafttas.tasmod.util.Ducks.SubtickDuck; +import com.minecrafttas.tasmod.virtual.VirtualInput; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.EntityRenderer; import net.minecraft.util.math.MathHelper; @@ -18,7 +19,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; /** - * Redirects the camera to use {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualCameraAngleInput}.
+ * Redirects the camera to use {@link VirtualInput.VirtualCameraAngleInput}.
* Also conforms the camera to 20tps as */ @Mixin(EntityRenderer.class) diff --git a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiChat.java b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiChat.java index 7b76c7eb..c4b0880d 100644 --- a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiChat.java +++ b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiChat.java @@ -1,5 +1,6 @@ package com.minecrafttas.tasmod.mixin.playbackhooks; +import com.minecrafttas.tasmod.virtual.VirtualInput; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; @@ -11,7 +12,7 @@ @Mixin(GuiChat.class) public class MixinGuiChat { /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualMouseInput#getEventMouseScrollWheel()} + * @return {@link VirtualInput.VirtualMouseInput#getEventMouseScrollWheel()} */ @Redirect(method = "handleMouseInput", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Mouse;getEventDWheel()I", remap = false)) public int redirectHandleMouseInput4() { diff --git a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiClickableScrolledSelectionListProxy.java b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiClickableScrolledSelectionListProxy.java index 40d6c916..bfa57dd3 100644 --- a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiClickableScrolledSelectionListProxy.java +++ b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiClickableScrolledSelectionListProxy.java @@ -1,5 +1,6 @@ package com.minecrafttas.tasmod.mixin.playbackhooks; +import com.minecrafttas.tasmod.virtual.VirtualInput; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; @@ -12,7 +13,7 @@ public class MixinGuiClickableScrolledSelectionListProxy { /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualMouseInput#getEventMouseState()} + * @return {@link VirtualInput.VirtualMouseInput#getEventMouseState()} */ @Redirect(method = "handleMouseInput", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Mouse;getEventButtonState()Z", ordinal = 0, remap = false)) public boolean redirectHandleMouseInput() { diff --git a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiContainer.java b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiContainer.java index 106a88ed..97dbf995 100644 --- a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiContainer.java +++ b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiContainer.java @@ -5,7 +5,7 @@ import org.spongepowered.asm.mixin.injection.Redirect; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.virtual.VirtualKey2; +import com.minecrafttas.tasmod.virtual.VirtualKey; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; @@ -15,7 +15,7 @@ public class MixinGuiContainer { /** - * Redirects the check for {@link VirtualKey2#LSHIFT} and {@link VirtualKey2#RSHIFT} in mouseClicked + * Redirects the check for {@link VirtualKey#LSHIFT} and {@link VirtualKey#RSHIFT} in mouseClicked * @param i The keycode to check for * @return If the keycode is down */ @@ -25,7 +25,7 @@ private boolean redirectIsKeyDown(int i) { } /** - * Redirects the check for {@link VirtualKey2#LSHIFT} and {@link VirtualKey2#RSHIFT} in mouseReleased + * Redirects the check for {@link VirtualKey#LSHIFT} and {@link VirtualKey#RSHIFT} in mouseReleased * @param i The keycode to check for * @return If the keycode is down */ diff --git a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiScreen.java b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiScreen.java index 1cfb45c7..0a8a330b 100644 --- a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiScreen.java +++ b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinGuiScreen.java @@ -1,5 +1,6 @@ package com.minecrafttas.tasmod.mixin.playbackhooks; +import com.minecrafttas.tasmod.virtual.VirtualInput; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -10,8 +11,7 @@ import com.minecrafttas.tasmod.TASmodClient; import com.minecrafttas.tasmod.util.Ducks.GuiScreenDuck; -import com.minecrafttas.tasmod.virtual.VirtualInput2; -import com.minecrafttas.tasmod.virtual.VirtualKeyboardEvent; +import com.minecrafttas.tasmod.virtual.event.VirtualKeyboardEvent; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; @@ -21,7 +21,7 @@ public class MixinGuiScreen implements GuiScreenDuck { /** * Run at the start of run handleInput. Runs every tick. - * @see com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#nextKeyboardTick() + * @see VirtualInput.VirtualKeyboardInput#nextKeyboardTick() * @param ci CBI */ @Inject(method = "handleInput", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;isCreated()Z", shift = Shift.AFTER, remap = false)) @@ -30,9 +30,9 @@ public void injectAfterKeyboardCreated(CallbackInfo ci) { } /** - * Redirects a {@link org.lwjgl.input.Keyboard#next()}. Starts running every tick and continues as long as there are {@link VirtualKeyboardEvent}s in {@link VirtualInput2} - * @see com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#nextKeyboardSubtick() - * @return If {@link VirtualKeyboardEvent}s are present in {@link VirtualInput2} + * Redirects a {@link org.lwjgl.input.Keyboard#next()}. Starts running every tick and continues as long as there are {@link VirtualKeyboardEvent}s in {@link VirtualInput} + * @see VirtualInput.VirtualKeyboardInput#nextKeyboardSubtick() + * @return If {@link VirtualKeyboardEvent}s are present in {@link VirtualInput} */ @Redirect(method = "handleInput", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;next()Z", remap = false)) public boolean redirectKeyboardNext() { diff --git a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinMinecraft.java b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinMinecraft.java index 07885e89..8f01ad90 100644 --- a/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinMinecraft.java +++ b/src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinMinecraft.java @@ -1,5 +1,6 @@ package com.minecrafttas.tasmod.mixin.playbackhooks; +import com.minecrafttas.tasmod.virtual.VirtualInput; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -8,10 +9,9 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.virtual.VirtualInput2; -import com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualMouseInput; -import com.minecrafttas.tasmod.virtual.VirtualKeyboardEvent; -import com.minecrafttas.tasmod.virtual.VirtualMouseEvent; +import com.minecrafttas.tasmod.virtual.VirtualInput.VirtualMouseInput; +import com.minecrafttas.tasmod.virtual.event.VirtualKeyboardEvent; +import com.minecrafttas.tasmod.virtual.event.VirtualMouseEvent; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; @@ -24,7 +24,7 @@ public class MixinMinecraft { /** * Runs every frame. - * @see VirtualInput2#update(GuiScreen) + * @see VirtualInput#update(GuiScreen) * @param ci CBI */ @Inject(method = "runGameLoop", at = @At(value = "HEAD")) @@ -36,7 +36,7 @@ public void playback_injectRunGameLoop(CallbackInfo ci) { /** * Run at the start of run tick keyboard. Runs every tick. - * @see com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#nextKeyboardTick() + * @see VirtualInput.VirtualKeyboardInput#nextKeyboardTick() * @param ci CBI */ @Inject(method = "runTickKeyboard", at = @At(value = "HEAD")) @@ -45,9 +45,9 @@ public void playback_injectRunTickKeyboard(CallbackInfo ci) { } /** - * Redirects a {@link org.lwjgl.input.Keyboard#next()}. Starts running every tick and continues as long as there are {@link VirtualKeyboardEvent}s in {@link VirtualInput2} - * @see com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#nextKeyboardSubtick() - * @return If {@link VirtualKeyboardEvent}s are present in {@link VirtualInput2} + * Redirects a {@link org.lwjgl.input.Keyboard#next()}. Starts running every tick and continues as long as there are {@link VirtualKeyboardEvent}s in {@link VirtualInput} + * @see VirtualInput.VirtualKeyboardInput#nextKeyboardSubtick() + * @return If {@link VirtualKeyboardEvent}s are present in {@link VirtualInput} */ @Redirect(method = "runTickKeyboard", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;next()Z", remap = false)) public boolean playback_redirectKeyboardNext() { @@ -55,7 +55,7 @@ public boolean playback_redirectKeyboardNext() { } /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#getEventKeyboardKey()} + * @return {@link VirtualInput.VirtualKeyboardInput#getEventKeyboardKey()} */ @Redirect(method = "runTickKeyboard", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventKey()I", remap = false)) public int playback_redirectKeyboardGetEventKey() { @@ -63,7 +63,7 @@ public int playback_redirectKeyboardGetEventKey() { } /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#getEventKeyboardState()} + * @return {@link VirtualInput.VirtualKeyboardInput#getEventKeyboardState()} */ @Redirect(method = "runTickKeyboard", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventKeyState()Z", remap = false)) public boolean playback_redirectGetEventState() { @@ -71,7 +71,7 @@ public boolean playback_redirectGetEventState() { } /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#getEventKeyboardCharacter()} + * @return {@link VirtualInput.VirtualKeyboardInput#getEventKeyboardCharacter()} */ @Redirect(method = "runTickKeyboard", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventCharacter()C", remap = false)) public char playback_redirectKeyboardGetEventCharacter() { @@ -80,8 +80,8 @@ public char playback_redirectKeyboardGetEventCharacter() { /** * Runs everytime {@link #playback_redirectKeyboardNext()} has an event ready. Redirects {@link org.lwjgl.input.Keyboard#isKeyDown(int)} - * @see com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#isKeyDown(int) - * @return Whether the key is down in {@link VirtualInput2} + * @see VirtualInput.VirtualKeyboardInput#isKeyDown(int) + * @return Whether the key is down in {@link VirtualInput} */ @Redirect(method = "runTickKeyboard", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;isKeyDown(I)Z", remap = false)) public boolean playback_redirectIsKeyDown(int keyCode) { @@ -89,7 +89,7 @@ public boolean playback_redirectIsKeyDown(int keyCode) { } /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#getEventKeyboardKey()} + * @return {@link VirtualInput.VirtualKeyboardInput#getEventKeyboardKey()} */ @Redirect(method = "dispatchKeypresses", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventKey()I", remap = false)) public int playback_redirectGetEventKeyDPK() { @@ -97,7 +97,7 @@ public int playback_redirectGetEventKeyDPK() { } /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#getEventKeyboardState()} + * @return {@link VirtualInput.VirtualKeyboardInput#getEventKeyboardState()} */ @Redirect(method = "dispatchKeypresses", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventKeyState()Z", remap = false)) public boolean playback_redirectGetEventKeyStateDPK() { @@ -105,7 +105,7 @@ public boolean playback_redirectGetEventKeyStateDPK() { } /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput#getEventKeyboardCharacter()} + * @return {@link VirtualInput.VirtualKeyboardInput#getEventKeyboardCharacter()} */ @Redirect(method = "dispatchKeypresses", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventCharacter()C", remap = false)) public char playback_redirectGetEventCharacterDPK() { @@ -116,7 +116,7 @@ public char playback_redirectGetEventCharacterDPK() { /** * Run at the start of run tick mouse. Runs every tick. - * @see com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualMouseInput#nextMouseTick() + * @see VirtualInput.VirtualMouseInput#nextMouseTick() * @param ci CBI */ @Inject(method = "runTickMouse", at = @At(value = "HEAD")) @@ -125,9 +125,9 @@ public void playback_injectRunTickMouse(CallbackInfo ci) { } /** - * Redirects a {@link org.lwjgl.input.Mouse#next()}. Starts running every tick and continues as long as there are {@link VirtualMouseEvent}s in {@link VirtualInput2} - * @see com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualMouseInput#nextMouseSubtick() - * @return If {@link VirtualMouseInput}s are present in {@link VirtualInput2} + * Redirects a {@link org.lwjgl.input.Mouse#next()}. Starts running every tick and continues as long as there are {@link VirtualMouseEvent}s in {@link VirtualInput} + * @see VirtualInput.VirtualMouseInput#nextMouseSubtick() + * @return If {@link VirtualMouseInput}s are present in {@link VirtualInput} */ @Redirect(method = "runTickMouse", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Mouse;next()Z", remap = false)) public boolean playback_redirectMouseNext() { @@ -135,7 +135,7 @@ public boolean playback_redirectMouseNext() { } /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualMouseInput#getEventMouseKey()} + * @return {@link VirtualInput.VirtualMouseInput#getEventMouseKey()} */ @Redirect(method = "runTickMouse", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Mouse;getEventButton()I", remap = false)) public int playback_redirectMouseGetEventButton() { @@ -143,7 +143,7 @@ public int playback_redirectMouseGetEventButton() { } /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualMouseInput#getEventMouseState()} + * @return {@link VirtualInput.VirtualMouseInput#getEventMouseState()} */ @Redirect(method = "runTickMouse", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Mouse;getEventButtonState()Z", remap = false)) public boolean playback_redirectGetEventButtonState() { @@ -151,7 +151,7 @@ public boolean playback_redirectGetEventButtonState() { } /** - * @return {@link com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualMouseInput#getEventMouseScrollWheel()} + * @return {@link VirtualInput.VirtualMouseInput#getEventMouseScrollWheel()} */ @Redirect(method = "runTickMouse", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Mouse;getEventDWheel()I", remap = false)) public int playback_redirectGetEventDWheel() { diff --git a/src/main/java/com/minecrafttas/tasmod/playback/PlaybackControllerClient.java b/src/main/java/com/minecrafttas/tasmod/playback/PlaybackControllerClient.java index c3a9e115..b734f08f 100644 --- a/src/main/java/com/minecrafttas/tasmod/playback/PlaybackControllerClient.java +++ b/src/main/java/com/minecrafttas/tasmod/playback/PlaybackControllerClient.java @@ -683,9 +683,9 @@ private void tpPlayer(String startLocation) throws NumberFormatException { * Clears {@link #keyboard} and {@link #mouse} */ public void unpressContainer() { - LOGGER.trace(LoggerMarkers.Playback, "Unpressing container"); - keyboard.clear(); - mouse.clear(); +// LOGGER.trace(LoggerMarkers.Playback, "Unpressing container"); +// keyboard.clear(); +// mouse.clear(); } // ============================================================== @@ -747,7 +747,7 @@ public TickInputContainer(int tick) { this.tick = tick; this.keyboard = new VirtualKeyboard(); this.mouse = new VirtualMouse(); - this.subticks = new VirtualCameraAngle(0, 0); +// this.subticks = new VirtualCameraAngle(0, 0); } @Override diff --git a/src/main/java/com/minecrafttas/tasmod/playback/PlaybackSerialiser.java b/src/main/java/com/minecrafttas/tasmod/playback/PlaybackSerialiser.java index ce383fee..0d3c2b98 100644 --- a/src/main/java/com/minecrafttas/tasmod/playback/PlaybackSerialiser.java +++ b/src/main/java/com/minecrafttas/tasmod/playback/PlaybackSerialiser.java @@ -1,29 +1,27 @@ package com.minecrafttas.tasmod.playback; -import static com.minecrafttas.tasmod.TASmod.LOGGER; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.apache.commons.io.FileUtils; - import com.dselent.bigarraylist.BigArrayList; import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.monitoring.DesyncMonitoring; import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TickInputContainer; import com.minecrafttas.tasmod.util.FileThread; import com.minecrafttas.tasmod.util.LoggerMarkers; +import com.minecrafttas.tasmod.virtual.VirtualCameraAngle; import com.minecrafttas.tasmod.virtual.VirtualKey; import com.minecrafttas.tasmod.virtual.VirtualKeyboard; import com.minecrafttas.tasmod.virtual.VirtualMouse; -import com.minecrafttas.tasmod.virtual.VirtualMouse.PathNode; -import com.minecrafttas.tasmod.virtual.VirtualCameraAngle; import com.mojang.realmsclient.util.Pair; +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static com.minecrafttas.tasmod.TASmod.LOGGER; /** * Saves a given {@linkplain PlaybackControllerClient} to a file. Is also able to read an input container from a file.
@@ -334,19 +332,19 @@ private VirtualKeyboard readKeyboard(String section, int linenumber) throws IOEx for (String key : splitKeys) { - VirtualKey vkey = null; - // Check if the key is a keycode - if (isNumeric(key)) { - vkey = keyboard.get(Integer.parseInt(key)); - } else { - vkey = keyboard.get(key); - } - - if (vkey == null) { - throw new IOException(key + " is not a recognised keyboard key in line " + linenumber); - } - - vkey.setPressed(true); +// VirtualKey vkey = null; +// // Check if the key is a keycode +// if (isNumeric(key)) { +// vkey = keyboard.get(Integer.parseInt(key)); +// } else { +// vkey = keyboard.get(key); +// } +// +// if (vkey == null) { +// throw new IOException(key + " is not a recognised keyboard key in line " + linenumber); +// } +// +// vkey.setPressed(true); } } @@ -357,7 +355,7 @@ private VirtualKeyboard readKeyboard(String section, int linenumber) throws IOEx } for (char onechar : chars) { - keyboard.addChar(onechar); +// keyboard.addChar(onechar); } return keyboard; } @@ -379,58 +377,58 @@ private VirtualMouse readMouse(String section, int linenumber) throws IOExceptio String[] splitButtons=buttons.split(","); for (String button : splitButtons) { - VirtualKey vkey = null; - // Check if the key is a keycode - if (isNumeric(button)) { - vkey = mouse.get(Integer.parseInt(button)); - } else { - vkey = mouse.get(button); - } - if (vkey == null) { - throw new IOException(button + " is not a recognised mouse key in line " + linenumber); - } - mouse.get(button).setPressed(true); +// VirtualKey vkey = null; +// // Check if the key is a keycode +// if (isNumeric(button)) { +// vkey = mouse.get(Integer.parseInt(button)); +// } else { +// vkey = mouse.get(button); +// } +// if (vkey == null) { +// throw new IOException(button + " is not a recognised mouse key in line " + linenumber); +// } +// mouse.get(button).setPressed(true); } } - mouse.setPath(readPath(path, linenumber, mouse)); +// mouse.setPath(readPath(path, linenumber, mouse)); return mouse; } - private List readPath(String section, int linenumber, VirtualMouse mouse) throws IOException { - List path = new ArrayList(); - - section = section.replace("[", "").replace("]", ""); - String[] pathNodes = section.split("->"); - - for (String pathNode : pathNodes) { - String[] split = pathNode.split(","); - - int length=split.length; - int scrollWheel = 0; - int cursorX = 0; - int cursorY = 0; - try { - scrollWheel = Integer.parseInt(split[length-3]); - cursorX = Integer.parseInt(split[length-2]); - cursorY = Integer.parseInt(split[length-1]); - } catch (NumberFormatException e) { - throw new IOException("'" + pathNode + "' couldn't be read in line " + linenumber+": Something is not a number"); - } catch (ArrayIndexOutOfBoundsException e) { - throw new IOException("'" + pathNode + "' couldn't be read in line " + linenumber+": Something is missing or is too much"); - } - PathNode node = mouse.new PathNode(); - for (int i=0; i readPath(String section, int linenumber, VirtualMouse mouse) throws IOException { +// List path = new ArrayList(); +// +// section = section.replace("[", "").replace("]", ""); +// String[] pathNodes = section.split("->"); +// +// for (String pathNode : pathNodes) { +// String[] split = pathNode.split(","); +// +// int length=split.length; +// int scrollWheel = 0; +// int cursorX = 0; +// int cursorY = 0; +// try { +// scrollWheel = Integer.parseInt(split[length-3]); +// cursorX = Integer.parseInt(split[length-2]); +// cursorY = Integer.parseInt(split[length-1]); +// } catch (NumberFormatException e) { +// throw new IOException("'" + pathNode + "' couldn't be read in line " + linenumber+": Something is not a number"); +// } catch (ArrayIndexOutOfBoundsException e) { +// throw new IOException("'" + pathNode + "' couldn't be read in line " + linenumber+": Something is missing or is too much"); +// } +// PathNode node = mouse.new PathNode(); +// for (int i=0; i - *
- * This mimics peripherals used to control minecraft which are: The keyboard, - * the mouse and the angle of the player, which is called "Subticks" in this - * case (this came from a time when the camera angle was actually updated on - * a subtick level).
- *
- * For each peripheral there are 2 states. The "current" state (e.g. - * {@linkplain #currentKeyboard}), which is the state of what the game - * actually currently recognizes and the "next" state (e.g. - * {@linkplain #nextKeyboard}) which either the buttons pressed on the - * keyboard, or the buttons pressed in the next playback tick.
- *
- * Outside of this class, there is a third state, which is the Vanilla Minecraft - * keybindings, which, in the best case, should be a copy of the "current" - * state.
- *

Events

To update the vanilla keybindings you need something called - * key events. An event for a keyboard might look like this
- *
- * 17,true,'w'
- *
- * Something like this is sent by the LWJGL methods to the vanilla methods to - * update the keybindings.
- * In this case it is the key with the keycode 17 (The 'W' key), in the state - * true which means it is pressed down. And the 'w' is the character that is - * associated with that key
- *
- * You can find a complete list of LWJGL keycodes in - * {@link VirtualKeyboard}.
+ * Main component for redirecting inputs.
*
- * If W is released, the key event for this would look something like this: - * 17, false, NULL
- * From that, the vanilla keybindings know which key is currently pressed down. - * As a bonus, the character 'w' is used when typing in a textfield.
- *

Emulating events

With the key events from LWJGL, so from the - * "physical keyboard", we can update the nextKeyboard in the - * {@link #updateNextKeyboard(int, boolean, char)} method.
- * This method is called every frame and also works in tickrate 0.
+ * This class mimics the LWJGL classes {@link org.lwjgl.input.Keyboard} and + * {@link org.lwjgl.input.Mouse}.
*
- * And on every tick, we call {@link #updateCurrentKeyboard()}, which updates - * the currentKeyboard with a copy of nextKeyboard.
- * However, we still need to update the Vanilla Minecraft keybinding by using - * key events.
- * To solve this problem we can use - * {@link VirtualKeyboard#getDifference(VirtualKeyboard)}, which compares 2 - * keyboards and extracts the key events from that.
- *
- * For instance if we have a keyboard, where nothing is pressed, then a keyboard - * where only "W" is pressed, we can assume that the key event responsible for - * that change 17,true,? is.
- * But as indicated by the ? we actually don't know the character that is typed - * there. And for that we need to store the characters seperately in the - * keyboard ({@link VirtualKeyboard#getCharList()}).
- *
- * The advantage of this system is: - *
    - *
  • Better support for savestates
  • - *
  • Better support for tickrate 0
  • - *
  • Less cluttering in the resulting files
  • - *
  • Recording support for the full keyboard/eventual modding support
  • - *
- * - * @author Scribble - * */ -@Deprecated -public class VirtualInput implements EventPlayerJoinedClientSide{ - - // ===========================Keyboard================================= - +public class VirtualInput { + private final Logger LOGGER; /** - * The state of the keyboard recognized by the game. Updated on a tick basis - *
- * See also: {@link VirtualInput} + * Instance of the {@link VirtualKeyboardInput} subclass, intended to improve readability. */ - private VirtualKeyboard currentKeyboard = new VirtualKeyboard(); - + public final VirtualKeyboardInput KEYBOARD; /** - * The state of the keyboard which will replace - * {@linkplain VirtualInput#currentKeyboard} in the next tick. Updated every - * frame
- * See also: {@link VirtualInput} + * Instance of the {@link VirtualMouseInput} subclass, intended to improve readability. */ - private VirtualKeyboard nextKeyboard = new VirtualKeyboard(); - - private List currentKeyboardEvents = new ArrayList(); - private Iterator currentKeyboardEventIterator = currentKeyboardEvents.iterator(); - - private VirtualKeyboardEvent currentKeyboardEvent = null; + public final VirtualMouseInput MOUSE; + /** + * Instance of the {@link VirtualCameraAngleInput} subclass, intended to improve readability. + */ + public final VirtualCameraAngleInput CAMERA_ANGLE; - public VirtualKeyboard getCurrentKeyboard() { - return currentKeyboard; + /** + * Creates a new virtual input with an empty {@link VirtualKeyboardInput}, {@link VirtualMouseInput} and {@link VirtualCameraAngleInput} + * @param logger + */ + public VirtualInput(Logger logger) { + this(logger, new VirtualKeyboard(), new VirtualMouse(), new VirtualCameraAngle()); } - public VirtualKeyboard getNextKeyboard() { - return nextKeyboard; - } - /** - * Loads the inputs and starts a TAS on initialize - * @param fileToLoad (Nullable) Loads this filename and starts playing back the TAS + * Creates a virtual input with pre-loaded values + * + * @param preloadedKeyboard A keyboard loaded when creating {@link VirtualKeyboardInput} + * @param preloadedMouse A mouse loaded when creating {@link VirtualMouseInput} + * @param preloadedCamera A camera loaded when creating {@link VirtualCameraAngleInput} */ - public VirtualInput(String fileToLoad) { - if (fileToLoad != null) { - try { - loadInputs(fileToLoad); - TASmodClient.controller.setStateWhenOpened(TASstate.PLAYBACK); - } catch (IOException e) { - LOGGER.error("Cannot load inputs from the start of the TAS: {}", e.getMessage()); - } - } + public VirtualInput(Logger logger, VirtualKeyboard preloadedKeyboard, VirtualMouse preloadedMouse, VirtualCameraAngle preloadedCamera) { + this.LOGGER = logger; + KEYBOARD = new VirtualKeyboardInput(preloadedKeyboard); + MOUSE = new VirtualMouseInput(preloadedMouse); + CAMERA_ANGLE = new VirtualCameraAngleInput(preloadedCamera); } - public void updateNextKeyboard(int keycode, boolean keystate, char character) { - -// System.out.println(keycode+" "+keystate+" "+character); - - if (keystate) { - character = nextKeyboard.encodeUnicode(keycode, character); - } else { - if (keycode == 15) { - character = '\u2907'; - } - } - nextKeyboard.addChar(character); - if (VirtualKeybindings.isKeyCodeAlwaysBlocked(keycode)) { - return; + /** + * Updates the logic for {@link #KEYBOARD}, {@link #MOUSE} and + * {@link #CAMERA_ANGLE}
+ * Runs every frame + * + * @see MixinMinecraft#playback_injectRunGameLoop(CallbackInfo) + * @param currentScreen The current screen from Minecraft.class. Used for + * checking if the mouse logic should be adapted to + * GUIScreens + */ + public void update(GuiScreen currentScreen) { + while (Keyboard.next()) { + KEYBOARD.updateNextKeyboard(Keyboard.getEventKey(), Keyboard.getEventKeyState(), Keyboard.getEventCharacter(), Keyboard.areRepeatEventsEnabled()); } - VirtualKey key = nextKeyboard.get(keycode); - - key.setPressed(keystate); - } - - public List getCurrentKeyboardEvents() { - return currentKeyboard.getDifference(nextKeyboard); - } - - public void updateCurrentKeyboard() { - currentKeyboardEvents = getCurrentKeyboardEvents(); - currentKeyboardEventIterator = currentKeyboardEvents.iterator(); - -// currentKeyboardEvents.forEach(action->{ -// System.out.println(action.toString()); -// }); - - nextKeyboard.clearCharList(); - - currentKeyboard = nextKeyboard.clone(); - } - - public boolean nextKeyboardEvent() { - boolean hasnext = currentKeyboardEventIterator.hasNext(); - if (hasnext) { - currentKeyboardEvent = currentKeyboardEventIterator.next(); + while (Mouse.next()) { + if (currentScreen == null) { + MOUSE.updateNextMouse(Mouse.getEventButton(), Mouse.getEventButtonState(), Mouse.getEventDWheel(), 0, 0); + } else { + Ducks.GuiScreenDuck screen = (Ducks.GuiScreenDuck) currentScreen; + int eventX = screen.unscaleX(Mouse.getEventX()); + int eventY = screen.unscaleY(Mouse.getEventY()); + eventX = PointerNormalizer.getNormalizedX(eventX); + eventY = PointerNormalizer.getNormalizedY(eventY); + MOUSE.updateNextMouse(Mouse.getEventButton(), Mouse.getEventButtonState(), Mouse.getEventDWheel(), eventX, eventY); + } } - return hasnext; - } - - public int getEventKeyboardKey() { - return currentKeyboardEvent.getKeyCode(); - } - - public boolean getEventKeyboardState() { - return currentKeyboardEvent.isState(); - } - - public char getEventKeyboardCharacter() { - return currentKeyboardEvent.getCharacter(); - } - - public void clearNextKeyboard() { - nextKeyboard.clear(); } + /** + * If the keyboard or mouse key is currently down. + * If keycode >= 0 then {@link VirtualKeyboardInput#isKeyDown(int)} will be called,
+ * otherwise {@link VirtualMouseInput#isKeyDown(int)} + * + * @param keycode The keycode in question + * @return If the key is down either on mouse or keyboard + */ public boolean isKeyDown(int keycode) { - if (keycode >= 0) - return currentKeyboard.get(keycode).isKeyDown(); - else - return currentMouse.get(keycode).isKeyDown(); - } - - public boolean isKeyDown(String keyname) { - if (currentKeyboard.get(keyname).isKeyDown()) { - return true; - } else if (currentMouse.get(keyname).isKeyDown()) { - return true; + if(keycode >= 0) { + return KEYBOARD.isKeyDown(keycode); } else { - return false; + return MOUSE.isKeyDown(keycode); } } - + + /** + * If the keyboard or mouse key is will be down in the next tick. + * If keycode >= 0 then {@link VirtualKeyboardInput#willKeyBeDown(int)} will be called,
+ * otherwise {@link VirtualMouseInput#willKeyBeDown(int)} + * + * @param keycode The keycode in question + * @return If the key will be down either on mouse or keyboard + */ public boolean willKeyBeDown(int keycode) { - if (keycode >= 0) - return nextKeyboard.get(keycode).isKeyDown(); - else - return nextMouse.get(keycode).isKeyDown(); - } - - public boolean willKeyBeDown(String keyname) { - if (nextKeyboard.get(keyname).isKeyDown()) { - return true; - } else if (nextMouse.get(keyname).isKeyDown()) { - return true; + if(keycode >= 0) { + return KEYBOARD.willKeyBeDown(keycode); } else { - return false; + return MOUSE.willKeyBeDown(keycode); } } - - public List getCurrentKeyboardPresses() { - List out = new ArrayList(); - - currentKeyboard.getKeyList().forEach((keycodes, virtualkeys) -> { - if (virtualkeys.isKeyDown()) { - out.add(virtualkeys.getName()); - } - }); - - return out; + + /** + * Unpresses all keys in {@link VirtualKeyboardInput#nextKeyboard} and {@link VirtualMouseInput#nextMouse} + */ + public void unpress() { + KEYBOARD.nextKeyboard.clear(); + MOUSE.nextMouse.clear(); } - - public List getNextKeyboardPresses() { - - List out = new ArrayList(); - if (TASmodClient.controller.isPlayingback() && TASmodClient.controller.get(TASmodClient.controller.index()) != null) { - TASmodClient.controller.get(TASmodClient.controller.index()).getKeyboard().getKeyList().forEach((keycodes, virtualkeys) -> { - if (virtualkeys.isKeyDown()) { - out.add(virtualkeys.getName()); - } - }); - } else { - nextKeyboard.getKeyList().forEach((keycodes, virtualkeys) -> { - if (virtualkeys.isKeyDown()) { - out.add(virtualkeys.getName()); - } - }); + + /** + * Subclass of {@link VirtualInput} handling keyboard logic.
+ *
+ * Vanilla keyboard handling looks something like this: + * + *
+	 *	public void runTickKeyboard()  { // Executed every tick in runTick()
+	 *		while({@linkplain Keyboard#next()}){
+	 *			int keycode = {@linkplain Keyboard#getEventKey()};
+	 *			boolean keystate = {@linkplain Keyboard#getEventKey()};
+	 *			char character = {@linkplain Keyboard#getEventCharacter()}
+	 *
+	 *			Keybindings.updateKeybind(keycode, keystate, character)
+	 *		}
+	 *	}
+	 * 
+ * + * After redirecting the calls in {@link MixinMinecraft}, the resulting logic + * now looks like this: + * + *
+	 *	public void runTickKeyboard()  {
+	 *		{@linkplain #nextKeyboardTick()}
+	 *		while({@linkplain #nextKeyboardSubtick()}){
+	 *			int keycode = {@linkplain #getEventKeyboardKey()}};
+	 *			boolean keystate = {@linkplain #getEventKeyboardState()};
+	 *			char character = {@linkplain #getEventKeyboardCharacter()}
+	 *
+	 *			Keybindings.updateKeybind(keycode, keystate, character)
+	 *		}
+	 *	}
+	 * 
+ * @see VirtualKeyboard + */ + public class VirtualKeyboardInput { + /** + * The keyboard "state" that is currently recognized by the game,
+ * meaning it is a direct copy of the vanilla keybindings. Updated every + * tick.
+ * Updated in {@link #nextKeyboardTick()} + */ + private final VirtualKeyboard currentKeyboard; + /** + * The "state" of the real physical keyboard.
+ * This is updated every frame.
+ * Updates {@link #currentKeyboard} in {@link #nextKeyboardTick()} + */ + private final VirtualKeyboard nextKeyboard = new VirtualKeyboard(); + /** + * Queue for keyboard events.
+ * Is filled in {@link #nextKeyboardTick()} and read in + * {@link #nextKeyboardSubtick()} + */ + private final Queue keyboardEventQueue = new ConcurrentLinkedQueue(); + /** + * The current keyboard event where the vanilla keybindings are reading + * from.
+ * Updated in {@link #nextKeyboardSubtick()} and read out in + *
    + *
  • {@link #getEventKeyboardKey()}
  • + *
  • {@link #getEventKeyboardState()}
  • + *
  • {@link #getEventKeyboardCharacter()}
  • + *
+ */ + private VirtualKeyboardEvent currentKeyboardEvent = new VirtualKeyboardEvent(); + + /** + * Constructor to preload the {@link #currentKeyboard} with an existing keyboard + * @param preloadedKeyboard The new {@link #currentKeyboard} + */ + public VirtualKeyboardInput(VirtualKeyboard preloadedKeyboard) { + currentKeyboard = preloadedKeyboard; } - return out; - } - - // =======================================Mouse============================================ - - private VirtualMouse currentMouse = new VirtualMouse(); - - private VirtualMouse nextMouse = new VirtualMouse(); - - private List currentMouseEvents = new ArrayList(); - private Iterator currentMouseEventIterator = currentMouseEvents.iterator(); - - private VirtualMouseEvent currentMouseEvent = null; - - public VirtualMouse getCurrentMouse() { - return currentMouse; - } - public VirtualMouse getNextMouse() { - return nextMouse; - } - - public void updateNextMouse(int keycode, boolean keystate, int scrollwheel, int cursorX, int cursorY, boolean filter) { - - boolean flag = true; - if (filter) { - flag = nextMouse.isSomethingDown() || scrollwheel != 0 || keycode != -1; + /** + * Updates the next keyboard + * + * @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 + */ + public void updateNextKeyboard(int keycode, boolean keystate, char character) { + updateNextKeyboard(keycode, keystate, character, false); } - VirtualKey key = nextMouse.get(keycode - 100); - - if (VirtualKeybindings.isKeyCodeAlwaysBlocked(keycode - 100)) { - key.setPressed(false); - return; + /** + * Updates the next keyboard + * + * @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 + */ + public void updateNextKeyboard(int keycode, boolean keystate, char character, boolean repeatEventsEnabled) { + LOGGER.debug(LoggerMarkers.Keyboard, "Update: {}, {}, {}, {}", keycode, keystate, character); // Activate with -Dtasmod.marker.keyboard=ACCEPT in VM arguments (and -Dtasmod.log.level=debug) + nextKeyboard.update(keycode, keystate, character, repeatEventsEnabled); } - key.setPressed(keystate); - - nextMouse.setScrollWheel(scrollwheel); - - nextMouse.setCursor(PointerNormalizer.getNormalizedX(cursorX), PointerNormalizer.getNormalizedY(cursorY)); - - if (flag == true) - nextMouse.addPathNode(); - } - - public List getCurrentMouseEvents() { - return currentMouse.getDifference(nextMouse); - } - - public void updateCurrentMouseEvents() { - currentMouseEvents = getCurrentMouseEvents(); - currentMouseEventIterator = currentMouseEvents.iterator(); - - // Prints the mouse events given to the keybindings... very useful -// currentMouseEvents.forEach(action->{ -// System.out.println(action.toString()); -// }); - - resetNextMouseLists(); - - currentMouse = nextMouse.clone(); - } - - public void resetNextMouseLists() { - nextMouse.resetPath(); - } - - public boolean nextMouseEvent() { - boolean hasnext = currentMouseEventIterator.hasNext(); - if (hasnext) { - currentMouseEvent = currentMouseEventIterator.next(); + /** + * Runs when the next keyboard tick is about to occur.
+ * Used to load {@link #nextKeyboard} into {@link #currentKeyboard}, creating + * {@link VirtualKeyboardEvent}s in the process. + * + * @see MixinMinecraft#playback_injectRunTickKeyboard(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) + */ + public void nextKeyboardTick() { + currentKeyboard.getVirtualEvents(nextKeyboard, keyboardEventQueue); + currentKeyboard.copyFrom(nextKeyboard); } - return hasnext; - } - - public int getEventMouseKey() { - return currentMouseEvent.getKeyCode(); - } - - public boolean getEventMouseState() { - return currentMouseEvent.isState(); - } - - public int getEventMouseScrollWheel() { - return currentMouseEvent.getScrollwheel(); - } - - public int getEventCursorX() { - return PointerNormalizer.getCoordsX(currentMouseEvent.getCursorX()); - } - - public int getEventCursorY() { - return PointerNormalizer.getCoordsY(currentMouseEvent.getCursorY()); - } - - public void clearNextMouse() { - nextMouse.clear(); - } - - public List getCurrentMousePresses() { - List out = new ArrayList(); - - currentMouse.getKeyList().forEach((keycodes, virtualkeys) -> { - if (virtualkeys.isKeyDown()) { - out.add(virtualkeys.getName()); - } - }); - - return out; - } - - public List getNextMousePresses() { - List out = new ArrayList(); - if (TASmodClient.controller.isPlayingback() && TASmodClient.controller.get(TASmodClient.controller.index()) != null) { - TASmodClient.controller.get(TASmodClient.controller.index()).getMouse().getKeyList().forEach((keycodes, virtualkeys) -> { - if (virtualkeys.isKeyDown()) { - out.add(virtualkeys.getName()); - } - }); - } else { - nextMouse.getKeyList().forEach((keycodes, virtualkeys) -> { - if (virtualkeys.isKeyDown()) { - out.add(virtualkeys.getName()); - } - }); + /** + * Runs in a while loop. Used for updating {@link #currentKeyboardEvent} and + * ending the while loop. + * + * @see MixinMinecraft#playback_redirectKeyboardNext() + * @return If a keyboard event is in {@link #keyboardEventQueue} + */ + public boolean nextKeyboardSubtick() { + return (currentKeyboardEvent = keyboardEventQueue.poll()) != null; } - return out; - } - - public void unpressEverything() { - TASmodClient.controller.unpressContainer(); - unpressNext(); - } - - public void unpressNext() { - clearNextKeyboard(); - clearNextMouse(); - } - - // ======================================Subticks=========================================== - - VirtualCameraAngle currentSubtick = new VirtualCameraAngle(0, 0); - - public void updateSubtick(float pitch, float yaw) { - currentSubtick = TASmodClient.controller.addSubticksToContainer(new VirtualCameraAngle(pitch, yaw)); - } - - public float getSubtickPitch() { - return currentSubtick.getPitch(); - } - - public float getSubtickYaw() { - return currentSubtick.getYaw(); - } + /** + * @return The keycode of {@link #currentKeyboardEvent} + */ + public int getEventKeyboardKey() { + return currentKeyboardEvent.getKeyCode(); + } - // =====================================Container=========================================== + /** + * @return The keystate of {@link #currentKeyboardEvent} + */ + public boolean getEventKeyboardState() { + return currentKeyboardEvent.isState(); + } - /** - * Updates the input container and the {@link #nextKeyboard} as well as - * {@link #nextMouse}
- * Gets executed each game tick - */ - public void updateContainer() { - nextKeyboard = TASmodClient.controller.addKeyboardToContainer(nextKeyboard); - nextMouse = TASmodClient.controller.addMouseToContainer(nextMouse); + /** + * @return The character(s) of {@link #currentKeyboardEvent} + */ + public char getEventKeyboardCharacter() { + return currentKeyboardEvent.getCharacter(); + } + + /** + * If the key is currently down and recognised by Minecraft + * @param keycode The keycode of the key in question + * @return Whether the key of the {@link #currentKeyboard} is down + */ + public boolean isKeyDown(int keycode) { + return currentKeyboard.isKeyDown(keycode); + } + + /** + * If the key will be down and recognised in the next tick by Minecraft.
+ * This is equal to checking if a key on the physical keyboard is pressed + * @param keycode The keycode of the key in question + * @return Whether the key of the {@link #nextKeyboard} is down + */ + public boolean willKeyBeDown(int keycode) { + return nextKeyboard.isKeyDown(keycode); + } } /** - * Replaces the {@link TASmodClient#controller}, used in + * Subclass of {@link VirtualInput} handling mouse logic.
+ *
+ * Vanilla mouse handling looks something like this: * - * @param container to replace the current one - */ - public void setContainer(PlaybackControllerClient container) { - TASmodClient.controller = container; - } - - // =====================================Savestates=========================================== - - /** - * Loads and preloads the inputs from the new InputContainer to - * {@link TASmodClient#controller} + *
+	 *	public void runTickMouse()  { // Executed every tick in runTick()
+	 *		while({@linkplain Mouse#next()}){
+	 *			int keycode = {@linkplain Mouse#getEventButton()};
+	 *			boolean keystate = {@linkplain Mouse#getEventButtonState()};
+	 *			int scrollWheel = {@linkplain Mouse#getEventDWheel()}
+	 *			int cursorX = {@linkplain Mouse#getEventX()} // Important in GUIs
+	 *			int cursorY = {@linkplain Mouse#getEventY()}
+	 *
+	 *			Keybindings.updateKeybind(keycode, keystate, etc...)
+	 *		}
+	 *	}
+	 * 
* - * Saving a savestate is done via {@linkplain com.minecrafttas.tasmod.playback.PlaybackSerialiser#saveToFileV1(File, PlaybackControllerClient)} in {@linkplain com.minecrafttas.tasmod.savestates.SavestateHandlerClient#savestate(String)} + * After redirecting the calls in {@link MixinMinecraft}, the resulting logic + * now looks like this: * - * @param savestatecontainer The container that should be loaded. + *
+	 *	public void runTickMouse()  { // Executed every tick in runTick()
+	 *		{@linkplain #nextMouseTick()}
+	 *		while({@linkplain #nextMouseSubtick}){
+	 *			int keycode = {@linkplain #getEventMouseKey};
+	 *			boolean keystate = {@linkplain #getEventMouseState()} ()};
+	 *			int scrollWheel = {@linkplain #getEventMouseScrollWheel()}
+	 *			int cursorX = {@linkplain #getEventCursorX()} // Important in GUIs
+	 *			int cursorY = {@linkplain #getEventCursorY()}
+	 *
+	 *			Keybindings.updateKeybind(keycode, keystate, etc...)
+	 *		}
+	 *	}
+	 * 
+ * @see VirtualMouse */ - public void loadClientSavestate(PlaybackControllerClient savestatecontainer) { - - if (TASmodClient.controller.isPlayingback()) { - preloadInput(TASmodClient.controller, savestatecontainer.size() - 1); // Preloading from the current container and - // from the second to last index of - // the savestatecontainer. Since this is - // executed during playback, - // we will only load the position of the - // savestate container and not replace the - // container itself. This is due to the fact - // that the playback would immediately end - // when you replace the container. - - if (TASmodClient.controller.size() >= savestatecontainer.size()) { // Check if the current container is bigger than the - // savestated one. - - try { - TASmodClient.controller.setIndex(savestatecontainer.size()); // Set the "playback" index of the current - // container to the latest index of the - // savestatecontainer. Meaning this index will - // be played next - } catch (IndexOutOfBoundsException e) { - e.printStackTrace(); - } - } else { - String start = savestatecontainer.getStartLocation(); - savestatecontainer.setStartLocation(""); - - try { - savestatecontainer.setIndex(savestatecontainer.size() - 1); - } catch (IndexOutOfBoundsException e) { - e.printStackTrace(); - } - savestatecontainer.setTASStateClient(TASstate.PLAYBACK); - savestatecontainer.setStartLocation(start); - TASmodClient.controller = savestatecontainer; - } + public class VirtualMouseInput { + /** + * The mouse "state" that is currently recognized by the game,
+ * meaning it is a direct copy of the vanilla mouse. Updated every + * tick.
+ * Updated in {@link #nextMouseTick()} + */ + private final VirtualMouse currentMouse; + /** + * The "state" of the real physical mouse.
+ * This is updated every frame.
+ * Updates {@link #currentMouse} in {@link #nextMouseTick()} + */ + private final VirtualMouse nextMouse = new VirtualMouse(); + /** + * Queue for keyboard events.
+ * Is filled in {@link #nextMouseTick()} and read in + * {@link #nextMouseSubtick()} + */ + private final Queue mouseEventQueue = new ConcurrentLinkedQueue<>(); + /** + * The current mouse event where the vanilla mouse is reading + * from.
+ * Updated in {@link #nextMouseSubtick()} and read out in + *
    + *
  • {@link #getEventMouseKey()}
  • + *
  • {@link #getEventMouseState()}
  • + *
  • {@link #getEventMouseScrollWheel()}
  • + *
  • {@link #getEventCursorX()}
  • + *
  • {@link #getEventCursorY()}
  • + *
+ */ + private VirtualMouseEvent currentMouseEvent = new VirtualMouseEvent(); + + /** + * Constructor to preload the {@link #currentMouse} with an existing mouse + * @param preloadedMouse The new {@link #currentMouse} + */ + public VirtualMouseInput(VirtualMouse preloadedMouse) { + currentMouse = preloadedMouse; + } - } else if (TASmodClient.controller.isRecording()) { - String start = savestatecontainer.getStartLocation(); - preloadInput(savestatecontainer, savestatecontainer.size() - 1); // Preload the input of the savestate + /** + * Updates the next keyboard + * + * @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 + */ + public void updateNextMouse(int keycode, boolean keystate, int scrollwheel, int cursorX, int cursorY) { + keycode-=100; + LOGGER.debug(LoggerMarkers.Mouse,"Update: {} ({}), {}, {}, {}, {}", keycode, VirtualKey.getName(keycode), keystate, scrollwheel, cursorX, cursorY); // Activate with -Dtasmod.marker.mouse=ACCEPT in VM arguments (and -Dtasmod.log.level=debug) + nextMouse.update(keycode, keystate, scrollwheel, cursorX, cursorY); + } - nextKeyboard = new VirtualKeyboard(); // Unpress the nextKeyboard and mouse to get rid of the preloaded inputs in the - // next keyboard. Note that these inputs are still loaded in the current - // keyboard - nextMouse = new VirtualMouse(); + /** + * Runs when the next mouse tick is about to occur.
+ * Used to load {@link #nextMouse} into {@link #currentMouse}, creating + * {@link VirtualMouseEvent}s in the process. + * + * @see MixinMinecraft#playback_injectRunTickMouse(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) + */ + public void nextMouseTick() { + currentMouse.getVirtualEvents(nextMouse, mouseEventQueue); + currentMouse.copyFrom(nextMouse); + } - try { - savestatecontainer.setIndex(savestatecontainer.size()); - } catch(IndexOutOfBoundsException e) { - e.printStackTrace(); - } - - savestatecontainer.setTASStateClient(TASstate.RECORDING); - savestatecontainer.setStartLocation(start); - TASmodClient.controller = savestatecontainer; // Replace the current container with the savestated container + /** + * Runs in a while loop. Used for updating {@link #currentMouseEvent} and + * ending the while loop. + * + * @see MixinMinecraft#playback_redirectMouseNext() + * @return If a mouse event is in {@link #mouseEventQueue} + */ + public boolean nextMouseSubtick() { + return (currentMouseEvent = mouseEventQueue.poll()) != null; } - } - /** - * Preloads the specified index from, the container to {@link #nextMouse} and - * {@link #nextKeyboard} - * - * @param container The container from which the inputs should be pre loaded - * @param index The index of the container from which the inputs should be - * loaded - */ - private void preloadInput(PlaybackControllerClient container, int index) { - TickInputContainer tickcontainer = container.get(index); + /** + * @return The keycode of {@link #currentMouseEvent} + */ + public int getEventMouseKey() { + return currentMouseEvent.getKeyCode(); + } - if (tickcontainer != null) { - tickcontainer = tickcontainer.clone(); - nextKeyboard = tickcontainer.getKeyboard().clone(); - nextMouse = tickcontainer.getMouse().clone(); + /** + * @return The keystate of {@link #currentMouseEvent} + */ + public boolean getEventMouseState() { + return currentMouseEvent.isState(); + } - Minecraft.getMinecraft().runTickKeyboard(); // Letting mouse and keyboard tick once to load inputs into the - // "currentKeyboard" - Minecraft.getMinecraft().runTickMouse(); - } else { - LOGGER.warn("Can't preload inputs, specified inputs are null!"); + /** + * @return The scroll wheel of {@link #currentMouseEvent} + */ + public int getEventMouseScrollWheel() { + return currentMouseEvent.getScrollwheel(); } - } - // ================================Load/Save Inputs===================================== - - public void loadInputs(String filename) throws IOException { - setContainer(TASmodClient.serialiser.fromEntireFileV1(new File(TASmodClient.tasdirectory + "/" + filename + ".mctas"))); - TASmodClient.controller.fixTicks(); - } - - public void saveInputs(String filename) throws IOException { - TASmodClient.createTASDir(); - TASmodClient.serialiser.saveToFileV1(new File(TASmodClient.tasdirectory + "/" + filename + ".mctas"), TASmodClient.controller); - } - // =====================================Debug=========================================== + /** + * @return The x coordinate of the cursor of {@link #currentMouseEvent} + */ + public int getEventCursorX() { + return PointerNormalizer.getCoordsX(currentMouseEvent.getCursorX()); + } - public class InputEvent { - public int tick; - public List keyboardevent; - public List mouseEvent; - public VirtualCameraAngle subticks; + /** + * @return The y coordinate of the cursor of {@link #currentMouseEvent} + */ + public int getEventCursorY() { + return PointerNormalizer.getCoordsY(currentMouseEvent.getCursorY()); + } - public InputEvent(int tick, List keyboardevent, List mouseEvent, VirtualCameraAngle subticks) { - this.tick = tick; - this.keyboardevent = keyboardevent; - this.mouseEvent = mouseEvent; - this.subticks = subticks; + /** + * If the key is currently down and recognised by Minecraft + * @param keycode The keycode of the key in question + * @return Whether the key of the {@link #currentMouse} is down + */ + public boolean isKeyDown(int keycode) { + return currentMouse.isKeyDown(keycode); + } + + /** + * If the key will be down and recognised in the next tick by Minecraft.
+ * This is equal to checking if a key on the physical mouse is pressed + * @param keycode The keycode of the key in question + * @return Whether the key of the {@link #nextMouse} is down + */ + public boolean willKeyBeDown(int keycode) { + return nextMouse.isKeyDown(keycode); } + } - /** - * Gets all InputEvents from the current container.
- *
- * Container and input events differ in that input events are the events that - * get accepted by Minecraft in the runTickKeyboard.
- * The container however stores the current inputs and can calculate the - * corresponding input events from that, but it does it only when you are - * playing back or recording.
- *
- * This however runs through the {@link TASmodClient#controller} and generates - * the input events on for debug purposes. - * - * @return - */ - public List getAllInputEvents() { - - List main = new ArrayList<>(); - - for (int i = 0; i < TASmodClient.controller.size(); i++) { - - TickInputContainer tick = TASmodClient.controller.get(i); - TickInputContainer nextTick = TASmodClient.controller.get(i + 1); + public class VirtualCameraAngleInput { + private final VirtualCameraAngle cameraAngle; - if (nextTick == null) { - nextTick = new TickInputContainer(i + 1); // Fills the last tick in the container with an empty TickInputContainer - } - - VirtualKeyboard keyboard = tick.getKeyboard(); - List keyboardEventsList = keyboard.getDifference(nextTick.getKeyboard()); - - VirtualMouse mouse = tick.getMouse(); - List mouseEventsList = mouse.getDifference(nextTick.getMouse()); - - main.add(new InputEvent(tick.getTick(), keyboardEventsList, mouseEventsList, tick.getSubticks())); + public VirtualCameraAngleInput(VirtualCameraAngle preloadedCamera) { + cameraAngle = preloadedCamera; + } + + public void updateCameraAngle(float pitch, float yaw) { + cameraAngle.update(pitch, yaw); + } + + public float getPitch() { + return cameraAngle.getPitch(); + } + + public float getYaw() { + return cameraAngle.getYaw(); } - return main; - } - - @Override - public void onPlayerJoinedClientSide(EntityPlayerSP player) { - unpressNext(); } } diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput2.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput2.java deleted file mode 100644 index db3f13d5..00000000 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput2.java +++ /dev/null @@ -1,486 +0,0 @@ -package com.minecrafttas.tasmod.virtual; - -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; - -import org.apache.logging.log4j.Logger; -import org.lwjgl.input.Keyboard; -import org.lwjgl.input.Mouse; - -import com.minecrafttas.tasmod.mixin.playbackhooks.MixinMinecraft; -import com.minecrafttas.tasmod.util.Ducks; -import com.minecrafttas.tasmod.util.LoggerMarkers; -import com.minecrafttas.tasmod.util.PointerNormalizer; - -import net.minecraft.client.gui.GuiScreen; - -/** - * Main component for redirecting inputs.
- *
- * This class mimics the LWJGL classes {@link org.lwjgl.input.Keyboard} and - * {@link org.lwjgl.input.Mouse}.
- *
- */ -public class VirtualInput2 { - private final Logger LOGGER; - /** - * Instance of the {@link VirtualKeyboardInput} subclass, intended to improve readability. - */ - public final VirtualKeyboardInput KEYBOARD; - /** - * Instance of the {@link VirtualMouseInput} subclass, intended to improve readability. - */ - public final VirtualMouseInput MOUSE; - /** - * Instance of the {@link VirtualCameraAngleInput} subclass, intended to improve readability. - */ - public final VirtualCameraAngleInput CAMERA_ANGLE; - - /** - * Creates a new virtual input with an empty {@link VirtualKeyboardInput}, {@link VirtualMouseInput} and {@link VirtualCameraAngleInput} - * @param logger - */ - public VirtualInput2(Logger logger) { - this(logger, new VirtualKeyboard2(), new VirtualMouse2(), new VirtualCameraAngle2()); - } - - /** - * Creates a virtual input with pre-loaded values - * - * @param preloadedKeyboard A keyboard loaded when creating {@link VirtualKeyboardInput} - * @param preloadedMouse A mouse loaded when creating {@link VirtualMouseInput} - * @param preloadedCamera A camera loaded when creating {@link VirtualCameraAngleInput} - */ - public VirtualInput2(Logger logger, VirtualKeyboard2 preloadedKeyboard, VirtualMouse2 preloadedMouse, VirtualCameraAngle2 preloadedCamera) { - this.LOGGER = logger; - KEYBOARD = new VirtualKeyboardInput(preloadedKeyboard); - MOUSE = new VirtualMouseInput(preloadedMouse); - CAMERA_ANGLE = new VirtualCameraAngleInput(preloadedCamera); - } - - /** - * Updates the logic for {@link #KEYBOARD}, {@link #MOUSE} and - * {@link #CAMERA_ANGLE}
- * Runs every frame - * - * @see MixinMinecraft#playback_injectRunGameLoop(CallbackInfo) - * @param currentScreen The current screen from Minecraft.class. Used for - * checking if the mouse logic should be adapted to - * GUIScreens - */ - public void update(GuiScreen currentScreen) { - while (Keyboard.next()) { - KEYBOARD.updateNextKeyboard(Keyboard.getEventKey(), Keyboard.getEventKeyState(), Keyboard.getEventCharacter(), Keyboard.areRepeatEventsEnabled()); - } - while (Mouse.next()) { - if (currentScreen == null) { - MOUSE.updateNextMouse(Mouse.getEventButton(), Mouse.getEventButtonState(), Mouse.getEventDWheel(), 0, 0); - } else { - Ducks.GuiScreenDuck screen = (Ducks.GuiScreenDuck) currentScreen; - int eventX = screen.unscaleX(Mouse.getEventX()); - int eventY = screen.unscaleY(Mouse.getEventY()); - eventX = PointerNormalizer.getNormalizedX(eventX); - eventY = PointerNormalizer.getNormalizedY(eventY); - MOUSE.updateNextMouse(Mouse.getEventButton(), Mouse.getEventButtonState(), Mouse.getEventDWheel(), eventX, eventY); - } - } - } - - /** - * If the keyboard or mouse key is currently down. - * If keycode >= 0 then {@link VirtualKeyboardInput#isKeyDown(int)} will be called,
- * otherwise {@link VirtualMouseInput#isKeyDown(int)} - * - * @param keycode The keycode in question - * @return If the key is down either on mouse or keyboard - */ - public boolean isKeyDown(int keycode) { - if(keycode >= 0) { - return KEYBOARD.isKeyDown(keycode); - } else { - return MOUSE.isKeyDown(keycode); - } - } - - /** - * If the keyboard or mouse key is will be down in the next tick. - * If keycode >= 0 then {@link VirtualKeyboardInput#willKeyBeDown(int)} will be called,
- * otherwise {@link VirtualMouseInput#willKeyBeDown(int)} - * - * @param keycode The keycode in question - * @return If the key will be down either on mouse or keyboard - */ - public boolean willKeyBeDown(int keycode) { - if(keycode >= 0) { - return KEYBOARD.willKeyBeDown(keycode); - } else { - return MOUSE.willKeyBeDown(keycode); - } - } - - /** - * Unpresses all keys in {@link VirtualKeyboardInput#nextKeyboard} and {@link VirtualMouseInput#nextMouse} - */ - public void unpress() { - KEYBOARD.nextKeyboard.clear(); - MOUSE.nextMouse.clear(); - } - - /** - * Subclass of {@link VirtualInput2} handling keyboard logic.
- *
- * Vanilla keyboard handling looks something like this: - * - *
-	 *	public void runTickKeyboard()  { // Executed every tick in runTick()
-	 *		while({@linkplain Keyboard#next()}){
-	 *			int keycode = {@linkplain Keyboard#getEventKey()};
-	 *			boolean keystate = {@linkplain Keyboard#getEventKey()};
-	 *			char character = {@linkplain Keyboard#getEventCharacter()}
-	 *
-	 *			Keybindings.updateKeybind(keycode, keystate, character)
-	 *		}
-	 *	}
-	 * 
- * - * After redirecting the calls in {@link MixinMinecraft}, the resulting logic - * now looks like this: - * - *
-	 *	public void runTickKeyboard()  {
-	 *		{@linkplain #nextKeyboardTick()}
-	 *		while({@linkplain #nextKeyboardSubtick()}){
-	 *			int keycode = {@linkplain #getEventKeyboardKey()}};
-	 *			boolean keystate = {@linkplain #getEventKeyboardState()};
-	 *			char character = {@linkplain #getEventKeyboardCharacter()}
-	 *
-	 *			Keybindings.updateKeybind(keycode, keystate, character)
-	 *		}
-	 *	}
-	 * 
- * @see com.minecrafttas.tasmod.virtual.VirtualKeyboard2 - */ - public class VirtualKeyboardInput { - /** - * The keyboard "state" that is currently recognized by the game,
- * meaning it is a direct copy of the vanilla keybindings. Updated every - * tick.
- * Updated in {@link #nextKeyboardTick()} - */ - private final VirtualKeyboard2 currentKeyboard; - /** - * The "state" of the real physical keyboard.
- * This is updated every frame.
- * Updates {@link #currentKeyboard} in {@link #nextKeyboardTick()} - */ - private final VirtualKeyboard2 nextKeyboard = new VirtualKeyboard2(); - /** - * Queue for keyboard events.
- * Is filled in {@link #nextKeyboardTick()} and read in - * {@link #nextKeyboardSubtick()} - */ - private final Queue keyboardEventQueue = new ConcurrentLinkedQueue(); - /** - * The current keyboard event where the vanilla keybindings are reading - * from.
- * Updated in {@link #nextKeyboardSubtick()} and read out in - *
    - *
  • {@link #getEventKeyboardKey()}
  • - *
  • {@link #getEventKeyboardState()}
  • - *
  • {@link #getEventKeyboardCharacter()}
  • - *
- */ - private VirtualKeyboardEvent currentKeyboardEvent = new VirtualKeyboardEvent(); - - /** - * Constructor to preload the {@link #currentKeyboard} with an existing keyboard - * @param preloadedKeyboard The new {@link #currentKeyboard} - */ - public VirtualKeyboardInput(VirtualKeyboard2 preloadedKeyboard) { - currentKeyboard = preloadedKeyboard; - } - - /** - * Updates the next keyboard - * - * @see VirtualInput2#update(GuiScreen) - * @param keycode The keycode of this event - * @param keystate The keystate of this event - * @param character The character of this event - */ - public void updateNextKeyboard(int keycode, boolean keystate, char character) { - updateNextKeyboard(keycode, keystate, character, false); - } - - /** - * Updates the next keyboard - * - * @see VirtualInput2#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 - */ - public void updateNextKeyboard(int keycode, boolean keystate, char character, boolean repeatEventsEnabled) { - LOGGER.debug(LoggerMarkers.Keyboard, "Update: {}, {}, {}, {}", keycode, keystate, character); // Activate with -Dtasmod.marker.keyboard=ACCEPT in VM arguments (and -Dtasmod.log.level=debug) - nextKeyboard.update(keycode, keystate, character, repeatEventsEnabled); - } - - /** - * Runs when the next keyboard tick is about to occur.
- * Used to load {@link #nextKeyboard} into {@link #currentKeyboard}, creating - * {@link VirtualKeyboardEvent}s in the process. - * - * @see MixinMinecraft#playback_injectRunTickKeyboard(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) - */ - public void nextKeyboardTick() { - currentKeyboard.getVirtualEvents(nextKeyboard, keyboardEventQueue); - currentKeyboard.copyFrom(nextKeyboard); - } - - /** - * Runs in a while loop. Used for updating {@link #currentKeyboardEvent} and - * ending the while loop. - * - * @see MixinMinecraft#playback_redirectKeyboardNext() - * @return If a keyboard event is in {@link #keyboardEventQueue} - */ - public boolean nextKeyboardSubtick() { - return (currentKeyboardEvent = keyboardEventQueue.poll()) != null; - } - - /** - * @return The keycode of {@link #currentKeyboardEvent} - */ - public int getEventKeyboardKey() { - return currentKeyboardEvent.getKeyCode(); - } - - /** - * @return The keystate of {@link #currentKeyboardEvent} - */ - public boolean getEventKeyboardState() { - return currentKeyboardEvent.isState(); - } - - /** - * @return The character(s) of {@link #currentKeyboardEvent} - */ - public char getEventKeyboardCharacter() { - return currentKeyboardEvent.getCharacter(); - } - - /** - * If the key is currently down and recognised by Minecraft - * @param keycode The keycode of the key in question - * @return Whether the key of the {@link #currentKeyboard} is down - */ - public boolean isKeyDown(int keycode) { - return currentKeyboard.isKeyDown(keycode); - } - - /** - * If the key will be down and recognised in the next tick by Minecraft.
- * This is equal to checking if a key on the physical keyboard is pressed - * @param keycode The keycode of the key in question - * @return Whether the key of the {@link #nextKeyboard} is down - */ - public boolean willKeyBeDown(int keycode) { - return nextKeyboard.isKeyDown(keycode); - } - } - - /** - * Subclass of {@link VirtualInput2} handling mouse logic.
- *
- * Vanilla mouse handling looks something like this: - * - *
-	 *	public void runTickMouse()  { // Executed every tick in runTick()
-	 *		while({@linkplain Mouse#next()}){
-	 *			int keycode = {@linkplain Mouse#getEventButton()};
-	 *			boolean keystate = {@linkplain Mouse#getEventButtonState()};
-	 *			int scrollWheel = {@linkplain Mouse#getEventDWheel()}
-	 *			int cursorX = {@linkplain Mouse#getEventX()} // Important in GUIs
-	 *			int cursorY = {@linkplain Mouse#getEventY()}
-	 *
-	 *			Keybindings.updateKeybind(keycode, keystate, etc...)
-	 *		}
-	 *	}
-	 * 
- * - * After redirecting the calls in {@link MixinMinecraft}, the resulting logic - * now looks like this: - * - *
-	 *	public void runTickMouse()  { // Executed every tick in runTick()
-	 *		{@linkplain #nextMouseTick()}
-	 *		while({@linkplain #nextMouseSubtick}){
-	 *			int keycode = {@linkplain #getEventMouseKey};
-	 *			boolean keystate = {@linkplain #getEventMouseState()} ()};
-	 *			int scrollWheel = {@linkplain #getEventMouseScrollWheel()}
-	 *			int cursorX = {@linkplain #getEventCursorX()} // Important in GUIs
-	 *			int cursorY = {@linkplain #getEventCursorY()}
-	 *
-	 *			Keybindings.updateKeybind(keycode, keystate, etc...)
-	 *		}
-	 *	}
-	 * 
- * @see com.minecrafttas.tasmod.virtual.VirtualMouse2 - */ - public class VirtualMouseInput { - /** - * The mouse "state" that is currently recognized by the game,
- * meaning it is a direct copy of the vanilla mouse. Updated every - * tick.
- * Updated in {@link #nextMouseTick()} - */ - private final VirtualMouse2 currentMouse; - /** - * The "state" of the real physical mouse.
- * This is updated every frame.
- * Updates {@link #currentMouse} in {@link #nextMouseTick()} - */ - private final VirtualMouse2 nextMouse = new VirtualMouse2(); - /** - * Queue for keyboard events.
- * Is filled in {@link #nextMouseTick()} and read in - * {@link #nextMouseSubtick()} - */ - private final Queue mouseEventQueue = new ConcurrentLinkedQueue<>(); - /** - * The current mouse event where the vanilla mouse is reading - * from.
- * Updated in {@link #nextMouseSubtick()} and read out in - *
    - *
  • {@link #getEventMouseKey()}
  • - *
  • {@link #getEventMouseState()}
  • - *
  • {@link #getEventMouseScrollWheel()}
  • - *
  • {@link #getEventCursorX()}
  • - *
  • {@link #getEventCursorY()}
  • - *
- */ - private VirtualMouseEvent currentMouseEvent = new VirtualMouseEvent(); - - /** - * Constructor to preload the {@link #currentMouse} with an existing mouse - * @param preloadedMouse The new {@link #currentMouse} - */ - public VirtualMouseInput(VirtualMouse2 preloadedMouse) { - currentMouse = preloadedMouse; - } - - /** - * Updates the next keyboard - * - * @see VirtualInput2#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 - */ - public void updateNextMouse(int keycode, boolean keystate, int scrollwheel, int cursorX, int cursorY) { - keycode-=100; - LOGGER.debug(LoggerMarkers.Mouse,"Update: {} ({}), {}, {}, {}, {}", keycode, VirtualKey2.getName(keycode), keystate, scrollwheel, cursorX, cursorY); // Activate with -Dtasmod.marker.mouse=ACCEPT in VM arguments (and -Dtasmod.log.level=debug) - nextMouse.update(keycode, keystate, scrollwheel, cursorX, cursorY); - } - - /** - * Runs when the next mouse tick is about to occur.
- * Used to load {@link #nextMouse} into {@link #currentMouse}, creating - * {@link VirtualMouseEvent}s in the process. - * - * @see MixinMinecraft#playback_injectRunTickMouse(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) - */ - public void nextMouseTick() { - currentMouse.getVirtualEvents(nextMouse, mouseEventQueue); - currentMouse.copyFrom(nextMouse); - } - - /** - * Runs in a while loop. Used for updating {@link #currentMouseEvent} and - * ending the while loop. - * - * @see MixinMinecraft#playback_redirectMouseNext() - * @return If a mouse event is in {@link #mouseEventQueue} - */ - public boolean nextMouseSubtick() { - return (currentMouseEvent = mouseEventQueue.poll()) != null; - } - - /** - * @return The keycode of {@link #currentMouseEvent} - */ - public int getEventMouseKey() { - return currentMouseEvent.getKeyCode(); - } - - /** - * @return The keystate of {@link #currentMouseEvent} - */ - public boolean getEventMouseState() { - return currentMouseEvent.isState(); - } - - /** - * @return The scroll wheel of {@link #currentMouseEvent} - */ - public int getEventMouseScrollWheel() { - return currentMouseEvent.getScrollwheel(); - } - - /** - * @return The x coordinate of the cursor of {@link #currentMouseEvent} - */ - public int getEventCursorX() { - return PointerNormalizer.getCoordsX(currentMouseEvent.getCursorX()); - } - - /** - * @return The y coordinate of the cursor of {@link #currentMouseEvent} - */ - public int getEventCursorY() { - return PointerNormalizer.getCoordsY(currentMouseEvent.getCursorY()); - } - - /** - * If the key is currently down and recognised by Minecraft - * @param keycode The keycode of the key in question - * @return Whether the key of the {@link #currentMouse} is down - */ - public boolean isKeyDown(int keycode) { - return currentMouse.isKeyDown(keycode); - } - - /** - * If the key will be down and recognised in the next tick by Minecraft.
- * This is equal to checking if a key on the physical mouse is pressed - * @param keycode The keycode of the key in question - * @return Whether the key of the {@link #nextMouse} is down - */ - public boolean willKeyBeDown(int keycode) { - return nextMouse.isKeyDown(keycode); - } - - } - - public class VirtualCameraAngleInput { - private final VirtualCameraAngle2 cameraAngle; - - public VirtualCameraAngleInput(VirtualCameraAngle2 preloadedCamera) { - cameraAngle = preloadedCamera; - } - - public void updateCameraAngle(float pitch, float yaw) { - cameraAngle.update(pitch, yaw); - } - - public float getPitch() { - return cameraAngle.getPitch(); - } - - public float getYaw() { - return cameraAngle.getYaw(); - } - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKey.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKey.java index 4b70aa8e..2c175166 100644 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKey.java +++ b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKey.java @@ -1,61 +1,189 @@ package com.minecrafttas.tasmod.virtual; -import java.io.Serializable; - -/** - * Class to store which keys on the keyboard have been pressed, similar to how Keybindings work.
- * - * @author Scribble - * - */ -@Deprecated -public class VirtualKey implements Serializable { - - /** - * - */ - private static final long serialVersionUID = 2186369910433056224L; - private String name; - private int keycode; - private boolean isKeyDown=false; - - public VirtualKey(String name, int keycode) { - this.name = name; - this.keycode = keycode; - } - - private VirtualKey(String name, int keycode, boolean isKeyDown) { - this.name = name; - this.keycode = keycode; - this.isKeyDown = isKeyDown; - } - - public String getName() { - return name; - } - - public int getKeycode() { - return keycode; - } - - public boolean isKeyDown() { - return isKeyDown; - } - - public void setPressed(boolean pressed) { - isKeyDown=pressed; - } - - @Override - public boolean equals(Object obj) { - VirtualKey key = (VirtualKey) obj; - if(key.isKeyDown!=isKeyDown) return false; - if(key.keycode!=keycode) return false; - return true; - } - - @Override - public VirtualKey clone() { - return new VirtualKey(name, keycode, isKeyDown); - } +public enum VirtualKey { + // Keyboard + ZERO(0), + ESC(1), + KEY_1(2), + KEY_2(3), + KEY_3(4), + KEY_4(5), + KEY_5(6), + KEY_6(7), + KEY_7(8), + KEY_8(9), + KEY_9(10), + KEY_0(11), + MINUS(12), + EQUALS(13), + BACK(14), + TAB(15), + Q(16), + W(17), + E(18), + R(19), + T(20), + Y(21), + U(22), + I(23), + O(24), + P(25), + LBRACKET(26), + RBRACKET(27), + RETURN(28), + LCONTROL(29), + A(30), + S(31), + D(32), + F(33), + G(34), + H(35), + J(36), + K(37), + L(38), + SEMICOLON(39), + APOSTROPHE(40), + GRAVE(41), + LSHIFT(42), + BACKSLASH(43), + Z(44), + X(45), + C(46), + V(47), + B(48), + N(49), + M(50), + COMMA(51), + PERIOD(52), + SLASH(53), + RSHIFT(54), + MULTIPLY(55), + ALT(56), + SPACE(57), + CAPSLOCK(58), + F1(59), + F2(60), + F3(61), + F4(62), + F5(63), + F6(64), + F7(65), + F8(66), + F9(67), + F10(68), + NUMLOCK(69), + SCROLL(70), + NUMPAD7(71), + NUMPAD8(72), + NUMPAD9(73), + SUBTRACT(74), + NUMPAD4(75), + NUMPAD5(76), + NUMPAD6(77), + ADD(78), + NUMPAD1(79), + NUMPAD2(80), + NUMPAD3(81), + NUMPAD0(82), + DECIMAL(83), + F11(87), + F12(88), + F13(100), + F14(101), + F15(102), + F16(103), + F17(104), + F18(105), + KANA(112), + F19(113), + CONVERT(121), + NOCONVERT(123), + YEN(125), + NUMPADEQUALS(141), + CIRCUMFLEX(144), + AT(145), + COLON(146), + UNDERLINE(147), + KANJI(148), + STOP(149), + NUMPADENTER(156), + RCONTROL(157), + NUMPADCOMMA(179), + DIVIDE(181), + PRINT(183), + ALT_GR(184), + PAUSE(197), + HOME(199), + UP(200), + PRIOR(201), + LEFT(203), + RIGHT(205), + END(207), + DOWN(208), + NEXT(209), + INSERT(210), + DELETE(211), + WIN(219), + APPS(221), + + // Mouse + MOUSEMOVED(-101), + LC(-100), + RC(-99), + MC(-98), + MBUTTON4(-97), + MBUTTON5(-96), + MBUTTON6(-95), + MBUTTON7(-94), + MBUTTON8(-93), + MBUTTON9(-92), + MBUTTON10(-91), + MBUTTON11(-90), + MBUTTON12(-89), + MBUTTON13(-88), + MBUTTON14(-87), + MBUTTON15(-86), + MBUTTON16(-85); + + private final int keycode; + + private VirtualKey(int keycode) { + this.keycode = keycode; + } + + public int getKeycode() { + return keycode; + } + + public static Integer getKeycode(String keyname) { + VirtualKey key = get(keyname); + if (key != null) + return key.getKeycode(); + return null; + } + + public static String getName(int keycode) { + VirtualKey key = get(keycode); + if (key != null) + return key.name(); + return Integer.toString(keycode); + } + + public static VirtualKey get(int keycode) { + for (VirtualKey key : values()) { + if (key.getKeycode() == keycode) { + return key; + } + } + return null; + } + + public static VirtualKey get(String keyname) { + for (VirtualKey key : values()) { + if (key.name().equalsIgnoreCase(keyname)) { + return key; + } + } + return null; + } } diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKey2.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKey2.java deleted file mode 100644 index ddce26b3..00000000 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKey2.java +++ /dev/null @@ -1,189 +0,0 @@ -package com.minecrafttas.tasmod.virtual; - -public enum VirtualKey2 { - // Keyboard - ZERO(0), - ESC(1), - KEY_1(2), - KEY_2(3), - KEY_3(4), - KEY_4(5), - KEY_5(6), - KEY_6(7), - KEY_7(8), - KEY_8(9), - KEY_9(10), - KEY_0(11), - MINUS(12), - EQUALS(13), - BACK(14), - TAB(15), - Q(16), - W(17), - E(18), - R(19), - T(20), - Y(21), - U(22), - I(23), - O(24), - P(25), - LBRACKET(26), - RBRACKET(27), - RETURN(28), - LCONTROL(29), - A(30), - S(31), - D(32), - F(33), - G(34), - H(35), - J(36), - K(37), - L(38), - SEMICOLON(39), - APOSTROPHE(40), - GRAVE(41), - LSHIFT(42), - BACKSLASH(43), - Z(44), - X(45), - C(46), - V(47), - B(48), - N(49), - M(50), - COMMA(51), - PERIOD(52), - SLASH(53), - RSHIFT(54), - MULTIPLY(55), - ALT(56), - SPACE(57), - CAPSLOCK(58), - F1(59), - F2(60), - F3(61), - F4(62), - F5(63), - F6(64), - F7(65), - F8(66), - F9(67), - F10(68), - NUMLOCK(69), - SCROLL(70), - NUMPAD7(71), - NUMPAD8(72), - NUMPAD9(73), - SUBTRACT(74), - NUMPAD4(75), - NUMPAD5(76), - NUMPAD6(77), - ADD(78), - NUMPAD1(79), - NUMPAD2(80), - NUMPAD3(81), - NUMPAD0(82), - DECIMAL(83), - F11(87), - F12(88), - F13(100), - F14(101), - F15(102), - F16(103), - F17(104), - F18(105), - KANA(112), - F19(113), - CONVERT(121), - NOCONVERT(123), - YEN(125), - NUMPADEQUALS(141), - CIRCUMFLEX(144), - AT(145), - COLON(146), - UNDERLINE(147), - KANJI(148), - STOP(149), - NUMPADENTER(156), - RCONTROL(157), - NUMPADCOMMA(179), - DIVIDE(181), - PRINT(183), - ALT_GR(184), - PAUSE(197), - HOME(199), - UP(200), - PRIOR(201), - LEFT(203), - RIGHT(205), - END(207), - DOWN(208), - NEXT(209), - INSERT(210), - DELETE(211), - WIN(219), - APPS(221), - - // Mouse - MOUSEMOVED(-101), - LC(-100), - RC(-99), - MC(-98), - MBUTTON4(-97), - MBUTTON5(-96), - MBUTTON6(-95), - MBUTTON7(-94), - MBUTTON8(-93), - MBUTTON9(-92), - MBUTTON10(-91), - MBUTTON11(-90), - MBUTTON12(-89), - MBUTTON13(-88), - MBUTTON14(-87), - MBUTTON15(-86), - MBUTTON16(-85); - - private final int keycode; - - private VirtualKey2(int keycode) { - this.keycode = keycode; - } - - public int getKeycode() { - return keycode; - } - - public static Integer getKeycode(String keyname) { - VirtualKey2 key = get(keyname); - if (key != null) - return key.getKeycode(); - return null; - } - - public static String getName(int keycode) { - VirtualKey2 key = get(keycode); - if (key != null) - return key.name(); - return Integer.toString(keycode); - } - - public static VirtualKey2 get(int keycode) { - for (VirtualKey2 key : values()) { - if (key.getKeycode() == keycode) { - return key; - } - } - return null; - } - - public static VirtualKey2 get(String keyname) { - for (VirtualKey2 key : values()) { - if (key.name().equalsIgnoreCase(keyname)) { - return key; - } - } - return null; - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard.java index 31912546..ae96365d 100644 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard.java +++ b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard.java @@ -2,381 +2,323 @@ import java.io.Serializable; import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.stream.Collectors; +import com.minecrafttas.tasmod.virtual.event.VirtualKeyboardEvent; import org.apache.commons.lang3.StringUtils; -import org.lwjgl.input.Keyboard; -import com.google.common.collect.Maps; -import com.minecrafttas.tasmod.playback.PlaybackSerialiser; - -@Deprecated -public class VirtualKeyboard implements Serializable { - - /** - * - */ - private static final long serialVersionUID = 4694772261313303998L; - - private Map keyList; - - private List charList; - - /** - * Creates a copy of the virtual keyboard with the given key list - * - * @param keyListIn - */ - public VirtualKeyboard(Map keyListIn, List charListIn) { - Map copy = new HashMap(); - - keyListIn.forEach((key, value) -> { - copy.put(key, value.clone()); - }); - keyList = copy; - - List charCopy = new ArrayList(); - - charListIn.forEach(charAction -> { - charCopy.add(charAction); - }); - charList = charCopy; +import com.google.common.collect.ImmutableList; + +/** + * Stores keyboard specific values in a given timeframe.
+ *
+ * This keyboard mimics the {@link org.lwjgl.input.Keyboard} Minecraft is using. + *

KeyboardEvent

+ * {@link org.lwjgl.input.Keyboard} has the following outputs, when a key is pressed or unpressed on the physical keyboard: + *
    + *
  • int KeyCode: The unique keycode of the key
  • + *
  • boolean KeyState: The new state of the key. True for pressed, false for unpressed
  • + *
  • char KeyCharacter: The character associated for each key
  • + *
+ * While the keycode is the same between physical keyboards, the key character might differ.
+ * It is also common that one keycode has multiple characters associated with it, e.g.
+ * holding shift results in a capitalised character.
+ *
+ * These three outputs together are what we call a "KeyboardEvent" and might look like this: + *
+ *     17, true, w
+ * 
+ * For keycode, keystate, keycharacter + *

Updating the keyboard

+ * This keyboard stores it's values in "states".
+ * That means that all the keys that are currently pressed are stored in {@link #pressedKeys}.
+ * And this list is updated via a keyboard event in {@link #update(int, boolean, char)}.
+ *

Difference

+ * When comparing 2 keyboard states, we can generate a list of differences from them in form of {@link VirtualKeyboardEvent}s.
+ *
+ * 	this: W A S
+ *	next: W   S D
+ * 
+ * Since there are 2 differences between this and the next keyboard, + * this will result in 2 {@link VirtualKeyboardEvent}s. And combined with the {@link #charList} we can also get the associated characters: + *
+ *	30, false, null // A is unpressed
+ * 	32, true, d 	// D is pressed
+ * 
+ *

Subticks

+ * Minecraft updates it's keyboard every tick. All the key events that occur inbetween are stored,
+ * then read out when a new tick has started.
We call these "inbetween" ticks subticks.
+ *

Parent->Subtick

+ * In a previous version of this keyboard, subticks were bundeled and flattened into one keyboard state.
+ * After all, Minecraft updates only occur once every tick, storing subticks seemed unnecessary.
+ *
+ * However this posed some usability issues when playing in a low game speed via {@link com.minecrafttas.tasmod.tickratechanger.TickrateChangerClient}.
+ * Now you had to hold the key until the next tick to get it recognised by the game.
+ *
+ * To fix this, now every subtick is stored as a keyboard state as well.
+ * When updating the keyboard in {@link #update(int, boolean, char)}, a clone of itself is created and stored in {@link #subtickList},
+ * with the difference that the subtick state has no {@link #subtickList}.
+ * In a nutshell, the keyboard stores it's past changes in {@link #subtickList} with the first being the oldest change. + * + * @author Scribble + * @see VirtualInput.VirtualKeyboardInput + */ +public class VirtualKeyboard extends VirtualPeripheral implements Serializable { + + /** + * The list of characters that were pressed on this keyboard. + */ + private final List charList; + + /** + * A queue of characters used in {@link #getDifference(VirtualKeyboard, Queue)}.
+ * Used for distributing characters to {@link VirtualKeyboardEvent}s in an order. + */ + private final ConcurrentLinkedQueue charQueue = new ConcurrentLinkedQueue<>(); + + /** + * Creates an empty parent keyboard with all keys unpressed + */ + public VirtualKeyboard() { + this(new LinkedHashSet<>(), new ArrayList<>(), new ArrayList<>(), true); + } + + /** + * Creates a subtick keyboard with {@link VirtualPeripheral#subtickList} uninitialized + * @param pressedKeys The new list of pressed keycodes for this subtickKeyboard + * @param charList A list of characters for this subtickKeyboard + */ + public VirtualKeyboard(Set pressedKeys, List charList){ + this(pressedKeys, charList, null, false); + } + + /** + * Creates a keyboard from existing variables + * @param pressedKeys The existing list of pressed keycodes + * @param charList The existing list of characters + */ + public VirtualKeyboard(Set pressedKeys, List charList, boolean ignoreFirstUpdate) { + this(pressedKeys, charList, null, ignoreFirstUpdate); } - + /** - * Creates a Keyboard, where the keys are all unpressed + * Creates a keyboard from existing variables + * @param pressedKeys The existing list of {@link VirtualPeripheral#pressedKeys} + * @param charList The {@link #charList} + * @param subtickList {@link VirtualPeripheral#subtickList} + * @param ignoreFirstUpdate The {@link VirtualPeripheral#ignoreFirstUpdate} */ - public VirtualKeyboard() { - charList = new ArrayList(); - - keyList = Maps.newHashMap(); - keyList.put(0, new VirtualKey("0", 0)); - keyList.put(1, new VirtualKey("ESC", 1)); - keyList.put(2, new VirtualKey("KEY_1", 2)); - keyList.put(3, new VirtualKey("KEY_2", 3)); - keyList.put(4, new VirtualKey("KEY_3", 4)); - keyList.put(5, new VirtualKey("KEY_4", 5)); - keyList.put(6, new VirtualKey("KEY_5", 6)); - keyList.put(7, new VirtualKey("KEY_6", 7)); - keyList.put(8, new VirtualKey("KEY_7", 8)); - keyList.put(9, new VirtualKey("KEY_8", 9)); - keyList.put(10, new VirtualKey("KEY_9", 10)); - keyList.put(11, new VirtualKey("KEY_0", 11)); - keyList.put(12, new VirtualKey("MINUS", 12)); - keyList.put(13, new VirtualKey("EQUALS", 13)); - keyList.put(14, new VirtualKey("BACK", 14)); - keyList.put(15, new VirtualKey("TAB", 15)); - keyList.put(16, new VirtualKey("Q", 16)); - keyList.put(17, new VirtualKey("W", 17)); - keyList.put(18, new VirtualKey("E", 18)); - keyList.put(19, new VirtualKey("R", 19)); - keyList.put(20, new VirtualKey("T", 20)); - keyList.put(21, new VirtualKey("Y", 21)); - keyList.put(22, new VirtualKey("U", 22)); - keyList.put(23, new VirtualKey("I", 23)); - keyList.put(24, new VirtualKey("O", 24)); - keyList.put(25, new VirtualKey("P", 25)); - keyList.put(26, new VirtualKey("LBRACKET", 26)); - keyList.put(27, new VirtualKey("RBRACKET", 27)); - keyList.put(28, new VirtualKey("RETURN", 28)); - keyList.put(29, new VirtualKey("LCONTROL", 29)); - keyList.put(30, new VirtualKey("A", 30)); - keyList.put(31, new VirtualKey("S", 31)); - keyList.put(32, new VirtualKey("D", 32)); - keyList.put(33, new VirtualKey("F", 33)); - keyList.put(34, new VirtualKey("G", 34)); - keyList.put(35, new VirtualKey("H", 35)); - keyList.put(36, new VirtualKey("J", 36)); - keyList.put(37, new VirtualKey("K", 37)); - keyList.put(38, new VirtualKey("L", 38)); - keyList.put(39, new VirtualKey("SEMICOLON", 39)); - keyList.put(40, new VirtualKey("APOSTROPHE", 40)); - keyList.put(41, new VirtualKey("GRAVE", 41)); - keyList.put(42, new VirtualKey("LSHIFT", 42)); - keyList.put(43, new VirtualKey("BACKSLASH", 43)); - keyList.put(44, new VirtualKey("Z", 44)); - keyList.put(45, new VirtualKey("X", 45)); - keyList.put(46, new VirtualKey("C", 46)); - keyList.put(47, new VirtualKey("V", 47)); - keyList.put(48, new VirtualKey("B", 48)); - keyList.put(49, new VirtualKey("N", 49)); - keyList.put(50, new VirtualKey("M", 50)); - keyList.put(51, new VirtualKey("COMMA", 51)); - keyList.put(52, new VirtualKey("PERIOD", 52)); - keyList.put(53, new VirtualKey("SLASH", 53)); - keyList.put(54, new VirtualKey("RSHIFT", 54)); - keyList.put(55, new VirtualKey("MULTIPLY", 55)); - keyList.put(56, new VirtualKey("ALT", 56)); - keyList.put(57, new VirtualKey("SPACE", 57)); - keyList.put(58, new VirtualKey("CAPSLOCK", 58)); - keyList.put(59, new VirtualKey("F1", 59)); - keyList.put(60, new VirtualKey("F2", 60)); - keyList.put(61, new VirtualKey("F3", 61)); - keyList.put(62, new VirtualKey("F4", 62)); - keyList.put(63, new VirtualKey("F5", 63)); - keyList.put(64, new VirtualKey("F6", 64)); - keyList.put(65, new VirtualKey("F7", 65)); - keyList.put(66, new VirtualKey("F8", 66)); - keyList.put(67, new VirtualKey("F9", 67)); - keyList.put(68, new VirtualKey("F10", 68)); - keyList.put(69, new VirtualKey("NUMLOCK", 69)); - keyList.put(70, new VirtualKey("SCROLL", 70)); - keyList.put(71, new VirtualKey("NUMPAD7", 71)); - keyList.put(72, new VirtualKey("NUMPAD8", 72)); - keyList.put(73, new VirtualKey("NUMPAD9", 73)); - keyList.put(74, new VirtualKey("SUBTRACT", 74)); - keyList.put(75, new VirtualKey("NUMPAD4", 75)); - keyList.put(76, new VirtualKey("NUMPAD5", 76)); - keyList.put(77, new VirtualKey("NUMPAD6", 77)); - keyList.put(78, new VirtualKey("ADD", 78)); - keyList.put(79, new VirtualKey("NUMPAD1", 79)); - keyList.put(80, new VirtualKey("NUMPAD2", 80)); - keyList.put(81, new VirtualKey("NUMPAD3", 81)); - keyList.put(82, new VirtualKey("NUMPAD0", 82)); - keyList.put(83, new VirtualKey("DECIMAL", 83)); - keyList.put(87, new VirtualKey("F11", 87)); - keyList.put(88, new VirtualKey("F12", 88)); - keyList.put(100, new VirtualKey("F13", 100)); - keyList.put(101, new VirtualKey("F14", 101)); - keyList.put(102, new VirtualKey("F15", 102)); - keyList.put(103, new VirtualKey("F16", 103)); - keyList.put(104, new VirtualKey("F17", 104)); - keyList.put(105, new VirtualKey("F18", 105)); - keyList.put(112, new VirtualKey("KANA", 112)); - keyList.put(113, new VirtualKey("F19", 113)); - keyList.put(121, new VirtualKey("CONVERT", 121)); - keyList.put(123, new VirtualKey("NOCONVERT", 123)); - keyList.put(125, new VirtualKey("YEN", 125)); - keyList.put(141, new VirtualKey("NUMPADEQUALS", 141)); - keyList.put(144, new VirtualKey("CIRCUMFLEX", 144)); - keyList.put(145, new VirtualKey("AT", 145)); - keyList.put(146, new VirtualKey("COLON", 146)); - keyList.put(147, new VirtualKey("UNDERLINE", 147)); - keyList.put(148, new VirtualKey("KANJI", 148)); - keyList.put(149, new VirtualKey("STOP", 149)); - keyList.put(156, new VirtualKey("NUMPADENTER", 156)); - keyList.put(157, new VirtualKey("RCONTROL", 157)); - keyList.put(179, new VirtualKey("NUMPADCOMMA", 179)); - keyList.put(181, new VirtualKey("DIVIDE", 181)); - keyList.put(183, new VirtualKey("PRINT", 183)); - keyList.put(184, new VirtualKey("ALT_GR", 184)); - keyList.put(197, new VirtualKey("PAUSE", 197)); - keyList.put(199, new VirtualKey("HOME", 199)); - keyList.put(200, new VirtualKey("UP", 200)); - keyList.put(201, new VirtualKey("PRIOR", 201)); - keyList.put(203, new VirtualKey("LEFT", 203)); - keyList.put(205, new VirtualKey("RIGHT", 205)); - keyList.put(207, new VirtualKey("END", 207)); - keyList.put(208, new VirtualKey("DOWN", 208)); - keyList.put(209, new VirtualKey("NEXT", 209)); - keyList.put(210, new VirtualKey("INSERT", 210)); - keyList.put(211, new VirtualKey("DELETE", 211)); - keyList.put(219, new VirtualKey("WIN", 219)); - keyList.put(221, new VirtualKey("APPS", 221)); - } - - public void add(int keycode) { - String keyString = Integer.toString(keycode); - - keyList.put(keycode, new VirtualKey(keyString, keycode)); - } - - public VirtualKey get(int keycode) { - VirtualKey key = keyList.get(keycode); - if (key == null) { - add(keycode); - return keyList.get(keycode); - } else - return key; - } - - public VirtualKey get(String keyname) { - Collection list = keyList.values(); - VirtualKey out = null; - - for (VirtualKey key : list) { - if (key.getName().equalsIgnoreCase(keyname)) { - out = key; - } - } - return out; - } - - public List getCurrentPresses() { - List out = new ArrayList(); - - keyList.forEach((keycodes, virtualkeys) -> { - if (keycodes >= 0) { - if (virtualkeys.isKeyDown()) { - out.add(virtualkeys.getName()); - } - } - }); - - return out; - } - - public Map getKeyList() { - return this.keyList; - } - - public void addChar(char charin) { - charList.add(charin); - } - - public List getCharList() { - return charList; - } - - public String getCharListAsString() { - String out=""; - for(char chars: charList) { - out=out.concat(Character.toString(chars)); + public VirtualKeyboard(Set pressedKeys, List charList, List subtickList, boolean ignoreFirstUpdate) { + super(pressedKeys, subtickList, ignoreFirstUpdate); + this.charList = charList; + } + + /** + * Updates the keyboard, adds a new subtick to this keyboard + * @param keycode The keycode of this key + * @param keystate The keystate of this key, true for pressed + * @param keycharacter The character that is associated with that key. Can change between keyboards or whenever shift is held in combination. + */ + public void update(int keycode, boolean keystate, char keycharacter, boolean repeatEventsEnabled) { + if(isParent() && !ignoreFirstUpdate()) { + addSubtick(clone()); + } + charList.clear(); + if (keystate) { + addChar(keycharacter, repeatEventsEnabled); } - return out; - } - - public void clearCharList() { - charList.clear(); - } - - public void clear() { - keyList.forEach((keycode, key) -> { - key.setPressed(false); - }); - charList.clear(); - } - - public List getDifference(VirtualKeyboard keyboardToCompare) { - - List eventList = new ArrayList(); - - keyList.forEach((keycodes, virtualkeys) -> { - - VirtualKey keyToCompare = keyboardToCompare.get(keycodes); + setPressed(keycode, keystate); + } + + public void update(int keycode, boolean keystate, char keycharacter) { + update(keycode, keystate, keycharacter, false); + } + + @Override + public void setPressed(int keycode, boolean keystate) { + if (keycode >= 0) { // Keyboard keys always have a keycode larger or equal than 0 + super.setPressed(keycode, keystate); + } + } - if (!virtualkeys.equals(keyToCompare)) { - if (Keyboard.areRepeatEventsEnabled()) { - if (isUnicodeInList(keycodes)) { - return; - } - } - eventList.add(new VirtualKeyboardEvent(keycodes, keyToCompare.isKeyDown(), Character.MIN_VALUE)); - } - - }); - keyboardToCompare.charList.forEach(action -> { - if (Keyboard.areRepeatEventsEnabled()) { - eventList.add(decodeUnicode(action)); - } else { - eventList.add(new VirtualKeyboardEvent(0, true, action)); + /** + * Calculates a list of {@link VirtualKeyboardEvent}s to the next peripheral, + * including the subticks. + * + * @see VirtualKeyboard#getDifference(VirtualKeyboard, Queue) + * + * @param nextKeyboard The keyboard that comes after this one.
+ * If this one is loaded at tick 15, the nextKeyboard should + * be the one from tick 16 + * @param reference The queue to fill. Passed in by reference. + */ + public void getVirtualEvents(VirtualKeyboard nextKeyboard, Queue reference) { + if (isParent()) { + VirtualKeyboard currentSubtick = this; + for(VirtualKeyboard subtick : nextKeyboard.getAll()) { + currentSubtick.getDifference(subtick, reference); + currentSubtick = subtick; } - }); - return eventList; - } - - public char encodeUnicode(int keycode, char character) { - switch (keycode) { - case 15: // Tab - return '\u21A6'; - case 199: // Pos1 - return '\u21E4'; - case 200: // Arrow Up - return '\u2191'; - case 201: // Prior - return '\u21E7'; - case 203: // Arrow Left - return '\u2190'; - case 205: // Arrow Right - return '\u2192'; - case 207: // End - return '\u21E5'; - case 208: // Arrow Down - return '\u2193'; - case 209: // Next - return '\u21E9'; - default: - return character; - } - } - - public VirtualKeyboardEvent decodeUnicode(char character) { - switch (character) { - case '\b': - return new VirtualKeyboardEvent(14, true, Character.MIN_VALUE); - case '\u21A6': - return new VirtualKeyboardEvent(15, true, Character.MIN_VALUE); - case '\u2907': - return new VirtualKeyboardEvent(15, false, Character.MIN_VALUE); - case '\u21E4': - return new VirtualKeyboardEvent(199, true, Character.MIN_VALUE); - case '\u2191': - return new VirtualKeyboardEvent(200, true, Character.MIN_VALUE); - case '\u21E7': - return new VirtualKeyboardEvent(201, true, Character.MIN_VALUE); - case '\u2190': - return new VirtualKeyboardEvent(203, true, Character.MIN_VALUE); - case '\u2192': - return new VirtualKeyboardEvent(205, true, Character.MIN_VALUE); - case '\u21E5': - return new VirtualKeyboardEvent(207, true, Character.MIN_VALUE); - case '\u2193': - return new VirtualKeyboardEvent(208, true, Character.MIN_VALUE); - case '\u21E9': - return new VirtualKeyboardEvent(209, true, Character.MIN_VALUE); - default: - return new VirtualKeyboardEvent(0, true, character); } } - public boolean isUnicodeInList(int keycode) { - switch (keycode) { - case 14: - case 15: - case 199: - case 200: - case 201: - case 203: - case 205: - case 207: - case 208: - case 209: - return true; - default: - return false; + /** + * Calculates the difference between 2 keyboards via symmetric difference
+ * and returns a list of the changes between them in form of + * {@link VirtualKeyboardEvent}s + * + * @param nextKeyboard The keyboard that comes after this one.
+ * If this one is loaded at tick 15, the nextKeyboard should + * be the one from tick 16 + * @param reference The queue to fill. Passed in by reference. + */ + public void getDifference(VirtualKeyboard nextKeyboard, Queue reference) { + charQueue.addAll(nextKeyboard.charList); + + /* Calculate symmetric difference of keycodes */ + + /* + Calculate unpressed keys + this: W A S + next: W S D + ------------- + A <- unpressed + */ + for(int key : pressedKeys) { + if (!nextKeyboard.getPressedKeys().contains(key)) { + reference.add(new VirtualKeyboardEvent(key, false, Character.MIN_VALUE)); + } + } + + /* + Calculate pressed keys + next: W S D + this: W A S + ------------- + D <- pressed + */ + int lastKey = 0; + for(int key : nextKeyboard.getPressedKeys()) { + lastKey = key; + if (!this.pressedKeys.contains(key)) { + reference.add(new VirtualKeyboardEvent(key, true, getOrMinChar(charQueue.poll()))); + } + } + + /* + Add the rest of the characters as keyboard events. + Also responsible for holding the key and adding a lot of characters in chat. + + The LWJGL Keyboard has a method called "areRepeatEventsEnabled" which returns true, when the user is in a gui. + Additionally when a key is held, the Keyboard resends the same keyboard event, in this case to the update method of the VirtualKeyboard. + + What ends up happening is, that the subtickList is filled with multiple characters, which are then converted to keyboard events + here. + + However, some functionality like \b or the arrow keys have no associated character, Minecraft instead listens for the keycode. + Thats where the "lastKey" comes in. Since we are using a LinkedHashSet, as pressedKeys, we can get the last pressed keycode. + + So, to get the repeat events working, one needs a pressed key and any character. + + */ + while (!charQueue.isEmpty()) { + reference.add(new VirtualKeyboardEvent(lastKey, true, getOrMinChar(charQueue.poll()))); + } + + } + + private char getOrMinChar(Character charr){ + if(charr==null){ + charr = Character.MIN_VALUE; + } + return charr; + } + + /** + * Add a character to the {@link #charList}
+ * Null characters will be discarded; + * @param character The character to add + */ + public void addChar(char character, boolean repeatEventsEnabled) { + if(character != Character.MIN_VALUE || repeatEventsEnabled) { + charList.add(character); + } + } + + @Override + protected void clear(){ + super.clear(); + charList.clear(); + } + + @Override + public String toString() { + if (isParent()) { + return getAll().stream().map(VirtualKeyboard::toString2).collect(Collectors.joining("\n")); + } else { + return toString2(); } } - @Override - public VirtualKeyboard clone() { - return new VirtualKeyboard(keyList, charList); + private String toString2(){ + return String.format("%s;%s", super.toString(), charListToString(charList)); } - @Override - public String toString() { - List stringy = getCurrentPresses(); - String keyString = ""; - if (!stringy.isEmpty()) { - String seperator = ","; - for (int i = 0; i < stringy.size(); i++) { - if (i == stringy.size() - 1) { - seperator = ""; - } - keyString = keyString.concat(stringy.get(i) + seperator); - } - } + private String charListToString(List charList) { String charString = ""; if (!charList.isEmpty()) { - for (int i = 0; i < charList.size(); i++) { - charString = charString.concat(Character.toString(charList.get(i))); - } + charString = charList.stream().map(Object::toString).collect(Collectors.joining()); charString = StringUtils.replace(charString, "\r", "\\n"); charString = StringUtils.replace(charString, "\n", "\\n"); } - - return PlaybackSerialiser.SectionsV1.KEYBOARD.getName()+":"+keyString + ";" + charString; + return charString; } + + /** + * Clones this VirtualKeyboard without subticks + */ + @Override + public VirtualKeyboard clone() { + return new VirtualKeyboard(new HashSet<>(this.pressedKeys), new ArrayList<>(this.charList), isIgnoreFirstUpdate()); + } + + @Override + public void copyFrom(VirtualKeyboard keyboard) { + super.copyFrom(keyboard); + charList.clear(); + charList.addAll(keyboard.charList); + keyboard.charList.clear(); + } + + @Override + public boolean equals(Object obj) { + if(obj instanceof VirtualKeyboard) { + VirtualKeyboard keyboard = (VirtualKeyboard) obj; + + if(charList.size() != keyboard.charList.size()) { + return false; + } + + for (int i = 0; i < charList.size(); i++) { + if(charList.get(i)!=keyboard.charList.get(i)) { + return false; + } + } + return super.equals(obj); + } + return super.equals(obj); + } + + /** + * @return An immutable {@link #charList} + */ + public List getCharList() { + return ImmutableList.copyOf(charList); + } } diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard2.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard2.java deleted file mode 100644 index 55ee90fc..00000000 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard2.java +++ /dev/null @@ -1,323 +0,0 @@ -package com.minecrafttas.tasmod.virtual; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Queue; -import java.util.Set; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.stream.Collectors; - -import org.apache.commons.lang3.StringUtils; - -import com.google.common.collect.ImmutableList; - -/** - * Stores keyboard specific values in a given timeframe.
- *
- * This keyboard mimics the {@link org.lwjgl.input.Keyboard} Minecraft is using. - *

KeyboardEvent

- * {@link org.lwjgl.input.Keyboard} has the following outputs, when a key is pressed or unpressed on the physical keyboard: - *
    - *
  • int KeyCode: The unique keycode of the key
  • - *
  • boolean KeyState: The new state of the key. True for pressed, false for unpressed
  • - *
  • char KeyCharacter: The character associated for each key
  • - *
- * While the keycode is the same between physical keyboards, the key character might differ.
- * It is also common that one keycode has multiple characters associated with it, e.g.
- * holding shift results in a capitalised character.
- *
- * These three outputs together are what we call a "KeyboardEvent" and might look like this: - *
- *     17, true, w
- * 
- * For keycode, keystate, keycharacter - *

Updating the keyboard

- * This keyboard stores it's values in "states".
- * That means that all the keys that are currently pressed are stored in {@link #pressedKeys}.
- * And this list is updated via a keyboard event in {@link #update(int, boolean, char)}.
- *

Difference

- * When comparing 2 keyboard states, we can generate a list of differences from them in form of {@link VirtualKeyboardEvent}s.
- *
- * 	this: W A S
- *	next: W   S D
- * 
- * Since there are 2 differences between this and the next keyboard, - * this will result in 2 {@link VirtualKeyboardEvent}s. And combined with the {@link #charList} we can also get the associated characters: - *
- *	30, false, null // A is unpressed
- * 	32, true, d 	// D is pressed
- * 
- *

Subticks

- * Minecraft updates it's keyboard every tick. All the key events that occur inbetween are stored,
- * then read out when a new tick has started.
We call these "inbetween" ticks subticks.
- *

Parent->Subtick

- * In a previous version of this keyboard, subticks were bundeled and flattened into one keyboard state.
- * After all, Minecraft updates only occur once every tick, storing subticks seemed unnecessary.
- *
- * However this posed some usability issues when playing in a low game speed via {@link com.minecrafttas.tasmod.tickratechanger.TickrateChangerClient}.
- * Now you had to hold the key until the next tick to get it recognised by the game.
- *
- * To fix this, now every subtick is stored as a keyboard state as well.
- * When updating the keyboard in {@link #update(int, boolean, char)}, a clone of itself is created and stored in {@link #subtickList},
- * with the difference that the subtick state has no {@link #subtickList}.
- * In a nutshell, the keyboard stores it's past changes in {@link #subtickList} with the first being the oldest change. - * - * @author Scribble - * @see com.minecrafttas.tasmod.virtual.VirtualInput2.VirtualKeyboardInput - */ -public class VirtualKeyboard2 extends VirtualPeripheral implements Serializable { - - /** - * The list of characters that were pressed on this keyboard. - */ - private final List charList; - - /** - * A queue of characters used in {@link #getDifference(VirtualKeyboard2, Queue)}.
- * Used for distributing characters to {@link VirtualKeyboardEvent}s in an order. - */ - private final ConcurrentLinkedQueue charQueue = new ConcurrentLinkedQueue<>(); - - /** - * Creates an empty parent keyboard with all keys unpressed - */ - public VirtualKeyboard2() { - this(new LinkedHashSet<>(), new ArrayList<>(), new ArrayList<>(), true); - } - - /** - * Creates a subtick keyboard with {@link VirtualPeripheral#subtickList} uninitialized - * @param pressedKeys The new list of pressed keycodes for this subtickKeyboard - * @param charList A list of characters for this subtickKeyboard - */ - public VirtualKeyboard2(Set pressedKeys, List charList){ - this(pressedKeys, charList, null, false); - } - - /** - * Creates a keyboard from existing variables - * @param pressedKeys The existing list of pressed keycodes - * @param charList The existing list of characters - */ - public VirtualKeyboard2(Set pressedKeys, List charList, boolean ignoreFirstUpdate) { - this(pressedKeys, charList, null, ignoreFirstUpdate); - } - - /** - * Creates a keyboard from existing variables - * @param pressedKeys The existing list of {@link VirtualPeripheral#pressedKeys} - * @param charList The {@link #charList} - * @param subtickList {@link VirtualPeripheral#subtickList} - * @param ignoreFirstUpdate The {@link VirtualPeripheral#ignoreFirstUpdate} - */ - public VirtualKeyboard2(Set pressedKeys, List charList, List subtickList, boolean ignoreFirstUpdate) { - super(pressedKeys, subtickList, ignoreFirstUpdate); - this.charList = charList; - } - - /** - * Updates the keyboard, adds a new subtick to this keyboard - * @param keycode The keycode of this key - * @param keystate The keystate of this key, true for pressed - * @param keycharacter The character that is associated with that key. Can change between keyboards or whenever shift is held in combination. - */ - public void update(int keycode, boolean keystate, char keycharacter, boolean repeatEventsEnabled) { - if(isParent() && !ignoreFirstUpdate()) { - addSubtick(clone()); - } - charList.clear(); - if (keystate) { - addChar(keycharacter, repeatEventsEnabled); - } - setPressed(keycode, keystate); - } - - public void update(int keycode, boolean keystate, char keycharacter) { - update(keycode, keystate, keycharacter, false); - } - - @Override - public void setPressed(int keycode, boolean keystate) { - if (keycode >= 0) { // Keyboard keys always have a keycode larger or equal than 0 - super.setPressed(keycode, keystate); - } - } - - /** - * Calculates a list of {@link VirtualKeyboardEvent}s to the next peripheral, - * including the subticks. - * - * @see VirtualKeyboard2#getDifference(VirtualKeyboard2, Queue) - * - * @param nextKeyboard The keyboard that comes after this one.
- * If this one is loaded at tick 15, the nextKeyboard should - * be the one from tick 16 - * @param reference The queue to fill. Passed in by reference. - */ - public void getVirtualEvents(VirtualKeyboard2 nextKeyboard, Queue reference) { - if (isParent()) { - VirtualKeyboard2 currentSubtick = this; - for(VirtualKeyboard2 subtick : nextKeyboard.getAll()) { - currentSubtick.getDifference(subtick, reference); - currentSubtick = subtick; - } - } - } - - /** - * Calculates the difference between 2 keyboards via symmetric difference
- * and returns a list of the changes between them in form of - * {@link VirtualKeyboardEvent}s - * - * @param nextKeyboard The keyboard that comes after this one.
- * If this one is loaded at tick 15, the nextKeyboard should - * be the one from tick 16 - * @param reference The queue to fill. Passed in by reference. - */ - public void getDifference(VirtualKeyboard2 nextKeyboard, Queue reference) { - charQueue.addAll(nextKeyboard.charList); - - /* Calculate symmetric difference of keycodes */ - - /* - Calculate unpressed keys - this: W A S - next: W S D - ------------- - A <- unpressed - */ - for(int key : pressedKeys) { - if (!nextKeyboard.getPressedKeys().contains(key)) { - reference.add(new VirtualKeyboardEvent(key, false, Character.MIN_VALUE)); - } - } - - /* - Calculate pressed keys - next: W S D - this: W A S - ------------- - D <- pressed - */ - int lastKey = 0; - for(int key : nextKeyboard.getPressedKeys()) { - lastKey = key; - if (!this.pressedKeys.contains(key)) { - reference.add(new VirtualKeyboardEvent(key, true, getOrMinChar(charQueue.poll()))); - } - } - - /* - Add the rest of the characters as keyboard events. - Also responsible for holding the key and adding a lot of characters in chat. - - The LWJGL Keyboard has a method called "areRepeatEventsEnabled" which returns true, when the user is in a gui. - Additionally when a key is held, the Keyboard resends the same keyboard event, in this case to the update method of the VirtualKeyboard. - - What ends up happening is, that the subtickList is filled with multiple characters, which are then converted to keyboard events - here. - - However, some functionality like \b or the arrow keys have no associated character, Minecraft instead listens for the keycode. - Thats where the "lastKey" comes in. Since we are using a LinkedHashSet, as pressedKeys, we can get the last pressed keycode. - - So, to get the repeat events working, one needs a pressed key and any character. - - */ - while (!charQueue.isEmpty()) { - reference.add(new VirtualKeyboardEvent(lastKey, true, getOrMinChar(charQueue.poll()))); - } - - } - - private char getOrMinChar(Character charr){ - if(charr==null){ - charr = Character.MIN_VALUE; - } - return charr; - } - - /** - * Add a character to the {@link #charList}
- * Null characters will be discarded; - * @param character The character to add - */ - public void addChar(char character, boolean repeatEventsEnabled) { - if(character != Character.MIN_VALUE || repeatEventsEnabled) { - charList.add(character); - } - } - - @Override - protected void clear(){ - super.clear(); - charList.clear(); - } - - @Override - public String toString() { - if (isParent()) { - return getAll().stream().map(VirtualKeyboard2::toString2).collect(Collectors.joining("\n")); - } else { - return toString2(); - } - } - - private String toString2(){ - return String.format("%s;%s", super.toString(), charListToString(charList)); - } - - private String charListToString(List charList) { - String charString = ""; - if (!charList.isEmpty()) { - charString = charList.stream().map(Object::toString).collect(Collectors.joining()); - charString = StringUtils.replace(charString, "\r", "\\n"); - charString = StringUtils.replace(charString, "\n", "\\n"); - } - return charString; - } - - /** - * Clones this VirtualKeyboard without subticks - */ - @Override - public VirtualKeyboard2 clone() { - return new VirtualKeyboard2(new HashSet<>(this.pressedKeys), new ArrayList<>(this.charList), isIgnoreFirstUpdate()); - } - - @Override - public void copyFrom(VirtualKeyboard2 keyboard) { - super.copyFrom(keyboard); - charList.clear(); - charList.addAll(keyboard.charList); - keyboard.charList.clear(); - } - - @Override - public boolean equals(Object obj) { - if(obj instanceof VirtualKeyboard2) { - VirtualKeyboard2 keyboard = (VirtualKeyboard2) obj; - - if(charList.size() != keyboard.charList.size()) { - return false; - } - - for (int i = 0; i < charList.size(); i++) { - if(charList.get(i)!=keyboard.charList.get(i)) { - return false; - } - } - return super.equals(obj); - } - return super.equals(obj); - } - - /** - * @return An immutable {@link #charList} - */ - public List getCharList() { - return ImmutableList.copyOf(charList); - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse.java index f5e38990..d2e7c552 100644 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse.java +++ b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse.java @@ -1,348 +1,256 @@ package com.minecrafttas.tasmod.virtual; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import com.minecrafttas.tasmod.virtual.event.VirtualMouseEvent; -import com.google.common.collect.Maps; -import com.minecrafttas.tasmod.playback.PlaybackSerialiser; +import java.io.Serializable; +import java.util.*; +import java.util.stream.Collectors; -@Deprecated -public class VirtualMouse implements Serializable { +public class VirtualMouse extends VirtualPeripheral implements Serializable { /** - * + * The direction of the scrollWheel
+ *
+ * If the number is positive or negative depending on scroll direction. */ - private static final long serialVersionUID = -5389661329436686190L; - - private Map keyList = Maps.newHashMap(); - - private int scrollwheel = 0; - - private int cursorX = 0; - - private int cursorY = 0; - - public VirtualMouse(Map keyListIn, int scrollwheel, int cursorX, int cursorY, List path) { - Map copy = new HashMap(); - - keyListIn.forEach((key, value) -> { - copy.put(key, value.clone()); - }); - keyList = copy; - - this.scrollwheel = scrollwheel; - - this.cursorX = cursorX; - - this.cursorY = cursorY; - - List pathCopy = new ArrayList(); - path.forEach(pathNode -> { - pathCopy.add(pathNode); - }); - this.path = pathCopy; - - } - + private int scrollWheel; /** - * Creates a Keyboard, where the keys are all unpressed + * X coordinate of the on-screen cursor, used in GUI screens.
+ * When null, no change to the cursor is applied. */ - public VirtualMouse() { - keyList.put(-101, new VirtualKey("MOUSEMOVED", -101)); - keyList.put(-100, new VirtualKey("LC", -100)); - keyList.put(-99, new VirtualKey("RC", -99)); - keyList.put(-98, new VirtualKey("MC", -98)); - keyList.put(-97, new VirtualKey("MBUTTON4", -97)); - keyList.put(-96, new VirtualKey("MBUTTON5", -96)); - keyList.put(-95, new VirtualKey("MBUTTON6", -95)); - keyList.put(-94, new VirtualKey("MBUTTON7", -94)); - keyList.put(-93, new VirtualKey("MBUTTON8", -93)); - keyList.put(-92, new VirtualKey("MBUTTON9", -92)); - keyList.put(-91, new VirtualKey("MBUTTON10", -91)); - keyList.put(-90, new VirtualKey("MBUTTON11", -90)); - keyList.put(-89, new VirtualKey("MBUTTON12", -89)); - keyList.put(-88, new VirtualKey("MBUTTON13", -88)); - keyList.put(-87, new VirtualKey("MBUTTON14", -87)); - keyList.put(-86, new VirtualKey("MBUTTON15", -86)); - keyList.put(-85, new VirtualKey("MBUTTON16", -85)); - - this.scrollwheel = 0; - - this.cursorX = 0; - - this.cursorY = 0; - - addPathNode(); - } - - public void add(int keycode) { - String keyString = Integer.toString(keycode); + private int cursorX; + /** + * Y coordinate of the on-screen cursor, used in GUI screens.
+ * When null, no change to the cursor is applied. + */ + private int cursorY; - keyList.put(keycode, new VirtualKey(keyString, keycode)); + /** + * Creates a mouse with no buttons pressed and no data + */ + public VirtualMouse(){ + this(new LinkedHashSet<>(), 0, 0, 0, new ArrayList<>(), true); } - public VirtualKey get(int keycode) { - return keyList.get(keycode); + /** + * Creates a subtick mouse with {@link VirtualPeripheral#subtickList} uninitialized + * @param pressedKeys The new list of pressed keycodes for this subtickMouse + * @param scrollWheel The scroll wheel direction for this subtickMouse + * @param cursorX The X coordinate of the cursor for this subtickMouse + * @param cursorY The Y coordinate of the cursor for this subtickMouse + */ + public VirtualMouse(Set pressedKeys, int scrollWheel, int cursorX, int cursorY) { + this(pressedKeys, scrollWheel, cursorX, cursorY, null); } - public VirtualKey get(String keyname) { - Collection list = keyList.values(); - VirtualKey out = null; - - for (VirtualKey key : list) { - if (key.getName().equalsIgnoreCase(keyname)) { - out = key; - } - } - return out; + /** + * Creates a mouse from existing values with + * {@link VirtualPeripheral#ignoreFirstUpdate} set to false + * + * @param pressedKeys The list of {@link #pressedKeys} + * @param scrollWheel The {@link #scrollWheel} + * @param cursorX The {@link #cursorX} + * @param cursorY The {@link #cursorY} + * @param subtick The {@link VirtualPeripheral#subtickList} + */ + public VirtualMouse(Set pressedKeys, int scrollWheel, Integer cursorX, Integer cursorY, List subtick) { + this(pressedKeys, scrollWheel, cursorX, cursorY, subtick, false); } - public List getCurrentPresses() { - List out = new ArrayList(); - - keyList.forEach((keycodes, virtualkeys) -> { - if (keycodes <= 0) { - if (virtualkeys.isKeyDown()) { - out.add(virtualkeys.getName()); - } - } - }); - - return out; + /** + * Creates a mouse from existing values + * + * @param pressedKeys The list of {@link #pressedKeys} + * @param scrollWheel The {@link #scrollWheel} + * @param cursorX The {@link #cursorX} + * @param cursorY The {@link #cursorY} + * @param subtick The {@link VirtualPeripheral#subtickList} + * @param ignoreFirstUpdate Whether the first call to {@link #update(int, boolean, int, Integer, Integer)} should create a new subtick + */ + public VirtualMouse(Set pressedKeys, int scrollWheel, Integer cursorX, Integer cursorY, List subtick, boolean ignoreFirstUpdate) { + super(pressedKeys, subtick, ignoreFirstUpdate); + this.scrollWheel = scrollWheel; + this.cursorX = cursorX; + this.cursorY = cursorY; } - public Map getKeyList() { - return this.keyList; + public void update(int keycode, boolean keystate, int scrollwheel, Integer cursorX, Integer cursorY) { + if(isParent() && !ignoreFirstUpdate()) { + addSubtick(clone()); + } + setPressed(keycode, keystate); + this.scrollWheel = scrollwheel; + this.cursorX = cursorX; + this.cursorY = cursorY; } - public List getDifference(VirtualMouse mouseToCompare) { - List eventList = new ArrayList(); - - List path = mouseToCompare.getPath(); - - if (path.size() != 1) { - for (int i = 0; i < path.size() - 1; i++) { - PathNode currentNode = path.get(i); - PathNode nextNode = path.get(i + 1); - - boolean flag = false; - - for (VirtualKey key : nextNode.keyList.values()) { - if (!key.equals(currentNode.keyList.get(key.getKeycode()))) { - eventList.add(new VirtualMouseEvent(key.getKeycode(), key.isKeyDown(), nextNode.scrollwheel, nextNode.cursorX, nextNode.cursorY)); - flag = true; - break; - } - } - if (!flag) { - eventList.add(new VirtualMouseEvent(-101, false, nextNode.scrollwheel, nextNode.cursorX, nextNode.cursorY)); - } - - } - } else { - keyList.forEach((keycodes, virtualkeys) -> { - - VirtualKey keyToCompare = mouseToCompare.get(keycodes); - - if (!virtualkeys.equals(keyToCompare)) { - eventList.add(new VirtualMouseEvent(keycodes, keyToCompare.isKeyDown(), scrollwheel, cursorX, cursorY)); - } - - }); + @Override + public void setPressed(int keycode, boolean keystate) { + if (keycode < 0) { // Mouse buttons always have a keycode smaller than 0 + super.setPressed(keycode, keystate); } - return eventList; } - public boolean isSomethingDown() { - for (VirtualKey key : keyList.values()) { - if (key.isKeyDown()) { - return true; + /** + * Calculates a list of {@link VirtualMouseEvent}s, when comparing this mouse to + * the next mouse in the sequence,
+ * which also includes the subticks. + * + * @see VirtualMouse#getDifference(VirtualMouse, Queue) + * + * @param nextMouse The mouse that comes after this one.
+ * If this one is loaded at tick 15, the nextMouse should be + * the one from tick 16 + * @param reference The queue to fill. Passed in by reference. + */ + public void getVirtualEvents(VirtualMouse nextMouse, Queue reference) { + if (isParent()) { + VirtualMouse currentSubtick = this; + for(VirtualMouse subtick : nextMouse.getAll()) { + currentSubtick.getDifference(subtick, reference); + currentSubtick = subtick; } } - return false; } - public boolean isSomethingDown(Map keyList) { - for (VirtualKey key : keyList.values()) { - if (key.isKeyDown()) { - return true; + /** + * Calculates the difference between 2 mice via symmetric difference
+ * and returns a list of the changes between them in form of + * {@link VirtualMouseEvent}s + * + * @param nextMouse The mouse that comes after this one.
+ * If this one is loaded at tick 15, the nextMouse should be + * the one from tick 16 + * @param reference The queue to fill. Passed in by reference. + */ + public void getDifference(VirtualMouse nextPeripheral, Queue reference) { + + /* + * Checks if pressedKeys are the same... + */ + if(pressedKeys.equals(nextPeripheral.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)); } + return; } - return false; + int scrollWheelCopy = nextPeripheral.scrollWheel; + int cursorXCopy = nextPeripheral.cursorX; + int cursorYCopy = nextPeripheral.cursorY; + + /* Calculate symmetric difference of keycodes */ + + /* + Calculate unpressed keys + this: LC RC + next: LC MC + ------------- + RC <- unpressed + */ + for(int keycode : pressedKeys) { + if (!nextPeripheral.getPressedKeys().contains(keycode)) { + reference.add(new VirtualMouseEvent(keycode, false, scrollWheelCopy, cursorXCopy, cursorYCopy)); + scrollWheelCopy = 0; + cursorXCopy = 0; + cursorYCopy = 0; + } + }; + + /* + Calculate pressed keys + next: LC MC + this: LC RC + ------------- + MC <- pressed + */ + for(int keycode : nextPeripheral.getPressedKeys()) { + if (!this.pressedKeys.contains(keycode)) { + reference.add(new VirtualMouseEvent(keycode, true, scrollWheelCopy, cursorXCopy, cursorYCopy)); + } + }; } @Override - public VirtualMouse clone() { - return new VirtualMouse(keyList, scrollwheel, cursorX, cursorY, path); - } - - public void setCursor(int x, int y) { - cursorX = x; - cursorY = y; + protected void clear() { + super.clear(); + clearMouseData(); } - - public void setScrollWheel(int scrollwheel) { - this.scrollwheel = scrollwheel; - } - - List path = new ArrayList(); - - public List getPath() { - return path; + + /** + * Resets mouse specific data to it's defaults + */ + private void clearMouseData() { + scrollWheel = 0; + cursorX = 0; + cursorY = 0; } - public String getPathAsString() { - String out=""; - for(PathNode node: path) { - out=out.concat(node.toString()); + + @Override + public String toString() { + if (isParent()) { + return getAll().stream().map(VirtualMouse::toString2).collect(Collectors.joining("\n")); + } else { + return toString2(); } - return out; - } - - public void addPathNode() { - path.add(new PathNode(keyList, scrollwheel, cursorX, cursorY)); } - - public void resetPath() { - path.clear(); - addPathNode(); + + private String toString2(){ + return String.format("%s;%s,%s,%s", super.toString(), scrollWheel, cursorX, cursorY); } - public class PathNode implements Serializable { - /** - * - */ - private static final long serialVersionUID = -2221602955260299028L; - - private Map keyList = Maps.newHashMap(); - - public int scrollwheel = 0; - - public int cursorX = 0; - - public int cursorY = 0; - - public PathNode(Map keyList, int scrollwheel, int cursorX, int cursorY) { - Map copy = new HashMap(); - - keyList.forEach((key, value) -> { - copy.put(key, value.clone()); - }); - this.keyList = copy; - this.scrollwheel = scrollwheel; - this.cursorX = cursorX; - this.cursorY = cursorY; - } - - public PathNode() { - keyList.put(-101, new VirtualKey("MOUSEMOVED", -101)); - keyList.put(-100, new VirtualKey("LC", -100)); - keyList.put(-99, new VirtualKey("RC", -99)); - keyList.put(-98, new VirtualKey("MC", -98)); - keyList.put(-97, new VirtualKey("MBUTTON3", -97)); - keyList.put(-96, new VirtualKey("MBUTTON4", -96)); - keyList.put(-95, new VirtualKey("MBUTTON5", -95)); - keyList.put(-94, new VirtualKey("MBUTTON6", -94)); - keyList.put(-93, new VirtualKey("MBUTTON7", -93)); - keyList.put(-92, new VirtualKey("MBUTTON8", -92)); - keyList.put(-91, new VirtualKey("MBUTTON9", -91)); - keyList.put(-90, new VirtualKey("MBUTTON10", -90)); - keyList.put(-89, new VirtualKey("MBUTTON11", -89)); - keyList.put(-88, new VirtualKey("MBUTTON12", -88)); - keyList.put(-87, new VirtualKey("MBUTTON13", -87)); - keyList.put(-86, new VirtualKey("MBUTTON14", -86)); - keyList.put(-85, new VirtualKey("MBUTTON15", -85)); - - this.scrollwheel = 0; - - this.cursorX = 0; - - this.cursorY = 0; - } - - @Override - public String toString() { - String keyString = ""; - List strings = new ArrayList(); - - keyList.forEach((keycodes, virtualkeys) -> { - if (virtualkeys.isKeyDown()) { - strings.add(virtualkeys.getName()); - } - }); - if (!strings.isEmpty()) { - String seperator = ","; - for (int i = 0; i < strings.size(); i++) { - if (i == strings.size() - 1) { - seperator = ""; - } - keyString = keyString.concat(strings.get(i) + seperator); - } - } - if (keyString.isEmpty()) { - return "MOUSEMOVED," + scrollwheel + "," + cursorX + "," + cursorY; - } else { - return keyString + "," + scrollwheel + "," + cursorX + "," + cursorY; - } - } - - public VirtualKey get(String keyname) { - Collection list = keyList.values(); - VirtualKey out = null; - - for (VirtualKey key : list) { - if (key.getName().equalsIgnoreCase(keyname)) { - out = key; - } - } - return out; - } - + /** + * Clones this VirtualMouse without subticks + */ + @Override + public VirtualMouse clone() { + return new VirtualMouse(new HashSet<>(this.pressedKeys), scrollWheel, cursorX, cursorY, null, ignoreFirstUpdate()); } - public void clear() { - keyList.forEach((keycode, key) -> { - key.setPressed(false); - }); - resetPath(); + @Override + public void copyFrom(VirtualMouse mouse) { + super.copyFrom(mouse); + this.scrollWheel = mouse.scrollWheel; + this.cursorX = mouse.cursorX; + this.cursorY = mouse.cursorY; + mouse.clearMouseData(); } - + @Override - public String toString() { - List stringy = getCurrentPresses(); - String keyString = ""; - if (!stringy.isEmpty()) { - String seperator = ","; - for (int i = 0; i < stringy.size(); i++) { - if (i == stringy.size() - 1) { - seperator = ""; - } - keyString = keyString.concat(stringy.get(i) + seperator); - } - } - String pathString = ""; - if (!path.isEmpty()) { - String seperator = "->"; - for (int i = 0; i < path.size(); i++) { - if (i == path.size() - 1) { - seperator = ""; - } - pathString = pathString.concat("[" + path.get(i).toString() + "]" + seperator); - } + public boolean equals(Object obj) { + if (obj instanceof VirtualMouse) { + VirtualMouse mouse = (VirtualMouse) obj; + return super.equals(obj) && + scrollWheel == mouse.scrollWheel && + cursorX == mouse.cursorX && + cursorY == mouse.cursorY; } - return PlaybackSerialiser.SectionsV1.MOUSE.getName()+":"+keyString + ";" + pathString; + return super.equals(obj); } - public void setPath(List path) { - List pathCopy = new ArrayList(); - path.forEach(pathNode -> { - pathCopy.add(pathNode); - }); - this.path = pathCopy; + /** + * @return {@link #scrollWheel} + */ + public int getScrollWheel() { + return scrollWheel; + } + + /** + * @return {@link #cursorX} + */ + public int getCursorX() { + return cursorX; + } + + /** + * @return {@link #cursorY} + */ + public int getCursorY() { + return cursorY; } - } diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse2.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse2.java deleted file mode 100644 index f8e3d4a2..00000000 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouse2.java +++ /dev/null @@ -1,254 +0,0 @@ -package com.minecrafttas.tasmod.virtual; - -import java.io.Serializable; -import java.util.*; -import java.util.stream.Collectors; - -public class VirtualMouse2 extends VirtualPeripheral implements Serializable { - - /** - * The direction of the scrollWheel
- *
- * If the number is positive or negative depending on scroll direction. - */ - private int scrollWheel; - /** - * X coordinate of the on-screen cursor, used in GUI screens.
- * When null, no change to the cursor is applied. - */ - private int cursorX; - /** - * Y coordinate of the on-screen cursor, used in GUI screens.
- * When null, no change to the cursor is applied. - */ - private int cursorY; - - /** - * Creates a mouse with no buttons pressed and no data - */ - public VirtualMouse2(){ - this(new LinkedHashSet<>(), 0, 0, 0, new ArrayList<>(), true); - } - - /** - * Creates a subtick mouse with {@link VirtualPeripheral#subtickList} uninitialized - * @param pressedKeys The new list of pressed keycodes for this subtickMouse - * @param scrollWheel The scroll wheel direction for this subtickMouse - * @param cursorX The X coordinate of the cursor for this subtickMouse - * @param cursorY The Y coordinate of the cursor for this subtickMouse - */ - public VirtualMouse2(Set pressedKeys, int scrollWheel, int cursorX, int cursorY) { - this(pressedKeys, scrollWheel, cursorX, cursorY, null); - } - - /** - * Creates a mouse from existing values with - * {@link VirtualPeripheral#ignoreFirstUpdate} set to false - * - * @param pressedKeys The list of {@link #pressedKeys} - * @param scrollWheel The {@link #scrollWheel} - * @param cursorX The {@link #cursorX} - * @param cursorY The {@link #cursorY} - * @param subtick The {@link VirtualPeripheral#subtickList} - */ - public VirtualMouse2(Set pressedKeys, int scrollWheel, Integer cursorX, Integer cursorY, List subtick) { - this(pressedKeys, scrollWheel, cursorX, cursorY, subtick, false); - } - - /** - * Creates a mouse from existing values - * - * @param pressedKeys The list of {@link #pressedKeys} - * @param scrollWheel The {@link #scrollWheel} - * @param cursorX The {@link #cursorX} - * @param cursorY The {@link #cursorY} - * @param subtick The {@link VirtualPeripheral#subtickList} - * @param ignoreFirstUpdate Whether the first call to {@link #update(int, boolean, int, Integer, Integer)} should create a new subtick - */ - public VirtualMouse2(Set pressedKeys, int scrollWheel, Integer cursorX, Integer cursorY, List subtick, boolean ignoreFirstUpdate) { - super(pressedKeys, subtick, ignoreFirstUpdate); - this.scrollWheel = scrollWheel; - this.cursorX = cursorX; - this.cursorY = cursorY; - } - - public void update(int keycode, boolean keystate, int scrollwheel, Integer cursorX, Integer cursorY) { - if(isParent() && !ignoreFirstUpdate()) { - addSubtick(clone()); - } - setPressed(keycode, keystate); - this.scrollWheel = scrollwheel; - this.cursorX = cursorX; - this.cursorY = cursorY; - } - - @Override - public void setPressed(int keycode, boolean keystate) { - if (keycode < 0) { // Mouse buttons always have a keycode smaller than 0 - super.setPressed(keycode, keystate); - } - } - - /** - * Calculates a list of {@link VirtualMouseEvent}s, when comparing this mouse to - * the next mouse in the sequence,
- * which also includes the subticks. - * - * @see VirtualMouse2#getDifference(VirtualMouse2, Queue) - * - * @param nextMouse The mouse that comes after this one.
- * If this one is loaded at tick 15, the nextMouse should be - * the one from tick 16 - * @param reference The queue to fill. Passed in by reference. - */ - public void getVirtualEvents(VirtualMouse2 nextMouse, Queue reference) { - if (isParent()) { - VirtualMouse2 currentSubtick = this; - for(VirtualMouse2 subtick : nextMouse.getAll()) { - currentSubtick.getDifference(subtick, reference); - currentSubtick = subtick; - } - } - } - - /** - * Calculates the difference between 2 mice via symmetric difference
- * and returns a list of the changes between them in form of - * {@link VirtualMouseEvent}s - * - * @param nextMouse The mouse that comes after this one.
- * If this one is loaded at tick 15, the nextMouse should be - * the one from tick 16 - * @param reference The queue to fill. Passed in by reference. - */ - public void getDifference(VirtualMouse2 nextPeripheral, Queue reference) { - - /* - * Checks if pressedKeys are the same... - */ - if(pressedKeys.equals(nextPeripheral.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(VirtualKey2.MOUSEMOVED.getKeycode(), false, nextPeripheral.scrollWheel, nextPeripheral.cursorX, nextPeripheral.cursorY)); - } - return; - } - int scrollWheelCopy = nextPeripheral.scrollWheel; - int cursorXCopy = nextPeripheral.cursorX; - int cursorYCopy = nextPeripheral.cursorY; - - /* Calculate symmetric difference of keycodes */ - - /* - Calculate unpressed keys - this: LC RC - next: LC MC - ------------- - RC <- unpressed - */ - for(int keycode : pressedKeys) { - if (!nextPeripheral.getPressedKeys().contains(keycode)) { - reference.add(new VirtualMouseEvent(keycode, false, scrollWheelCopy, cursorXCopy, cursorYCopy)); - scrollWheelCopy = 0; - cursorXCopy = 0; - cursorYCopy = 0; - } - }; - - /* - Calculate pressed keys - next: LC MC - this: LC RC - ------------- - MC <- pressed - */ - for(int keycode : nextPeripheral.getPressedKeys()) { - if (!this.pressedKeys.contains(keycode)) { - reference.add(new VirtualMouseEvent(keycode, true, scrollWheelCopy, cursorXCopy, cursorYCopy)); - } - }; - } - - @Override - protected void clear() { - super.clear(); - clearMouseData(); - } - - /** - * Resets mouse specific data to it's defaults - */ - private void clearMouseData() { - scrollWheel = 0; - cursorX = 0; - cursorY = 0; - } - - - @Override - public String toString() { - if (isParent()) { - return getAll().stream().map(VirtualMouse2::toString2).collect(Collectors.joining("\n")); - } else { - return toString2(); - } - } - - private String toString2(){ - return String.format("%s;%s,%s,%s", super.toString(), scrollWheel, cursorX, cursorY); - } - - /** - * Clones this VirtualMouse without subticks - */ - @Override - public VirtualMouse2 clone() { - return new VirtualMouse2(new HashSet<>(this.pressedKeys), scrollWheel, cursorX, cursorY, null, ignoreFirstUpdate()); - } - - @Override - public void copyFrom(VirtualMouse2 mouse) { - super.copyFrom(mouse); - this.scrollWheel = mouse.scrollWheel; - this.cursorX = mouse.cursorX; - this.cursorY = mouse.cursorY; - mouse.clearMouseData(); - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof VirtualMouse2) { - VirtualMouse2 mouse = (VirtualMouse2) obj; - return super.equals(obj) && - scrollWheel == mouse.scrollWheel && - cursorX == mouse.cursorX && - cursorY == mouse.cursorY; - } - return super.equals(obj); - } - - /** - * @return {@link #scrollWheel} - */ - public int getScrollWheel() { - return scrollWheel; - } - - /** - * @return {@link #cursorX} - */ - public int getCursorX() { - return cursorX; - } - - /** - * @return {@link #cursorY} - */ - public int getCursorY() { - return cursorY; - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualPeripheral.java b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualPeripheral.java index 21e7a917..f7d504f9 100644 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualPeripheral.java +++ b/src/main/java/com/minecrafttas/tasmod/virtual/VirtualPeripheral.java @@ -7,9 +7,10 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.minecrafttas.tasmod.virtual.event.VirtualEvent; /** - * Base class for {@link VirtualKeyboard2} and {@link VirtualMouse2}
+ * Base class for {@link VirtualKeyboard} and {@link VirtualMouse}
*
* Contains the shared code for keeping track of which buttons are pressed.
* This works by storing the keycodes of the buttons in a set, as keycodes are supposed to be unique
@@ -83,7 +84,7 @@ protected void addSubtick(T peripheral) { * @param keystate The keystate of the keyname */ public void setPressed(String keyname, boolean keystate) { - Integer keycode = VirtualKey2.getKeycode(keyname); + Integer keycode = VirtualKey.getKeycode(keyname); if (keycode != null) { setPressed(keycode, keystate); } @@ -95,7 +96,7 @@ public void setPressed(String keyname, boolean keystate) { public List getCurrentPresses() { List out = new ArrayList<>(); pressedKeys.forEach(keycode -> { - out.add(VirtualKey2.getName(keycode)); + out.add(VirtualKey.getName(keycode)); }); return out; } @@ -156,7 +157,7 @@ public boolean isKeyDown(int keycode) { * @return If the key is pressed */ public boolean isKeyDown(String keyname) { - return pressedKeys.contains(VirtualKey2.getKeycode(keyname)); + return pressedKeys.contains(VirtualKey.getKeycode(keyname)); } /** diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualEvent.java b/src/main/java/com/minecrafttas/tasmod/virtual/event/VirtualEvent.java similarity index 93% rename from src/main/java/com/minecrafttas/tasmod/virtual/VirtualEvent.java rename to src/main/java/com/minecrafttas/tasmod/virtual/event/VirtualEvent.java index 0cedbca9..ea376bfd 100644 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualEvent.java +++ b/src/main/java/com/minecrafttas/tasmod/virtual/event/VirtualEvent.java @@ -1,4 +1,4 @@ -package com.minecrafttas.tasmod.virtual; +package com.minecrafttas.tasmod.virtual.event; public class VirtualEvent { diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboardEvent.java b/src/main/java/com/minecrafttas/tasmod/virtual/event/VirtualKeyboardEvent.java similarity index 88% rename from src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboardEvent.java rename to src/main/java/com/minecrafttas/tasmod/virtual/event/VirtualKeyboardEvent.java index cd66b1a5..5c0b0cc8 100644 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboardEvent.java +++ b/src/main/java/com/minecrafttas/tasmod/virtual/event/VirtualKeyboardEvent.java @@ -1,6 +1,6 @@ -package com.minecrafttas.tasmod.virtual; +package com.minecrafttas.tasmod.virtual.event; -import com.minecrafttas.tasmod.virtual.VirtualEvent.VirtualButtonEvent; +import com.minecrafttas.tasmod.virtual.event.VirtualEvent.VirtualButtonEvent; public class VirtualKeyboardEvent extends VirtualButtonEvent { private final char character; diff --git a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouseEvent.java b/src/main/java/com/minecrafttas/tasmod/virtual/event/VirtualMouseEvent.java similarity index 89% rename from src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouseEvent.java rename to src/main/java/com/minecrafttas/tasmod/virtual/event/VirtualMouseEvent.java index f0bcb276..251eb865 100644 --- a/src/main/java/com/minecrafttas/tasmod/virtual/VirtualMouseEvent.java +++ b/src/main/java/com/minecrafttas/tasmod/virtual/event/VirtualMouseEvent.java @@ -1,6 +1,6 @@ -package com.minecrafttas.tasmod.virtual; +package com.minecrafttas.tasmod.virtual.event; -import com.minecrafttas.tasmod.virtual.VirtualEvent.VirtualButtonEvent; +import com.minecrafttas.tasmod.virtual.event.VirtualEvent.VirtualButtonEvent; /** * Template for recording Mouse.next() events. diff --git a/src/test/java/tasmod/virtual/VirtualInputTest.java b/src/test/java/tasmod/virtual/VirtualInputTest.java index 2d4a8cf7..ad71d15e 100644 --- a/src/test/java/tasmod/virtual/VirtualInputTest.java +++ b/src/test/java/tasmod/virtual/VirtualInputTest.java @@ -5,15 +5,12 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import com.minecrafttas.tasmod.virtual.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.Test; -import com.minecrafttas.tasmod.virtual.VirtualCameraAngle2; -import com.minecrafttas.tasmod.virtual.VirtualInput2; -import com.minecrafttas.tasmod.virtual.VirtualKey2; -import com.minecrafttas.tasmod.virtual.VirtualKeyboard2; -import com.minecrafttas.tasmod.virtual.VirtualMouse2; +import com.minecrafttas.tasmod.virtual.VirtualInput; class VirtualInputTest { @@ -24,7 +21,7 @@ class VirtualInputTest { */ @Test void testConstructor() { - VirtualInput2 virtual = new VirtualInput2(LOGGER); + VirtualInput virtual = new VirtualInput(LOGGER); assertNotNull(virtual.KEYBOARD); assertNotNull(virtual.MOUSE); @@ -36,23 +33,23 @@ void testConstructor() { */ @Test void testPreloadedConstructor() { - VirtualKeyboard2 preloadedKeyboard = new VirtualKeyboard2(); - VirtualMouse2 preloadedMouse = new VirtualMouse2(); - VirtualCameraAngle2 preloadedCameraAngle = new VirtualCameraAngle2(1f, 2f); + VirtualKeyboard preloadedKeyboard = new VirtualKeyboard(); + VirtualMouse preloadedMouse = new VirtualMouse(); + VirtualCameraAngle preloadedCameraAngle = new VirtualCameraAngle(1f, 2f); - preloadedKeyboard.update(VirtualKey2.W.getKeycode(), true, 'w'); - preloadedMouse.update(VirtualKey2.LC.getKeycode(), true, 15, 0, 0); + preloadedKeyboard.update(VirtualKey.W.getKeycode(), true, 'w'); + preloadedMouse.update(VirtualKey.LC.getKeycode(), true, 15, 0, 0); - VirtualInput2 virtual = new VirtualInput2(LOGGER, preloadedKeyboard, preloadedMouse, preloadedCameraAngle); + VirtualInput virtual = new VirtualInput(LOGGER, preloadedKeyboard, preloadedMouse, preloadedCameraAngle); virtual.KEYBOARD.nextKeyboardTick(); assertTrue(virtual.KEYBOARD.nextKeyboardSubtick()); - assertEquals(VirtualKey2.W.getKeycode(), virtual.KEYBOARD.getEventKeyboardKey()); + assertEquals(VirtualKey.W.getKeycode(), virtual.KEYBOARD.getEventKeyboardKey()); virtual.MOUSE.nextMouseTick(); assertTrue(virtual.MOUSE.nextMouseSubtick()); - assertEquals(VirtualKey2.LC.getKeycode(), virtual.MOUSE.getEventMouseKey()); + assertEquals(VirtualKey.LC.getKeycode(), virtual.MOUSE.getEventMouseKey()); assertEquals(1f, virtual.CAMERA_ANGLE.getPitch()); assertEquals(2f, virtual.CAMERA_ANGLE.getYaw()); @@ -63,12 +60,12 @@ void testPreloadedConstructor() { */ @Test void testKeyboardAddPresses() { - VirtualInput2 virtual = new VirtualInput2(LOGGER); + VirtualInput virtual = new VirtualInput(LOGGER); // Simulate pressing keys WAS on the keyboard - virtual.KEYBOARD.updateNextKeyboard(VirtualKey2.W.getKeycode(), true, 'w'); - virtual.KEYBOARD.updateNextKeyboard(VirtualKey2.A.getKeycode(), true, 'a'); - virtual.KEYBOARD.updateNextKeyboard(VirtualKey2.S.getKeycode(), true, 's'); + virtual.KEYBOARD.updateNextKeyboard(VirtualKey.W.getKeycode(), true, 'w'); + virtual.KEYBOARD.updateNextKeyboard(VirtualKey.A.getKeycode(), true, 'a'); + virtual.KEYBOARD.updateNextKeyboard(VirtualKey.S.getKeycode(), true, 's'); // Load the next keyboard events virtual.KEYBOARD.nextKeyboardTick(); @@ -79,7 +76,7 @@ void testKeyboardAddPresses() { assertTrue(virtual.KEYBOARD.nextKeyboardSubtick()); // Read out values from the subtick - assertEquals(VirtualKey2.W.getKeycode(), virtual.KEYBOARD.getEventKeyboardKey()); + assertEquals(VirtualKey.W.getKeycode(), virtual.KEYBOARD.getEventKeyboardKey()); assertTrue(virtual.KEYBOARD.getEventKeyboardState()); assertEquals('w', virtual.KEYBOARD.getEventKeyboardCharacter()); @@ -89,7 +86,7 @@ void testKeyboardAddPresses() { assertTrue(virtual.KEYBOARD.nextKeyboardSubtick()); // Read out values from the subtick - assertEquals(VirtualKey2.A.getKeycode(), virtual.KEYBOARD.getEventKeyboardKey()); + assertEquals(VirtualKey.A.getKeycode(), virtual.KEYBOARD.getEventKeyboardKey()); assertTrue(virtual.KEYBOARD.getEventKeyboardState()); assertEquals('a', virtual.KEYBOARD.getEventKeyboardCharacter()); @@ -99,7 +96,7 @@ void testKeyboardAddPresses() { assertTrue(virtual.KEYBOARD.nextKeyboardSubtick()); // Read out values from the subtick - assertEquals(VirtualKey2.S.getKeycode(), virtual.KEYBOARD.getEventKeyboardKey()); + assertEquals(VirtualKey.S.getKeycode(), virtual.KEYBOARD.getEventKeyboardKey()); assertEquals(true, virtual.KEYBOARD.getEventKeyboardState()); assertEquals('s', virtual.KEYBOARD.getEventKeyboardCharacter()); @@ -112,12 +109,12 @@ void testKeyboardAddPresses() { */ @Test void testKeyboardRemovePresses() { - VirtualKeyboard2 preloadedKeyboard = new VirtualKeyboard2(); + VirtualKeyboard preloadedKeyboard = new VirtualKeyboard(); - preloadedKeyboard.update(VirtualKey2.W.getKeycode(), true, 'w'); - VirtualInput2 virtual = new VirtualInput2(LOGGER, preloadedKeyboard, new VirtualMouse2(), new VirtualCameraAngle2()); + preloadedKeyboard.update(VirtualKey.W.getKeycode(), true, 'w'); + VirtualInput virtual = new VirtualInput(LOGGER, preloadedKeyboard, new VirtualMouse(), new VirtualCameraAngle()); - virtual.KEYBOARD.updateNextKeyboard(VirtualKey2.W.getKeycode(), false, Character.MIN_VALUE); + virtual.KEYBOARD.updateNextKeyboard(VirtualKey.W.getKeycode(), false, Character.MIN_VALUE); // Load the next keyboard events virtual.KEYBOARD.nextKeyboardTick(); @@ -126,7 +123,7 @@ void testKeyboardRemovePresses() { assertTrue(virtual.KEYBOARD.nextKeyboardSubtick()); // Read out values from the subtick - assertEquals(VirtualKey2.W.getKeycode(), virtual.KEYBOARD.getEventKeyboardKey()); + assertEquals(VirtualKey.W.getKeycode(), virtual.KEYBOARD.getEventKeyboardKey()); assertFalse(virtual.KEYBOARD.getEventKeyboardState()); assertEquals(Character.MIN_VALUE, virtual.KEYBOARD.getEventKeyboardCharacter()); diff --git a/src/test/java/tasmod/virtual/VirtualKeyboardTest.java b/src/test/java/tasmod/virtual/VirtualKeyboardTest.java index 399fb953..322bb8dd 100644 --- a/src/test/java/tasmod/virtual/VirtualKeyboardTest.java +++ b/src/test/java/tasmod/virtual/VirtualKeyboardTest.java @@ -7,9 +7,9 @@ import org.junit.jupiter.api.Test; -import com.minecrafttas.tasmod.virtual.VirtualKey2; -import com.minecrafttas.tasmod.virtual.VirtualKeyboard2; -import com.minecrafttas.tasmod.virtual.VirtualKeyboardEvent; +import com.minecrafttas.tasmod.virtual.VirtualKey; +import com.minecrafttas.tasmod.virtual.VirtualKeyboard; +import com.minecrafttas.tasmod.virtual.event.VirtualKeyboardEvent; class VirtualKeyboardTest { @@ -18,7 +18,7 @@ class VirtualKeyboardTest { */ @Test void testEmptyConstructor(){ - VirtualKeyboard2 actual = new VirtualKeyboard2(); + VirtualKeyboard actual = new VirtualKeyboard(); assertTrue(actual.getPressedKeys().isEmpty()); assertTrue(actual.getCharList().isEmpty()); assertTrue(actual.isParent()); @@ -30,14 +30,14 @@ void testEmptyConstructor(){ @Test void testSubtickConstructor(){ Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.W.getKeycode()); - testKeycodeSet.add(VirtualKey2.S.getKeycode()); + testKeycodeSet.add(VirtualKey.W.getKeycode()); + testKeycodeSet.add(VirtualKey.S.getKeycode()); List testCharList = new ArrayList<>(); testCharList.add('w'); testCharList.add('s'); - VirtualKeyboard2 actual = new VirtualKeyboard2(testKeycodeSet, testCharList); + VirtualKeyboard actual = new VirtualKeyboard(testKeycodeSet, testCharList); assertIterableEquals(testKeycodeSet, actual.getPressedKeys()); assertIterableEquals(testCharList, actual.getCharList()); @@ -49,10 +49,10 @@ void testSubtickConstructor(){ */ @Test void testSetPressedByKeycode(){ - VirtualKeyboard2 actual = new VirtualKeyboard2(); - actual.setPressed(VirtualKey2.W.getKeycode(), true); + VirtualKeyboard actual = new VirtualKeyboard(); + actual.setPressed(VirtualKey.W.getKeycode(), true); - assertIterableEquals(Arrays.asList(VirtualKey2.W.getKeycode()), actual.getPressedKeys()); + assertIterableEquals(Arrays.asList(VirtualKey.W.getKeycode()), actual.getPressedKeys()); assertTrue(actual.isParent()); } @@ -61,8 +61,8 @@ void testSetPressedByKeycode(){ */ @Test void testFailingSetPressedByKeycode(){ - VirtualKeyboard2 actual = new VirtualKeyboard2(); - actual.setPressed(VirtualKey2.LC.getKeycode(), true); + VirtualKeyboard actual = new VirtualKeyboard(); + actual.setPressed(VirtualKey.LC.getKeycode(), true); assertTrue(actual.getPressedKeys().isEmpty()); assertTrue(actual.isParent()); @@ -73,10 +73,10 @@ void testFailingSetPressedByKeycode(){ */ @Test void testSetPressedByKeyname(){ - VirtualKeyboard2 actual = new VirtualKeyboard2(); + VirtualKeyboard actual = new VirtualKeyboard(); actual.setPressed("W", true); - assertIterableEquals(Arrays.asList(VirtualKey2.W.getKeycode()), actual.getPressedKeys()); + assertIterableEquals(Arrays.asList(VirtualKey.W.getKeycode()), actual.getPressedKeys()); assertTrue(actual.isParent()); } @@ -86,12 +86,12 @@ void testSetPressedByKeyname(){ @Test void testSetUnPressedByKeycode(){ Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.W.getKeycode()); - testKeycodeSet.add(VirtualKey2.S.getKeycode()); - VirtualKeyboard2 actual = new VirtualKeyboard2(testKeycodeSet, new ArrayList<>()); - actual.setPressed(VirtualKey2.W.getKeycode(), false); + testKeycodeSet.add(VirtualKey.W.getKeycode()); + testKeycodeSet.add(VirtualKey.S.getKeycode()); + VirtualKeyboard actual = new VirtualKeyboard(testKeycodeSet, new ArrayList<>()); + actual.setPressed(VirtualKey.W.getKeycode(), false); - assertIterableEquals(Arrays.asList(VirtualKey2.S.getKeycode()), actual.getPressedKeys()); + assertIterableEquals(Arrays.asList(VirtualKey.S.getKeycode()), actual.getPressedKeys()); } /** @@ -100,12 +100,12 @@ void testSetUnPressedByKeycode(){ @Test void testSetUnPressedByKeyname(){ Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.W.getKeycode()); - testKeycodeSet.add(VirtualKey2.S.getKeycode()); - VirtualKeyboard2 actual = new VirtualKeyboard2(testKeycodeSet, new ArrayList<>()); + testKeycodeSet.add(VirtualKey.W.getKeycode()); + testKeycodeSet.add(VirtualKey.S.getKeycode()); + VirtualKeyboard actual = new VirtualKeyboard(testKeycodeSet, new ArrayList<>()); actual.setPressed("S", false); - assertIterableEquals(Arrays.asList(VirtualKey2.W.getKeycode()), actual.getPressedKeys()); + assertIterableEquals(Arrays.asList(VirtualKey.W.getKeycode()), actual.getPressedKeys()); } /** @@ -113,7 +113,7 @@ void testSetUnPressedByKeyname(){ */ @Test void testAddCharacter(){ - VirtualKeyboard2 actual = new VirtualKeyboard2(); + VirtualKeyboard actual = new VirtualKeyboard(); actual.addChar('w', false); assertIterableEquals(Arrays.asList('w'), actual.getCharList()); @@ -125,15 +125,15 @@ void testAddCharacter(){ @Test void testToString(){ Set testKeycodeSet = new LinkedHashSet<>(); - testKeycodeSet.add(VirtualKey2.W.getKeycode()); - testKeycodeSet.add(VirtualKey2.S.getKeycode()); + testKeycodeSet.add(VirtualKey.W.getKeycode()); + testKeycodeSet.add(VirtualKey.S.getKeycode()); List testCharList = new ArrayList<>(); testCharList.add('w'); testCharList.add('s'); - VirtualKeyboard2 actual = new VirtualKeyboard2(testKeycodeSet, testCharList); - VirtualKeyboard2 actual2 = new VirtualKeyboard2(testKeycodeSet, new ArrayList<>()); + VirtualKeyboard actual = new VirtualKeyboard(testKeycodeSet, testCharList); + VirtualKeyboard actual2 = new VirtualKeyboard(testKeycodeSet, new ArrayList<>()); assertEquals("W,S;ws", actual.toString()); assertEquals("W,S;", actual2.toString()); @@ -144,10 +144,10 @@ void testToString(){ */ @Test void testToStringSubticks(){ - VirtualKeyboard2 actual = new VirtualKeyboard2(); + VirtualKeyboard actual = new VirtualKeyboard(); - actual.update(VirtualKey2.W.getKeycode(), true, 'w'); - actual.update(VirtualKey2.S.getKeycode(), true, 's'); + actual.update(VirtualKey.W.getKeycode(), true, 'w'); + actual.update(VirtualKey.S.getKeycode(), true, 's'); assertEquals("W;w\nW,S;s", actual.toString()); } @@ -158,15 +158,15 @@ void testToStringSubticks(){ @Test void testEquals() { Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.W.getKeycode()); - testKeycodeSet.add(VirtualKey2.S.getKeycode()); + testKeycodeSet.add(VirtualKey.W.getKeycode()); + testKeycodeSet.add(VirtualKey.S.getKeycode()); List testCharList = new ArrayList<>(); testCharList.add('w'); testCharList.add('s'); - VirtualKeyboard2 actual = new VirtualKeyboard2(testKeycodeSet, testCharList); - VirtualKeyboard2 actual2 = new VirtualKeyboard2(testKeycodeSet, testCharList); + VirtualKeyboard actual = new VirtualKeyboard(testKeycodeSet, testCharList); + VirtualKeyboard actual2 = new VirtualKeyboard(testKeycodeSet, testCharList); assertEquals(actual, actual2); } @@ -177,8 +177,8 @@ void testEquals() { @Test void testNotEquals() { Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.W.getKeycode()); - testKeycodeSet.add(VirtualKey2.S.getKeycode()); + testKeycodeSet.add(VirtualKey.W.getKeycode()); + testKeycodeSet.add(VirtualKey.S.getKeycode()); List testCharList = new ArrayList<>(); testCharList.add('w'); @@ -191,9 +191,9 @@ void testNotEquals() { List testCharList3 = new ArrayList<>(); testCharList3.add('w'); - VirtualKeyboard2 actual = new VirtualKeyboard2(testKeycodeSet, testCharList); - VirtualKeyboard2 test2 = new VirtualKeyboard2(testKeycodeSet, testCharList2); - VirtualKeyboard2 test3 = new VirtualKeyboard2(testKeycodeSet, testCharList3); + VirtualKeyboard actual = new VirtualKeyboard(testKeycodeSet, testCharList); + VirtualKeyboard test2 = new VirtualKeyboard(testKeycodeSet, testCharList2); + VirtualKeyboard test3 = new VirtualKeyboard(testKeycodeSet, testCharList3); assertNotEquals(actual, test2); assertNotEquals(actual, test3); @@ -206,15 +206,15 @@ void testNotEquals() { @Test void testClone() { Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.W.getKeycode()); - testKeycodeSet.add(VirtualKey2.S.getKeycode()); + testKeycodeSet.add(VirtualKey.W.getKeycode()); + testKeycodeSet.add(VirtualKey.S.getKeycode()); List testCharList = new ArrayList<>(); testCharList.add('w'); testCharList.add('s'); - VirtualKeyboard2 actual = new VirtualKeyboard2(testKeycodeSet, testCharList); - VirtualKeyboard2 test2 = actual.clone(); + VirtualKeyboard actual = new VirtualKeyboard(testKeycodeSet, testCharList); + VirtualKeyboard test2 = actual.clone(); assertEquals(actual, test2); } @@ -224,16 +224,16 @@ void testClone() { */ @Test void testCopyFrom(){ - VirtualKeyboard2 copyFrom = new VirtualKeyboard2(); - VirtualKeyboard2 actual = new VirtualKeyboard2(); + VirtualKeyboard copyFrom = new VirtualKeyboard(); + VirtualKeyboard actual = new VirtualKeyboard(); - copyFrom.update(VirtualKey2.W.getKeycode(), true, 'w'); - copyFrom.update(VirtualKey2.A.getKeycode(), true, 'a'); + copyFrom.update(VirtualKey.W.getKeycode(), true, 'w'); + copyFrom.update(VirtualKey.A.getKeycode(), true, 'a'); - VirtualKeyboard2 expected = copyFrom.clone(); + VirtualKeyboard expected = copyFrom.clone(); - actual.update(VirtualKey2.S.getKeycode(), true, 's'); - actual.update(VirtualKey2.D.getKeycode(), true, 'd'); + actual.update(VirtualKey.S.getKeycode(), true, 's'); + actual.update(VirtualKey.D.getKeycode(), true, 'd'); actual.copyFrom(copyFrom); @@ -249,13 +249,13 @@ void testCopyFrom(){ */ @Test void testUpdate(){ - VirtualKeyboard2 actual = new VirtualKeyboard2(); - actual.update(VirtualKey2.W.getKeycode(), true, 'w'); - actual.update(VirtualKey2.A.getKeycode(), true, 'A'); + VirtualKeyboard actual = new VirtualKeyboard(); + actual.update(VirtualKey.W.getKeycode(), true, 'w'); + actual.update(VirtualKey.A.getKeycode(), true, 'A'); - List expected = new ArrayList<>(); - expected.add(new VirtualKeyboard2(new HashSet(Arrays.asList(VirtualKey2.W.getKeycode())), Arrays.asList('w'))); - expected.add(new VirtualKeyboard2(new HashSet(Arrays.asList(VirtualKey2.W.getKeycode(), VirtualKey2.A.getKeycode())), Arrays.asList('A'))); + List expected = new ArrayList<>(); + expected.add(new VirtualKeyboard(new HashSet(Arrays.asList(VirtualKey.W.getKeycode())), Arrays.asList('w'))); + expected.add(new VirtualKeyboard(new HashSet(Arrays.asList(VirtualKey.W.getKeycode(), VirtualKey.A.getKeycode())), Arrays.asList('A'))); assertIterableEquals(expected, actual.getAll()); } @@ -265,11 +265,11 @@ void testUpdate(){ */ @Test void testGetDifference(){ - VirtualKeyboard2 test = new VirtualKeyboard2(new HashSet<>(Arrays.asList(VirtualKey2.W.getKeycode())), Arrays.asList('w')); - VirtualKeyboard2 test2 = new VirtualKeyboard2(new HashSet<>(Arrays.asList(VirtualKey2.W.getKeycode(), VirtualKey2.S.getKeycode())), Arrays.asList('S')); + VirtualKeyboard test = new VirtualKeyboard(new HashSet<>(Arrays.asList(VirtualKey.W.getKeycode())), Arrays.asList('w')); + VirtualKeyboard test2 = new VirtualKeyboard(new HashSet<>(Arrays.asList(VirtualKey.W.getKeycode(), VirtualKey.S.getKeycode())), Arrays.asList('S')); Queue actual = new ConcurrentLinkedQueue<>(); test.getDifference(test2, actual); - Queue expected = new ConcurrentLinkedQueue<>(Arrays.asList(new VirtualKeyboardEvent(VirtualKey2.S.getKeycode(), true, 'S'))); + Queue expected = new ConcurrentLinkedQueue<>(Arrays.asList(new VirtualKeyboardEvent(VirtualKey.S.getKeycode(), true, 'S'))); assertIterableEquals(expected, actual); } @@ -279,17 +279,17 @@ void testGetDifference(){ */ @Test void testGetVirtualEventsPress() { - VirtualKeyboard2 unpressed = new VirtualKeyboard2(); + VirtualKeyboard unpressed = new VirtualKeyboard(); - VirtualKeyboard2 pressed = new VirtualKeyboard2(); - pressed.update(VirtualKey2.W.getKeycode(), true, 'w'); + VirtualKeyboard pressed = new VirtualKeyboard(); + pressed.update(VirtualKey.W.getKeycode(), true, 'w'); // Load actual with the events Queue actual = new ConcurrentLinkedQueue<>(); unpressed.getVirtualEvents(pressed, actual); // Load expected - List expected = Arrays.asList(new VirtualKeyboardEvent(VirtualKey2.W.getKeycode(), true, 'w')); + List expected = Arrays.asList(new VirtualKeyboardEvent(VirtualKey.W.getKeycode(), true, 'w')); assertIterableEquals(expected, actual); } @@ -299,17 +299,17 @@ void testGetVirtualEventsPress() { */ @Test void testGetVirtualEventsUnpress() { - VirtualKeyboard2 unpressed = new VirtualKeyboard2(); + VirtualKeyboard unpressed = new VirtualKeyboard(); - VirtualKeyboard2 pressed = new VirtualKeyboard2(); - pressed.update(VirtualKey2.W.getKeycode(), true, 'w'); + VirtualKeyboard pressed = new VirtualKeyboard(); + pressed.update(VirtualKey.W.getKeycode(), true, 'w'); // Load actual with the events Queue actual = new ConcurrentLinkedQueue<>(); pressed.getVirtualEvents(unpressed, actual); // Load expected - List expected = Arrays.asList(new VirtualKeyboardEvent(VirtualKey2.W.getKeycode(), false, Character.MIN_VALUE)); + List expected = Arrays.asList(new VirtualKeyboardEvent(VirtualKey.W.getKeycode(), false, Character.MIN_VALUE)); assertIterableEquals(expected, actual); } @@ -319,9 +319,9 @@ void testGetVirtualEventsUnpress() { */ @Test void testRepeatEvents(){ - VirtualKeyboard2 testKb = new VirtualKeyboard2(); + VirtualKeyboard testKb = new VirtualKeyboard(); - int keycode = VirtualKey2.BACK.getKeycode(); + int keycode = VirtualKey.BACK.getKeycode(); // Update the keyboard multiple times with the same value testKb.update(keycode, true, Character.MIN_VALUE, true); @@ -330,7 +330,7 @@ void testRepeatEvents(){ Queue actual = new ConcurrentLinkedQueue<>(); // Fill "actual" with VirtualKeyboardEvents - new VirtualKeyboard2().getVirtualEvents(testKb, actual); + new VirtualKeyboard().getVirtualEvents(testKb, actual); List expected = new ArrayList<>(); // Add expected VirtualKeyboardEvents @@ -346,9 +346,9 @@ void testRepeatEvents(){ */ @Test void testRepeatEventsFail(){ - VirtualKeyboard2 testKb = new VirtualKeyboard2(); + VirtualKeyboard testKb = new VirtualKeyboard(); - int keycode = VirtualKey2.BACK.getKeycode(); + int keycode = VirtualKey.BACK.getKeycode(); // Update the keyboard multiple times with the same value. testKb.update(keycode, true, Character.MIN_VALUE, false); testKb.update(keycode, true, Character.MIN_VALUE, false); @@ -356,7 +356,7 @@ void testRepeatEventsFail(){ Queue actual = new ConcurrentLinkedQueue<>(); // Fill "actual" with VirtualKeyboardEvents - new VirtualKeyboard2().getVirtualEvents(testKb, actual); + new VirtualKeyboard().getVirtualEvents(testKb, actual); List expected = new ArrayList<>(); diff --git a/src/test/java/tasmod/virtual/VirtualMouseTest.java b/src/test/java/tasmod/virtual/VirtualMouseTest.java index 2b39bf55..e7dc4b13 100644 --- a/src/test/java/tasmod/virtual/VirtualMouseTest.java +++ b/src/test/java/tasmod/virtual/VirtualMouseTest.java @@ -15,13 +15,11 @@ import java.util.Set; import java.util.concurrent.ConcurrentLinkedQueue; +import com.minecrafttas.tasmod.virtual.VirtualMouse; import org.junit.jupiter.api.Test; -import com.minecrafttas.tasmod.virtual.VirtualKey2; -import com.minecrafttas.tasmod.virtual.VirtualKeyboard2; -import com.minecrafttas.tasmod.virtual.VirtualKeyboardEvent; -import com.minecrafttas.tasmod.virtual.VirtualMouse2; -import com.minecrafttas.tasmod.virtual.VirtualMouseEvent; +import com.minecrafttas.tasmod.virtual.VirtualKey; +import com.minecrafttas.tasmod.virtual.event.VirtualMouseEvent; class VirtualMouseTest { @@ -30,7 +28,7 @@ class VirtualMouseTest { */ @Test void testEmptyConstructor() { - VirtualMouse2 actual = new VirtualMouse2(); + VirtualMouse actual = new VirtualMouse(); assertTrue(actual.getPressedKeys().isEmpty()); assertEquals(0, actual.getScrollWheel()); assertEquals(0, actual.getCursorX()); @@ -44,10 +42,10 @@ void testEmptyConstructor() { @Test void testSubtickConstructor() { Set expected = new HashSet<>(); - expected.add(VirtualKey2.LC.getKeycode()); - expected.add(VirtualKey2.RC.getKeycode()); + expected.add(VirtualKey.LC.getKeycode()); + expected.add(VirtualKey.RC.getKeycode()); - VirtualMouse2 actual = new VirtualMouse2(expected, -15, 0, 2); + VirtualMouse actual = new VirtualMouse(expected, -15, 0, 2); assertIterableEquals(expected, actual.getPressedKeys()); assertEquals(-15, actual.getScrollWheel()); @@ -61,10 +59,10 @@ void testSubtickConstructor() { */ @Test void testSetPressedByKeycode(){ - VirtualMouse2 actual = new VirtualMouse2(); - actual.setPressed(VirtualKey2.LC.getKeycode(), true); + VirtualMouse actual = new VirtualMouse(); + actual.setPressed(VirtualKey.LC.getKeycode(), true); - assertIterableEquals(Arrays.asList(VirtualKey2.LC.getKeycode()), actual.getPressedKeys()); + assertIterableEquals(Arrays.asList(VirtualKey.LC.getKeycode()), actual.getPressedKeys()); assertTrue(actual.isParent()); } @@ -73,10 +71,10 @@ void testSetPressedByKeycode(){ */ @Test void testSetPressedByKeyname(){ - VirtualMouse2 actual = new VirtualMouse2(); + VirtualMouse actual = new VirtualMouse(); actual.setPressed("LC", true); - assertIterableEquals(Arrays.asList(VirtualKey2.LC.getKeycode()), actual.getPressedKeys()); + assertIterableEquals(Arrays.asList(VirtualKey.LC.getKeycode()), actual.getPressedKeys()); assertTrue(actual.isParent()); } @@ -86,12 +84,12 @@ void testSetPressedByKeyname(){ @Test void testSetUnPressedByKeycode(){ Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.LC.getKeycode()); - testKeycodeSet.add(VirtualKey2.MBUTTON9.getKeycode()); - VirtualMouse2 actual = new VirtualMouse2(testKeycodeSet, 0, 0, 0); - actual.setPressed(VirtualKey2.MBUTTON9.getKeycode(), false); + testKeycodeSet.add(VirtualKey.LC.getKeycode()); + testKeycodeSet.add(VirtualKey.MBUTTON9.getKeycode()); + VirtualMouse actual = new VirtualMouse(testKeycodeSet, 0, 0, 0); + actual.setPressed(VirtualKey.MBUTTON9.getKeycode(), false); - assertIterableEquals(Arrays.asList(VirtualKey2.LC.getKeycode()), actual.getPressedKeys()); + assertIterableEquals(Arrays.asList(VirtualKey.LC.getKeycode()), actual.getPressedKeys()); } /** @@ -100,12 +98,12 @@ void testSetUnPressedByKeycode(){ @Test void testSetUnPressedByKeyname(){ Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.LC.getKeycode()); - testKeycodeSet.add(VirtualKey2.MBUTTON9.getKeycode()); - VirtualMouse2 actual = new VirtualMouse2(testKeycodeSet, 0, 0, 0); + testKeycodeSet.add(VirtualKey.LC.getKeycode()); + testKeycodeSet.add(VirtualKey.MBUTTON9.getKeycode()); + VirtualMouse actual = new VirtualMouse(testKeycodeSet, 0, 0, 0); actual.setPressed("MBUTTON9", false); - assertIterableEquals(Arrays.asList(VirtualKey2.LC.getKeycode()), actual.getPressedKeys()); + assertIterableEquals(Arrays.asList(VirtualKey.LC.getKeycode()), actual.getPressedKeys()); } /** @@ -114,10 +112,10 @@ void testSetUnPressedByKeyname(){ @Test void testToString(){ Set testKeycodeSet = new LinkedHashSet<>(); - testKeycodeSet.add(VirtualKey2.LC.getKeycode()); - testKeycodeSet.add(VirtualKey2.MC.getKeycode()); + testKeycodeSet.add(VirtualKey.LC.getKeycode()); + testKeycodeSet.add(VirtualKey.MC.getKeycode()); - VirtualMouse2 actual = new VirtualMouse2(testKeycodeSet, 10, 100, 120); + VirtualMouse actual = new VirtualMouse(testKeycodeSet, 10, 100, 120); assertEquals("LC,MC;10,100,120", actual.toString()); } @@ -127,9 +125,9 @@ void testToString(){ */ @Test void testToStringSubtick(){ - VirtualMouse2 actual = new VirtualMouse2(); - actual.update(VirtualKey2.LC.getKeycode(), true, 10, 100, 120); - actual.update(VirtualKey2.MC.getKeycode(), true, 0, 12, 3); + VirtualMouse actual = new VirtualMouse(); + actual.update(VirtualKey.LC.getKeycode(), true, 10, 100, 120); + actual.update(VirtualKey.MC.getKeycode(), true, 0, 12, 3); assertEquals("LC;10,100,120\nLC,MC;0,12,3", actual.toString()); } @@ -140,12 +138,12 @@ void testToStringSubtick(){ @Test void testEquals() { Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.W.getKeycode()); - testKeycodeSet.add(VirtualKey2.S.getKeycode()); + testKeycodeSet.add(VirtualKey.W.getKeycode()); + testKeycodeSet.add(VirtualKey.S.getKeycode()); - VirtualMouse2 actual = new VirtualMouse2(testKeycodeSet, -15, 129, 340); - VirtualMouse2 actual2 = new VirtualMouse2(testKeycodeSet, -15, 129, 340); + VirtualMouse actual = new VirtualMouse(testKeycodeSet, -15, 129, 340); + VirtualMouse actual2 = new VirtualMouse(testKeycodeSet, -15, 129, 340); assertEquals(actual, actual2); } @@ -156,20 +154,20 @@ void testEquals() { @Test void testNotEquals() { Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.LC.getKeycode()); + testKeycodeSet.add(VirtualKey.LC.getKeycode()); - VirtualMouse2 actual = new VirtualMouse2(testKeycodeSet, -15, 1, 1); + VirtualMouse actual = new VirtualMouse(testKeycodeSet, -15, 1, 1); Set testKeycodeSet2 = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.RC.getKeycode()); - VirtualMouse2 test2 = new VirtualMouse2(testKeycodeSet2, -15, 1, 1); + testKeycodeSet.add(VirtualKey.RC.getKeycode()); + VirtualMouse test2 = new VirtualMouse(testKeycodeSet2, -15, 1, 1); - VirtualMouse2 test3 = new VirtualMouse2(testKeycodeSet, -16, 1, 1); + VirtualMouse test3 = new VirtualMouse(testKeycodeSet, -16, 1, 1); - VirtualMouse2 test4 = new VirtualMouse2(testKeycodeSet, -15, 2, 1); - VirtualMouse2 test5 = new VirtualMouse2(testKeycodeSet, -15, 1, 2); + VirtualMouse test4 = new VirtualMouse(testKeycodeSet, -15, 2, 1); + VirtualMouse test5 = new VirtualMouse(testKeycodeSet, -15, 1, 2); assertNotEquals(actual, test2); assertNotEquals(actual, test3); @@ -184,11 +182,11 @@ void testNotEquals() { @Test void testClone() { Set testKeycodeSet = new HashSet<>(); - testKeycodeSet.add(VirtualKey2.LC.getKeycode()); - testKeycodeSet.add(VirtualKey2.MC.getKeycode()); + testKeycodeSet.add(VirtualKey.LC.getKeycode()); + testKeycodeSet.add(VirtualKey.MC.getKeycode()); - VirtualMouse2 actual = new VirtualMouse2(testKeycodeSet, 10, 3, 2); - VirtualMouse2 test2 = actual.clone(); + VirtualMouse actual = new VirtualMouse(testKeycodeSet, 10, 3, 2); + VirtualMouse test2 = actual.clone(); assertEquals(actual, test2); } @@ -198,16 +196,16 @@ void testClone() { */ @Test void testCopyFrom() { - VirtualMouse2 copyFrom = new VirtualMouse2(); - VirtualMouse2 actual = new VirtualMouse2(); + VirtualMouse copyFrom = new VirtualMouse(); + VirtualMouse actual = new VirtualMouse(); - copyFrom.update(VirtualKey2.LC.getKeycode(), true, 0, 0, 0); - copyFrom.update(VirtualKey2.MOUSEMOVED.getKeycode(), false, 120, 10, 20); + copyFrom.update(VirtualKey.LC.getKeycode(), true, 0, 0, 0); + copyFrom.update(VirtualKey.MOUSEMOVED.getKeycode(), false, 120, 10, 20); - VirtualMouse2 expected = copyFrom.clone(); + VirtualMouse expected = copyFrom.clone(); - actual.update(VirtualKey2.MBUTTON12.getKeycode(), true, 0,0,0); - actual.update(VirtualKey2.MOUSEMOVED.getKeycode(), true, -120, -10, -10); + actual.update(VirtualKey.MBUTTON12.getKeycode(), true, 0,0,0); + actual.update(VirtualKey.MOUSEMOVED.getKeycode(), true, -120, -10, -10); actual.copyFrom(copyFrom); @@ -227,13 +225,13 @@ void testCopyFrom() { */ @Test void testUpdate(){ - VirtualMouse2 actual = new VirtualMouse2(); - actual.update(VirtualKey2.LC.getKeycode(), true, -30, 118, 42); - actual.update(VirtualKey2.MOUSEMOVED.getKeycode(), false, 0, 23, 144); + VirtualMouse actual = new VirtualMouse(); + actual.update(VirtualKey.LC.getKeycode(), true, -30, 118, 42); + actual.update(VirtualKey.MOUSEMOVED.getKeycode(), false, 0, 23, 144); - List expected = new ArrayList<>(); - expected.add(new VirtualMouse2(new HashSet(Arrays.asList(VirtualKey2.LC.getKeycode())), -30, 118, 42)); - expected.add(new VirtualMouse2(new HashSet(Arrays.asList(VirtualKey2.LC.getKeycode())), 0, 23, 144)); + List expected = new ArrayList<>(); + expected.add(new VirtualMouse(new HashSet(Arrays.asList(VirtualKey.LC.getKeycode())), -30, 118, 42)); + expected.add(new VirtualMouse(new HashSet(Arrays.asList(VirtualKey.LC.getKeycode())), 0, 23, 144)); assertIterableEquals(expected, actual.getAll()); } @@ -243,11 +241,11 @@ void testUpdate(){ */ @Test void testGetDifference(){ - VirtualMouse2 test = new VirtualMouse2(new HashSet<>(Arrays.asList(VirtualKey2.LC.getKeycode())), 15, 0, 0); - VirtualMouse2 test2 = new VirtualMouse2(new HashSet<>(Arrays.asList(VirtualKey2.LC.getKeycode(), VirtualKey2.RC.getKeycode())), 30, 1, 2); + VirtualMouse test = new VirtualMouse(new HashSet<>(Arrays.asList(VirtualKey.LC.getKeycode())), 15, 0, 0); + VirtualMouse test2 = new VirtualMouse(new HashSet<>(Arrays.asList(VirtualKey.LC.getKeycode(), VirtualKey.RC.getKeycode())), 30, 1, 2); Queue actual = new ConcurrentLinkedQueue<>(); test.getDifference(test2, actual); - Queue expected = new ConcurrentLinkedQueue<>(Arrays.asList(new VirtualMouseEvent(VirtualKey2.RC.getKeycode(), true, 30, 1, 2))); + Queue expected = new ConcurrentLinkedQueue<>(Arrays.asList(new VirtualMouseEvent(VirtualKey.RC.getKeycode(), true, 30, 1, 2))); assertIterableEquals(expected, actual); } @@ -257,17 +255,17 @@ void testGetDifference(){ */ @Test void testGetVirtualEventsPress() { - VirtualMouse2 unpressed = new VirtualMouse2(); + VirtualMouse unpressed = new VirtualMouse(); - VirtualMouse2 pressed = new VirtualMouse2(); - pressed.update(VirtualKey2.LC.getKeycode(), true, 15, 10, 12); + VirtualMouse pressed = new VirtualMouse(); + pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12); // Load actual with the events Queue actual = new ConcurrentLinkedQueue<>(); unpressed.getVirtualEvents(pressed, actual); // Load expected - List expected = Arrays.asList(new VirtualMouseEvent(VirtualKey2.LC.getKeycode(), true, 15, 10, 12)); + List expected = Arrays.asList(new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 15, 10, 12)); assertIterableEquals(expected, actual); } @@ -277,17 +275,17 @@ void testGetVirtualEventsPress() { */ @Test void testGetVirtualEventsUnpress() { - VirtualMouse2 unpressed = new VirtualMouse2(); + VirtualMouse unpressed = new VirtualMouse(); - VirtualMouse2 pressed = new VirtualMouse2(); - pressed.update(VirtualKey2.LC.getKeycode(), true, 15, 10, 12); + VirtualMouse pressed = new VirtualMouse(); + pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12); // Load actual with the events Queue actual = new ConcurrentLinkedQueue<>(); pressed.getVirtualEvents(unpressed, actual); // Load expected - List expected = Arrays.asList(new VirtualMouseEvent(VirtualKey2.LC.getKeycode(), false, 0, 0, 0)); + List expected = Arrays.asList(new VirtualMouseEvent(VirtualKey.LC.getKeycode(), false, 0, 0, 0)); assertIterableEquals(expected, actual); } @@ -297,11 +295,11 @@ void testGetVirtualEventsUnpress() { */ @Test void testSameUpdate() { - VirtualMouse2 unpressed = new VirtualMouse2(); + VirtualMouse unpressed = new VirtualMouse(); - VirtualMouse2 pressed = new VirtualMouse2(); - pressed.update(VirtualKey2.LC.getKeycode(), true, 15, 10, 12); - pressed.update(VirtualKey2.LC.getKeycode(), true, 15, 10, 12); + VirtualMouse pressed = new VirtualMouse(); + pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12); + pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12); // Load actual with the events Queue actual = new ConcurrentLinkedQueue<>(); @@ -309,7 +307,7 @@ void testSameUpdate() { // Load expected List expected = Arrays.asList( - new VirtualMouseEvent(VirtualKey2.LC.getKeycode(), true, 15, 10, 12) // Should only have one keyboard event + new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 15, 10, 12) // Should only have one keyboard event ); assertIterableEquals(expected, actual); @@ -320,11 +318,11 @@ void testSameUpdate() { */ @Test void testScrollWheelDifferent() { - VirtualMouse2 unpressed = new VirtualMouse2(); + VirtualMouse unpressed = new VirtualMouse(); - VirtualMouse2 pressed = new VirtualMouse2(); - pressed.update(VirtualKey2.LC.getKeycode(), true, 15, 10, 12); - pressed.update(VirtualKey2.LC.getKeycode(), true, -30, 10, 12); + VirtualMouse pressed = new VirtualMouse(); + pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12); + pressed.update(VirtualKey.LC.getKeycode(), true, -30, 10, 12); // Load actual with the events Queue actual = new ConcurrentLinkedQueue<>(); @@ -332,8 +330,8 @@ void testScrollWheelDifferent() { // Load expected List expected = Arrays.asList( - new VirtualMouseEvent(VirtualKey2.LC.getKeycode(), true, 15, 10, 12), - new VirtualMouseEvent(VirtualKey2.MOUSEMOVED.getKeycode(), false, -30, 10, 12) // Adds an additional "MOUSEMOVED" event with the scroll wheel + new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 15, 10, 12), + new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, -30, 10, 12) // Adds an additional "MOUSEMOVED" event with the scroll wheel ); assertIterableEquals(expected, actual); @@ -344,11 +342,11 @@ void testScrollWheelDifferent() { */ @Test void testCursorXDifferent() { - VirtualMouse2 unpressed = new VirtualMouse2(); + VirtualMouse unpressed = new VirtualMouse(); - VirtualMouse2 pressed = new VirtualMouse2(); - pressed.update(VirtualKey2.LC.getKeycode(), true, 15, 10, 12); - pressed.update(VirtualKey2.LC.getKeycode(), true, 15, 11, 12); + VirtualMouse pressed = new VirtualMouse(); + pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12); + pressed.update(VirtualKey.LC.getKeycode(), true, 15, 11, 12); // Load actual with the events Queue actual = new ConcurrentLinkedQueue<>(); @@ -356,8 +354,8 @@ void testCursorXDifferent() { // Load expected List expected = Arrays.asList( - new VirtualMouseEvent(VirtualKey2.LC.getKeycode(), true, 15, 10, 12), - new VirtualMouseEvent(VirtualKey2.MOUSEMOVED.getKeycode(), false, 15, 11, 12) // Adds an additional "MOUSEMOVED" event with the cursorX + new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 15, 10, 12), + new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, 15, 11, 12) // Adds an additional "MOUSEMOVED" event with the cursorX ); assertIterableEquals(expected, actual); @@ -368,11 +366,11 @@ void testCursorXDifferent() { */ @Test void testCursorYDifferent() { - VirtualMouse2 unpressed = new VirtualMouse2(); + VirtualMouse unpressed = new VirtualMouse(); - VirtualMouse2 pressed = new VirtualMouse2(); - pressed.update(VirtualKey2.LC.getKeycode(), true, 15, 10, 12); - pressed.update(VirtualKey2.LC.getKeycode(), true, 15, 10, 120); + VirtualMouse pressed = new VirtualMouse(); + pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12); + pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 120); // Load actual with the events Queue actual = new ConcurrentLinkedQueue<>(); @@ -380,8 +378,8 @@ void testCursorYDifferent() { // Load expected List expected = Arrays.asList( - new VirtualMouseEvent(VirtualKey2.LC.getKeycode(), true, 15, 10, 12), - new VirtualMouseEvent(VirtualKey2.MOUSEMOVED.getKeycode(), false, 15, 10, 120) // Adds an additional "MOUSEMOVED" event with the cursorY + new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 15, 10, 12), + new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, 15, 10, 120) // Adds an additional "MOUSEMOVED" event with the cursorY ); assertIterableEquals(expected, actual);