var ctx=com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
var apWindow=null,button=null;
var menux=0,menuy=150;   
/*背包整理*/
function ISor(){try{
	var isl=[],pisl=[];
	for(var i=0;i<8;i++){
		pisl.push([Player.getInventorySlot(i),Player.getInventorySlotCount(i),Player.getInventorySlotData(i),Player.getEnchantments(i)]);
		Player.clearInventorySlot(i);
	}
	for(var i=8;i<=44;i++){
		isl.push([Player.getInventorySlot(i),Player.getInventorySlotCount(i),Player.getInventorySlotData(i),Player.getEnchantments(i)]);
		Player.clearInventorySlot(i);
	}
	//clientMessage("IN: "+isl);
	var sois=isl.sort(function (a,b){return a[2]-b[2];}).sort(function (a,b){return a[0]-b[0];});
	//clientMessage("OUT: "+sois);
	for(var i in pisl){Player.addItemInventory(pisl[i][0],pisl[i][1],pisl[i][2]);
		if(pisl[i][3]!=null)for(var ie in pisl[i][3])Player.enchant(i,pisl[i][3][ie].type, pisl[i][3][ie].level);
	}
	for(var i in sois){
		Player.addItemInventory(sois[i][0],sois[i][1],sois[i][2]);
		if(sois[i][3]!=null)for(var ie in sois[i][3])Player.enchant(i, sois[i][3][ie].type, sois[i][3][ie].level);
	}
	}catch(err){print("I排序错误: "+err);}
}

/*暂停使用;箱子整理
function CSor(x,y,z,coi){try{
	var csl=[];
	for(var i=0;i<=coi;i++){
		csl.push([Level.getChestSlot(x,y,z,i),Level.getChestSlotCount(x,y,z,i),Level.getChestSlotData(x,y,z,i)]);
	}
	//clientMessage("CIN: "+csl);
	var csois=csl.sort(function (a,b){return a[2]-b[2];}).sort(function (a,b){return a[0]-b[0];}).sort(function (a,b){return (a[0]==0||b[0]==0)?-1:0;});
	//clientMessage("COUT: "+csois);
	for(var i in csois){
		Level.setChestSlot(x,y,z,i,csois[i][0],csois[i][2],csois[i][1]);
	}
	}catch(err){print("C排序错误: "+err);}
}*/

/*按钮*/
function newL(boo){try{
if(boo){
	ctx.runOnUiThread(new java.lang.Runnable({run:function(){
	var layout = new android.widget.LinearLayout(ctx);
	var dx=menux,dy=menuy;
	var canmove=false;
	button =new android.widget.Button(ctx);
    button.setText("R");
	button.setTextSize(30);
	button.setTextColor(android.graphics.Color.WHITE);
	button.setShadowLayer(255,0,0,android.graphics.Color.argb(200,200,200,200));
	button.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.TRANSPARENT));
	button.setOnClickListener(new android.view.View.OnClickListener({
		onClick: function(v){
			Level.playSoundEnt(Player.getEntity(),"random.click",100,0);
			/*
			var x=Player.getPointedBlockX();
			var y=Player.getPointedBlockY();
			var z=Player.getPointedBlockZ();
			if(getTile(x,y,z)==54)CSor(x,y,z,26);else 
			*/
			ISor();
		}
	}));
	button.setOnLongClickListener(new android.view.View.OnLongClickListener({
		onLongClick:function(v){
			if(canmove==false){
				ctx.getSystemService(android.content.Context.VIBRATOR_SERVICE).vibrate(40);//震动提醒
				canmove=true;//长按启动移动算法
			}
			return true;
		}
	}));
/*移动算法 感谢setTile工作室*/
	button.setOnTouchListener(new android.view.View.OnTouchListener(
	{
		onTouch:function(v,e){
			if(e.getAction()==android.view.MotionEvent.ACTION_DOWN) 
			{
				dx=e.getX();
				dy=e.getY();
			}/*记录初始坐标*/
			if(canmove==true) 
			{
				menux+=parseInt((e.getX()-dx)/3);
				menuy+=-parseInt((e.getY()-dy)/3);
				apWindow.update(menux, menuy, -1, -1);  
			}/*移动算法*/
			if(e.getAction()==android.view.MotionEvent.ACTION_UP)   //松开时，将长按设置为false
			{
				canmove=false;
			}
		return false;
		}
	}));
	layout.addView(button);//显示按钮
/*隐形背景*/
	apWindow = new android.widget.PopupWindow(layout, dip2px(ctx,50), dip2px(ctx,50));  
	apWindow.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.TRANSPARENT));//菜单颜色透明
	apWindow.showAtLocation(ctx.getWindow().getDecorView(), android.view.Gravity.LEFT| android.view.Gravity.BOTTOM, dip2px(ctx,menux), dip2px(ctx,menuy));
	}}));
	}else{
		ctx.runOnUiThread(new java.lang.Runnable({
			run: function(){
				apWindow.dismiss();
			}
		}));
	}}catch(err){print("按钮错误: "+err);}
}
function dip2px(ctx, dips){
	return Math.ceil(dips * ctx.getResources().getDisplayMetrics().density); 
}

//进入存档后召唤按钮
function newLevel(){
	newL(true);
}
function leaveGame() {
	newL(false);
}
