From 28dd76ebbf239a2cf01a57bc0b180b57deb0964b Mon Sep 17 00:00:00 2001
From: Harley O'Connor
Date: Thu, 25 Nov 2021 20:03:05 +0000
Subject: [PATCH] Changed control flow of post mouse events to allow listening
to events handled by vanilla. (#8133)
---
.../minecraft/client/MouseHandler.java.patch | 14 ++--
.../client/ForgeHooksClient.java | 16 ++--
.../client/event/GuiScreenEvent.java | 80 +++++++++++++++++--
3 files changed, 91 insertions(+), 19 deletions(-)
mode change 100644 => 100755 patches/minecraft/net/minecraft/client/MouseHandler.java.patch
mode change 100644 => 100755 src/main/java/net/minecraftforge/client/ForgeHooksClient.java
mode change 100644 => 100755 src/main/java/net/minecraftforge/client/event/GuiScreenEvent.java
diff --git a/patches/minecraft/net/minecraft/client/MouseHandler.java.patch b/patches/minecraft/net/minecraft/client/MouseHandler.java.patch
old mode 100644
new mode 100755
index 230c2fe4a6..b0de2cf98f
--- a/patches/minecraft/net/minecraft/client/MouseHandler.java.patch
+++ b/patches/minecraft/net/minecraft/client/MouseHandler.java.patch
@@ -8,21 +8,25 @@
boolean[] aboolean = new boolean[]{false};
if (this.f_91503_.m_91265_() == null) {
if (this.f_91503_.f_91080_ == null) {
-@@ -82,11 +_,15 @@
+@@ -82,11 +_,19 @@
if (flag) {
screen.m_169415_();
Screen.m_96579_(() -> {
- aboolean[0] = screen.m_6375_(d0, d1, i);
+ aboolean[0] = net.minecraftforge.client.ForgeHooksClient.onGuiMouseClickedPre(this.f_91503_.f_91080_, d0, d1, i);
-+ if (!aboolean[0]) aboolean[0] = this.f_91503_.f_91080_.m_6375_(d0, d1, i);
-+ if (!aboolean[0]) aboolean[0] = net.minecraftforge.client.ForgeHooksClient.onGuiMouseClickedPost(this.f_91503_.f_91080_, d0, d1, i);
++ if (!aboolean[0]) {
++ aboolean[0] = this.f_91503_.f_91080_.m_6375_(d0, d1, i);
++ aboolean[0] = net.minecraftforge.client.ForgeHooksClient.onGuiMouseClickedPost(this.f_91503_.f_91080_, d0, d1, i, aboolean[0]);
++ }
}, "mouseClicked event handler", screen.getClass().getCanonicalName());
} else {
Screen.m_96579_(() -> {
- aboolean[0] = screen.m_6348_(d0, d1, i);
+ aboolean[0] = net.minecraftforge.client.ForgeHooksClient.onGuiMouseReleasedPre(this.f_91503_.f_91080_, d0, d1, i);
-+ if (!aboolean[0]) aboolean[0] = this.f_91503_.f_91080_.m_6348_(d0, d1, i);
-+ if (!aboolean[0]) aboolean[0] = net.minecraftforge.client.ForgeHooksClient.onGuiMouseReleasedPost(this.f_91503_.f_91080_, d0, d1, i);
++ if (!aboolean[0]) {
++ aboolean[0] = this.f_91503_.f_91080_.m_6348_(d0, d1, i);
++ aboolean[0] = net.minecraftforge.client.ForgeHooksClient.onGuiMouseReleasedPost(this.f_91503_.f_91080_, d0, d1, i, aboolean[0]);
++ }
}, "mouseReleased event handler", screen.getClass().getCanonicalName());
}
}
diff --git a/src/main/java/net/minecraftforge/client/ForgeHooksClient.java b/src/main/java/net/minecraftforge/client/ForgeHooksClient.java
old mode 100644
new mode 100755
index 851bf20c0b..6d247efa7e
--- a/src/main/java/net/minecraftforge/client/ForgeHooksClient.java
+++ b/src/main/java/net/minecraftforge/client/ForgeHooksClient.java
@@ -717,10 +717,12 @@ public class ForgeHooksClient
return MinecraftForge.EVENT_BUS.post(event);
}
- public static boolean onGuiMouseClickedPost(Screen guiScreen, double mouseX, double mouseY, int button)
+ public static boolean onGuiMouseClickedPost(Screen guiScreen, double mouseX, double mouseY, int button, boolean handled)
{
- Event event = new GuiScreenEvent.MouseClickedEvent.Post(guiScreen, mouseX, mouseY, button);
- return MinecraftForge.EVENT_BUS.post(event);
+ Event event = new GuiScreenEvent.MouseClickedEvent.Post(guiScreen, mouseX, mouseY, button, handled);
+ event.setCanceled(handled);
+ MinecraftForge.EVENT_BUS.post(event);
+ return event.getResult() == Event.Result.DEFAULT ? handled : event.getResult() == Event.Result.ALLOW;
}
public static boolean onGuiMouseReleasedPre(Screen guiScreen, double mouseX, double mouseY, int button)
@@ -729,10 +731,12 @@ public class ForgeHooksClient
return MinecraftForge.EVENT_BUS.post(event);
}
- public static boolean onGuiMouseReleasedPost(Screen guiScreen, double mouseX, double mouseY, int button)
+ public static boolean onGuiMouseReleasedPost(Screen guiScreen, double mouseX, double mouseY, int button, boolean handled)
{
- Event event = new GuiScreenEvent.MouseReleasedEvent.Post(guiScreen, mouseX, mouseY, button);
- return MinecraftForge.EVENT_BUS.post(event);
+ Event event = new GuiScreenEvent.MouseReleasedEvent.Post(guiScreen, mouseX, mouseY, button, handled);
+ event.setCanceled(handled);
+ MinecraftForge.EVENT_BUS.post(event);
+ return event.getResult() == Event.Result.DEFAULT ? handled : event.getResult() == Event.Result.ALLOW;
}
public static boolean onGuiMouseDragPre(Screen guiScreen, double mouseX, double mouseY, int mouseButton, double dragX, double dragY)
diff --git a/src/main/java/net/minecraftforge/client/event/GuiScreenEvent.java b/src/main/java/net/minecraftforge/client/event/GuiScreenEvent.java
old mode 100644
new mode 100755
index 77c7419797..5ffac1343b
--- a/src/main/java/net/minecraftforge/client/event/GuiScreenEvent.java
+++ b/src/main/java/net/minecraftforge/client/event/GuiScreenEvent.java
@@ -283,15 +283,47 @@ public class GuiScreenEvent extends Event
}
/**
- * This event fires after {@link IGuiEventListener#mouseClicked(double, double, int)} if the click was not already handled.
- * Cancel this event when you successfully use the mouse click, to prevent other handlers from using the same input.
+ * This event fires after {@link GuiEventListener#mouseClicked(double, double, int)}.
+ *
+ * This event {@linkplain HasResult has a result}.
+ *
+ * - {@link Result#ALLOW} - to force set the mouse click as handled
+ * - {@link Result#DEFAULT} - to use the default value of {@link #handled}
+ * - {@link Result#DENY} - to force set the mouse click as not handled
+ *
+ *
+ *
+ * Note that this event is currently pre-cancelled if {@link Screen#mouseClicked} returns {@code true}
+ * to retain old behavior. This will be changed in 1.18 when the event is made non-cancellable.
*/
- @Cancelable
+ @Cancelable // TODO: Make non-cancellable in 1.18.
+ @HasResult
public static class Post extends MouseClickedEvent
{
- public Post(Screen gui, double mouseX, double mouseY, int button)
+ private final boolean handled;
+
+ public Post(Screen gui, double mouseX, double mouseY, int button, boolean handled)
{
super(gui, mouseX, mouseY, button);
+ this.handled = handled;
+ }
+
+ /**
+ * @deprecated event now has a result instead and will no longer be cancellable in 1.18; use {@link #setResult(Result)}
+ */
+ @Override
+ @Deprecated
+ public void setCanceled(boolean cancel)
+ {
+ this.setResult(cancel ? Result.ALLOW : Result.DEFAULT);
+ }
+
+ /**
+ * @return {@code true} if the mouse click was already handled by its screen
+ */
+ public boolean wasHandled()
+ {
+ return handled;
}
}
}
@@ -325,15 +357,47 @@ public class GuiScreenEvent extends Event
}
/**
- * This event fires after {@link IGuiEventListener#mouseReleased(double, double, int)} if the release was not already handled.
- * Cancel this event when you successfully use the mouse release, to prevent other handlers from using the same input.
+ * This event fires after {@link GuiEventListener#mouseReleased(double, double, int)}.
+ *
+ * This event {@linkplain HasResult has a result}.
+ *
+ * - {@link Result#ALLOW} - to force set the mouse release as handled
+ * - {@link Result#DEFAULT} - to use the default value of {@link #handled}
+ * - {@link Result#DENY} - to force set the mouse release as not handled
+ *
+ *
+ *
+ * Note that this event is currently pre-cancelled if {@link Screen#mouseReleased} returns {@code true}
+ * to retain old behavior. This will be changed in 1.18 when the event is made non-cancellable.
*/
- @Cancelable
+ @Cancelable // TODO: Make non-cancellable in 1.18.
+ @HasResult
public static class Post extends MouseReleasedEvent
{
- public Post(Screen gui, double mouseX, double mouseY, int button)
+ private final boolean handled;
+
+ public Post(Screen gui, double mouseX, double mouseY, int button, boolean handled)
{
super(gui, mouseX, mouseY, button);
+ this.handled = handled;
+ }
+
+ /**
+ * @deprecated event now has a result instead and will no longer be cancellable in 1.18; use {@link #setResult(Result)}
+ */
+ @Override
+ @Deprecated
+ public void setCanceled(boolean cancel)
+ {
+ this.setResult(cancel ? Result.ALLOW : Result.DEFAULT);
+ }
+
+ /**
+ * @return {@code true} if the mouse release was already handled by its screen
+ */
+ public boolean wasHandled()
+ {
+ return handled;
}
}
}