var script = registerScript({
    name: "AsFlyPro",
    version: "1.0",
    authors: ["As丶One"]
});
var LiquidBounce = Java.type("net.ccbluex.liquidbounce.LiquidBounce")
var MovementUtils = Java.type("net.ccbluex.liquidbounce.utils.MovementUtils")
var classProvider = LiquidBounce.INSTANCE.getWrapper().getClassProvider();
var packets = []
var doAsFly = false
var doClipY = false
var stage = 0
var maxStage = 5
var jump = false
var timer = 0
var MotionCheck = 0
var Mode = Setting.list({
    name: "Mode",
    values: ["Custom", "Fast", "Fast2", "OldRedesky", "Clip", "ClipFloat", "Slow", "UpClip"],
    default: "Custom"
})
var YMode = Setting.list({
    name: "YMode",
    values: ["MotionY", "Clip"],
    default: "MotionY"
})
var ThirdPersonView = Setting.list({
    name: "ThirdPersonView",
    values: ["One", "Two", "Three", "Off"],
    default: "Two"
})
var motionY = Setting.float({
    name: "MotionY",
    default: 1.8,
    min: 0.42,
    max: 6
})
var ClipY = Setting.integer({
    name: "ClipY",
    default: 5,
    min: 2,
    max: 30
})
var ClipH = Setting.integer({
    name: "ClipH",
    default: 5,
    min: 2,
    max: 30
})
var motionXZ = Setting.float({
    name: "MotionXZ",
    default: 2.5,
    min: 0,
    max: 10
})
var Bob = Setting.float({
    name: "Bob",
    default: 0.5,
    min: 0,
    max: 2
})
var delay = Setting.integer({
    name: "Delay",
    default: 2,
    min: 0,
    max: 10
})
var Clipdelay = Setting.integer({
    name: "Clipdelay",
    default: 0,
    min: 0,
    max: 10
})
var BindSlowSpeed = Setting.integer({
    name: "BindSlowSpeed",
    default: 7,
    min: 1,
    max: 10
})
var DownY = Setting.float({
    name: "DownY",
    default: 1.25,
    min: 0.42,
    max: 2
})
var AntiLagDelay = Setting.float({
    name: "AntiLagDelay",
    default: 2,
    min: 1,
    max: 6
})
var NormalDownY = Setting.float({
    name: "NormalDownY",
    default: 0.05,
    min: 0.01,
    max: 2
})
var y = 0
var Delay = 0
function move() {
	Delay++
    mc.thePlayer.cameraYaw = Bob.get()
    if (Mode.get() == "Custom") {
        if (mc.thePlayer.posY <= y && mc.gameSettings.keyBindSneak.isKeyDown() == false) {
            MotionCheck = MotionCheck - 1
            if (YMode.get() == "MotionY") {
                mc.thePlayer.motionY = motionY.get()
            }
            if (YMode.get() == "Clip") {
                Clipy(ClipY.get());
            }
            if (MotionCheck > 0 || MotionCheck < 0) {
                chat.print("§e[§cAsFly§e]§bMotionCheck:" + MotionCheck)
            } else {
                chat.print("§e[§cAsFly§e]§bMotionCheck:" + MotionCheck + " " + "Next MotionY you will be Kicked")
            }

        } else {
            if (mc.gameSettings.keyBindForward.isKeyDown()) {
                var dir = mc.thePlayer.rotationYaw / 180 * Math.PI
                if (mc.thePlayer.motionY < 0 && mc.gameSettings.keyBindSneak.isKeyDown() == false) mc.thePlayer.motionY = 0 - NormalDownY.get()
                if (mc.thePlayer.motionY < 0 && mc.gameSettings.keyBindSneak.isKeyDown() == true) mc.thePlayer.motionY = 0 - DownY.get()
                mc.thePlayer.motionX = -Math.sin(dir) * motionXZ.get()
                mc.thePlayer.motionZ = Math.cos(dir) * motionXZ.get()
            } else {
                if (mc.thePlayer.motionY < 0 && mc.gameSettings.keyBindSneak.isKeyDown() == false) mc.thePlayer.motionY = 0 - NormalDownY.get()
                if (mc.thePlayer.motionY < 0 && mc.gameSettings.keyBindSneak.isKeyDown() == true) mc.thePlayer.motionY = 0 - DownY.get()
            }
        }
    }
    if (Mode.get() == "Fast") {
        if (mc.thePlayer.posY <= y) {
            mc.thePlayer.motionY = 0.42
        }
        var dir = mc.thePlayer.rotationYaw / 180 * Math.PI
        if (mc.thePlayer.motionY < 0) mc.thePlayer.motionY = 0 - NormalDownY.get()
        mc.thePlayer.motionX = -Math.sin(dir) * 0.8
        mc.thePlayer.motionZ = Math.cos(dir) * 0.8
    }
    if (Mode.get() == "Fast2") {
        if (mc.thePlayer.posY <= y) {
            mc.thePlayer.motionY = 0.62
        }
        var dir = mc.thePlayer.rotationYaw / 180 * Math.PI
        if (mc.thePlayer.motionY < 0) mc.thePlayer.motionY = 0 - NormalDownY.get()
        mc.thePlayer.motionX = -Math.sin(dir) * 0.61
        mc.thePlayer.motionZ = Math.cos(dir) * 0.61
    }
    if (Mode.get() == "Oldredesky") {
        if (mc.thePlayer.posY <= y) {
            mc.thePlayer.motionY = 0.42
        }
        var dir = mc.thePlayer.rotationYaw / 180 * Math.PI
        if (mc.thePlayer.motionY < 0) mc.thePlayer.motionY = 0 - NormalDownY.get()
        mc.thePlayer.motionX = -Math.sin(dir) * 0.4
        mc.thePlayer.motionZ = Math.cos(dir) * 0.4
    }
    if (Mode.get() == "Slow") {
        if (mc.thePlayer.posY <= y) {
            mc.thePlayer.motionY = 0.42
        }
        var dir = mc.thePlayer.rotationYaw / 180 * Math.PI
        if (mc.thePlayer.motionY < 0) mc.thePlayer.motionY = 0 - NormalDownY.get()
        mc.thePlayer.motionX = -Math.sin(dir) * 0.41
        mc.thePlayer.motionZ = Math.cos(dir) * 0.41
    }
	    if (Mode.get() == "Clip") {
		mc.thePlayer.motionY = 0
        if (mc.thePlayer.posY <= y) {
            Clipy(Clipy.get());
        }
   if(Delay > Clipdelay.get()){
   hClip(ClipH.get());
   Delay = 0
   }
    }
	if (Mode.get() == "ClipFloat") {
		mc.thePlayer.motionY = 0
        if (mc.thePlayer.posY <= y) {
            Clipy(5);
        }
   if(Delay > Clipdelay.get()){
   hClip(ClipH.get());
   Delay = 0
   }
    }
   	if (Mode.get() == "UpClip") {
        if (mc.thePlayer.posY <= y) {
            Clipy(5);
        }
   if(Delay > Clipdelay.get()){
   hClip(5);
   Clipy(Clipy.get());
   Delay = 0
   }
    }

}
script.registerModule({
    name: "AsFlyPro",
    description: "AsFlyPro By As丶One",
    category: "Fun",
    settings: {
        Mode: Mode,
        YMode: YMode,
        ThirdPersonView: ThirdPersonView,
        motionY: motionY,
        ClipY: ClipY,
		Clipdelay: Clipdelay,
		ClipH: ClipH,
        motionXZ: motionXZ,
        delay: delay,
        BindSlowSpeed: BindSlowSpeed,
        AntiLagDelay: AntiLagDelay,
        DownY: DownY,
        NormalDownY: NormalDownY,
        Bob: Bob
    }
}, function (module) {
    var File = Java.type("java.io.File");
    var FileReader = Java.type("java.io.FileReader");
    var BufferedReader = Java.type("java.io.BufferedReader");
    var LiquidBounce = Java.type("net.ccbluex.liquidbounce.LiquidBounce")
    var classProvider = LiquidBounce.INSTANCE.getWrapper().getClassProvider();
    var FileManager = LiquidBounce.fileManager
    var version = Java.type("net.ccbluex.liquidbounce.injection.backend.Backend").MINECRAFT_VERSION
    var sendPacket;
    if (version == "1.8.9") {
        sendPacket = function (packet) {
            try {
                imc.getNetHandler().addToSendQueue(packet)
            } catch (error) {
                mc.getNetHandler().addToSendQueue(packet)
            }
        }
    } else {
        sendPacket = function (packet) {
            try {
                mc.netHandler.networkManager.sendPacket(packet)
            } catch (error) {
                imc.getNetHandler().addToSendQueue(packet)
            }
        }
    }

    function readFile(filePath) {
        var file = new File(filePath);
        var reader = new BufferedReader(new FileReader(file));
        var content = "";
        var line;
        while ((line = reader.readLine()) !== null) content += line
        return content;
    }
    //chat.print(FileManager.hudConfig)
    //chat.print(Java.from(FileManager.class.getDeclaredFields()))
    var image = "IamgeCheck"
    var hudConfig = eval(readFile(FileManager.hudConfig.getFile()))
    var flag = true
    for (var i in hudConfig) {
        if (hudConfig[i].Type == "Image" && hudConfig[i].Image == image) flag = false
    }
    //if (flag) chat.print(FileManager.hudConfig + " " + "AsoneHwidCheck:CookieConfig")
    //if (flag) return;
    module.on("enable", function () {
        y = mc.thePlayer.posY
        timer = 999
        MotionCheck = 4
        //chat.print("§e[§cAsFly§e]§bStartMotionY:" + motionY.get())
        //chat.print(FileManager.hudConfig + " " + "AsoneHwidCheck:CookieConfig")
    });
    module.on("disable", function () {
        mc.gameSettings.thirdPersonView = 0;
        mc.timer.timerSpeed = 1;
        MotionCheck = 0
		Delay = 0
        packets.forEach(function () {
            sendPacket(packets.shift())
        })
    });
    module.on("update", function () {
        if (ThirdPersonView.get() == "One") {
            mc.gameSettings.thirdPersonView = 0
        }
		if (Mode.get() == "ClipFloat") {
           mc.thePlayer.motionY = 0
        }
        if (Mode.get() == "UpClip") {
        if(Delay < Clipdelay.get()){
        mc.thePlayer.motionY = 0
        mc.thePlayer.motionZ = 0
        mc.thePlayer.motionX = 0    
        }
        }
        if (ThirdPersonView.get() == "Two") {
            mc.gameSettings.thirdPersonView = 1
        }
        if (ThirdPersonView.get() == "Three") {
            mc.gameSettings.thirdPersonView = 2
        }
        if (!doAsFly) timer++
        if (timer > delay.get()) {
            timer = 0
            doAsFly = true
            stage = 0
            move()
        }
        if (mc.gameSettings.keyBindJump.isKeyDown()) {
            mc.timer.timerSpeed = BindSlowSpeed.get() / 10
        } else {
            mc.timer.timerSpeed = 1;
        }
        if (stage >= 1) {

            doAsFly = false
            packets.forEach(function () {
                sendPacket(packets.shift())
            })
        }
    });
    module.on("packet", function (event) {
        if (!doAsFly) return
        var packet = event.packet
        if (classProvider.isCPacketPlayer(packet)) event.cancelEvent()
        if (classProvider.isCPacketPlayerPosition(packet) || classProvider.isCPacketPlayerPosLook(packet) ||
            classProvider.isCPacketPlayerBlockPlacement(packet) ||
            classProvider.isCPacketAnimation(packet) ||
            classProvider.isCPacketEntityAction(packet) || classProvider.isCPacketUseEntity(packet)) {
            event.cancelEvent()
            packets.push(packet)
            stage++
        }
    });
});
//by Baguette Fly
function Clipy(d) {
    mc.thePlayer.setPosition(mc.thePlayer.posX, mc.thePlayer.posY + d, mc.thePlayer.posZ);
}
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));
}
Math.radians = function(degrees) {
	return degrees * Math.PI / 180;
};