diff --git a/src/main/java/top/fpsmaster/forge/mixin/MixinGuiScreen.java b/src/main/java/top/fpsmaster/forge/mixin/MixinGuiScreen.java index a51299fb..6ee6ed72 100644 --- a/src/main/java/top/fpsmaster/forge/mixin/MixinGuiScreen.java +++ b/src/main/java/top/fpsmaster/forge/mixin/MixinGuiScreen.java @@ -18,6 +18,7 @@ import top.fpsmaster.features.impl.interfaces.BetterScreen; import top.fpsmaster.utils.math.anim.AnimMath; import top.fpsmaster.utils.render.effects.Blur; +import top.fpsmaster.utils.system.OptifineUtil; import java.awt.*; import java.io.IOException; @@ -54,53 +55,71 @@ public void handleKeyboardInput() throws IOException { mc.dispatchKeypresses(); } - @Unique - float arch$alpha = 0; - @Unique - int v1_8_9$iteration = 0; - - /** - * @author SuperSkidder - * @reason 自定义背景 - */ - @Overwrite - public void drawWorldBackground(int tint) { - if (mc.theWorld != null) { - if (BetterScreen.using) { - if (BetterScreen.useBG.getValue()) { - if (BetterScreen.backgroundAnimation.getValue()) { - arch$alpha = (float) AnimMath.base(arch$alpha, 170, 0.2f); - } else { - arch$alpha = 170; - } - this.drawGradientRect(0, 0, this.width, this.height, Colors.alpha(Colors.toColor(-1072689136), ((int) arch$alpha)).getRGB(), Colors.alpha(Colors.toColor(-804253680), ((int) arch$alpha)).getRGB()); +@Unique +float arch$alpha = 0; + +@Unique +int v1_8_9$iteration = 0; + +/** + * @author SuperSkidder + * @reason 自定义背景 + */ +@Overwrite +public void drawWorldBackground(int tint) { + if (mc.theWorld != null) { + if (BetterScreen.using) { + if (BetterScreen.useBG.getValue()) { + if (BetterScreen.backgroundAnimation.getValue()) { + arch$alpha = (float) AnimMath.base(arch$alpha, 170, 0.2f); } else { - GlStateManager.disableBlend(); - GlStateManager.enableAlpha(); - GlStateManager.enableTexture2D(); - } - if (BetterScreen.blur.getValue()) { - v1_8_9$iteration = Math.min(++v1_8_9$iteration, 3); - Blur.area(0, 0, width, height, 1, new Color(255, 255, 255), v1_8_9$iteration, v1_8_9$iteration); + arch$alpha = 170; } + + this.drawGradientRect( + 0, + 0, + this.width, + this.height, + Colors.alpha(Colors.toColor(-1072689136), ((int) arch$alpha)).getRGB(), + Colors.alpha(Colors.toColor(-804253680), ((int) arch$alpha)).getRGB() + ); } else { - this.drawGradientRect(0, 0, this.width, this.height, -1072689136, -804253680); + GlStateManager.disableBlend(); + GlStateManager.enableAlpha(); + GlStateManager.enableTexture2D(); + } + + if (BetterScreen.blur.getValue() && !OptifineUtil.isFastRender()) { + v1_8_9$iteration = Math.min(++v1_8_9$iteration, 3); + Blur.area( + 0, + 0, + width, + height, + 1, + new Color(255, 255, 255), + v1_8_9$iteration, + v1_8_9$iteration + ); } } else { - this.drawBackground(tint); + this.drawGradientRect(0, 0, this.width, this.height, -1072689136, -804253680); } + } else { + this.drawBackground(tint); } +} - @Inject(method = "sendChatMessage(Ljava/lang/String;Z)V", at = @At("HEAD"), cancellable = true) - public void sendChat(String msg, boolean addToChat, CallbackInfo ci) { - EventSendChatMessage eventSendChatMessage = new EventSendChatMessage(msg); - EventDispatcher.dispatchEvent(eventSendChatMessage); - if (eventSendChatMessage.isCanceled()) { - ci.cancel(); - } +@Inject(method = "sendChatMessage(Ljava/lang/String;Z)V", at = @At("HEAD"), cancellable = true) +public void sendChat(String msg, boolean addToChat, CallbackInfo ci) { + EventSendChatMessage eventSendChatMessage = new EventSendChatMessage(msg); + EventDispatcher.dispatchEvent(eventSendChatMessage); + if (eventSendChatMessage.isCanceled()) { + ci.cancel(); } } - +} diff --git a/src/main/java/top/fpsmaster/utils/input/raw/RawInputMod.java b/src/main/java/top/fpsmaster/utils/input/raw/RawInputMod.java index a37e6eb0..bea5d3a7 100644 --- a/src/main/java/top/fpsmaster/utils/input/raw/RawInputMod.java +++ b/src/main/java/top/fpsmaster/utils/input/raw/RawInputMod.java @@ -8,207 +8,210 @@ import top.fpsmaster.modules.logger.ClientLogger; import java.io.File; +import java.lang.reflect.Constructor; import java.util.ArrayList; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; public class RawInputMod { public static final AtomicInteger dx = new AtomicInteger(0); public static final AtomicInteger dy = new AtomicInteger(0); + private static final AtomicBoolean acceptingInput = new AtomicBoolean(false); + + private final Object lock = new Object(); + private Thread inputThread; - private static ArrayList mouses = new ArrayList<>(); - private static Controller[] controllers; - private static boolean hasDx8 = false; - //**Periodically poll activeMouse** - //26.5.3 - private static Mouse activeMouse = null; + private final ArrayList mice = new ArrayList<>(); + private Controller[] controllers; - private static long lastFullScanTime = 0L; + private boolean hasDx8 = false; + private Mouse activeMouse = null; + + private long lastFullScanTime = 0L; private static final long FULL_SCAN_INTERVAL = 1000L; public void start() { - try { - if (inputThread != null && inputThread.isAlive()) { - return; - } - - Minecraft.getMinecraft().mouseHelper = new RawMouseHelper(); + synchronized (lock) { + try { + if (inputThread != null && inputThread.isAlive()) { + return; + } - String environment; - if (checkLibrary("jinput-dx8")) { - environment = "DirectInputEnvironmentPlugin"; - hasDx8 = true; - } else if (checkLibrary("jinput-raw")) { - environment = "DirectAndRawInputEnvironmentPlugin"; - } else { - return; - } + String environment; + if (checkLibrary("jinput-dx8")) { + environment = "DirectInputEnvironmentPlugin"; + hasDx8 = true; + } else if (checkLibrary("jinput-raw")) { + environment = "DirectAndRawInputEnvironmentPlugin"; + hasDx8 = false; + } else { + return; + } - Class aClass = Class.forName("net.java.games.input." + environment); - aClass.getDeclaredConstructor().setAccessible(true); - controllers = (((ControllerEnvironment) aClass.newInstance())).getControllers(); + Class clazz = Class.forName("net.java.games.input." + environment); + Constructor constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible(true); - inputThread = new Thread(() -> { - try { - while (!Thread.currentThread().isInterrupted()) { - initMouses(); + ControllerEnvironment controllerEnvironment = (ControllerEnvironment) constructor.newInstance(); + controllers = controllerEnvironment.getControllers(); - if (Minecraft.getMinecraft().currentScreen != null) { - dx.set(0); - dy.set(0); + clearDeltas(); + acceptingInput.set(false); - pollActiveOrAll(false); + Minecraft.getMinecraft().mouseHelper = new RawMouseHelper(); - try { - Thread.sleep(5L); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } + inputThread = new Thread(this::inputLoop, "RawInputThread"); + inputThread.setDaemon(true); + inputThread.start(); + } catch (Exception e) { + ClientLogger.error("Failed to start raw input"); + } + } + } - continue; - } + private void inputLoop() { + try { + while (!Thread.currentThread().isInterrupted()) { + initMice(); - int totalDx = 0; - int totalDy = 0; + int totalDx = 0; + int totalDy = 0; - long now = System.currentTimeMillis(); - boolean needFullScan = activeMouse == null || now - lastFullScanTime >= FULL_SCAN_INTERVAL; + if (!mice.isEmpty()) { + long now = System.currentTimeMillis(); + boolean fullScan = activeMouse == null || now - lastFullScanTime >= FULL_SCAN_INTERVAL; - if (needFullScan) { - lastFullScanTime = now; + if (fullScan) { + lastFullScanTime = now; - for (Mouse mouse : mouses) { - mouse.poll(); + for (Mouse mouse : mice) { + mouse.poll(); - int mouseDx = (int) mouse.getX().getPollData(); - int mouseDy = (int) mouse.getY().getPollData(); + int mouseDx = (int) mouse.getX().getPollData(); + int mouseDy = (int) mouse.getY().getPollData(); - if (mouseDx != 0 || mouseDy != 0) { - activeMouse = mouse; - } + if (mouseDx != 0 || mouseDy != 0) { + activeMouse = mouse; + } - totalDx += mouseDx; - totalDy += mouseDy; + totalDx += mouseDx; + totalDy += mouseDy; - if (hasDx8) { - break; - } + if (hasDx8) { + break; } - } else { - activeMouse.poll(); - - totalDx = (int) activeMouse.getX().getPollData(); - totalDy = (int) activeMouse.getY().getPollData(); } + } else { + activeMouse.poll(); - if (totalDx != 0) { - dx.addAndGet(totalDx); - } + totalDx = (int) activeMouse.getX().getPollData(); + totalDy = (int) activeMouse.getY().getPollData(); + } + } - if (totalDy != 0) { - dy.addAndGet(totalDy); - } + if (acceptingInput.get()) { + if (totalDx != 0) { + dx.addAndGet(totalDx); + } - try { - Thread.sleep(1L); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } + if (totalDy != 0) { + dy.addAndGet(totalDy); } - } catch (Exception e) { - ClientLogger.error("Failed to start raw input"); - } - }); - inputThread.setName("inputThread"); - inputThread.setDaemon(true); - inputThread.start(); + Thread.sleep(1L); + } else { + clearDeltas(); + Thread.sleep(5L); + } + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } catch (Exception e) { - ClientLogger.error("Failed to start raw input"); + ClientLogger.error("Failed to poll raw input"); } } - private static void initMouses() { - if (controllers != null && mouses.isEmpty()) { - for (Controller controller : controllers) { - if (controller.getType() == Controller.Type.MOUSE) { - RawInputMod.mouses.add((Mouse) controller); + private void initMice() { + if (controllers == null || !mice.isEmpty()) { + return; + } - if (RawInputMod.hasDx8) { - break; - } + for (Controller controller : controllers) { + if (controller.getType() == Controller.Type.MOUSE) { + mice.add((Mouse) controller); + + if (hasDx8) { + break; } } } } - private static void pollActiveOrAll(boolean readMovement) { - if (activeMouse != null) { - activeMouse.poll(); + public void stop() { + synchronized (lock) { + try { + if (inputThread != null && inputThread.isAlive()) { + inputThread.interrupt(); - if (readMovement) { - activeMouse.getX().getPollData(); - activeMouse.getY().getPollData(); - } + if (Thread.currentThread() != inputThread) { + inputThread.join(200L); + } + } - return; - } + inputThread = null; - for (Mouse mouse : mouses) { - mouse.poll(); + mice.clear(); + controllers = null; + activeMouse = null; + lastFullScanTime = 0L; + hasDx8 = false; - if (readMovement) { - mouse.getX().getPollData(); - mouse.getY().getPollData(); - } + acceptingInput.set(false); + clearDeltas(); - if (hasDx8) { - break; + Minecraft.getMinecraft().mouseHelper = new MouseHelper(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (Exception e) { + ClientLogger.error("Failed to stop raw input"); } } } - public void stop() { - try { - if (inputThread != null && inputThread.isAlive()) { - inputThread.interrupt(); - inputThread.join(200L); - } - - inputThread = null; - mouses = new ArrayList<>(); - controllers = null; - activeMouse = null; - lastFullScanTime = 0L; - hasDx8 = false; + public static void setAcceptingInput(boolean value) { + acceptingInput.set(value); - dx.set(0); - dy.set(0); - - Minecraft.getMinecraft().mouseHelper = new MouseHelper(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } catch (Exception e) { - ClientLogger.error("Failed to stop raw input"); + if (!value) { + clearDeltas(); } } + public static void clearDeltas() { + dx.set(0); + dy.set(0); + } + public static boolean checkLibrary(String name) { try { String path = System.getProperty("java.library.path"); - if (path != null) { - String mapped = System.mapLibraryName(name); - String[] paths = path.split(File.pathSeparator); - for (String libPath : paths) { - if (new File(libPath, mapped).exists()) { - return true; - } + if (path == null) { + return false; + } + + String mapped = System.mapLibraryName(name); + String[] paths = path.split(File.pathSeparator); + + for (String libPath : paths) { + if (new File(libPath, mapped).exists()) { + return true; } } + return false; } catch (Exception e) { return false; } } -} +} \ No newline at end of file