#Spawn in Nether by default issue

25 messages · Page 1 of 1 (latest)

viscid crescent
#

Tryna make a script to have players spawn in the nether by default - i'm almost there, but I can't seem to get the ServerLevel object for the nether on startup. Any ideas? Code below

hearty meadowBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

viscid crescent
#
const ServerLevelData = Java.loadClass('net.minecraft.world.level.storage.ServerLevelData')
const ServerLevel = Java.loadClass('net.minecraft.server.level.ServerLevel')
const BlockPos = Java.loadClass('net.minecraft.core.BlockPos')
const ChunkPos = Java.loadClass('net.minecraft.world.level.ChunkPos')
const PlayerRespawnLogic = Java.loadClass('net.minecraft.server.level.PlayerRespawnLogic')
const CreateSpawnPosition = Java.loadClass('net.minecraftforge.event.level.LevelEvent$CreateSpawnPosition')

ForgeEvents.onEvent('net.minecraftforge.event.level.LevelEvent$CreateSpawnPosition', event => {
    let nether = ServerLevel(event.getLevel().getServer().getLevel(Level.NETHER));

    let spawnPos = null;
    for (let i = 0; i < 16 && spawnPos == null; i++) { // 16 attempts
        // get random point in radius 
        let randomX = (Math.random() < 0.5 ? -1 : 1) * (16 + (Math.random() * 112));
        let randomZ = (Math.random() < 0.5 ? -1 : 1) * (16 + (Math.random() * 112));
        spawnPos = PlayerRespawnLogic.getSpawnPosInChunk(nether, new ChunkPos(new BlockPos(randomX, 60, randomZ)));
    }
    nether.setDefaultSpawnPos(spawnPos, 0.0);
    
    let spawnSettings = event.getSettings();
    spawnSettings.setSpawn(spawnPos, 0.0);
    event = CreateSpawnPosition(nether, spawnSettings);
})```
placid egretBOT
#

Paste version of crash-2024-08-27_03.47.41-client.txt from @viscid crescent

viscid crescent
#

Which is odd, since getLevel(ResourceKey<Level>) is a valid method in MinecraftServer

#

and I know the rest of the script works cuz when I replace let nether = ServerLevel(event.getLevel().getServer().getLevel(Level.NETHER)); with let nether = ServerLevel(event.getLevel()); it works just fine, it just puts me in the overworld instead

#

Any ideas?

sacred fulcrum
#

Try event.server.getLevel("nether")

viscid crescent
#

I don't think this event has a server parameter but i'll try

#

yeah event.server is undefined, cuz this is a ForgeEvent so it won't have the same stuff as KubeJS events

viscid crescent
#

wait a second

#

ok I just found the spreadplayers command, this might be what i'm looking for

#

All i need to do now is figure out how to execute a few commands when the player spawns for the first time and i'm fine

#

k i think i've got this, holup

placid egretBOT
#

[➤](#1266242336413974641 message)
Trying to spawn the player at y=150, show a welcome message and put the player into spectator mode the first time the player enters the world - which works.

What's wrong:
When I then put myself into survival mode and die, when I respawn it puts me back into spectator mode on the ground, but doesn't show the welcome message again. I don't want it to put me into spectator mode after I respawn, only the first time I spawn into the world. So the part that isn't working is that when I respawn after dying it puts me back into spectator mode. Any ideas?

PlayerEvents.loggedIn(event => {
    const player = event.player;

    // Check if the player has a 'first_join' tag in persistent data
    if (!player.persistentData.hasOwnProperty('first_join')) {
        // Player is joining for the first time
        // Set the player's spawn position to y=150
        player.setPosition(player.x, 150, player.z);

        // Set the player to spectator mode
        player.setGameMode('spectator');

        // Set the "first_join" tag to true
        player.persistentData.first_join = true;

        // Notify the player
        player.tell('Welcome to the world for the first time! You are now at y=150 and in spectator mode.');
    } else {
        // Handle case where the player is not a first-time joiner
        // No action needed on respawn or subsequent logins
        player.tell('Welcome back!');
    }
});```
viscid crescent
#

