datablock PlayerData(PlayerMinecraftArmor : PlayerStandardArmor) {
	canJet = 0;
	jumpForce = 9 * 90;

	uiName = "Minecraft Player";
};

function PlayerMinecraftArmor::onNewDataBlock(%this, %obj) {
	parent::onNewDataBlock(%this, %obj);

	%obj.setScale("0.65 0.65 0.65");
	%obj.setDimension(0);

	%obj.chunkTick = %obj.schedule(500, "chunkTick");
	%obj.footstepUpdateTick();
}

function Player::setDimension(%this, %dimension)
{
	if (%dimension $= "") {
		return;
	}

	%this.dimension = %dimension;
	%this.setTransform(getDimensionSpawn(%dimension));
}

function Player::chunkTick(%this) {
	cancel(%this.chunkTick);

	if (!isObject(MCWorld)) {
		%this.chunkTick = %this.schedule(500, "chunkTick");
		return;
	}

	%pos = %this.getTransform();
	%z = getWord(%pos, 2);

	%zl = 1024 * %this.dimension;
	%zh = 1024 * %this.dimension + 256;

	if (%z < %zl || %z > %zh)
	{
		%this.setDimension(%this.dimension);
	}

	if ($MC::Clearing) {
		return;
	}

	%pos = roundToChunkIndex(%this.getPosition());

	%px = getWord(%pos, 0);
	%py = getWord(%pos, 1);

	//%radius = 2;
	%radius = 1;

	for (%x = %radius * -1; %x <= %radius; %x++) {
		for (%y = %radius * -1; %y <= %radius; %y++ ) {
			%npos = %px + %x SPC %py + %y SPC 0;

			if (vectorDist(%pos, %npos) > %radius) {
				continue;
			}

			ChunkSO(%px + %x, %py + %y, %this.dimension);
		}
	}

	if (isObject(%cl=%this.client)) {
		%x = mFloatLength(getWord(%this.position, 0), 0);
		%y = mFloatLength(getWord(%this.position, 1), 0);

		%temp = MCWorld.getTemperature(%x, %y);
		%humi = MCWorld.getHumidity(%x, %y);

		%cl.bottomPrint("\c6Temperature: " @ mFloatLength(%temp*100,0) @ "%\n\c6Humidity: " @ mFloatLength(%humi*100,0) @ "%\n\c6ChunkContainer size: " @ MCWorld.getCount(),1,1);
	}

	%this.chunkTick = %this.schedule(500, "chunkTick");
}

function getDimensionSpawn(%dim) {
	%x = $MC::CenterChunkX * 16 + 8;
	%y = $MC::CenterChunkY * 16 + 8;

	%start = %x SPC %y SPC %dim * 1024 + 256;
	%end = %x SPC %y SPC %dim * 1024;

	%ray = containerRayCast(%start, %end, $TypeMasks::All);

	if ( %ray $= "0" )
	{
		return %x SPC %y SPC 0.1;
	}

	%pos = getWords(%ray, 1, 3);
	%pos = vectorAdd(%pos, "0 0 0.1");

	return %pos;
}
