Skip to content

Commit

Permalink
[VirtualInput] Fix subticks not being recorded in GuiScreens (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScribbleTAS authored Dec 25, 2024
2 parents 19b091d + af20f21 commit 5f39737
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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;
Expand All @@ -11,6 +10,7 @@

import com.minecrafttas.tasmod.TASmodClient;
import com.minecrafttas.tasmod.util.Ducks.GuiScreenDuck;
import com.minecrafttas.tasmod.virtual.VirtualInput;
import com.minecrafttas.tasmod.virtual.event.VirtualKeyboardEvent;

import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -39,7 +39,6 @@ public boolean redirectKeyboardNext() {
return TASmodClient.virtual.KEYBOARD.nextKeyboardSubtick();
}


@Redirect(method = "handleKeyboardInput", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventCharacter()C", remap = false))
public char redirectGetEventCharacter() {
return TASmodClient.virtual.KEYBOARD.getEventKeyboardCharacter();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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;
Expand All @@ -9,6 +8,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.minecrafttas.tasmod.TASmodClient;
import com.minecrafttas.tasmod.virtual.VirtualInput;
import com.minecrafttas.tasmod.virtual.VirtualInput.VirtualMouseInput;
import com.minecrafttas.tasmod.virtual.event.VirtualKeyboardEvent;
import com.minecrafttas.tasmod.virtual.event.VirtualMouseEvent;
Expand All @@ -18,10 +18,10 @@

@Mixin(Minecraft.class)
public class MixinMinecraft {

@Shadow
private GuiScreen currentScreen;

/**
* Runs every frame.
* @see VirtualInput#update(GuiScreen)
Expand All @@ -31,19 +31,29 @@ public class MixinMinecraft {
public void playback_injectRunGameLoop(CallbackInfo ci) {
TASmodClient.virtual.update(currentScreen);
}

// ============================ Keyboard

/**
* Run at the start of run tick keyboard. Runs every tick.
* @see VirtualInput.VirtualKeyboardInput#nextKeyboardTick()
* @param ci CBI
*/
@Inject(method = "runTickKeyboard", at = @At(value = "HEAD"))
public void playback_injectRunTickKeyboard(CallbackInfo ci) {
TASmodClient.virtual.KEYBOARD.nextKeyboardTick();
/*
* This "currentScreen == null" (and the one in runTickMouse) fixes
* a particularly interesting bug where subticks are not recorded in GuiScreens...
*
* The reason this failed is because nextKeyboardTick is called twice in a row,
* when a gui screen is open. The subticks are cleared after calling this
* once, so having it be called twice essentially removes subticks alltogether.
*/
if (currentScreen == null) {
TASmodClient.virtual.KEYBOARD.nextKeyboardTick();
}
}

/**
* 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()
Expand All @@ -53,31 +63,31 @@ public void playback_injectRunTickKeyboard(CallbackInfo ci) {
public boolean playback_redirectKeyboardNext() {
return TASmodClient.virtual.KEYBOARD.nextKeyboardSubtick();
}

/**
* @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() {
return TASmodClient.virtual.KEYBOARD.getEventKeyboardKey();
}

/**
* @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() {
return TASmodClient.virtual.KEYBOARD.getEventKeyboardState();
}

/**
* @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() {
return TASmodClient.virtual.KEYBOARD.getEventKeyboardCharacter();
}

/**
* Runs everytime {@link #playback_redirectKeyboardNext()} has an event ready. Redirects {@link org.lwjgl.input.Keyboard#isKeyDown(int)}
* @see VirtualInput.VirtualKeyboardInput#isKeyDown(int)
Expand All @@ -87,43 +97,45 @@ public char playback_redirectKeyboardGetEventCharacter() {
public boolean playback_redirectIsKeyDown(int keyCode) {
return TASmodClient.virtual.KEYBOARD.isKeyDown(keyCode);
}

/**
* @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() {
return TASmodClient.virtual.KEYBOARD.getEventKeyboardKey();
}

/**
* @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() {
return TASmodClient.virtual.KEYBOARD.getEventKeyboardState();
}

/**
* @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() {
return TASmodClient.virtual.KEYBOARD.getEventKeyboardCharacter();
}

// ============================ Mouse

/**
* Run at the start of run tick mouse. Runs every tick.
* @see VirtualInput.VirtualMouseInput#nextMouseTick()
* @param ci CBI
*/
@Inject(method = "runTickMouse", at = @At(value = "HEAD"))
public void playback_injectRunTickMouse(CallbackInfo ci) {
TASmodClient.virtual.MOUSE.nextMouseTick();
if (currentScreen == null) {
TASmodClient.virtual.MOUSE.nextMouseTick();
}
}

/**
* 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()
Expand All @@ -133,29 +145,29 @@ public void playback_injectRunTickMouse(CallbackInfo ci) {
public boolean playback_redirectMouseNext() {
return TASmodClient.virtual.MOUSE.nextMouseSubtick();
}

/**
* @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() {
return TASmodClient.virtual.MOUSE.getEventMouseKey() + 100;
}

/**
* @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() {
return TASmodClient.virtual.MOUSE.getEventMouseState();
}

/**
* @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() {
return TASmodClient.virtual.MOUSE.getEventMouseScrollWheel();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public boolean equals(Object obj) {
protected void moveFrom(T peripheral) {
if (peripheral == null)
return;
copyFrom(peripheral);
deepCopyFrom(peripheral);
peripheral.subtickList.clear();
peripheral.resetFirstUpdate();
}
Expand Down

0 comments on commit 5f39737

Please sign in to comment.