/*
 * 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.item.Item;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import tk.wurst_client.events.LeftClickEvent;
import tk.wurst_client.events.listeners.LeftClickListener;
import tk.wurst_client.events.listeners.UpdateListener;
import tk.wurst_client.features.Feature;
import tk.wurst_client.utils.InventoryUtils;

@Mod.Info(
	description = "Automatically uses the best weapon in your hotbar to attack entities.\n"
		+ "Tip: This works with Killaura.",
	name = "AutoSword",
	tags = "auto sword",
	help = "Mods/AutoSword")
@Mod.Bypasses(ghostMode = false,
	latestNCP = false,
	olderNCP = false,
	antiCheat = false)
public class AutoSwordMod extends Mod
	implements LeftClickListener, UpdateListener
{
	private int oldSlot;
	private int timer;
	
	@Override
	public Feature[] getSeeAlso()
	{
		return new Feature[]{wurst.mods.autoToolMod, wurst.mods.killauraMod};
	}
	
	@Override
	public void onEnable()
	{
		wurst.events.add(LeftClickListener.class, this);
	}
	
	@Override
	public void onDisable()
	{
		wurst.events.remove(LeftClickListener.class, this);
	}
	
	@Override
	public void onUpdate()
	{
		// update timer
		if(timer > 0)
		{
			timer--;
			return;
		}
		
		// reset slot
		mc.player.inventory.currentItem = oldSlot;
		wurst.events.remove(UpdateListener.class, this);
	}
	
	@Override
	public void onLeftClick(LeftClickEvent event)
	{
		// check hitResult
		if(mc.objectMouseOver == null || mc.objectMouseOver.entityHit == null)
			return;
		
		setSlot();
	}
	
	public void setSlot()
	{
		// check if active
		if(!isActive())
			return;
		
		// wait for AutoEat
		if(wurst.mods.autoEatMod.isEating())
			return;
		
		// find best weapon
		float bestDamage = 0;
		int bestSlot = -1;
		for(int i = 0; i < 9; i++)
		{
			// skip empty slots
			if(InventoryUtils.isSlotEmpty(i))
				continue;
			
			Item item = mc.player.inventory.getStackInSlot(i).getItem();
			
			// get damage
			float damage = 0;
			if(item instanceof ItemSword)
				damage = ((ItemSword)item).attackDamage;
			else if(item instanceof ItemTool)
				damage = ((ItemTool)item).damageVsEntity;
			
			// compare with previous best weapon
			if(damage > bestDamage)
			{
				bestDamage = damage;
				bestSlot = i;
			}
		}
		
		// check if any weapon was found
		if(bestSlot == -1)
			return;
		
		// set slot
		oldSlot = mc.player.inventory.currentItem;
		mc.player.inventory.currentItem = bestSlot;
		
		// start timer
		timer = 4;
		wurst.events.add(UpdateListener.class, this);
	}
}

@@ -1069,16 +1069,21 @@
  oldSlot
+ = -1
 ;%0A%09priva
@@ -1399,35 +1399,145 @@
 r.class, this);%0A
+%09%09%0A%09%09// reset slot%0A%09%09if(oldSlot != -1)%0A%09%09%7B%0A%09%09%09mc.player.inventory.currentItem = oldSlot;%0A%09%09%09oldSlot = -1;%0A%09%09%7D%0A
 %09%7D%0A
-
 %09%0A%09@Override%0A%09pu
@@ -1644,16 +1644,41 @@
 et slot%0A
+%09%09if(oldSlot != -1)%0A%09%09%7B%0A%09
 %09%09mc.pla
@@ -1714,16 +1714,37 @@
 ldSlot;%0A
+%09%09%09oldSlot = -1;%0A%09%09%7D%0A
 %09%09wurst.
@@ -2784,24 +2784,50 @@
 %09%0A%09%09// s
-et
+ave old
  slot%0A
+%09%09if(oldSlot == -1)%0A%09
 %09%09oldSlo
@@ -2863,16 +2863,33 @@
 ntItem;%0A
+%09%09%0A%09%09// set slot%0A
 %09%09mc.pla

Fix AutoSword not resetting the selected slot sometimes