/// api_version=2
var script = registerScript({
    name: "BridgeHelper",
    version: "1.0.0",
    authors: ["Qther"]
});

"use strict";

var Items = Java.type("net.minecraft.init.Items");
var ItemBlock = Java.type("net.minecraft.item.ItemBlock");
var Blocks = Java.type("net.minecraft.init.Blocks");
var BlockFalling = Java.type("net.minecraft.block.BlockFalling");
var MovingObjectPosition = Java.type("net.minecraft.util.MovingObjectPosition");
var Material = Java.type("net.minecraft.block.material.Material");
var Vec3 = Java.type("net.minecraft.util.Vec3");
var EnumFacing = Java.type("net.minecraft.util.EnumFacing");

var delay = 0;
var horizontalFaces = [EnumFacing.NORTH, EnumFacing.SOUTH, EnumFacing.EAST, EnumFacing.WEST];

script.registerModule({
    name: "BridgeHelper",
    description: "Automatically places blocks",
    category: "World",
    settings: {
        Mode: Setting.list({
            name: "Mode",
            default: "Both",
            values: ["Horizontal", "Vertical", "Both"]
        }),
        DelayB: Setting.boolean({
            name: "Delay",
            default: false
        }),
        Delay: Setting.integer({
            name: "Ticks",
            min: 0,
            max: 20,
            default: 0
        }),
        Range: Setting.float({
            name: "Range",
            min: 0.0,
            max: 8.0,
            default: 4.0
        }),
        EnsureNewtonIsNotObeyedByHeldItem: Setting.boolean({
            name: "EnsureAntiGravity",
            default: true
        })
    }
}, function (module) {
    module.on("enable", function () {
        delay = 0;
    });

    module.on("disable", function () {
        delay = 0;
    });

    module.on("update", function () {
        if (module.settings.DelayB.get() && delay != 0) { // DelayB is true and delay is not 0
            --delay;
        } 
        else {
            var heldItemStack = mc.thePlayer.inventory.getCurrentItem();
            var heldItem = heldItemStack.getItem();
            var block;
            if (heldItem instanceof ItemBlock) {
                var mop = mc.objectMouseOver;
                
                if (mop.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {

                    block = heldItem.block;
                    var blockPos = mop.getBlockPos();
                    var hitSide = mop.sideHit;
                    var blockHit = mc.theWorld.getBlockState(blockPos).getBlock();
                    var hitVec = mop.hitVec;
                    var eyeVec = mc.thePlayer.getPositionEyes(1.0);
                    var isHorizontalFace = horizontalFaces.indexOf(hitSide) != -1;
    
                    if (((module.settings.Mode.get() === "Horizontal" && isHorizontalFace) ||       // Mode is Horizontal and hitSide is horizontal face, or
                            (module.settings.Mode.get() === "Vertical" && !isHorizontalFace) ||     // Mode is Vertical and hitSide is not horizontal face, or
                            (module.settings.Mode.get() === "Both")) &&                             // Mode is Both.

                        blockHit.getMaterial() != Material.air &&                                   // blockHit's material is not air.

                        !(module.settings.EnsureNewtonIsNotObeyedByHeldItem.get() &&                // If EnsureAntiGravity is true,
                            block instanceof BlockFalling) &&                                       // block is not an instance of BlockFalling.

                        eyeVec.distanceTo(hitVec) <= module.settings.Range.get()) {                 // Distance from hitVec to eyeVec is less than Range.
                        
                        if (mc.playerController.onPlayerRightClick(mc.thePlayer, mc.theWorld, heldItemStack, blockPos, hitSide, hitVec)) {
                            mc.thePlayer.swingItem();
                            //mc.rightClickDelayTimer = module.settings.Delay.get();
                            delay = module.settings.Delay.get(); // Reset delay
                        }
                    }
                    delay = module.settings.Delay.get(); // Reset delay
                }
            }
        }
    });
});