Skip to content

ModPE Script Templates

KillerBLS edited this page May 23, 2015 · 33 revisions

GUI Scripting Templates

If you use any of these templates, please make sure that you use different variables for your layout, buttons and popup windows. The only variable that you may keep as it is "ctx" which is the Minecraft PE activity.

Custom UI Setup:

var GUI;

function newLevel(){
    var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
    ctx.runOnUiThread(new java.lang.Runnable({ run: function(){
        try{
            var layout = new android.widget.LinearLayout(ctx);
            layout.setOrientation(1);

            var button = new android.widget.Button(ctx);
            button.setText("Button");
            button.setOnClickListener(new android.view.View.OnClickListener({
                onClick: function(viewarg){
                    //Your Code;
                }
            }));
            layout.addView(button);

            GUI = new android.widget.PopupWindow(layout, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            GUI.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.TRANSPARENT));
            GUI.showAtLocation(ctx.getWindow().getDecorView(), android.view.Gravity.RIGHT | android.view.Gravity.TOP, 0, 0);
        }catch(err){
            print("An error occured: " + err);
        }
    }}));
}

function leaveGame(){
    var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
    ctx.runOnUiThread(new java.lang.Runnable({ run: function(){
        if(GUI != null){
            GUI.dismiss();
            GUI = null;
        }
    }}));
}

Custom UI + Menu Setup:

var GUI;
var menu;
var exitUI;

function dip2px(dips){
    var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
    return Math.ceil(dips * ctx.getResources().getDisplayMetrics().density);
}

function newLevel(){
    var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
    ctx.runOnUiThread(new java.lang.Runnable({ run: function(){
        try{
            var layout = new android.widget.LinearLayout(ctx);
            layout.setOrientation(1);

            var menuBtn = new android.widget.Button(ctx);
            menuBtn.setText("Menu");
            menuBtn.setOnClickListener(new android.view.View.OnClickListener({
                onClick: function(viewarg){
                    mainMenu();
                }
            }));
            layout.addView(menuBtn);

            GUI = new android.widget.PopupWindow(layout, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            GUI.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.TRANSPARENT));
            GUI.showAtLocation(ctx.getWindow().getDecorView(), android.view.Gravity.RIGHT | android.view.Gravity.BOTTOM, 0, 0);
        }catch(err){
            print("An error occured: " + err);
        }
    }}));
}

function mainMenu(){
    var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
    ctx.runOnUiThread(new java.lang.Runnable({ run: function(){
        try{
            var menuLayout = new android.widget.LinearLayout(ctx);
            var menuScroll = new android.widget.ScrollView(ctx);
            var menuLayout1 = new android.widget.LinearLayout(ctx);
            menuLayout.setOrientation(1);
            menuLayout1.setOrientation(1);

            menuScroll.addView(menuLayout);
            menuLayout1.addView(menuScroll);

            var button = new android.widget.Button(ctx);
            button.setText("Button");
            button.setOnClickListener(new android.view.View.OnClickListener({
                onClick: function(viewarg){
                    //Your Code
                }
            }));
            menuLayout.addView(button);

            //Add more buttons in this section

            menu = new android.widget.PopupWindow(menuLayout1, ctx.getWindowManager().getDefaultDisplay().getWidth()/2, ctx.getWindowManager().getDefaultDisplay().getHeight());
            menu.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.BLACK));
            menu.showAtLocation(ctx.getWindow().getDecorView(), android.view.Gravity.RIGHT | android.view.Gravity.TOP, 0, 0);
        }catch(error){
            print("An error occured: " + error);
        }
    }}));
}

function exit(){
    var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
    ctx.runOnUiThread(new java.lang.Runnable({ run: function(){
        try{
            var xLayout = new android.widget.LinearLayout(ctx);
			
            var xButton = new android.widget.Button(ctx);
            xButton.setText("x");
            xButton.setTextColor(android.graphics.Color.WHITE);
            xButton.setOnClickListener(new android.view.View.OnClickListener({
                onClick: function(viewarg){
                    exitUI.dismiss();
                    menu.dismiss();
                }
            }));
            xLayout.addView(xButton);
			
            exitUI = new android.widget.PopupWindow(xLayout, dip2px(40), dip2px(40));
            exitUI.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.TRANSPARENT));
            exitUI.showAtLocation(ctx.getWindow().getDecorView(), android.view.Gravity.RIGHT | android.view.Gravity.TOP, 0, 0);
        }catch(exception){
            print(exception);
        }
    }}));
}

