var scriptName = "CleanJoelDamage";
var scriptVersion = 1.0;
var scriptAuthor = "yorik100";
var C03PacketPlayer = Java.type('net.minecraft.network.play.client.C03PacketPlayer');
var S12PacketEntityVelocity = Java.type('net.minecraft.network.play.server.S12PacketEntityVelocity');
var S27PacketExplosion = Java.type('net.minecraft.network.play.server.S27PacketExplosion');

var Timer = Java.type("java.util.Timer");

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

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

function setInterval(func, milliseconds) {
    var timer = new Timer("setInterval", true);
    timer.schedule(function() {
        func();
    }, milliseconds, milliseconds);

    return timer;
}

function clearInterval(timer) {
    timer.cancel();
}

function setTimeout(func, milliseconds) {
    var timer = new Timer("setTimeout", true);
    timer.schedule(function() {
        func();
    }, milliseconds);

    return timer;
}

function clearTimeout(timer) {
    timer.cancel();
}

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

function getMoveYaw() {
    var moveYaw = mc.thePlayer.rotationYaw;
    if (mc.thePlayer.moveForward != 0 && mc.thePlayer.moveStrafing == 0) {
        moveYaw += mc.thePlayer.moveForward > 0 ? 0 : 180;
    } else if (mc.thePlayer.moveForward != 0 && mc.thePlayer.moveStrafing != 0) {
        if (mc.thePlayer.moveForward > 0)
            moveYaw += mc.thePlayer.moveStrafing > 0 ? -45 : 45;
        else
            moveYaw -= mc.thePlayer.moveStrafing > 0 ? -45 : 45;

        moveYaw += mc.thePlayer.moveForward > 0 ? 0 : 180;
    } else if (mc.thePlayer.moveStrafing != 0 && mc.thePlayer.moveForward == 0) {
        moveYaw += mc.thePlayer.moveStrafing > 0 ? -90 : 90;
    }
    return moveYaw;
}

function setMoveSpeed(speed) {
    var yaw = getMoveYaw();
    if (mc.thePlayer.moveForward != 0 || mc.thePlayer.moveStrafing != 0) {
        mc.thePlayer.motionX = -Math.sin(Math.radians(yaw)) * speed;
        mc.thePlayer.motionZ = Math.cos(Math.radians(yaw)) * speed;
    }
}

var cleanJoelDamage = new CleanJoelDamage();

var client;

function CleanJoelDamage() {
    this.getName = function() {
        return "JoelSelfDamage";
    };

    this.getDescription = function() {
        return "JoelDamage";
    };

    this.getCategory = function() {
        return "Exploit";
    };
    var AutoDisable = value.createBoolean("AutoDisable", true);
    var DamageBoost = value.createBoolean("DamageBoost", false);
    var DamageBoostSpeed = value.createFloat("DamageBoostSpeed", 5, 0.00, 10.00);
    var DamageBoostHeight = value.createFloat("DamageBoostHeight", 2, 0.00, 10.00);
    this.addValues = function(values) {
        values.add(AutoDisable);
        values.add(DamageBoost);
        values.add(DamageBoostSpeed);
        values.add(DamageBoostHeight);
    }
    var firstonground = false;
    this.onEnable = function() {
        joel = 0;
        disabling = false;
        once = false;
        packetonce = false;
        if (mc.thePlayer.onGround) {
            firstonground = true;
        }
    }
    var joel = 0;
    this.onPacket = function(event) {
        var packet = event.getPacket();
        if (packet instanceof C03PacketPlayer && mc.thePlayer.hurtTime > 8 && packetonce == false) {
            packetonce = true;
            packet.onGround = true;
			joel = 5;
            if (DamageBoost.get() && once == false) {
                if (DamageBoostSpeed.get() != 0) {
                    setMoveSpeed(DamageBoostSpeed.get())
                }
                if (DamageBoostHeight.get() != 0) {
                    mc.thePlayer.motionY = DamageBoostHeight.get()
                }
                once = true
            }
            if (AutoDisable.get()) {
                var autoDisableModule = moduleManager.getModule("JoelSelfDamage");
                autoDisableModule.setState(false);
            }
        }
        if (packet instanceof C03PacketPlayer && joel < 4 && firstonground == true && !((joel >= 3 && mc.thePlayer.onGround) || joel >= 4) && !(mc.thePlayer.hurtTime > 8)) {
            packet.onGround = false
        }
        if (packet instanceof S27PacketExplosion && joel < 4) {
            event.cancelEvent();
        }
        if (packet instanceof S12PacketEntityVelocity && mc.theWorld.getEntityByID(packet.getEntityID()) == mc.thePlayer && joel < 4) {
            event.cancelEvent();
        }
    }
    this.onUpdate = function() {
        if (mc.thePlayer.onGround && firstonground == false) {
            firstonground = true;
        }
        if (mc.thePlayer.isOnLadder() && mc.thePlayer.isInWater() && mc.thePlayer.isInLava() && mc.thePlayer.isInWeb) {
            firstonground = false;
            joel = 0;
        }
        if (mc.thePlayer.onGround && firstonground == true) {
            joel++;
            if (joel < 4) {
                mc.thePlayer.jump();
            }
        }
        if (joel >= 4 && AutoDisable.get() && disabling == false) {
            disabling = true;
            setTimeout(function() {
                if (joel >= 4) {
                    var autoDisableModule = moduleManager.getModule("JoelSelfDamage");
                    autoDisableModule.setState(false);
                }
            }, 1000);
        }
    }
    this.onMove = function(event) {
        if (joel < 4 && firstonground == true) {
            event.setX(0)
            event.setZ(0)
            mc.thePlayer.motionX = 0
            mc.thePlayer.motionZ = 0
        }
    }
    this.onDisable = function() {
        firstonground = false;
    }
}

function onLoad() {}

function onEnable() {
    client = moduleManager.registerModule(cleanJoelDamage);
}

function onDisable() {
    moduleManager.unregisterModule(client);
}