Skip to content

Commit

Permalink
modify InputEventHandler to remove this.mc
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Sep 23, 2024
1 parent 3168d9b commit 70d5baf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 75 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ author = masa
mod_file_name = malilib-fabric

# Current mod version
mod_version = 0.20.3-sakura.4
mod_version = 0.20.3-sakura.5

# Minecraft, Fabric Loader and API and mappings versions
minecraft_version_out = 1.21
Expand Down
52 changes: 11 additions & 41 deletions src/main/java/fi/dy/masa/malilib/event/InputEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nonnull;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.jetbrains.annotations.ApiStatus;
Expand All @@ -27,18 +28,14 @@ public class InputEventHandler implements IKeybindManager, IInputManager
{
private static final InputEventHandler INSTANCE = new InputEventHandler();

private final MinecraftClient mc;
private final Multimap<Integer, IKeybind> hotkeyMap = ArrayListMultimap.create();
private final List<KeybindCategory> allKeybinds = new ArrayList<>();
private final List<IKeybindProvider> keybindProviders = new ArrayList<>();
private final List<IKeyboardInputHandler> keyboardHandlers = new ArrayList<>();
private final List<IMouseInputHandler> mouseHandlers = new ArrayList<>();
private double mouseWheelDeltaSum;

private InputEventHandler()
{
this.mc = MinecraftClient.getInstance();
}
private InputEventHandler() { }

public static IKeybindManager getKeybindManager()
{
Expand Down Expand Up @@ -76,11 +73,6 @@ public List<KeybindCategory> getKeybindCategories()
@Override
public void updateUsedKeys()
{
if (this.mc == null)
{
return;
}

this.hotkeyMap.clear();

for (IKeybindProvider handler : this.keybindProviders)
Expand Down Expand Up @@ -141,15 +133,9 @@ public void unregisterMouseInputHandler(IMouseInputHandler handler)
}

@ApiStatus.Internal
public boolean onKeyInput(int keyCode, int scanCode, int modifiers, int action)
public boolean onKeyInput(int keyCode, int scanCode, int modifiers, int action, @Nonnull MinecraftClient mc)
{
boolean eventKeyState;

if (this.mc == null)
{
return false;
}
eventKeyState = action != GLFW.GLFW_RELEASE;
boolean eventKeyState = action != GLFW.GLFW_RELEASE;

// Update the cached pressed keys status
KeybindMulti.onKeyInputPre(keyCode, scanCode, modifiers, action);
Expand All @@ -172,14 +158,10 @@ public boolean onKeyInput(int keyCode, int scanCode, int modifiers, int action)
}

@ApiStatus.Internal
public boolean onMouseClick(int mouseX, int mouseY, int eventButton, int action)
public boolean onMouseClick(int mouseX, int mouseY, int eventButton, int action, @Nonnull MinecraftClient mc)
{
boolean cancel = false;

if (this.mc == null)
{
return false;
}
if (eventButton != -1)
{
boolean eventButtonState = action == GLFW.GLFW_PRESS;
Expand Down Expand Up @@ -216,24 +198,16 @@ private void printInputCancellationDebugMessage(Object handler)
}

@ApiStatus.Internal
public boolean onMouseScroll(final int mouseX, final int mouseY, final double xOffset, final double yOffset)
public boolean onMouseScroll(final int mouseX, final int mouseY, final double xOffset, final double yOffset, @Nonnull MinecraftClient mc)
{
boolean discrete;
double sensitivity;
double amount;

if (this.mc == null)
{
return false;
}
discrete = this.mc.options.getDiscreteMouseScroll().getValue();
sensitivity = this.mc.options.getMouseWheelSensitivity().getValue();
amount = (discrete ? Math.signum(yOffset) : yOffset) * sensitivity;
boolean discrete = mc.options.getDiscreteMouseScroll().getValue();
double sensitivity = mc.options.getMouseWheelSensitivity().getValue();
double amount = (discrete ? Math.signum(yOffset) : yOffset) * sensitivity;

if (MaLiLibConfigs.Debug.MOUSE_SCROLL_DEBUG.getBooleanValue())
{
int time = (int) (System.currentTimeMillis() & 0xFFFF);
int tick = this.mc.world != null ? (int) (this.mc.world.getTime() & 0xFFFF) : 0;
int tick = mc.world != null ? (int) (mc.world.getTime() & 0xFFFF) : 0;
String timeStr = String.format("time: %04X, tick: %04X", time, tick);
MaLiLib.logger.info("{} - xOffset: {}, yOffset: {}, discrete: {}, sensitivity: {}, amount: {}",
timeStr, xOffset, yOffset, discrete, sensitivity, amount);
Expand Down Expand Up @@ -268,12 +242,8 @@ public boolean onMouseScroll(final int mouseX, final int mouseY, final double xO
}

@ApiStatus.Internal
public void onMouseMove(final int mouseX, final int mouseY)
public void onMouseMove(final int mouseX, final int mouseY, @Nonnull MinecraftClient mc)
{
if (this.mc == null)
{
return;
}
if (this.mouseHandlers.isEmpty() == false)
{
for (IMouseInputHandler handler : this.mouseHandlers)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fi/dy/masa/malilib/hotkeys/KeybindMulti.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ else if (pressedLast == false && this.heldTime == 0)
if (this.keyCodes.contains(KeyCodes.KEY_F3))
{
// Prevent the debug GUI from opening after the F3 key is released
((IF3KeyStateSetter) MinecraftClient.getInstance().keyboard).setF3KeyState(true);
((IF3KeyStateSetter) MinecraftClient.getInstance().keyboard).malilib$setF3KeyState(true);
}

KeyAction activateOn = this.settings.getActivateOn();
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/fi/dy/masa/malilib/mixin/MixinKeyboard.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package fi.dy.masa.malilib.mixin;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.client.Keyboard;
import net.minecraft.client.MinecraftClient;

import fi.dy.masa.malilib.event.InputEventHandler;
import fi.dy.masa.malilib.util.IF3KeyStateSetter;

@Mixin(Keyboard.class)
public abstract class MixinKeyboard implements IF3KeyStateSetter
{
@Shadow
private boolean switchF3State;
@Shadow private boolean switchF3State;
@Shadow @Final private MinecraftClient client;

@Override
public void setF3KeyState(boolean value)
public void malilib$setF3KeyState(boolean value)
{
this.switchF3State = value;
}
Expand All @@ -25,7 +28,7 @@ public void setF3KeyState(boolean value)
at = @At(value = "FIELD", target = "Lnet/minecraft/client/Keyboard;debugCrashStartTime:J", ordinal = 0))
private void onKeyboardInput(long windowPointer, int key, int scanCode, int action, int modifiers, CallbackInfo ci)
{
if (((InputEventHandler) InputEventHandler.getInputManager()).onKeyInput(key, scanCode, modifiers, action))
if (((InputEventHandler) InputEventHandler.getInputManager()).onKeyInput(key, scanCode, modifiers, action, this.client))
{
ci.cancel();
}
Expand Down
45 changes: 18 additions & 27 deletions src/main/java/fi/dy/masa/malilib/mixin/MixinMouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,40 @@ public abstract class MixinMouse
at = @At(value = "FIELD", target = "Lnet/minecraft/client/Mouse;hasResolutionChanged:Z", ordinal = 0))
private void hookOnMouseMove(long handle, double xpos, double ypos, CallbackInfo ci)
{
if (this.client != null)
{
Window window = this.client.getWindow();
int mouseX = (int) (((Mouse) (Object) this).getX() * (double) window.getScaledWidth() / (double) window.getWidth());
int mouseY = (int) (((Mouse) (Object) this).getY() * (double) window.getScaledHeight() / (double) window.getHeight());
Window window = this.client.getWindow();
int mouseX = (int) (((Mouse) (Object) this).getX() * (double) window.getScaledWidth() / (double) window.getWidth());
int mouseY = (int) (((Mouse) (Object) this).getY() * (double) window.getScaledHeight() / (double) window.getHeight());

((InputEventHandler) InputEventHandler.getInputManager()).onMouseMove(mouseX, mouseY);
}
((InputEventHandler) InputEventHandler.getInputManager()).onMouseMove(mouseX, mouseY, this.client);
}

@Inject(method = "onMouseScroll", cancellable = true,
at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;currentScreen:Lnet/minecraft/client/gui/screen/Screen;", ordinal = 0))
private void hookOnMouseScroll(long handle, double xOffset, double yOffset, CallbackInfo ci)
{
if (this.client != null)
{
Window window = this.client.getWindow();
int mouseX = (int) (((Mouse) (Object) this).getX() * (double) window.getScaledWidth() / (double) window.getWidth());
int mouseY = (int) (((Mouse) (Object) this).getY() * (double) window.getScaledHeight() / (double) window.getHeight());
Window window = this.client.getWindow();
int mouseX = (int) (((Mouse) (Object) this).getX() * (double) window.getScaledWidth() / (double) window.getWidth());
int mouseY = (int) (((Mouse) (Object) this).getY() * (double) window.getScaledHeight() / (double) window.getHeight());

if (((InputEventHandler) InputEventHandler.getInputManager()).onMouseScroll(mouseX, mouseY, xOffset, yOffset))
{
this.eventDeltaHorizontalWheel = 0.0;
this.eventDeltaVerticalWheel = 0.0;
ci.cancel();
}
if (((InputEventHandler) InputEventHandler.getInputManager()).onMouseScroll(mouseX, mouseY, xOffset, yOffset, this.client))
{
this.eventDeltaHorizontalWheel = 0.0;
this.eventDeltaVerticalWheel = 0.0;
ci.cancel();
}
}

@Inject(method = "onMouseButton", cancellable = true,
at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;IS_SYSTEM_MAC:Z", ordinal = 0))
private void hookOnMouseClick(long handle, final int button, final int action, int mods, CallbackInfo ci)
{
if (this.client != null)
{
Window window = this.client.getWindow();
int mouseX = (int) (((Mouse) (Object) this).getX() * (double) window.getScaledWidth() / (double) window.getWidth());
int mouseY = (int) (((Mouse) (Object) this).getY() * (double) window.getScaledHeight() / (double) window.getHeight());
Window window = this.client.getWindow();
int mouseX = (int) (((Mouse) (Object) this).getX() * (double) window.getScaledWidth() / (double) window.getWidth());
int mouseY = (int) (((Mouse) (Object) this).getY() * (double) window.getScaledHeight() / (double) window.getHeight());

if (((InputEventHandler) InputEventHandler.getInputManager()).onMouseClick(mouseX, mouseY, button, action))
{
ci.cancel();
}
if (((InputEventHandler) InputEventHandler.getInputManager()).onMouseClick(mouseX, mouseY, button, action, this.client))
{
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface IF3KeyStateSetter
{
void setF3KeyState(boolean value);
void malilib$setF3KeyState(boolean value);
}

0 comments on commit 70d5baf

Please sign in to comment.