package mezz.jei.input;

import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import mezz.jei.input.click.MouseClickState;
import net.minecraft.client.gui.screens.Screen;

import javax.annotation.Nullable;
import java.util.Arrays;

public class CombinedMouseHandler implements IMouseHandler {
	private final Int2ObjectMap<IMouseHandler> mousedDown = new Int2ObjectArrayMap<>();
	private final Iterable<IMouseHandler> mouseHandlers;

	public CombinedMouseHandler(IMouseHandler... mouseHandlers) {
		this.mouseHandlers = Arrays.asList(mouseHandlers);
	}

	public CombinedMouseHandler(Iterable<IMouseHandler> mouseHandlers) {
		this.mouseHandlers = mouseHandlers;
	}

	@Nullable
	@Override
	public IMouseHandler handleClick(Screen screen, double mouseX, double mouseY, int mouseButton, MouseClickState clickState) {
		if (clickState.isSimulate() || clickState.isVanilla()) {
			this.mousedDown.remove(mouseButton);
			IMouseHandler handled = null;
			for (IMouseHandler mouseHandler : this.mouseHandlers) {
				if (handled == null) {
					handled = mouseHandler.handleClick(screen, mouseX, mouseY, mouseButton, clickState);
				} else {
					mouseHandler.handleMouseClickedOut(mouseButton);
				}
			}
			if (handled != null) {
				if (!clickState.isVanilla()) {
					this.mousedDown.put(mouseButton, handled);
				}
			} else {
				this.handleMouseClickedOut(mouseButton);
			}
			return handled;
		} else {
			IMouseHandler mouseHandler = this.mousedDown.remove(mouseButton);
			if (mouseHandler == null) {
				return null;
			}
			return mouseHandler.handleClick(screen, mouseX, mouseY, mouseButton, clickState);
		}
	}

	@Override
	public void handleMouseClickedOut(int mouseButton) {
		for (IMouseHandler mouseHandler : this.mouseHandlers) {
			mouseHandler.handleMouseClickedOut(mouseButton);
		}
	}

	@Override
	public boolean handleMouseScrolled(double mouseX, double mouseY, double scrollDelta) {
		for (IMouseHandler mouseHandler : this.mouseHandlers) {
			if (mouseHandler.handleMouseScrolled(mouseX, mouseY, scrollDelta)) {
				return true;
			}
		}
		return false;
	}
}

@@ -271,17 +271,68 @@
 il.Array
-s
+List;%0Aimport java.util.Arrays;%0Aimport java.util.List
 ;%0A%0Apubli
@@ -1006,16 +1006,55 @@
 utton);%0A
+%09%09%09IMouseHandler handledSource = null;%0A
 %09%09%09IMous
@@ -1141,36 +1141,8 @@
 ) %7B%0A
-%09%09%09%09if (handled == null) %7B%0A%09
 %09%09%09%09
@@ -1230,25 +1230,227 @@
 e);%0A%09%09%09%09
-%7D else %7B%0A
+if (handled != null) %7B%0A%09%09%09%09%09handledSource = mouseHandler;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09if (handled != null) %7B%0A%09%09%09%09for (IMouseHandler mouseHandler : this.mouseHandlers) %7B%0A%09%09%09%09%09if (mouseHandler != handledSource) %7B%0A%09
 %09%09%09%09%09mou
@@ -1501,20 +1501,16 @@
 ;%0A%09%09
-%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09
 if (
@@ -1505,38 +1505,18 @@
 %09%09%09%7D%0A%09%09%09
-if (handled != null) %7B
+%09%7D
 %0A%09%09%09%09if 

Fix #2459 Mouse clicks did not always unfocus the search bar