#include "NoFall.h"
#include "Client.h"

#include "Utils/MoveUtils.h"
#include "Utils/ChatUtils.h"

auto NoFall::onWorldTick(Player* _this) -> void {
	auto clientinstance = Client::clientinstance;
	if (!clientinstance) return;

	auto blocksource = clientinstance->getBlockSource();
	if (!blocksource) return;
	
	glm::vec3 blockpos = Utils::Move::getBlockPosition(_this);
	glm::ivec3 iblockpos = glm::ivec3(
		std::floor(blockpos.x),
		std::floor(blockpos.y),
		std::floor(blockpos.z)
	);

	if (Utils::Move::isOnGround(_this)) {
		if (cancelpacket) {
			cancelpacket = false;

			Utils::Chat::display_(_("Blink Stopped."), Utils::Chat::Type::RED);
		}
		return;
	}

	int aircount = 0;
	for (int i = 0; i < 100; i++) {
		if (blocksource->getBlock(&iblockpos)->getBlockLegacy()->getName() == _("tile.air"))
			aircount++;
		else
			break;

		iblockpos.y -= 1;
	}

	switch (mode) {
	case 0:
		if (aircount > 4 && aircount != 100) {
			cancelpacket = true;
			Utils::Chat::display_(_("Blinking..."), Utils::Chat::Type::GREEN);
		}
	}
}

auto NoFall::onPacketSendAfter2(Packet* packet, bool* cancel) -> void {
	*cancel = cancelpacket;
}
