Skip to content

Commit

Permalink
[PlaybackController] Add events that fire when updating VirtualInput
Browse files Browse the repository at this point in the history
- Add null check to copyFrom
  • Loading branch information
ScribbleTAS committed Feb 27, 2024
1 parent dc9334d commit 7d7bab2
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 55 deletions.
78 changes: 70 additions & 8 deletions src/main/java/com/minecrafttas/tasmod/events/EventClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.minecrafttas.tasmod.events;

import com.minecrafttas.mctcommon.events.EventListenerRegistry.EventBase;
import com.minecrafttas.tasmod.virtual.VirtualCameraAngle;
import com.minecrafttas.tasmod.virtual.VirtualInput.VirtualCameraAngleInput;
import com.minecrafttas.tasmod.virtual.VirtualInput.VirtualKeyboardInput;
import com.minecrafttas.tasmod.virtual.VirtualInput.VirtualMouseInput;
import com.minecrafttas.tasmod.virtual.VirtualKeyboard;
import com.minecrafttas.tasmod.virtual.VirtualMouse;

import net.minecraft.client.Minecraft;

/**
Expand All @@ -9,39 +16,94 @@
* @author Scribble
*/
public interface EventClient {

/**
* Fired when the hotbar is drawn on screen
*/
@FunctionalInterface
public static interface EventDrawHotbar extends EventBase{
public static interface EventDrawHotbar extends EventBase {
/**
* Fired when the hotbar is drawn on screen
*/
public void onDrawHotbar();
}

/**
* Fired at the end of a client tick
*/
@FunctionalInterface
public static interface EventClientTickPost extends EventBase{
public static interface EventClientTickPost extends EventBase {

/**
* Fired at the end of a client tick
*/
public void onClientTickPost(Minecraft mc);
}

/**
* Fired when the tickrate changes on the client side
*/
@FunctionalInterface
public static interface EventClientTickrateChange extends EventBase{
public static interface EventClientTickrateChange extends EventBase {

/**
* Fired at the end of a client tick
*/
public void onClientTickrateChange(float tickrate);
}


/**
* Fired when the {@link VirtualKeyboardInput#currentKeyboard} is updated
*
* @see VirtualKeyboardInput#nextKeyboardTick()
*/
@FunctionalInterface
public static interface EventVirtualKeyboardTick extends EventBase {

/**
* Fired when the {@link VirtualKeyboard} ticks
*
* @param vkeyboard The {@link VirtualKeyboardInput#nextKeyboard} that is supposed to be pressed
* @returns The redirected keyboard
* @see VirtualKeyboardInput#nextKeyboardTick()
*/
public VirtualKeyboard onVirtualKeyboardTick(VirtualKeyboard vkeyboard);
}

/**
* Fired when the {@link VirtualMouseInput#currentMouse} is updated
*
* @see VirtualMouseInput#nextMouseTick()
*/
@FunctionalInterface
public static interface EventVirtualMouseTick extends EventBase {

/**
* Fired when the {@link VirtualMouseInput#currentMouse} is updated
*
* @param vmouse The {@link VirtualMouseInput#nextMouse} that is supposed to be pressed
* @returns The redirected mouse
* @see VirtualMouseInput#nextMouseTick()
*/
public VirtualMouse onVirtualMouseTick(VirtualMouse vmouse);
}

/**
* Fired when the {@link VirtualCameraAngleInput#currentCameraAngle} is updated
*
* @see VirtualCameraAngleInput#nextCameraTick()
*/
@FunctionalInterface
public static interface EventVirtualCameraAngleTick extends EventBase {

/**
* Fired when the {@link VirtualCameraAngleInput#currentCameraAngle} is updated
*
* @param vcamera The {@link VirtualCameraAngleInput#nextCameraAngle}
* @returns The redirected cameraAngle
* @see VirtualCameraAngleInput#nextCameraTick()
*/
public VirtualCameraAngle onVirtualCameraTick(VirtualCameraAngle vcamera);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import static com.minecrafttas.tasmod.networking.TASmodPackets.PLAYBACK_TELEPORT;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.HashMap;
Expand All @@ -31,15 +30,18 @@
import com.minecrafttas.mctcommon.server.interfaces.PacketID;
import com.minecrafttas.tasmod.TASmod;
import com.minecrafttas.tasmod.TASmodClient;
import com.minecrafttas.tasmod.events.EventClient.EventVirtualCameraAngleTick;
import com.minecrafttas.tasmod.events.EventClient.EventVirtualKeyboardTick;
import com.minecrafttas.tasmod.events.EventClient.EventVirtualMouseTick;
import com.minecrafttas.tasmod.monitoring.DesyncMonitoring;
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
import com.minecrafttas.tasmod.networking.TASmodPackets;
import com.minecrafttas.tasmod.util.LoggerMarkers;
import com.minecrafttas.tasmod.util.Scheduler.Task;
import com.minecrafttas.tasmod.virtual.VirtualCameraAngle;
import com.minecrafttas.tasmod.virtual.VirtualInput;
import com.minecrafttas.tasmod.virtual.VirtualKeyboard;
import com.minecrafttas.tasmod.virtual.VirtualMouse;
import com.minecrafttas.tasmod.virtual.VirtualCameraAngle;
import com.mojang.realmsclient.gui.ChatFormatting;
import com.mojang.realmsclient.util.Pair;

Expand Down Expand Up @@ -68,7 +70,7 @@
* @author Scribble
*
*/
public class PlaybackControllerClient implements ClientPacketHandler {
public class PlaybackControllerClient implements ClientPacketHandler, EventVirtualKeyboardTick, EventVirtualMouseTick, EventVirtualCameraAngleTick{

/**
* The current state of the controller.
Expand Down Expand Up @@ -340,54 +342,21 @@ public TASstate getState() {
// These act as an input and output, depending if a recording or a playback is
// running

/**
* Adds or retrives a keyboard to the input container, depends on whether a
* recording or a playback is running
*
* @param keyboard Keyboard to add
* @return Keyboard to retrieve
*/
public VirtualKeyboard addKeyboardToContainer(VirtualKeyboard keyboard) {
if (state == TASstate.RECORDING) {
this.keyboard = keyboard.clone();
} else if (state == TASstate.PLAYBACK) {
keyboard = this.keyboard.clone();
}
return keyboard;
@Override
public VirtualCameraAngle onVirtualCameraTick(VirtualCameraAngle vcamera) {
return null;
}

/**
* Adds or retrives a mouse to the input container, depends on whether a
* recording or a playback is running
*
* @param mouse Mouse to add
* @return Mouse to retrieve
*/
public VirtualMouse addMouseToContainer(VirtualMouse mouse) {
if (state == TASstate.RECORDING) {
this.mouse = mouse.clone();
} else if (state == TASstate.PLAYBACK) {
mouse = this.mouse.clone();
}
return mouse;
@Override
public VirtualMouse onVirtualMouseTick(VirtualMouse vmouse) {
return null;
}

/**
* Adds or retrives the angle of the camera to the input container, depends on
* whether a recording or a playback is running
*
* @param subticks Subticks to add
* @return Subticks to retrieve
*/
public VirtualCameraAngle addSubticksToContainer(VirtualCameraAngle subticks) {
if (state == TASstate.RECORDING) {
this.subticks = subticks.clone();
} else if (state == TASstate.PLAYBACK) {
subticks = this.subticks.clone();
}
return subticks;
@Override
public VirtualKeyboard onVirtualKeyboardTick(VirtualKeyboard vkeyboard) {
return null;
}

/**
* Updates the input container.<br>
* <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public void getStates(List<VirtualCameraAngle> reference) {
* @param camera The camera to move from
*/
public void copyFrom(VirtualCameraAngle camera) {
if(camera == null)
return;
this.pitch = camera.pitch;
this.yaw = camera.yaw;
this.subtickList.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.lwjgl.input.Mouse;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.minecrafttas.mctcommon.events.EventListenerRegistry;
import com.minecrafttas.tasmod.events.EventClient.EventVirtualCameraAngleTick;
import com.minecrafttas.tasmod.events.EventClient.EventVirtualMouseTick;
import com.minecrafttas.tasmod.mixin.playbackhooks.MixinEntityRenderer;
import com.minecrafttas.tasmod.mixin.playbackhooks.MixinMinecraft;
import com.minecrafttas.tasmod.util.Ducks;
Expand Down Expand Up @@ -403,6 +406,7 @@ public void updateNextMouse(int keycode, boolean keystate, int scrollwheel, int
* @see MixinMinecraft#playback_injectRunTickMouse(org.spongepowered.asm.mixin.injection.callback.CallbackInfo)
*/
public void nextMouseTick() {
nextMouse.copyFrom((VirtualMouse) EventListenerRegistry.fireEvent(EventVirtualMouseTick.class, nextMouse));
currentMouse.getVirtualEvents(nextMouse, mouseEventQueue);
currentMouse.copyFrom(nextMouse);
}
Expand Down Expand Up @@ -588,6 +592,7 @@ public void updateNextCameraAngle(float pitchDelta, float yawDelta) {
* @see MixinEntityRenderer#runUpdate(float)
*/
public void nextCameraTick() {
nextCameraAngle.copyFrom((VirtualCameraAngle) EventListenerRegistry.fireEvent(EventVirtualCameraAngleTick.class, nextCameraAngle));
nextCameraAngle.getStates(cameraAngleInterpolationStates);
currentCameraAngle.copyFrom(nextCameraAngle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.google.common.collect.Maps;
import com.minecrafttas.tasmod.TASmodClient;

import net.minecraft.client.Minecraft;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ public VirtualKeyboard clone() {

@Override
public void copyFrom(VirtualKeyboard keyboard) {
if(keyboard == null)
return;
super.copyFrom(keyboard);
charList.clear();
charList.addAll(keyboard.charList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ public VirtualMouse clone() {

@Override
public void copyFrom(VirtualMouse mouse) {
if(mouse==null)
return;
super.copyFrom(mouse);
this.scrollWheel = mouse.scrollWheel;
this.cursorX = mouse.cursorX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public boolean equals(Object obj) {
* @param peripheral The peripheral to move from
*/
protected void copyFrom(T peripheral) {
if(peripheral == null)
return;
this.pressedKeys.clear();
this.pressedKeys.addAll(peripheral.pressedKeys);
peripheral.subtickList.clear();
Expand Down

0 comments on commit 7d7bab2

Please sign in to comment.