var scriptName = "Displacer";
var scriptVersion = 1.0;
var scriptAuthor = "soulplexis";

var PlayerPositionLook = Java.type('net.minecraft.network.play.client.C03PacketPlayer.C06PacketPlayerPosLook');
var PlayerLook = Java.type('net.minecraft.network.play.client.C03PacketPlayer.C05PacketPlayerLook');
var PlayerPosition = Java.type('net.minecraft.network.play.client.C03PacketPlayer.C04PacketPlayerPosition');

var Module = new Module();
var ModuleClient;


// 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 Module() {
    var VDistance = value.createFloat("VerticalDisplacement", 0.2, -6, 6);
    var HDistance = value.createFloat("HorizontalDisplacement", 0, -6, 6);
    var Vertical = value.createBoolean("Vertical", true);
    var Horizontal = value.createBoolean("Horizontal", true);
    var OnGround = value.createBoolean("OnGround", true);
    var Hypixel = value.createBoolean("DelayOnEnable", true);
    var DelayB = value.createBoolean("Delay", true);
    var Delay = value.createInteger("Ticks", 3, 0, 20);
    var delay = 0;
    var Debug = value.createBoolean("Debug", false);
    var swics = false;
    this.getName = function() {
        return "Displacer";
    };

    this.getDescription = function() {
        return "Displaces the player slightly.";
    };

    this.getCategory = function() {
        return "Movement";
    };
    this.onEnable = function() {
        delay = 0;
    }
    this.onUpdate = function() {
        if (delay < Delay.get()) {
            delay++
        }
    }

    this.onPacket = function(event) {
        var packet = event.getPacket();
        if (((delay >= Delay.get() && DelayB.get()) || DelayB.get() == false)) {
            if (Hypixel.get() == false) {
                delay = 0;
            }
            if (packet instanceof PlayerPosition || packet instanceof PlayerPositionLook) {
                if (Horizontal.get()) {
                    if (Debug.get()) {
                        swics = !swics
                        if (swics) {
                            chat.print("§c[Debug] §7Displaced H: " + HDistance.get() + " G: " + OnGround.get());
                        } else {
                            chat.print("§c[Debug] §fDisplaced H: " + HDistance.get() + " G: " + OnGround.get());
                        }
                    }
                    packet.x = (mc.thePlayer.posX - (Math.sin(Math.radians(mc.thePlayer.rotationYaw)) * HDistance.get()));
                    packet.z = (mc.thePlayer.posZ + (Math.cos(Math.radians(mc.thePlayer.rotationYaw)) * HDistance.get()));
                }
                if (Vertical.get()) {
                    packet.y = mc.thePlayer.posY + VDistance.get();
                    if (Debug.get()) {
                        swics = !swics
                        if (swics) {
                            chat.print("§c[Debug] §7Displaced V " + VDistance.get() + " G: " + OnGround.get());
                        } else {
                            chat.print("§c[Debug] §fDisplaced V " + VDistance.get() + " G: " + OnGround.get());
                        }
                    }
                }
                packet.onGround = OnGround.get();
            }
        }
    }
    this.addValues = function(values) {
        values.add(Horizontal);
        values.add(HDistance);
        values.add(Vertical);
        values.add(VDistance);
        values.add(OnGround);
        values.add(Hypixel);
        values.add(DelayB);
        values.add(Delay);
        values.add(Debug);
    }
}


function onLoad() {};

function onEnable() {
    ModuleClient = moduleManager.registerModule(Module);
};

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