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
#Opening a custom UI with commands
1 messages · Page 1 of 1 (latest)
u can do this yes, there are two ways
- using api fully
- using /scriptevent command
and how exactly do those work?
i'm a bit new to Script API
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
is there a difference between forms and Json UI?
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
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)
So can I open like a UI like a chest/crafting table with forms?
UI? No.
he wants to show form on block interact
Oh 
cause I want the player to be able to put like a crate key item in, to get the rewards
Don't mind me lol
nah he named title wrong
should I rename it? I named it that cause I have a "run_command" in my block that I can use to execute the command as the player
Ok
u didnt knew we can do it all by scripts in this case
@modest ember can u help a bit im so tired
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
edit the typeIds to your need
in your file that is in your pack's manifest- do you have that setup at all?
I haven't really used Script API much
Do I just make a new script file? (im using bridge)
alright, a new one it is
in Javascript or typescript?
javascript
ok
That's jsut a snippet though- I can send the full file you'd need to paste here
ok yeah that'd be helpful
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
is that the whole file?
do i need this import { register } from 'mojang-gametest'
copy-paste & that's it
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)
})
}
});`
The top lines are not needed
Not a bad idea, no
ok thanks
Bedrock Script API
Intro to ScriptAPI
https://aka.ms/startwithmcscript
Intro to Tests
https://docs.microsoft.com/en-us/minecraft/creator/documents/gametestgettingstarted
Doc Index
https://docs.microsoft.com/en-us/minecraft/creator/scriptapi/
Community Docs
https://wiki.bedrock.dev/scripting/game-tests
Script API examples
https://github.com/JaylyDev/ScriptAPI
Last updated:
·
Very much so, yes
Good luck 👍
thanks lol
are there any good tutorials for learning Script API? or jus the docs
https://learn.microsoft.com/en-us/minecraft/creator/scriptapi/minecraft/server/container would I be able to use this to make a custom container? or does this only work for vanilla containers