|
| 1 | +package dynamic_fps.impl.feature.state; |
| 2 | + |
| 3 | +import dynamic_fps.impl.config.DynamicFPSConfig; |
| 4 | +import dynamic_fps.impl.config.option.IgnoreInitialClick; |
| 5 | +import net.minecraft.client.Minecraft; |
| 6 | +import org.lwjgl.glfw.GLFW; |
| 7 | +import org.lwjgl.glfw.GLFWMouseButtonCallback; |
| 8 | +import org.lwjgl.glfw.GLFWWindowFocusCallback; |
| 9 | + |
| 10 | +import java.time.Instant; |
| 11 | + |
| 12 | +public class ClickIgnoreHandler { |
| 13 | + private final long address; |
| 14 | + private long focusedAt; |
| 15 | + |
| 16 | + private final GLFWWindowFocusCallback previousFocusCallback; |
| 17 | + private final GLFWMouseButtonCallback previousClickCallback; |
| 18 | + |
| 19 | + private static final Minecraft MINECRAFT = Minecraft.getInstance(); |
| 20 | + |
| 21 | + public ClickIgnoreHandler(long address) { |
| 22 | + this.address = address; |
| 23 | + |
| 24 | + this.previousFocusCallback = GLFW.glfwSetWindowFocusCallback(this.address, this::onFocusChanged); |
| 25 | + this.previousClickCallback = GLFW.glfwSetMouseButtonCallback(this.address, this::onMouseClicked); |
| 26 | + } |
| 27 | + |
| 28 | + public static boolean isFeatureActive() { |
| 29 | + return DynamicFPSConfig.INSTANCE.ignoreInitialClick() != IgnoreInitialClick.DISABLED; |
| 30 | + } |
| 31 | + |
| 32 | + private boolean shouldIgnoreClick() { |
| 33 | + IgnoreInitialClick config = DynamicFPSConfig.INSTANCE.ignoreInitialClick(); |
| 34 | + |
| 35 | + if (config == IgnoreInitialClick.DISABLED) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + if (config == IgnoreInitialClick.IN_WORLD && MINECRAFT.screen != null) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + return this.focusedAt + 20 >= Instant.now().toEpochMilli(); |
| 44 | + } |
| 45 | + |
| 46 | + private void onFocusChanged(long address, boolean focused) { |
| 47 | + if (this.isCurrentWindow(address) && focused) { |
| 48 | + this.focusedAt = Instant.now().toEpochMilli(); |
| 49 | + } |
| 50 | + |
| 51 | + if (this.previousFocusCallback != null) { |
| 52 | + this.previousFocusCallback.invoke(address, focused); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private void onMouseClicked(long window, int button, int action, int mods) { |
| 57 | + if (this.isCurrentWindow(window) && shouldIgnoreClick()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (this.previousClickCallback != null) { |
| 62 | + this.previousClickCallback.invoke(window, button, action, mods); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private boolean isCurrentWindow(long address) { |
| 67 | + return address == this.address; |
| 68 | + } |
| 69 | +} |
0 commit comments