var script = registerScript({
    name: "SSpeed",
    version: "1.0",
    authors: ["6Sence"]
});

script.on("enable", function () {});

script.on("disable", function () {});

script.on("load", function () {});

var Strafe = moduleManager.getModule("Strafe");
player = Java.type("net.minecraft.entity.Entity")
var C06PlayerPacket = Java.type('net.minecraft.network.play.client.C03PacketPlayer.C06PacketPlayerPosLook');
var C04PacketPlayerPosition = Java.type('net.minecraft.network.play.client.C03PacketPlayer.C04PacketPlayerPosition')
var C05PacketPlayerLook = Java.type('net.minecraft.network.play.client.C03PacketPlayer.C05PacketPlayerLook');
var C03PacketPlayer = Java.type('net.minecraft.network.play.client.C03PacketPlayer');
var S08PacketPlayerPosLook = Java.type("net.minecraft.network.play.server.S08PacketPlayerPosLook");
var packet;

BlockPos = Java.type('net.minecraft.util.BlockPos')
BlockAir = Java.type('net.minecraft.block.BlockAir')
EntityPlayer = Java.type("net.minecraft.entity.player.EntityPlayer")

function randomIntFrom(min, max) // Get a random integer from [min] to [max] 
{
    return Math.floor(Math.random() * (max - min + 1) + min);
}
Math.radians = function (degrees) {
    return degrees * Math.PI / 180;
};

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

function motion(offset) {
    mc.thePlayer.motionX = 0
    mc.thePlayer.motionZ = 0
    if (mc.gameSettings.keyBindRight.pressed && mc.gameSettings.keyBindForward.pressed == false && mc.gameSettings.keyBindLeft.pressed == false && mc.gameSettings.keyBindBack.pressed == false) {
        mc.thePlayer.motionX = parseFloat(Math.cos(Math.radians(mc.thePlayer.rotationYaw)) * offset * -1)
        mc.thePlayer.motionZ = parseFloat(Math.sin(Math.radians(mc.thePlayer.rotationYaw)) * offset * -1)
    }
    if (mc.gameSettings.keyBindForward.pressed && mc.gameSettings.keyBindRight.pressed == false && mc.gameSettings.keyBindLeft.pressed == false && mc.gameSettings.keyBindBack.pressed == false) {
        mc.thePlayer.motionX = parseFloat(Math.cos(Math.radians(mc.thePlayer.rotationYaw + 90.0)) * offset)
        mc.thePlayer.motionZ = parseFloat(Math.sin(Math.radians(mc.thePlayer.rotationYaw + 90.0)) * offset)
    }
    if (mc.gameSettings.keyBindLeft.pressed && mc.gameSettings.keyBindRight.pressed == false && mc.gameSettings.keyBindForward.pressed == false && mc.gameSettings.keyBindBack.pressed == false) {
        mc.thePlayer.motionX = parseFloat(Math.cos(Math.radians(mc.thePlayer.rotationYaw)) * offset)
        mc.thePlayer.motionZ = parseFloat(Math.sin(Math.radians(mc.thePlayer.rotationYaw)) * offset)
    }
    if (mc.gameSettings.keyBindBack.pressed && mc.gameSettings.keyBindRight.pressed == false && mc.gameSettings.keyBindForward.pressed == false && mc.gameSettings.keyBindLeft.pressed == false) {
        mc.thePlayer.motionX = parseFloat(Math.cos(Math.radians(mc.thePlayer.rotationYaw + 90.0)) * offset * -1)
        mc.thePlayer.motionZ = parseFloat(Math.sin(Math.radians(mc.thePlayer.rotationYaw + 90.0)) * offset * -1)
    }

    if (mc.gameSettings.keyBindRight.pressed && mc.gameSettings.keyBindForward.pressed == false && mc.gameSettings.keyBindLeft.pressed == false && mc.gameSettings.keyBindBack.pressed) {
        mc.thePlayer.motionX = parseFloat(Math.cos(Math.radians(mc.thePlayer.rotationYaw + 90.0) + 90) * offset)
        mc.thePlayer.motionZ = parseFloat(Math.sin(Math.radians(mc.thePlayer.rotationYaw + 90.0) + 90) * offset)
    }
    if (mc.gameSettings.keyBindRight.pressed && mc.gameSettings.keyBindForward.pressed && mc.gameSettings.keyBindLeft.pressed == false && mc.gameSettings.keyBindBack.pressed == false) {
        mc.thePlayer.motionX = parseFloat(Math.cos(Math.radians(mc.thePlayer.rotationYaw + 90.0) + 45) * offset)
        mc.thePlayer.motionZ = parseFloat(Math.sin(Math.radians(mc.thePlayer.rotationYaw + 90.0) + 45) * offset)
    }
    if (mc.gameSettings.keyBindRight.pressed == false && mc.gameSettings.keyBindForward.pressed && mc.gameSettings.keyBindLeft.pressed && mc.gameSettings.keyBindBack.pressed == false) {
        mc.thePlayer.motionX = parseFloat(Math.cos(Math.radians(mc.thePlayer.rotationYaw + 90.0) + -45) * offset)
        mc.thePlayer.motionZ = parseFloat(Math.sin(Math.radians(mc.thePlayer.rotationYaw + 90.0) + -45) * offset)
    }
    if (mc.gameSettings.keyBindRight.pressed == false && mc.gameSettings.keyBindForward.pressed == false && mc.gameSettings.keyBindLeft.pressed && mc.gameSettings.keyBindBack.pressed) {
        mc.thePlayer.motionX = parseFloat(Math.cos(Math.radians(mc.thePlayer.rotationYaw + 90.0) - 90) * offset)
        mc.thePlayer.motionZ = parseFloat(Math.sin(Math.radians(mc.thePlayer.rotationYaw + 90.0) - 90) * offset)
    }
}

