/// api_version=2
var script = registerScript({
	name: "ProRedefly",
	version: "3.0",
	authors: ["toriset, TheHeroGuy"]
});

var fstate = 0;
var mstate = 0;
var state2 = 0;
var state3 = 0;
var jumpstate;
var teststate;
var hasReset;
var ncp;
var hivestate;
var dist;
var blink = moduleManager.getModule("Blink");
var blocks = [];
var jump = 0;
function setSpeed(_speed) {
	var playerYaw = Math.radians(mc.thePlayer.rotationYaw);
	mc.thePlayer.motionX = _speed * -Math.sin(playerYaw);
	mc.thePlayer.motionZ = _speed * Math.cos(playerYaw);
}

script.registerModule({
	name: "CrystalFly",
	description: "Fly with cutting edge technology.",
	category: "Movement",
	settings: {
	Mode: Setting.list({
		name: "Mode",
		default: "RedeskyNew",
		values: ["RedeskyNew"]
	}),
	}
}, function (module) {
	module.on("enable", function () {

		jumpstate = 0;
		mstate = 21.49;
		dist = mc.thePlayer.posY;
		mc.thePlayer.ticksExisted = 0;
		if (module.settings.Mode.get() == "RedeskyNew") {
			candisable = false;
		}

		
	});

		module.on("packet", function (event) {
		var packet = event.getPacket();
		if (packet instanceof S08 && jumpstate == 1 && mc.thePlayer.onGround) {
			jumpstate = 0;
			mc.thePlayer.motionX = 0;
			mc.thePlayer.motionZ = 0;
		}

		if (packet instanceof S12 && packet.getEntityID() == mc.thePlayer.getEntityId() && hasReset) {
			hasReset = false;
			event.cancelEvent();
		}	  
	});

	module.on("move", function (event) {
		if (jumpstate == 1 && !mc.thePlayer.onGround) {
			event.setX(0);
			event.setZ(0);
		}
		if(module.settings.Mode.get() == "OldRedesky5") {
			if(mc.gameSettings.keyBindJump.isKeyDown() == true) {
				vClip2(4.151)
			}
		}
	});
	module.on("disable", function () {
		mc.thePlayer.capabilities.flying = false;
		mc.timer.timerSpeed = 1;
		mc.thePlayer.speedInAir = 0.02;
		blink.setState(false)
		if (module.settings.RedeBlink.get() == true) {
			blink.setState(true);
			blink.setState(false);
		}
		ncp = 0;
		setSpeed(0);
		if (module.settings.Mode.get() == "OldRedesky") {
			hClip2(0);
		}
	});

var pl = [];
var matrixfly = 0;
var sword;
	module.on("update", function () {

		if (module.settings.Mode.get() == "RedeskyNew") {
			mc.timer.timerSpeed = 0.23; // 0.23 
		
			if (mc.thePlayer.onGround && candisable) {
				module.toggle();
				return;
			}
		
			if (mc.thePlayer.onGround) {
				mc.thePlayer.jump();
				blink.setState(true)
				
				mc.thePlayer.speedInAir = 0.0701; //0.0701
				candisable = true;
				
			}
			if (mc['thePlayer']['fallDistance'] > -0x12e * -0x18 + 0x1 * -0x1f67 + 0x317) {
				if (mc['thePlayer']['fallDistance'] < -0x19a3 * -0x1 + 0x810 + -0x10d9 * 0x2) {
					mc['thePlayer']['speedInAir'] *= 0x237e + -0x1 * -0x1dd5 + -16722.15;
					mc['thePlayer']['motionY'] *= 0x2525 + 0x7 * -0xb + -9431.25;
				} else if (mc['thePlayer']['fallDistance'] < -0x1fba + 0x1da3 + 0x3 * 0xb3) {
					mc['thePlayer']['speedInAir'] *= -0x166 * -0x5 + 0xb41 + -4670.25;
					mc['thePlayer']['motionY'] *= 0x892 + 0x450 + -3297.15;
				} else {
					mc['thePlayer']['speedInAir'] = -0x1a * 0xd6 + -0x25e2 + 15262.02;
				}
			}
			if (mc.thePlayer.motionY > 0) {
				if (mc.thePlayer.motionY < 0.3) {
					mc.thePlayer.motionY *= 1.2425445; // 1.242544
					mc.thePlayer.speedInAir *= 1.259; // 1.258
				}
				blink.setState(false)
			}
		}

		
		});
	});


