
package nivia.modules.exploits;

import net.minecraft.item.*;
import net.minecraft.network.play.client.C03PacketPlayer;
import net.minecraft.network.play.client.C0APacketAnimation;
import nivia.Pandora;
import nivia.commands.Command;
import nivia.events.EventTarget;
import nivia.events.events.EventPacketSend;
import nivia.events.events.EventTick;
import nivia.managers.PropertyManager.DoubleProperty;
import nivia.managers.PropertyManager.Property;
import nivia.modules.Module;
import nivia.utils.Helper;
import nivia.utils.Logger;

public class FastUse extends Module {
	public FastUse() {
		super("FastUse", 0, 0xE6B800, Category.EXPLOITS, "Uses usable items faster.",
				new String[] { "fastuse", "fc", "fastc", "fconsume", "fs", "fu", "fuse" }, true);
	}

	public DoubleProperty delay = new DoubleProperty(this, "Delay", 13, 0, 35);
	public Property<Boolean> bows = new Property<Boolean>(this, "Bows", true);
	public Property<Boolean> ia = new Property<Boolean>(this, "In Air", true);


	@EventTarget
	public void onTick(EventTick event) {
		if (mc.thePlayer.getItemInUseDuration() >= this.delay.getValue()&& isUsable(mc.thePlayer.getCurrentEquippedItem()) && (ia.value ? true : mc.thePlayer.isCollidedVertically)) {
			for (int i = 0; i < 40; ++i) {
				Helper.sendPacket(new C03PacketPlayer(mc.thePlayer.onGround));
			}
			mc.playerController.onStoppedUsingItem(mc.thePlayer);

		}
	}

	@EventTarget
	public void onPacketSend(EventPacketSend e) {
		if (mc.thePlayer.isUsingItem() && (e.getPacket() instanceof C0APacketAnimation)
				&& isUsable(mc.thePlayer.getCurrentEquippedItem()) && !mc.thePlayer.isSwingInProgress)
			e.setCancelled(true);
	}

	private boolean isUsable(ItemStack stack) {
		if (stack.getItem() instanceof ItemBow) {
			return bows.value;
		}
		if (stack.getItem() instanceof ItemFood) {
			return true;
		}
		if (stack.getItem() instanceof ItemPotion) {
			return true;
		}
		if (stack.getItem() instanceof ItemBucketMilk) {
			return true;
		} else
			return false;
	}

	protected void addCommand() {
		Pandora.getCommandManager().cmds.add(new Command("FastUse", "Manages FastConsume' values",
				Logger.LogExecutionFail("Option, Options:", new String[] { "Delay", "Bows" }), "fs", "fastconsume",
				"fu") {
			@Override
			public void execute(String commandName, String[] arguments) {
				boolean item = isUsable(mc.thePlayer.getCurrentEquippedItem());
				String mode = arguments[1], message2 = "";
				try {
					message2 = arguments[2];
				} catch (Exception e) {
				}
				switch (mode.toLowerCase()) {
				case "delay":
				case "Delay":
				case "d":
					switch (message2) {
					case "actual":
						logValue(delay);
						break;
					case "reset":
						delay.reset();
						break;
					default:
						int newDelay = Integer.parseInt(message2);
						delay.setValue(newDelay);
						Logger.logSetMessage("FastConsume", "Delay", delay);
						break;
					}
					break;
				case "bow":
				case "Bow":
				case "Bows":
				case "bows":
				case "b":
					bows.value = !bows.value;
					Logger.logSetMessage("FastConsume", "Bows", bows);
					break;
				case "values":
					logValues();
					break;
				default:
					Logger.logChat(this.getError());
				}

			}
		});
	}
}