alright i've instead opted for something like this, though it doesn't seem to trigger when creating a world (seeing as i spawn in the overworld)
I have no doubt that it's not working cuz i don't know how player persistent data works lol```PlayerEvents.loggedIn(event => {
// Check if the player has a 'first_join' tag in persistent data
if (event.player.persistentData.getBoolean('first_join') == false) {
// Player is joining for the first time
event.getLevel().runCommandSilent('execute in minecraft:the_nether run tp ' + event.player.getUUID() + ' ~ ~ ~');
event.getLevel().runCommandSilent('execute at ' + event.player.getUUID() + ' run spreadplayers ~ ~ 1 100 under 120 false @s');

    // Set the "first_join" tag to true
    event.player.persistentData.putBoolean("first_join", true);
}

});```

viscid crescent
#

ok nvm i can figure this out on my own, i'll post my results once i'm done in case anyone has a similar question in the future

#

i'm close tho

#

ok this is what i've got, and it seems to work pretty well: ```PlayerEvents.loggedIn(event => {
if (!event.player.persistentData.getBoolean('first_join')) {
// Player is joining for the first time
let playerName = event.player.getGameProfile().getName()
console.log('Transferring' + playerName + 'to nether')
event.getLevel().runCommand('execute as ' + playerName + ' in minecraft:the_nether run tp ' + playerName + ' ~ ~ ~');
console.log('Applying spreadplayers to ' + playerName)
event.getLevel().runCommand('execute as ' + playerName + ' at ' + playerName + ' run spreadplayers ~ ~ 1 100 under 120 false ' + playerName + '');
console.log('Setting ' + playerName + ''s spawnpoint ')
event.getLevel().runCommand('execute as ' + playerName + ' at ' + playerName + ' run spawnpoint ~ ~ ~');
console.log('Setting worldspawn lmao')
event.getLevel().runCommand('execute as ' + playerName + ' at ' + playerName + ' run setworldspawn');

    // Set the "first_join" tag to true
    event.player.persistentData.putBoolean("first_join", true);
    console.log('Complete!')
}

});```

#

However, for some reason when the player is teleported to the nether, the terrain just doesn't want to load, and quitting the game at this point results in the "Saving World..." screen taking forever

#

Perhaps I change the event from loggedIn to something else? Since it seems like it's triggering before anything can load
I'll try EntityEvents.spawned and match it to a player object

viscid crescent
#

nope, this has the same issue ```EntityEvents.spawned(event => {
if (event.entity instanceof ServerPlayer) {
console.log(event.entity.getPersistentData().getBoolean('first_join'))
if (!event.entity.getPersistentData().getBoolean('first_join')) {
// Player is joining for the first time
let playerName = event.entity.getGameProfile().getName()
console.log('Transferring ' + playerName + 'to nether')
event.entity.getLevel().getServer().runCommand('execute as ' + playerName + ' in minecraft:the_nether run tp ' + playerName + ' ~ ~ ~');
console.log('Applying spreadplayers to ' + playerName)
event.entity.getLevel().getServer().runCommand('execute as ' + playerName + ' at ' + playerName + ' run spreadplayers ~ ~ 1 100 under 120 false ' + playerName + '');
console.log('Setting ' + playerName + ''s spawnpoint ')
event.entity.getLevel().getServer().runCommand('execute as ' + playerName + ' at ' + playerName + ' run spawnpoint ~ ~ ~');
console.log('Setting worldspawn lmao')
event.entity.getLevel().getServer().runCommand('execute as ' + playerName + ' at ' + playerName + ' run setworldspawn');

        // Set the "first_join" tag to true
        event.entity.getPersistentData().putBoolean("first_join", true);
    }
    console.log(event.entity.getPersistentData().getBoolean('first_join'))
}

});```

#

Yeah it's still getting stuck for some reason, not loading the dimension at all...

#

I'll just make a new thread for this