/*
 * Copyright  2014 - 2017 | Wurst-Imperium | All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package tk.wurst_client.features.mods;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.InventoryEffectRenderer;
import net.minecraft.inventory.ClickType;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import tk.wurst_client.events.listeners.UpdateListener;
import tk.wurst_client.utils.InventoryUtils;

@Mod.Info(description = "Manages your armor automatically.",
	name = "AutoArmor",
	tags = "auto armor",
	help = "Mods/AutoArmor")
@Mod.Bypasses(ghostMode = false)
public class AutoArmorMod extends Mod implements UpdateListener
{
	@Override
	public void onEnable()
	{
		wurst.events.add(UpdateListener.class, this);
	}
	
	@Override
	public void onDisable()
	{
		wurst.events.remove(UpdateListener.class, this);
	}
	
	@Override
	public void onUpdate()
	{
		// update timer
		updateMS();
		
		// check timer
		if(!hasTimePassedM(100))
			return;
		
		// check screen
		if(mc.currentScreen instanceof GuiContainer
			&& !(mc.currentScreen instanceof InventoryEffectRenderer))
			return;
		
		// store slots and values of best armor pieces
		int[] bestArmorSlots = new int[4];
		int[] bestArmorValues = new int[4];
		
		// initialize with currently equipped armor
		for(int armorType = 0; armorType < 4; armorType++)
		{
			ItemStack oldArmor = mc.player.inventory.armorItemInSlot(armorType);
			if(oldArmor != null && oldArmor.getItem() instanceof ItemArmor)
				bestArmorValues[armorType] =
					((ItemArmor)oldArmor.getItem()).damageReduceAmount;
			
			bestArmorSlots[armorType] = -1;
		}
		
		// search inventory for better armor
		for(int slot = 0; slot < 36; slot++)
		{
			ItemStack stack = mc.player.inventory.getStackInSlot(slot);
			if(stack == null || !(stack.getItem() instanceof ItemArmor))
				continue;
			
			ItemArmor armor = (ItemArmor)stack.getItem();
			int armorType = armor.armorType.ordinal() - 2;
			int armorValue = armor.damageReduceAmount;
			
			if(armorValue > bestArmorValues[armorType])
			{
				bestArmorSlots[armorType] = slot;
				bestArmorValues[armorType] = armorValue;
			}
		}
		
		// equip better armor
		for(int armorType = 0; armorType < 4; armorType++)
		{
			// check if better armor was found
			int slot = bestArmorSlots[armorType];
			if(slot == -1)
				continue;
				
			// check if armor can be swapped
			// needs 1 free slot where it can put the old armor
			ItemStack oldArmor = mc.player.inventory.armorItemInSlot(armorType);
			if(oldArmor == null || !InventoryUtils.isEmptySlot(oldArmor)
				|| mc.player.inventory.getFirstEmptyStack() != -1)
			{
				// hotbar fix
				if(slot < 9)
					slot += 36;
				
				// swap armor
				mc.playerController.windowClick(0, 8 - armorType, 0,
					ClickType.QUICK_MOVE, mc.player);
				mc.playerController.windowClick(0, slot, 0,
					ClickType.QUICK_MOVE, Minecraft.getMinecraft().player);
				
				break;
			}
		}
		
		// reset timer
		updateLastMS();
	}
}

@@ -315,47 +315,8 @@
 s;%0A%0A
-import net.minecraft.client.Minecraft;%0A
 impo
@@ -794,28 +794,9 @@
 sses
-(ghostMode = false)
 %0A
+
 publ
@@ -857,16 +857,38 @@
 tener%0A%7B%0A
+%09private int timer;%0A%09%0A
 %09@Overri
@@ -909,32 +909,65 @@
 d onEnable()%0A%09%7B%0A
+%09%09// reset timer%0A%09%09timer = 0;%0A%09%09%0A
 %09%09wurst.events.a
@@ -1141,22 +1141,24 @@
 %09%7B%0A%09%09// 
-update
+wait for
  timer%0A%09
@@ -1162,66 +1162,37 @@
 r%0A%09%09
-updateMS();
+if(timer %3E 0)
 %0A%09%09
+%7B
 %0A%09%09
-// check timer%0A%09%09if(!hasTimePassedM(100))
+%09timer--;
 %0A%09%09%09
@@ -1191,32 +1191,36 @@
 r--;%0A%09%09%09return;%0A
+%09%09%7D%0A
 %09%09%0A%09%09// check sc
@@ -3108,32 +3108,10 @@
 VE, 
-Minecraft.getMinecraft()
+mc
 .pla
@@ -3145,26 +3145,24 @@
 %09%09%7D%0A%09%09%0A%09%09// 
-re
 set timer%0A%09%09
@@ -3165,22 +3165,17 @@
 r%0A%09%09
-updateLastMS()
+timer = 2
 ;%0A%09%7D

Update AutoArmorMod