#Opening a custom UI with commands

1 messages · Page 1 of 1 (latest)

golden violet
#

Hey was just wondering wether it was possible to use Script API to open a custom UI when a player interacts with a block. Right now I have a custom block, that when you interact with it, it runs a command. Would I be able to open a UI for each player that interacts with the block? The block is supposed to be like a server crate, so the UI needs to be able to contain items, like you put in a crate key, and then it gives you rewards depending on the key rarity. Thanks

still remnant
#

u can do this yes, there are two ways

  1. using api fully
  2. using /scriptevent command
golden violet
#

i'm a bit new to Script API

still remnant
#

there is a event called itemUseOn which is ran when a u click on a block like with sword u can use that event and open form

golden violet
#

is there a difference between forms and Json UI?

still remnant
#

jsonui is used to edit looks of screen

#

server send a form data to client which gets rendered

#

like title buttons etc... and waits for response

modest ember
#
export function chatMenu(player) {
    let timeoutTicks = 100;
    const chatMenu = new ActionFormData()
    chatMenu.title('Chat Menu')
    chatMenu.body('Text Here')
    chatMenu.button('Button 1')
    chatMenu.button('Button 2')
    system.run(async function runnable() {
        const response = await chatMenu.show(player);
        if (response.cancelationReason == FormCancelationReason.userBusy) {
            if (timeoutTicks === 1) player.tell('§7Menu activation time expired.')
            timeoutTicks-- && system.run(runnable())
            return;
        } //Normal response goes here- switch, if statements, etc.
        console.warn(response.selection)
    })
};```
If you trigger this function through a chat event command, it'll wait to open the menu until the player leaves the chat
#

(Stole your code, Wave, and edited it a lil bit lol)

golden violet
#

So can I open like a UI like a chest/crafting table with forms?

modest ember
#

UI? No.

still remnant
#

he wants to show form on block interact

modest ember
#

Oh facepalm

golden violet
#

cause I want the player to be able to put like a crate key item in, to get the rewards

modest ember
#

Don't mind me lol

still remnant
#

nah he named title wrong

golden violet
still remnant
#

ye ik that

#

thats why i suggested 2 ways

golden violet
#

Ok

still remnant
#

u didnt knew we can do it all by scripts in this case

#

@modest ember can u help a bit im so tired

modest ember
#
world.events.beforeItemUse.subscribe(use => {
    const player = use.source;
    const blockLook = player.getBlockFromViewVector({ maxDistance: 7, includeLiquidBlocks: false });
    if (use.item.typeId === 'custom:key' && blockLook.typeId === 'custom:block') {
        new ActionFormData()
            .title('Menu')
            .body('Text Here')
            .button('Button 1')
            .button('Button 2')
            .show(player).then(response => {

            })
    }
})
#

Should open menu if player is holding custom:item and right-clicks on custom:block

golden violet
#

ok

#

so where do I like put this

modest ember
#

edit the typeIds to your need

#

in your file that is in your pack's manifest- do you have that setup at all?

golden violet
#

I haven't really used Script API much

#

Do I just make a new script file? (im using bridge)

modest ember
#

Do you have an existing script file?

#

If so, throw it in there

golden violet
#

nope

#

ok

modest ember
#

alright, a new one it is

golden violet
#

in Javascript or typescript?

modest ember
#

javascript

golden violet
#

ok

modest ember
#

That's jsut a snippet though- I can send the full file you'd need to paste here

golden violet
#

ok yeah that'd be helpful

modest ember
#
import { world } from '@minecraft/server';
import { ActionFormData } from '@minecraft/server-ui';
world.events.beforeItemUse.subscribe(use => {
    const player = use.source;
    const blockLook = player.getBlockFromViewVector({ maxDistance: 7, includeLiquidBlocks: false });
    if (use.item.typeId === 'custom:key' && blockLook.typeId === 'custom:block') {
        new ActionFormData()
            .title('Menu')
            .body('Text Here')
            .button('Button 1')
            .button('Button 2')
            .show(player).then(response => {
                console.warn(response.selection)
            })
    }
});``` Should be it here
golden violet
#

is that the whole file?

modest ember
#

ye

#

hopefully

golden violet
#

do i need this import { register } from 'mojang-gametest'

modest ember
#

copy-paste & that's it

golden violet
#

like this?:

`import { register } from 'mojang-gametest'

register('', '', (test) => { })

import { world } from '@minecraft/server';
import { ActionFormData } from '@minecraft/server-ui';
world.events.beforeItemUse.subscribe(use => {
const player = use.source;
const blockLook = player.getBlockFromViewVector({ maxDistance: 7, includeLiquidBlocks: false });
if (use.item.typeId === 'custom:key' && blockLook.typeId === 'custom:block') {
new ActionFormData()
.title('Menu')
.body('Text Here')
.button('Button 1')
.button('Button 2')
.show(player).then(response => {
console.warn(response.selection)
})
}
});`

modest ember
#

The top lines are not needed

golden violet
#

yk what imma go actually learn Script API

#

that might be a good idea lmao

modest ember
#

Not a bad idea, no

golden violet
golden violet
#

?script

modest ember
#

Very much so, yes

golden violet
#

oh nvm

#

thanks for the help

modest ember
#

Good luck 👍

golden violet
#

thanks lol

golden violet
#

hm

#

i'm not sure how I'm supposed to learn it

golden violet