function vClip(offset) {
    mc.thePlayer.setPosition(mc.thePlayer.posX, mc.thePlayer.posY + offset, mc.thePlayer.posZ);
}

function hClip(offset) {
    var playerYaw = Math.radians(mc.thePlayer.rotationYaw);
    mc.thePlayer.setPosition(mc.thePlayer.posX - (Math.sin(playerYaw) * offset), mc.thePlayer.posY, mc.thePlayer.posZ + (Math.cos(playerYaw) * offset));
}

function rightClip(offset) {
    var playerYaw = Math.radians(mc.thePlayer.rotationYaw);
    mc.thePlayer.setPosition(mc.thePlayer.posX - (Math.sin(playerYaw + 90) * offset), mc.thePlayer.posY, mc.thePlayer.posZ + (Math.cos(playerYaw + 90) * offset));
}

function hMotion(offset) {
    mc.thePlayer.motionX = parseFloat(Math.cos(Math.radians(mc.thePlayer.rotationYaw + 90.0)) * offset)
    mc.thePlayer.motionZ = parseFloat(Math.sin(Math.radians(mc.thePlayer.rotationYaw + 90.0)) * offset)
}

function packetHClip(offset) {
    var playerYaw = Math.radians(mc.thePlayer.rotationYaw);
    mc.thePlayer.sendQueue.addToSendQueue(new C04PacketPlayerPosition(mc.thePlayer.posX - (Math.sin(playerYaw) * offset), mc.thePlayer.posY, mc.thePlayer.posZ + (Math.cos(playerYaw) * offset), true));
}

function packetVClip(offset) {
    var playerYaw = Math.radians(mc.thePlayer.rotationYaw);
    mc.thePlayer.sendQueue.addToSendQueue(new C04PacketPlayerPosition(mc.thePlayer.posX, mc.thePlayer.posY + offset, mc.thePlayer.posZ, true));
}

function isMoving() {
    if (mc.gameSettings.keyBindForward.pressed || mc.gameSettings.keyBindBack.pressed || mc.gameSettings.keyBindLeft.pressed || mc.gameSettings.keyBindRight.pressed) {
        return true;
    } else {
        return false;
    }
}

