Skip to content

Commit

Permalink
Fix InputEvent.MouseScrollingEvent crash
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Nov 18, 2023
1 parent 302dd74 commit 0ff2f7f
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import dev.architectury.event.events.client.ClientChatEvent;
import dev.architectury.event.events.client.*;
import dev.architectury.event.events.common.InteractionEvent;
import dev.architectury.hooks.forgelike.ForgeLikeClientHooks;
import dev.architectury.impl.ScreenAccessImpl;
import dev.architectury.impl.TooltipEventColorContextImpl;
import dev.architectury.impl.TooltipEventPositionContextImpl;
import dev.architectury.platform.Platform;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
Expand All @@ -43,8 +43,6 @@
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;

import java.lang.reflect.InvocationTargetException;

@OnlyIn(Dist.CLIENT)
public class EventHandlerImplClient {
@SubscribeEvent(priority = EventPriority.HIGH)
Expand Down Expand Up @@ -220,40 +218,12 @@ public static void eventRenderTooltipEvent(RenderTooltipEvent.Color event) {

@SubscribeEvent(priority = EventPriority.HIGH)
public static void eventMouseScrollEvent(ScreenEvent.MouseScrolled.Pre event) {
double deltaX, deltaY;
if (Platform.isNeoForge()) {
try {
deltaX = (double) event.getClass().getMethod("getScrollDeltaX").invoke(event);
deltaY = (double) event.getClass().getMethod("getScrollDeltaY").invoke(event);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
} else {
deltaX = event.getDeltaX();
deltaY = event.getDeltaY();
}

if (ClientScreenInputEvent.MOUSE_SCROLLED_PRE.invoker().mouseScrolled(Minecraft.getInstance(), event.getScreen(), event.getMouseX(), event.getMouseY(), deltaX, deltaY).isFalse()) {
event.setCanceled(true);
}
ForgeLikeClientHooks.preMouseScroll(event);
}

@SubscribeEvent(priority = EventPriority.HIGH)
public static void eventMouseScrollEvent(ScreenEvent.MouseScrolled.Post event) {
double deltaX, deltaY;
if (Platform.isNeoForge()) {
try {
deltaX = (double) event.getClass().getMethod("getScrollDeltaX").invoke(event);
deltaY = (double) event.getClass().getMethod("getScrollDeltaY").invoke(event);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
} else {
deltaX = event.getDeltaX();
deltaY = event.getDeltaY();
}

ClientScreenInputEvent.MOUSE_SCROLLED_POST.invoker().mouseScrolled(Minecraft.getInstance(), event.getScreen(), event.getMouseX(), event.getMouseY(), deltaX, deltaY);
ForgeLikeClientHooks.postMouseScroll(event);
}

@SubscribeEvent(priority = EventPriority.HIGH)
Expand Down Expand Up @@ -330,9 +300,7 @@ public static void eventKeyboardKeyReleasedEvent(ScreenEvent.KeyReleased.Post ev

@SubscribeEvent(priority = EventPriority.HIGH)
public static void eventInputEvent(InputEvent.MouseScrollingEvent event) {
if (ClientRawInputEvent.MOUSE_SCROLLED.invoker().mouseScrolled(Minecraft.getInstance(), event.getDeltaX(), event.getDeltaY()).isFalse()) {
event.setCanceled(true);
}
ForgeLikeClientHooks.inputMouseScroll(event);
}

@SubscribeEvent(priority = EventPriority.HIGH)
Expand All @@ -356,7 +324,7 @@ public static void eventInputEvent(InputEvent.Key event) {
public static class ModBasedEventHandler {
// @SubscribeEvent(priority = EventPriority.HIGH)
// public static void eventTextureStitchEvent(TextureStitchEvent.Post event) {
// ClientTextureStitchEvent.POST.invoker().stitch(event.getAtlas());
// ClientTextureStitchEvent.POST.invoker().stitch(event.getAtlas());
// }

@SubscribeEvent(priority = EventPriority.HIGH)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package dev.architectury.hooks.forgelike;

import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.client.event.ScreenEvent;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public class ForgeLikeClientHooks {
@ExpectPlatform
public static void preMouseScroll(ScreenEvent.MouseScrolled.Pre event) {
throw new AssertionError();
}

@ExpectPlatform
public static void postMouseScroll(ScreenEvent.MouseScrolled.Post event) {
throw new AssertionError();
}

@ExpectPlatform
public static void inputMouseScroll(InputEvent.MouseScrollingEvent event) {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package dev.architectury.hooks.forgelike.forge;

import dev.architectury.event.events.client.ClientRawInputEvent;
import dev.architectury.event.events.client.ClientScreenInputEvent;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.client.event.ScreenEvent;

public class ForgeLikeClientHooksImpl {
public static void preMouseScroll(ScreenEvent.MouseScrolled.Pre event) {
if (ClientScreenInputEvent.MOUSE_SCROLLED_PRE.invoker().mouseScrolled(Minecraft.getInstance(), event.getScreen(), event.getMouseX(), event.getMouseY(), event.getDeltaX(), event.getDeltaY()).isFalse()) {
event.setCanceled(true);
}
}

public static void postMouseScroll(ScreenEvent.MouseScrolled.Post event) {
ClientScreenInputEvent.MOUSE_SCROLLED_POST.invoker().mouseScrolled(Minecraft.getInstance(), event.getScreen(), event.getMouseX(), event.getMouseY(), event.getDeltaX(), event.getDeltaY());
}

public static void inputMouseScroll(InputEvent.MouseScrollingEvent event) {
if (ClientRawInputEvent.MOUSE_SCROLLED.invoker().mouseScrolled(Minecraft.getInstance(), event.getDeltaX(), event.getDeltaY()).isFalse()) {
event.setCanceled(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package dev.architectury.hooks.forgelike.forge;

import dev.architectury.event.events.client.ClientRawInputEvent;
import dev.architectury.event.events.client.ClientScreenInputEvent;
import net.minecraft.client.Minecraft;
import net.neoforged.neoforge.client.event.InputEvent;
import net.neoforged.neoforge.client.event.ScreenEvent;

public class ForgeLikeClientHooksImpl {
public static void preMouseScroll(ScreenEvent.MouseScrolled.Pre event) {
if (ClientScreenInputEvent.MOUSE_SCROLLED_PRE.invoker().mouseScrolled(Minecraft.getInstance(), event.getScreen(), event.getMouseX(), event.getMouseY(), event.getScrollDeltaX(), event.getScrollDeltaY()).isFalse()) {
event.setCanceled(true);
}
}

public static void postMouseScroll(ScreenEvent.MouseScrolled.Post event) {
ClientScreenInputEvent.MOUSE_SCROLLED_POST.invoker().mouseScrolled(Minecraft.getInstance(), event.getScreen(), event.getMouseX(), event.getMouseY(), event.getScrollDeltaX(), event.getScrollDeltaY());
}

public static void inputMouseScroll(InputEvent.MouseScrollingEvent event) {
if (ClientRawInputEvent.MOUSE_SCROLLED.invoker().mouseScrolled(Minecraft.getInstance(), event.getScrollDeltaX(), event.getScrollDeltaY()).isFalse()) {
event.setCanceled(true);
}
}
}

0 comments on commit 0ff2f7f

Please sign in to comment.