//functions

function vClip(d) {
	mc.thePlayer.setPosition(mc.thePlayer.posX, mc.thePlayer.posY + d, mc.thePlayer.posZ);
}
function yClip(d) {
	mc.thePlayer.setPosition(mc.thePlayer.posX, d, mc.thePlayer.posZ);
}
function xClip(d) {
	mc.thePlayer.setPosition(mc.thePlayer.posX + d, mc.thePlayer.posY, mc.thePlayer.posZ);
}
function zClip(d) {
	mc.thePlayer.setPosition(mc.thePlayer.posX, mc.thePlayer.posY, mc.thePlayer.posZ + d);
}
function hClip(d) {
	var playerYaw = Math.radians(mc.thePlayer.rotationYaw);
	mc.thePlayer.setPosition(mc.thePlayer.posX + d * -Math.sin(playerYaw), mc.thePlayer.posY, mc.thePlayer.posZ + d * Math.cos(playerYaw));
}
function dClip(d) {
	var playerYaw = Math.radians(mc.thePlayer.rotationYaw) + 90;
	mc.thePlayer.setPosition(mc.thePlayer.posX + d * -Math.sin(playerYaw), mc.thePlayer.posY, mc.thePlayer.posZ + d * Math.cos(playerYaw));
}
function hClip2(d) {
	var playerYaw = Math.radians(mc.thePlayer.rotationYaw);
	mc.getNetHandler().addToSendQueue(new C04(mc.thePlayer.posX + d * -Math.sin(playerYaw), mc.thePlayer.posY, mc.thePlayer.posZ + d * Math.cos(playerYaw), false));
}
function vClip2(d) {
	mc.getNetHandler().addToSendQueue(new C04(mc.thePlayer.posX, mc.thePlayer.posY+d, mc.thePlayer.posZ, false));
}
function chatSyntax(message) {
	Chat.print("§8[§9§lMacros§8] §3Syntax: §7" + prefix + message);
}

function chatText(message) {
	Chat.print("§8[§9§lMacros§8] §3" + message);
}

function aacDamage(damages) {
	mc.thePlayer.motionY = (5 * damages);
}

function ncpDamage() {
	vClip(4);
}

function setSpeed(_speed) {
	var playerYaw = Math.radians(mc.thePlayer.rotationYaw);
	mc.thePlayer.motionX = _speed * -Math.sin(playerYaw);
	mc.thePlayer.motionZ = _speed * Math.cos(playerYaw);
}
function setDiagSpeed(_speed) {
	var playerYaw = Math.radians(mc.thePlayer.rotationYaw + 90);
	mc.thePlayer.motionX = _speed * -Math.sin(playerYaw);
	mc.thePlayer.motionZ = _speed * Math.cos(playerYaw);
}
function setMoveSpeed(_speed) {
	if (mc.gameSettings.keyBindLeft.isKeyDown() || mc.gameSettings.keyBindRight.isKeyDown()) {
		setDiagSpeed(_speed*-mc.thePlayer.moveStrafing);
	} else {
		setSpeed(_speed * mc.thePlayer.moveForward);
	}
}
function getSpeed() {
	return Math.sqrt(Math.pow(mc.thePlayer.motionX,2) + Math.pow(mc.thePlayer.motionZ,2))
}
function getRandom(max) {return Math.floor(Math.random() * Math.floor(max))};

var C03 = Java.type("net.minecraft.network.play.client.C03PacketPlayer");
var C04 = Java.type("net.minecraft.network.play.client.C03PacketPlayer.C04PacketPlayerPosition");
var C02 = Java.type("net.minecraft.network.play.client.C03PacketPlayer.C04PacketPlayerPosition");
var S08 = Java.type('net.minecraft.network.play.server.S08PacketPlayerPosLook');
var S12 = Java.type('net.minecraft.network.play.server.S12PacketEntityVelocity');
var BlockPos = Java.type('net.minecraft.util.BlockPos');
var Block = Java.type('net.minecraft.block.Block');
var Blocks = Java.type('net.minecraft.init.Blocks');

Math.radians = function(degrees) {
	return degrees * Math.PI / 180;
};