function onlyForward() {
    if (mc.gameSettings.keyBindRight.pressed == false && mc.gameSettings.keyBindForward.pressed && mc.gameSettings.keyBindLeft.pressed == false && mc.gameSettings.keyBindBack.pressed == false) {
        return true;
    } else {
        return false;
    }
}

function forwardPressed() {
    if (mc.gameSettings.keyBindForward.pressed) {
        return true;
    } else {
        return false;
    }
}

function side() {
    if (mc.gameSettings.keyBindRight.pressed || mc.gameSettings.keyBindLeft.pressed || mc.gameSettings.keyBindBack.pressed) {
        return true;
    } else {
        return false;
    }
}

script.registerModule({
    name: "SSpeed",
    description: "More speed modes by 6Sence",
    category: "Fun",
    tag: "Made with scriptapi2 ;)",
    settings: {
        Mode: Setting.list({
            name: "Mode",
            values: ["Spartan", "CubecraftGround", "MineplexHop", "SentinelHop", "MushMC", "HypixelHop", "NCP", "LegitHop"],
            default: "Spartan"
        })
    },
}, function (module) {
    module.on("enable", function () {
        chat.print("§6Script coded by §56Sence §5and §5HackingBros")
        ticks = 0;
        afterticks = 0;
        hurted = false;
        Speed = 8;
        stop = false;
        jumped = false;
        wasonground = false;
        OPosX = mc.thePlayer.posX;
        OPosY = mc.thePlayer.posY;
        OPosZ = mc.thePlayer.posZ;
        OMotionX = mc.thePlayer.motionX;
        OMotionZ = mc.thePlayer.motionZ;
        offGroundTicks = 0;
        onGroundTicks = 0;
        motion(0.13);
        wasSwinging = false;
        Strafe.setState(true);
        OldSameYValue = Scaffold.getValue("SameY").get();
        Scaffold.getValue("SameY").set(true);
    });
    module.on("disable", function () {
        //Activates on disabling module
        mc.timer.timerSpeed = 1.0;
        mc.thePlayer.speedInAir = 0.0199;
        motion(0.2);
        Strafe.setState(false);
        Scaffold.getValue("SameY").set(OldSameYValue);
    });
    module.on("update", function () {
        //ticks
        ticks += 1;

        if (mc.thePlayer.onGround) {
            offGroundTicks = 0;
            onGroundTicks += 1;
        } else {
            offGroundTicks += 1;
            onGroundTicks = 0;
        }
        mc.gameSettings.keyBindJump.pressed = false;
        switch (module.settings.Mode.get()) {
            case "LegitHop":
                Strafe.setState(false);
                mc.timer.timerSpeed = 1;
                mc.thePlayer.speedInAir = 0.02;
                if (isMoving()) {
                    if (mc.thePlayer.onGround) {
                        mc.thePlayer.jump();
                    }
                }
                break;
            case "Spartan":
                mc.timer.timerSpeed = 1.4325;
                if (mc.thePlayer.ticksExisted % 2 == 0) {
                    mc.timer.timerSpeed = 1.3234;
                }

                if (mc.thePlayer.ticksExisted % 5 == 0) {
                    mc.timer.timerSpeed = 1.752;
                }

                if (mc.thePlayer.onGround == false) {
                    mc.timer.timerSpeed = 1;
                }
                break;
            case "CubecraftGround":
                if (!mc.theWorld.isAirBlock(new BlockPos(mc.thePlayer.posX, mc.thePlayer.posY - 0.21, mc.thePlayer.posZ))) {
                    mc.timer.timerSpeed = 0.94;
                    if (mc.thePlayer.ticksExisted % 20 == 0) {
                        mc.thePlayer.motionY = -0.1;
                    }
                    mc.thePlayer.motionY = 0;
                    if (mc.thePlayer.ticksExisted % 3 == 0) {
                        if (onlyForward()) {
                            motion(2.9);
                        } else if (forwardPressed()) {
                            motion(2);
                        } else if (side()) {
                            motion(0.7);
                        }
                    } else {
                        motion(0.25);
                    }
                    wasonground = true;
                } else if (wasonground) {
                    motion(0.25);
                    mc.timer.timerSpeed = 1;
                    mc.thePlayer.motionY = -0.123001;
                    wasonground = false;
                }
                mc.gameSettings.keyBindSneak.pressed = false;
                break;
            case "MineplexHop":
                mc.timer.timerSpeed = 1;
                mc.thePlayer.speedInAir = 0.039;
                if (mc.thePlayer.fallDistance > 1) {
                    ticks--;
                }
                if (ticks >= 60) {
                    ticks = 60;
                }
                if (isMoving()) {
                    if (mc.thePlayer.onGround) {
                        mc.thePlayer.jump();
                        hMotion(0.001);
                        motion(0.001);
                        jumped = true;
                        mc.thePlayer.onGround = false;
                    }
                    if (offGroundTicks == 1 && jumped) {
                        motion(0.65 + ticks / 100);
                    }

                } else {
                    mc.thePlayer.motionX = 0;
                    mc.thePlayer.motionZ = 0;
                }

                if (!isMoving() || mc.thePlayer.motionX == 0 && mc.thePlayer.motionZ == 0) {
                    ticks = 0;
                    if (mc.thePlayer.hurtTime == 9) {
                        ticks = 0;
                    }
                }
                if (mc.thePlayer.isCollidedHorizontally) {
                    ticks = 10;
                }
                break;
            case "SentinelHop":
                if (mc.thePlayer.motionY == 0) {
                    chat.print("Test")
                }
                if (mc.thePlayer.onGround) {
                    motion(0);
                    if (isMoving()) {
                        mc.thePlayer.jump()
                    }
                }
                if (isMoving()) {
                    mc.timer.timerSpeed = 0.98;
                    if (offGroundTicks == 1) {
                        if (mc.thePlayer.isSwingInProgress) {
                            motion(0.49);
                        } else {
                            if (forwardPressed()) {
                                motion(0.55);
                            }
                        }
                    }
                    if (mc.thePlayer.onGround) {
                        motion(0.45);
                    }
                } else {
                    motion(0);
                }
                if (mc.thePlayer.isCollidedHorizontally) {
                    motion(0.1);
                }

                mc.thePlayer.speedInAir = 0.02;
                break;
            case "MushMC":
                if (mc.gameSettings.keyBindForward.pressed || mc.gameSettings.keyBindBack.pressed || mc.gameSettings.keyBindLeft.pressed || mc.gameSettings.keyBindRight.pressed) {
                    if (mc.thePlayer.onGround) {
                        wasonground = true;
                        mc.timer.timerSpeed = 1.4;
                        mc.thePlayer.jump();
                        mc.thePlayer.motionY = 0.5;
                        motion(1.4);
                        mc.thePlayer.onGround = false;
                    } else {
                        mc.thePlayer.speedInAir = 0.0999;
                        mc.timer.timerSpeed = 0.97;
                    }
                } else {
                    mc.thePlayer.motionX = 0;
                    mc.thePlayer.motionZ = 0;
                }

                if (mc.thePlayer.motionY < 0 && mc.thePlayer.fallDistance < 4) {
                    mc.thePlayer.motionY *= 1.3;
                }

                break;
            case "HypixelHop":
                if (isMoving()) {
                    chat.print(ticks);
                    if (mc.thePlayer.onGround) {
                        mc.thePlayer.jump();
                    }
                    if (offGroundTicks == 11) {
                        //motion(0.5);
                    }
                    mc.thePlayer.speedInAir = 0.022;
                }
                break;
            case "NCP":
                mc.timer.timerSpeed = 0.995;
                if (mc.thePlayer.onGround && isMoving()) {
                    mc.thePlayer.jump();
                }
                if (offGroundTicks == 1 || mc.thePlayer.onGround){
                    mc.thePlayer.motionX *= 1.054;
                    mc.thePlayer.motionZ *= 1.054;
                    mc.timer.timerSpeed = 1.2;
                }
                if (offGroundTicks == 2){
                    mc.thePlayer.motionX *= 0.99;
                    mc.thePlayer.motionZ *= 0.99;
                }
                break;
            default:
        }
    });
});