function leaveGame(){
    var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
    ctx.runOnUiThread(new java.lang.Runnable({ run: function(){
        if(GUI != null){
            GUI.dismiss();
            GUI = null;
        }
        if(menu != null){
            menu.dismiss();
            menu = null;
        }
        if(exitUI != null){
            exitUI.dismiss();
            exitUI = null;
        }
    }}));
}

Standard Custom UI Template:

var button = new android.widget.Button(ctx);
button.setText("Button");
button.setOnClickListener(new android.view.View.OnClickListener({
    onClick: function(viewarg){
        //Your Code
    }
}));
layout.addView(button);

Custom UI with OnTouchListener Template:

var active = false;
var button = new android.widget.Button(ctx);
button.setText("Button");
button.setOnTouchListener(new android.view.View.OnTouchListener({
    onClick: function(v, event){
        switch(event.getAction()){
            case 0:
                active = true;
                break;
            case 1:
                active = false;
                break;
        }
        return true;
    }
}));
layout.addView(button);

Checkbox Template:

var checked = false;
var button = new android.widget.CheckBox(ctx);
button.setText("Button");
button.setChecked(checked);
button.setOnClickListener(new android.view.View.OnClickListener({
    onClick: function(viewarg){
        if(!checked){
            checked = true;
        }else{
            checked = false;
        }
    }
}));
layout.addView(button);

Toggle Button Template:

var toggled = false;
var button = new android.widget.ToggleButton(ctx);
button.setText("Button");
button.setChecked(toggled);
button.setOnClickListener(new android.view.View.OnClickListener({
    onClick: function(viewarg){
        if(!toggled){
            toggled = true;
        }else{
            toggled = false;
        }
    }
}));
layout.addView(button);

ImageView Template:

var directory = new android.graphics.BitmapFactory.decodeFile("Your Directory");
var img = new android.graphics.drawable.BitmapDrawable(directory); 
var image = new android.widget.ImageView(ctx);
image.setImageBitmap(img);
//If you want this image to do something, put your OnClickListener here
//If you don't want this image to do anything do this:
yourpopupwindowvariable.setTouchable(false);

AlertDialog.Builder Template

var alert = new android.app.AlertDialog.Builder(ctx); 
alert.setTitle("Hello Sun"); 

var scroll = new android.widget.ScrollView(ctx); 
var layout = new android.widget.LinearLayout(ctx); 
layout.setOrientation(1);

var note = new android.widget.TextView(ctx); 
note.setText("It is now daytime :D"); 
note.setTextColor(android.graphics.Color.WHITE);
note.setTextSize(20);

var sethealth = new android.widget.EditText(ctx); 
sethealth.setHint("Set Health"); 

var params =new android.view.ViewGroup.LayoutParams(-2,-2); 
  
layout.addView(note,params); 
layout.addView(sethealth,params);

alert.setView(layout); 

alert.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener(){ 
  onClick: function(viewarg){
    Player.setHealth(sethealth.getText().toString());
      }});

alert.setNegativeButton("Ok (Don't set health)", new android.content.DialogInterface.OnClickListener(){ 
   onClick: function(viewarg){
      }});

alert.setNeutralButton("Set time to night",new android.content.DialogInterface.OnClickListener(){ 
  onClick: function(viewarg){
     Level.setTime(13500);
      }});
       
var dialog = alert.create();
dialog.show();
      

Switch Template

var switched = false;
var button = new android.widget.Switch(ctx);
button.setText("Button");
button.setChecked(switched);
button.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener({
onCheckedChanged: function(){
if(!switched){
switched = true;
//Your Code;
}else{
switched = false;
//Your Code;
}
button.setChecked(switched);
}
}));
layout.addView(button);

Seekbar Template

var sbpProgress = 0;
var sbp = new android.widget.SeekBar(ctx);
sbp.setMax(3);
sbp.setProgress(sbpProgress);
sbp.setOnSeekBarChangeListener(new android.widget.SeekBar.OnSeekBarChangeListener() {
onStopTrackingTouch: function(view)
{
sbpProgress = sbp.getProgress();
if(sbpProgress==0)
{
//Your Code (0/3)
}
if(sbpProgress==1)
{
//Your Code (1/3)
}
if(sbpProgress==2)
{
//Your Code (2/3)
}
if(sbpProgress==3)
{
//Your Code (3/3)
}
}
});
layout.addView(sbp);

ProgressBar Template

var bar = new android.widget.ProgressBar(ctx, null, android.R.attr.progressBarStyleHorizontal);
bar.setMax(100);
bar.setProgress(75);
layout.addView(bar);

EditText Template

var code = new android.widget.EditText(ctx);
code.setHint("//Your text");
layout.addView(code);