var scriptName = "magnet";
var scriptVersion = 1.0;
var scriptAuthor = "Soulplexis";

var Swords = Java.type('net.minecraft.item.ItemSword');
var BlockPos = Java.type('net.minecraft.util.BlockPos');
var C07PacketPlayerDigging = Java.type('net.minecraft.network.play.client.C07PacketPlayerDigging');
var EnumFacing = Java.type('net.minecraft.util.EnumFacing');
var util = Java.type("net.minecraft.util.MovementInput");
var MathHelper = Java.type("net.minecraft.util.MathHelper");
var orb = Java.type("net.minecraft.entity.item.EntityXPOrb");
var item = Java.type("net.minecraft.entity.item.EntityItem");
var PlayerPosition = Java.type('net.minecraft.network.play.client.C03PacketPlayer.C04PacketPlayerPosition')
 // Radians -> Degrees
 Math.rad = function(deg) {
    return deg * Math.PI / 180;
  };
	function getClosestItem(){
	var filteredEntites = []
	for (var i in mc.theWorld.loadedEntityList){
		var entity = mc.theWorld.loadedEntityList[i]

		if (entity instanceof item && entity !=mc.thePlayer){
			filteredEntites.push(entity)
		}
	}
	filteredEntites.sort(function(a, b){
		var distanceA = mc.thePlayer.getDistanceToEntity(a)
		var distanceB = mc.thePlayer.getDistanceToEntity(b)

		return distanceB - distanceA;
	})
	return filteredEntites[filteredEntites.length - 1]
}

	function getClosestItemandXP(){
	var filteredEntites = []
	for (var i in mc.theWorld.loadedEntityList){
		var entity = mc.theWorld.loadedEntityList[i]

		if (entity instanceof orb || entity instanceof item  && entity !=mc.thePlayer){
			filteredEntites.push(entity)
		}
	}
	filteredEntites.sort(function(a, b){
		var distanceA = mc.thePlayer.getDistanceToEntity(a)
		var distanceB = mc.thePlayer.getDistanceToEntity(b)

		return distanceB - distanceA;
	})
	return filteredEntites[filteredEntites.length - 1]
}



function Strafe() {
	var XPOrbs = value.createBoolean("XPOrbs", true);
	var Range = value.createInteger("Range", 25, 0, 100);
	var Delay = value.createInteger("TPDelay", 25, 0, 100);
    this.getName = function () {
        return "Magnet";
    };

    this.getDescription = function () {
        return "Gets all items in range through teleportation.";
    };

    this.getCategory = function () {
        return "Fun";
    };
	
	var time = 0;
	
    this.onUpdate = function () {
		time++;
		
	var listz = mc.theWorld.loadedEntityList;
     for (var z = 0;z < listz.size(); z++) {
    		var entity = mc.theWorld.loadedEntityList[z];		
			if(time >= Delay.get()) {
			if(XPOrbs.get() == false) {
				if(mc.thePlayer.getDistanceToEntity(getClosestItem()) <= Range.get()) {
			mc.thePlayer.sendQueue.addToSendQueue(new PlayerPosition(getClosestItem().posX, getClosestItem().posY, getClosestItem().posZ, false));
				}
			} else {
							if(mc.thePlayer.getDistanceToEntity(getClosestItemandXP()) <= Range.get()) {
				mc.thePlayer.sendQueue.addToSendQueue(new PlayerPosition(getClosestItemandXP().posX, getClosestItemandXP().posY, getClosestItemandXP().posZ, false));
							}
			}
			time = 0;
			}
    }
	}
    this.onDisable = function() {
    }
    this.onEnable = function() {
    }

    this.addValues = function(values) {
		values.add(Range);
		values.add(Delay);
		values.add(XPOrbs);
    }
}

var Strafe = new Strafe();
var StrafeClient;

function onEnable() {
	Strafe = moduleManager.registerModule(Strafe);
};

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