#How do I open a ActionFormData inside of another ActionFormData?

1 messages · Page 1 of 1 (latest)

lucid flameBOT
#

Description
Demostration of showing another Minecraft UI form after a player confirms or cancels the dialog.

Credits
These scripts were written by FrankyRayMS

clever parrot
#

@primal quarry Here.

/**
 * The first action form to show.
 * @param {Player} player The player to show it to.
 */
function actionForm(player) {
    const menu = new ActionFormData()
    .title('Title')
    .button('Button 1')
    .button('Button 2', 'textures/folder/image')
    .show(player).then((result) => {
        if (result.canceled) return
        switch (result.selection) {
            case 0:
                secondActionForm(player)
                break;
            case 1:
                // thirdActionForm(player)
                break;
            default:
        }
    })
}

/**
 * 
 * @param {Player} player 
 */
function secondActionForm(player) {
    const secondmenu = new ActionFormData()
    .title('Title 2')
    .button('Exit')
    .show(player).then((result) => {
        if (result.canceled) return
        switch (result.selection) {
            case 0:
                // customFunction(parameter)
            default:
        }
    })
}
#

Just replace the values with your own functions and such.

primal quarry
#

im still new to scripting, and this looks confusing, but do i replace the "secondActionForm"

clever parrot
#

Yes.

#

You need to know the basics of JavaScript to get a better understanding on what's going on here.

primal quarry
#

i would but i dont have time to study it

clever parrot
#

Make up a schedule to study it at some point.

#

I was able to learn the basics of JavaScript with Google Grasshopper. Unfortunately, that service is shutting down soon, so you won't have much time left.

primal quarry
#

😦

clever parrot
#

@primal quarry
But if you want to cut straight to the chase, then let me explain what this code does.
The actionForm is shown to the player as usual. If the player closes, or "cancels" the form, it will do nothing, as shown in the if (result.canceled) return line. Switch will go through all of the objects in result.selection, which is an array (a list of objects and variables). This array, result.selection, contains all of the buttons listed above, indexed from 0 to 1 instead of 1 to 2. Case 0 means it will do something if the selection is the first button, which you can define what it does below that case 0 line. (NOTE: ALWAYS put a break after your sequence! Case 1 is the exact same thing, but for the second option. I put in a comment so that the code ignores the thirdActionForm(player) in case if you want to replace it with something else. Once all of the code is done, default defines what happens if an option inside the form that doesn't have a case is selected or the form is closed. No need for a break there. It's good practice to define your forms as functions with the player parameter.

primal quarry
#

like with an item

#

idk how to open an actionformdata with an itme

clever parrot
#

@primal quarry An item is one of the many ways to open it! And you're absolutely correct: Items are the most common way. You can do this by putting this into your main file.

import { world } from '@minecraft/server'

world.events.beforeItemUse.subscribe((data) => {
  if (data.item.typeId === 'minecraft:netherite_sword") {
    actionForm(data.source)
  }
}

Since we already made the code for the menus earlier, we don't need to do it again. If the item that the player uses is, let's say a netherite sword, the menu will pop up. Again, we already made the menu as a function, so we can define it anywhere with ease, such as in the fifth line.

primal quarry
clever parrot
#

I meant to say we call it anywhere, not define it.

clever parrot
primal quarry
#

@clever parrot sorry for the ping but what does break must be inside or switch

#

how do i run commands?

clever parrot
#

Okay...

#

Basically, switch will do something different for every object inside of an array.

#

Break must be inserted into every case except for default.

#

As for commands, you'll need to do Player.runCommandAsync('command').

primal quarry
#

player commands still isnt working

#

is having system.runTimeout(() => { not good?

#

hello?

#

like this

                case 0:
                    system.runTimeout(() => {
                        player.runCommandAsync('replaceitem entity @a slot.hotbar diamond_sword 1 0');
                    }, 20);
                    system.runTimeout(() => {
                        player.runCommandAsync('say Diamond Sword was given!');
                    }, 40);
                    break;
#

nvm i got it working but now its saying the player.runCommand isntr a function