Skip to content

Commit

Permalink
[VirtualInput] Fixed multiple issues
Browse files Browse the repository at this point in the history
copyFrom
- Fixed unnecessary subtick generation when ignoreFirstUpdate was false
- Fixed unnecessary subtick generation by not clearing the charlist of the next keyboard

Characters
- Fixed chat adding too many characters on a key press
- Added support for repeat events

Markers
- Fixed logger failing in tests
  • Loading branch information
ScribbleTAS committed Feb 4, 2024
1 parent 106c29a commit 111e2ce
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.FileUtils;

/**
* A <i>very</i> simple configuration class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ private static HashMap<String, String> loadJson(InputStream inputStream) {
}
Gson gson = new Gson();
HashMap<String, String> template = new HashMap<>();
HashMap<String, String> out = (HashMap<String, String>) gson.fromJson(new InputStreamReader(inputStream), template.getClass());

@SuppressWarnings("unchecked")
HashMap<String, String> out = (HashMap<String, String>) gson.fromJson(new InputStreamReader(inputStream), template.getClass());
out.forEach((key, value) -> {
value = PATTERN.matcher(value).replaceAll("%$1s");
});
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/minecrafttas/tasmod/TASmodClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void onInitializeClient() {
} else {
config.reset(ConfigOptions.FileToOpen);
}
virtual=new VirtualInput2(); //TODO Move fileOnStart to PlaybackController
virtual=new VirtualInput2(LOGGER); //TODO Move fileOnStart to PlaybackController

// Initialize InfoHud
hud = new InfoHud();
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/minecrafttas/tasmod/gui/InfoHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import org.apache.commons.lang3.tuple.Pair;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import com.minecrafttas.mctcommon.events.EventClient.EventClientTick;
Expand Down
47 changes: 32 additions & 15 deletions src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput2.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.minecrafttas.tasmod.virtual;

import com.minecrafttas.tasmod.TASmod;
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

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;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import net.minecraft.client.gui.GuiScreen;

/**
* Main component for redirecting inputs.<br>
Expand All @@ -21,12 +23,13 @@
* <br>
*/
public class VirtualInput2 {
private final Logger LOGGER;
public final VirtualKeyboardInput KEYBOARD;
public final VirtualMouseInput MOUSE;
public final VirtualCameraAngleInput CAMERA_ANGLE;

public VirtualInput2() {
this(new VirtualKeyboard2(), new VirtualMouse2(), new VirtualCameraAngle2());
public VirtualInput2(Logger logger) {
this(logger, new VirtualKeyboard2(), new VirtualMouse2(), new VirtualCameraAngle2());
}

/**
Expand All @@ -36,7 +39,8 @@ public VirtualInput2() {
* @param preloadedMouse
* @param preloadedCamera
*/
public VirtualInput2(VirtualKeyboard2 preloadedKeyboard, VirtualMouse2 preloadedMouse, VirtualCameraAngle2 preloadedCamera) {
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);
Expand All @@ -54,7 +58,7 @@ public VirtualInput2(VirtualKeyboard2 preloadedKeyboard, VirtualMouse2 preloaded
*/
public void update(GuiScreen currentScreen) {
while (Keyboard.next()) {
KEYBOARD.updateNextKeyboard(Keyboard.getEventKey(), Keyboard.getEventKeyState(), Keyboard.getEventCharacter());
KEYBOARD.updateNextKeyboard(Keyboard.getEventKey(), Keyboard.getEventKeyState(), Keyboard.getEventCharacter(), Keyboard.areRepeatEventsEnabled());
}
while (Mouse.next()) {
if (currentScreen == null) {
Expand Down Expand Up @@ -194,10 +198,23 @@ public VirtualKeyboardInput(VirtualKeyboard2 preloadedKeyboard) {
* @param character The character of this event
*/
public void updateNextKeyboard(int keycode, boolean keystate, char character) {
TASmod.LOGGER.debug(LoggerMarkers.Keyboard,"Update: {}, {}, {}, {}", keycode, keystate, character, Keyboard.areRepeatEventsEnabled());
nextKeyboard.update(keycode, keystate, 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, repeatEventsEnabled);
nextKeyboard.update(keycode, keystate, character, repeatEventsEnabled);
}

/**
* Runs when the next keyboard tick is about to occur.<br>
* Used to load {@link #nextKeyboard} into {@link #currentKeyboard}, creating
Expand Down Expand Up @@ -316,7 +333,7 @@ public VirtualMouseInput(VirtualMouse2 preloadedMouse) {

public void updateNextMouse(int keycode, boolean keystate, int scrollwheel, Integer cursorX, Integer cursorY) {
keycode-=100;
TASmod.LOGGER.debug(LoggerMarkers.Mouse,"Update: {} ({}), {}, {}", keycode, VirtualKey2.getName(keycode), keystate, scrollwheel, cursorX, cursorY);
LOGGER.debug(LoggerMarkers.Mouse,"Update: {} ({}), {}, {}", keycode, VirtualKey2.getName(keycode), keystate, scrollwheel, cursorX, cursorY);
nextMouse.update(keycode, keystate, scrollwheel, cursorX, cursorY);
}

Expand Down
32 changes: 20 additions & 12 deletions src/main/java/com/minecrafttas/tasmod/virtual/VirtualKeyboard2.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -84,7 +85,7 @@ public class VirtualKeyboard2 extends VirtualPeripheral<VirtualKeyboard2> implem
* Creates an empty parent keyboard with all keys unpressed
*/
public VirtualKeyboard2() {
this(new HashSet<>(), new ArrayList<>(), new ArrayList<>(), true);
this(new LinkedHashSet<>(), new ArrayList<>(), new ArrayList<>(), true);
}

/**
Expand Down Expand Up @@ -123,15 +124,19 @@ public VirtualKeyboard2(Set<Integer> pressedKeys, List<Character> charList, List
* @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) {
public void update(int keycode, boolean keystate, char keycharacter, boolean repeatEventsEnabled) {
if(isParent() && !ignoreFirstUpdate()) {
addSubtick(clone());
}
charList.clear();
addChar(keycharacter);
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
Expand All @@ -149,7 +154,6 @@ public void setPressed(int keycode, boolean keystate) {
* @param reference The queue to fill. Passed in by reference.
*/
public void getDifference(VirtualKeyboard2 nextKeyboard, Queue<VirtualKeyboardEvent> reference) {

charQueue.addAll(nextKeyboard.charList);

/* Calculate symmetric difference of keycodes */
Expand All @@ -161,11 +165,11 @@ public void getDifference(VirtualKeyboard2 nextKeyboard, Queue<VirtualKeyboardEv
-------------
A <- unpressed
*/
this.pressedKeys.forEach(key -> {
for(int key : pressedKeys) {
if (!nextKeyboard.getPressedKeys().contains(key)) {
reference.add(new VirtualKeyboardEvent(key, false, Character.MIN_VALUE));
}
});
}

/*
Calculate pressed keys
Expand All @@ -174,11 +178,13 @@ public void getDifference(VirtualKeyboard2 nextKeyboard, Queue<VirtualKeyboardEv
-------------
D <- pressed
*/
nextKeyboard.getPressedKeys().forEach(key -> {
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.
Expand All @@ -191,7 +197,7 @@ public void getDifference(VirtualKeyboard2 nextKeyboard, Queue<VirtualKeyboardEv
otherwise you'd either need to add a keycode for each char or write it in new lines
*/
while (!charQueue.isEmpty()) {
reference.add(new VirtualKeyboardEvent(0, false, getOrMinChar(charQueue.poll())));
reference.add(new VirtualKeyboardEvent(lastKey, true, getOrMinChar(charQueue.poll())));
}

}
Expand Down Expand Up @@ -229,9 +235,10 @@ public void getVirtualEvents(VirtualKeyboard2 nextKeyboard, Queue<VirtualKeyboar
* Null characters will be discarded;
* @param character The character to add
*/
public void addChar(char character) {
if(character != Character.MIN_VALUE)
charList.add(character);
public void addChar(char character, boolean repeatEventsEnabled) {
if(character != Character.MIN_VALUE || repeatEventsEnabled) {
charList.add(character);
}
}

@Override
Expand Down Expand Up @@ -276,6 +283,7 @@ public void copyFrom(VirtualKeyboard2 keyboard) {
super.copyFrom(keyboard);
charList.clear();
charList.addAll(keyboard.charList);
keyboard.charList.clear();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ protected void copyFrom(T peripheral) {
this.pressedKeys.clear();
this.pressedKeys.addAll(peripheral.pressedKeys);
peripheral.subtickList.clear();
peripheral.resetFirstUpdate();
}

/**
Expand All @@ -208,4 +209,8 @@ protected boolean ignoreFirstUpdate() {
protected boolean isIgnoreFirstUpdate(){
return ignoreFirstUpdate;
}

protected void resetFirstUpdate() {
ignoreFirstUpdate = true;
}
}
12 changes: 8 additions & 4 deletions src/test/java/tasmod/virtual/VirtualInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;

import com.minecrafttas.tasmod.virtual.VirtualCameraAngle2;
Expand All @@ -15,12 +17,14 @@

class VirtualInputTest {

private Logger LOGGER = LogManager.getLogger("TASmod");

/**
* Test constructor initializing keyboard, mouse and camera_angle
*/
@Test
void testConstructor() {
VirtualInput2 virtual = new VirtualInput2();
VirtualInput2 virtual = new VirtualInput2(LOGGER);

assertNotNull(virtual.KEYBOARD);
assertNotNull(virtual.MOUSE);
Expand All @@ -40,7 +44,7 @@ void testPreloadedConstructor() {
preloadedMouse.update(VirtualKey2.LC.getKeycode(), true, 15, null, null);


VirtualInput2 virtual = new VirtualInput2(preloadedKeyboard, preloadedMouse, preloadedCameraAngle);
VirtualInput2 virtual = new VirtualInput2(LOGGER, preloadedKeyboard, preloadedMouse, preloadedCameraAngle);

virtual.KEYBOARD.nextKeyboardTick();
assertTrue(virtual.KEYBOARD.nextKeyboardSubtick());
Expand All @@ -59,7 +63,7 @@ void testPreloadedConstructor() {
*/
@Test
void testKeyboardAddPresses() {
VirtualInput2 virtual = new VirtualInput2();
VirtualInput2 virtual = new VirtualInput2(LOGGER);

// Simulate pressing keys WAS on the keyboard
virtual.KEYBOARD.updateNextKeyboard(VirtualKey2.W.getKeycode(), true, 'w');
Expand Down Expand Up @@ -108,7 +112,7 @@ void testKeyboardRemovePresses() {
VirtualKeyboard2 preloadedKeyboard = new VirtualKeyboard2();

preloadedKeyboard.update(VirtualKey2.W.getKeycode(), true, 'w');
VirtualInput2 virtual = new VirtualInput2(preloadedKeyboard, new VirtualMouse2(), new VirtualCameraAngle2());
VirtualInput2 virtual = new VirtualInput2(LOGGER, preloadedKeyboard, new VirtualMouse2(), new VirtualCameraAngle2());

virtual.KEYBOARD.updateNextKeyboard(VirtualKey2.W.getKeycode(), false, Character.MIN_VALUE);

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/tasmod/virtual/VirtualKeyboardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void testSetUnPressedByKeyname(){
@Test
void testAddCharacter(){
VirtualKeyboard2 actual = new VirtualKeyboard2();
actual.addChar('w');
actual.addChar('w', false);

assertIterableEquals(Arrays.asList('w'), actual.getCharList());
}
Expand Down

0 comments on commit 111e2ce

Please sign in to comment.