Skip to content

Commit

Permalink
Fix #2873 Ghost ingredient handling on Fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Jul 15, 2022
1 parent dde8618 commit b6813e7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import mezz.jei.common.input.handlers.CombinedInputHandler;
import mezz.jei.core.util.ReflectionUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.Screen;

Expand Down Expand Up @@ -69,7 +70,7 @@ public boolean onGuiMouseClicked(Screen screen, UserInput input) {
boolean handled = this.inputHandler.handleUserInput(screen, input, keybindings)
.isPresent();

if (input.is(keybindings.getLeftClick())) {
if (Minecraft.getInstance().screen == screen && input.is(keybindings.getLeftClick())) {
handled |= this.inputHandler.handleDragStart(screen, input)
.isPresent();
}
Expand Down
160 changes: 95 additions & 65 deletions Fabric/src/main/java/mezz/jei/fabric/startup/EventRegistration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mezz.jei.fabric.startup;

import com.mojang.blaze3d.vertex.PoseStack;
import mezz.jei.common.gui.GuiEventHandler;
import mezz.jei.common.input.ClientInputHandler;
import mezz.jei.common.input.InputType;
Expand All @@ -11,7 +12,10 @@
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.fabricmc.fabric.api.client.screen.v1.ScreenKeyboardEvents;
import net.fabricmc.fabric.api.client.screen.v1.ScreenMouseEvents;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import org.jetbrains.annotations.Nullable;

public class EventRegistration {
Expand All @@ -31,72 +35,98 @@ public void setEventHandlers(JeiEventHandlers eventHandlers) {
}

private void registerEvents() {
ScreenEvents.BEFORE_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if (guiEventHandler != null) {
guiEventHandler.onGuiInit(screen);

ScreenKeyboardEvents.allowKeyPress(screen).register((screen1, key, scancode, modifiers) -> {
if (clientInputHandler != null) {
UserInput userInput = UserInput.fromVanilla(key, scancode, modifiers, InputType.IMMEDIATE);
return !clientInputHandler.onKeyboardKeyPressedPre(screen1, userInput);
}
return true;
});

ScreenMouseEvents.allowMouseClick(screen).register((screen1, mouseX, mouseY, button) -> {
if (clientInputHandler == null) {
return true;
}
return UserInput.fromVanilla(mouseX, mouseY, button, InputType.IMMEDIATE)
.map(input -> !clientInputHandler.onGuiMouseClicked(screen1, input))
.orElse(true);
});

ScreenMouseEvents.allowMouseScroll(screen).register((screen1, mouseX, mouseY, horizontalAmount, verticalAmount) -> {
if (clientInputHandler == null) {
return false;
}
return !clientInputHandler.onGuiMouseScroll(mouseX, mouseY, verticalAmount);
});

ScreenEvents.afterRender(screen).register((screen1, poseStack, mouseX, mouseY, tickDelta) -> {
if (guiEventHandler != null) {
guiEventHandler.onDrawScreenPost(screen1, poseStack, mouseX, mouseY);
}
});
}
});

JeiCharTypedEvents.BEFORE_CHAR_TYPED.register((guiEventListener, codepoint, modifiers) -> {
if (clientInputHandler != null && guiEventListener instanceof Screen screen) {
return clientInputHandler.onKeyboardCharTypedPre(screen, codepoint, modifiers);
}
ScreenEvents.BEFORE_INIT.register((client, screen, scaledWidth, scaledHeight) ->
registerScreenEvents(screen)
);
JeiCharTypedEvents.BEFORE_CHAR_TYPED.register(this::beforeCharTyped);
ScreenEvents.AFTER_INIT.register(this::afterInit);
JeiScreenEvents.AFTER_RENDER_BACKGROUND.register(this::afterRenderBackground);
JeiScreenEvents.DRAW_FOREGROUND.register(this::drawForeground);
ClientTickEvents.START_CLIENT_TICK.register(this::onStartTick);
}

private void registerScreenEvents(Screen screen) {
if (guiEventHandler == null) {
return;
}

guiEventHandler.onGuiInit(screen);
ScreenKeyboardEvents.allowKeyPress(screen).register(this::allowKeyPress);
ScreenMouseEvents.allowMouseClick(screen).register(this::allowMouseClick);
ScreenMouseEvents.beforeMouseRelease(screen).register(this::beforeMouseRelease);
ScreenMouseEvents.allowMouseScroll(screen).register(this::allowMouseScroll);
ScreenEvents.afterRender(screen).register(this::afterRender);
}

private boolean allowMouseClick(Screen screen, double mouseX, double mouseY, int button) {
if (clientInputHandler == null) {
return true;
}
return UserInput.fromVanilla(mouseX, mouseY, button, InputType.SIMULATE)
.map(input -> !clientInputHandler.onGuiMouseClicked(screen, input))
.orElse(true);
}

@SuppressWarnings("UnusedReturnValue")
private boolean beforeMouseRelease(Screen screen, double mouseX, double mouseY, int button) {
if (clientInputHandler == null) {
return true;
}
return UserInput.fromVanilla(mouseX, mouseY, button, InputType.EXECUTE)
.map(input -> !clientInputHandler.onGuiMouseClicked(screen, input))
.orElse(true);
}

private boolean allowKeyPress(Screen screen, int key, int scancode, int modifiers) {
if (clientInputHandler == null) {
return true;
}
UserInput userInput = UserInput.fromVanilla(key, scancode, modifiers, InputType.IMMEDIATE);
return !clientInputHandler.onKeyboardKeyPressedPre(screen, userInput);
}

private boolean allowMouseScroll(Screen screen, double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
if (clientInputHandler == null) {
return false;
});

ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if (guiEventHandler != null) {
guiEventHandler.onGuiOpen(screen);
}
});

JeiScreenEvents.AFTER_RENDER_BACKGROUND.register((screen, poseStack) -> {
if (guiEventHandler != null) {
guiEventHandler.onDrawBackgroundPost(screen, poseStack);
}
});

JeiScreenEvents.DRAW_FOREGROUND.register(((screen, poseStack, mouseX, mouseY) -> {
if (guiEventHandler != null) {
guiEventHandler.onDrawForeground(screen, poseStack, mouseX, mouseY);
}
}));

ClientTickEvents.START_CLIENT_TICK.register(client -> {
if (guiEventHandler != null) {
guiEventHandler.onClientTick();
}
});
}
return !clientInputHandler.onGuiMouseScroll(mouseX, mouseY, verticalAmount);
}

