#nickname

1 messages · Page 1 of 1 (latest)

little monolith
#

You can change your nickname when entering the world

hardy breach
#
world.afterEvents.playerSpawn.subscribe(async (data) => {
    let { initialSpawn, player } = data
    if (!initialSpawn) return
    system.runTimeout(() => {

        let form = new ModalFormData()
        form.title("Write a nickname")
        form.textField("nickname", "", player.nameTag)

        form.show(player).then((response) => {
            let { canceled, formValues } = response
            if (canceled) return
            let [nickname] = formValues
            player.sendMessage("Nickname set to " + nickname)
            player.nameTag = `${nickname}`
        })
    }, 60)
})
```here an example you can use it or change it
#

thats why its an example

#

he can change it if he want

little monolith
#

does not work

hardy breach
#

It does

#

you need to rejoin the world

little monolith
#

Join word, the script didn't work

hushed edge
#

try creating a new one

little monolith
#

does not work

lilac pine
#

any errors?

lusty cedar
lilac pine
lusty cedar
#
world.beforeEvents.chatSend.subscribe((data) => {
  const { message, sender } = data;
  data.cancel = true;
  world.sendMessage(`${sender.nameTag}§r >> ${message}`)
});
lusty cedar
little monolith
little monolith
little monolith
#

import { world, system} from "@minecraft/server"
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
world.afterEvents.playerSpawn.subscribe(async (data) => {
let { initialSpawn, player } = data
if (!initialSpawn) return
system.runTimeout(() => {

    let form = new ModalFormData()
    form.title("Write a nickname")
    form.textField("nickname", "", player.nameTag)

    form.show(player).then((response) => {
        let { canceled, formValues } = response
        if (canceled) return
        let [nickname] = formValues
        player.sendMessage("Nickname set to " + nickname)
        player.nameTag = `${nickname}`
    })
}, 60)

})

#

the script doesn't work

#

@hardy breach

hardy breach
#

idk maybe there is an issue in your manifest

little monolith
#

Version minecraft 1.20.50

hardy breach
#

oh

#

you need to look at the docs. I think you can't use beta

little monolith
#

other scripts worked

#

Join world, the forum does not open

hardy breach
#

do you get any errors

#

?

little monolith
#

I'll check now

hardy breach
#

And???

#

I need to go

little monolith
#

There is not

#

error

#

No

lilac pine
little monolith
#

there were no errors

#

import { world, system} from "@minecraft/server"
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
world.afterEvents.playerSpawn.subscribe(async (data) => {
let { initialSpawn, player } = data
if (!initialSpawn) return
system.runTimeout(() => {

    let form = new ModalFormData()
    form.title("Write a nickname")
    form.textField("nickname", "", player.nameTag)

    form.show(player).then((response) => {
        let { canceled, formValues } = response
        if (canceled) return
        let [nickname] = formValues
        player.sendMessage("Nickname set to " + nickname)
        player.nameTag = `${nickname}`
    })
}, 60)

})

little monolith
#

manifest

#

@silver charm

hardy breach
#

Maybe add more delay change 60 to 120

little monolith
#

nick don't change

hardy breach
#

do you have other code that use player.nameTag?

little monolith
#

No

little monolith
winter grotto
# little monolith

I think it would simplify things if you just shared your behavior pack here so someone can review it as a whole.

little monolith
winter grotto
# little monolith

It looks good. I think the issue may be that the timeout for 120 isn't long enough. The spawn event will trigger before you see the world render on your screen. You could still be in the loading screen, loaded, but not able to interact yet. As such, if the form is loaded in this instance then I would imagine it won't show at all when the world finally does render after the loading screen goes away. Try extending the timer and see if that is what is happening.

little monolith
#

Ok

winter grotto
# little monolith Ok

If that is the issue and solution, then I would suggest handling it differently. Maybe verify if the user is busy, if that's an option, and having it loop back to create a new instance of the form again, until no longer busy. I say this because the loading time joining a world will vary between people. Some faster than others. You won't be able to make the timeout perfect with those conditions. Every device, and network is different for people so the experience will vary.

#

It can be a difference of someone loading in the world under 20 seconds and another loading in a world in about 1-1/2 minutes.

#

Just to put it in perspective.

little monolith
#

and how to solve it

winter grotto
# little monolith and how to solve it

Try this:

import { world, system, Player } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";

/**
 * Displays a modal form to set a nickname for the player.
 * If the player is busy, recalls the form until the player interacts with it or cancels.
 * @param {Player} player - The player to display the form to.
 */
function displayNicknameForm(player) {
    /**
     * Represents the data structure of the nickname form.
     * @type {ModalFormData}
     */
    let form = new ModalFormData();
    form.title("Write a nickname");
    form.textField("nickname", "", player.nameTag);

    form.show(player).then((response) => {
        const { canceled, formValues, cancelationReason } = response;
        if (response && canceled && cancelationReason === "UserBusy") {
            // If the player is busy, recall the form
            displayNicknameForm(player);
            return;
        }
        if (canceled) return;
        const [nickname] = formValues;
        player.sendMessage("Nickname set to " + nickname);
        player.nameTag = nickname;
    });
}

/**
 * Event handler for the playerSpawn event.
 * @param {object} data - The data object containing information about the event.
 */
world.afterEvents.playerSpawn.subscribe((data) => {
    let { initialSpawn, player } = data;
    if (!initialSpawn) return;

    system.runTimeout(() => {
        // Call the function to display the nickname form
        displayNicknameForm(player);
    }, 60);
});
worthy eagle
#

reduce slow running

//import{world,system}from"@minecraft/server";

world.afterEvents.playerSpawn.subscribe(({player,initialSpawn:spawn})=> {
  if(spawn) {
    system.run(()=> { //handler privillage
     //code.....
      }
  }
});
winter grotto
worthy eagle
#

add sleep function

#
async function sleep(tick) {
  new Promise(resolve => system.runTimeout(resolve, tick));
}
winter grotto
worthy eagle
#

why not like this?

//import{world,system}from"@minecraft/server";

world.afterEvents.playerSpawn.subscribe(({player,initialSpawn:spawn})=> {
  if(spawn) {
    system.runTimeout(()=> { //delay 3sec
     //code.....
      }, 60);
  }
});
winter grotto
worthy eagle
#

hmm

worthy eagle
#

use forceOpen

async function forceOpen(form,player) {
  while(true) {
    const res = await form.show(player);
  if (res.cancelationReason !== "UserBusy") return res;
  }
}
#

I've tried it and it works

worthy eagle
#

oh you right

small sapphire
little monolith
#

import {world, player} from "@minecraft/server"
import { ModalFormData } from "@minecraft/server-ui";

/**

  • Displays a modal form to set a nickname for the player.

  • If the player is busy, recalls the form until the player interacts with it or cancels.

  • @param {Player} player - The player to display the form to.
    /
    function displayNicknameForm(player) {
    /
    *

    • Represents the data structure of the nickname form.
    • @type {ModalFormData}
      */
      let form = new ModalFormData();
      form.title("Write a nickname");
      form.textField("nickname", "", player.nameTag);

    form.show(player).then((response) => {
    const { canceled, formValues, cancelationReason } = response;
    if (response && canceled && cancelationReason === "UserBusy") {
    // If the player is busy, recall the form
    displayNicknameForm(player);
    return;
    }
    if (canceled) return;
    const [nickname] = formValues;
    player.sendMessage("Nickname set to " + nickname);
    player.nameTag = nickname;
    });
    }

/**

  • Event handler for the playerSpawn event.

  • @param {object} data - The data object containing information about the event.
    */
    world.afterEvents.playerSpawn.subscribe(async (data) => {
    let { initialSpawn, player } = data;
    if (!initialSpawn) return;

    system.runTimeout(() => {
    // Call the function to display the nickname form
    displayNicknameForm(player);
    }, 60);
    });

worthy eagle
winter grotto
#

Yea, Player is from @minecraft/server. That was a clinical error on my part haha. I edited it above to fix that. Oops.

little monolith
lusty kelp
#

Not name. Name is readonly nameTag

#

Oh yeah

winter grotto
# little monolith

Watchdog simply let's you know if one or more scripts in the behavior pack run slower than anticipated. In some cases you can just ignore that, while with other cases you may need to investigate if it's an ongoing reoccurrence. It typically requires having to change the logic of the code. To refactor it with performance and memory preservation in mind.

little monolith
#

I got it

#

I just don’t understand how to do it

winter grotto
#

It's pretty clear you don't understand much about the API, but that is fine. You just need to start researching about how to optimize code and what are some good practices to implement. There are many different ways to accomplish the same goal but not every way is the most efficient way. This is something you learn as you write code and grow.

#

I think the first way to start on this is by first looking over the code you have, and understanding what the code is doing line by line.