package net.teamfruit.autoinput;

import java.util.HashSet;

import com.google.common.collect.ImmutableList;

import net.minecraft.client.gui.GuiChat;
import net.minecraft.client.gui.GuiDisconnected;
import net.minecraft.client.gui.GuiDownloadTerrain;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.GuiMultiplayer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiScreenWorking;
import net.minecraft.client.gui.GuiWorldSelection;
import net.minecraft.client.gui.inventory.GuiContainerCreative;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.client.multiplayer.GuiConnecting;
import net.minecraft.client.settings.KeyBinding;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

public class ClientHandler {
	public static final ClientHandler INSTANCE = new ClientHandler();
	private static final ImmutableList<Class<? extends GuiScreen>> EGNORE_CLASSES = ImmutableList.of(
			GuiInventory.class,
			GuiContainerCreative.class,
			GuiChat.class,
			GuiDownloadTerrain.class,
			GuiMainMenu.class,
			GuiConnecting.class,
			GuiDisconnected.class,
			GuiMultiplayer.class,
			GuiWorldSelection.class,
			GuiScreenWorking.class);

	private ClientHandler() {
	}

	public final HashSet<Integer> tickKeys = new HashSet<Integer>();

	@SubscribeEvent
	public void onClientTick(final TickEvent.ClientTickEvent event) {
		if (event.phase==TickEvent.Phase.START&&InputHandler.INSTANCE.isKeyInput())
			for (final Integer line : this.tickKeys)
				KeyBinding.onTick(line);
	}

	@SubscribeEvent
	public void onOpenGui(final GuiOpenEvent event) {
		if (event.getGui()!=null&&!EGNORE_CLASSES.contains(event.getGui().getClass()))
			InputHandler.INSTANCE.setKeyInput(false);
	}
}

@@ -105,24 +105,64 @@
 bleList;%0D%0A%0D%0A
+import net.minecraft.client.Minecraft;%0D%0A
 import net.m
@@ -943,16 +943,66 @@
 kEvent;%0D
+%0Aimport net.teamfruit.autoinput.gui.GuiAutoInput;%0D
 %0A%0D%0Apubli
@@ -1560,16 +1560,72 @@
 ger%3E();%0D
+%0A%09private GuiScreen currentgui;%0D%0A%09private boolean prev;%0D
 %0A%0D%0A%09@Sub
@@ -1854,24 +1854,181 @@
 Tick(line);%0D
+%0A%0D%0A%09%09if (this.prev&&Minecraft.getMinecraft().currentScreen==null) %7B%0D%0A%09%09%09onGuiClose();%0D%0A%09%09%09this.currentgui = null;%0D%0A%09%09%7D%0D%0A%09%09this.prev = this.currentgui!=null;%0D
 %0A%09%7D%0D%0A%0D%0A%09@Sub
@@ -2059,15 +2059,15 @@
 d on
+Gui
 Open
-Gui
 (fin
@@ -2122,11 +2122,18 @@
 null
-&&!
+)%0D%0A%09%09%09if (
 EGNO
@@ -2188,53 +2188,354 @@
 %0A%09%09%09
-InputHandler.INSTANCE.setKeyInput(false);%0D%0A%09%7D
+%09this.currentgui = Minecraft.getMinecraft().currentScreen;%0D%0A%09%09%09else%0D%0A%09%09%09%09InputHandler.INSTANCE.setKeyInput(false);%0D%0A%09%7D%0D%0A%0D%0A%09public void onGuiClose() %7B%0D%0A%09%09if (InputHandler.INSTANCE.isKeyInput())%0D%0A%09%09%09for (final AutoInputKey binding : GuiAutoInput.keys)%0D%0A%09%09%09%09if (!binding.getMode())%0D%0A%09%09%09%09%09KeyBinding.setKeyBindState(binding.getKeyCode(), true);%0D%0A%09%7D%0D%0A
 %0D%0A%7D%0D

Fix hold input not working after closing Gui