private void afterRender(Screen screen, PoseStack poseStack, int mouseX, int mouseY, float tickDelta) {
if (guiEventHandler != null) {
guiEventHandler.onDrawScreenPost(screen, poseStack, mouseX, mouseY);
}
}

private boolean beforeCharTyped(GuiEventListener guiEventListener, char codepoint, int modifiers) {
if (clientInputHandler != null && guiEventListener instanceof Screen screen) {
return clientInputHandler.onKeyboardCharTypedPre(screen, codepoint, modifiers);
}
return false;
}

private void afterInit(Minecraft client, Screen screen, int scaledWidth, int scaledHeight) {
if (guiEventHandler != null) {
guiEventHandler.onGuiOpen(screen);
}
}

private void afterRenderBackground(Screen screen, PoseStack poseStack) {
if (guiEventHandler != null) {
guiEventHandler.onDrawBackgroundPost(screen, poseStack);
}
}

private void drawForeground(AbstractContainerScreen<?> screen, PoseStack poseStack, int mouseX, int mouseY) {
if (guiEventHandler != null) {
guiEventHandler.onDrawForeground(screen, poseStack, mouseX, mouseY);
}
}

private void onStartTick(Minecraft client) {
if (guiEventHandler != null) {
guiEventHandler.onClientTick();
}
}

public void clear() {
Expand Down

0 comments on commit b6813e7

Please sign in to comment.