#fix up nether access script

160 messages · Page 1 of 1 (latest)

shell ferry
#

this script works good. but if a player already found a nehter portal that was already lit... they can actully get in.. but the portal breaks..

any way to stop the player.. or is it a missed tick that allows player to get in and cant stop it?

bright anvilBOT
#

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

rugged mauveBOT
#

Paste version of nethergate.js from @shell ferry

shell ferry
#

so ya... regardless of the achivment done or not, players can enter the nehter if they find a portal already lit.

and on Servers... they appear more and more. not sure how to rectify that , or even if its possible to.

regal quest
#

i dont think that script does what you think it should do

#

also the formatting is almost unreadable very_sad_kitty_lex

#

player.block is the block the player is standing on, to start with

shell ferry
#

Hey I never made this. Lol someone from here shared it. And Ive been using it

#

Suppose to gate nether access by achievement

#

I got an achievement for killing warden. Then u get access. But without killing warden u can still access if portal already lit . But portal loses its portal after

rugged mauveBOT
#

[➤](#1166308080317907014 message)

PlayerEvents.tick(event => {
    const {player,server,player:{username}} = event
    if(!(player.age % 20 == 0)) return
if (player.persistentData.canaccessnetherportal == 1) return
    if (player.block.id == 'minecraft:nether_portal') {
        player.block.set('air')
        player.displayClientMessage(Component.of('The forces of the universe deny your impudence').bold().red(), true)
    }else if (player.block.west == 'minecraft:nether_portal'){
        player.block.west.set('air')
        player.displayClientMessage(Component.of('The forces of the universe deny your impudence').bold().red(), true)
    }else if (player.block.east == 'minecraft:nether_portal'){
        player.block.east.set('air')
        player.displayClientMessage(Component.of('The forces of the universe deny your impudence').bold().red(), true)
    }else if (player.block.north == 'minecraft:nether_portal'){
        player.block.north.set('air')
        player.displayClientMessage(Component.of('The forces of the universe deny your impudence').bold().red(), true)
    }else if (player.block.south == 'minecraft:nether_portal'){
        player.block.south.set('air')
        player.displayClientMessage(Component.of('The forces of the universe deny your impudence').bold().red(), true)
    }
})
PlayerEvents.advancement(event => {
    if(event.advancement.id().toString() == "minecraft:story/mine_diamond") {
        event.player.persistentData.canaccessnetherportal = 1
    }
})```
regal quest
#

@rain knoll what is this travesty AngeryDog

rain knoll
#

oh thats been revised long ago heh

#

sec

#
PlayerEvents.tick(event => {
    const { player, player: { block, block: { id, south, west, north, east } } } = event;
    if (!(player.age % 20 == 0) || player.isCreative() || player.isSpectator()) return;
    const destroyPortalAndDisplayMessage = (targetBlock) => {
        targetBlock.set('air');
        player.displayClientMessage(Component.of(`It looks like a mysterious force prevents entry.. I'll have to find another way.`).bold().green(), true);
    };
    switch (true) {
        case id === 'minecraft:nether_portal':
            destroyPortalAndDisplayMessage(block);
            break;
        case west === 'minecraft:nether_portal':
            destroyPortalAndDisplayMessage(west);
            break;
        case east === 'minecraft:nether_portal':
            destroyPortalAndDisplayMessage(east);
            break;
        case north === 'minecraft:nether_portal':
            destroyPortalAndDisplayMessage(north);
            break;
        case south === 'minecraft:nether_portal':
            destroyPortalAndDisplayMessage(south);
            break;
        default:
            break;
    }
});``` try this
rain knoll
#

do you have a mod that insta teles them like if they were in creative mode or something?

steel furnace
#

Hey @shell ferry do you need to destroy the portal or just stop the player from going through if they aren't allowed Nether Access yet?

rain knoll
#

he wants to stop the player from going to the nether i think

#

im looking into morejs teleport event rn

shell ferry
#

I'm gating access. Don't need to be destroyed. Don't really care if it do either lol

steel furnace
#

Why not just do this?
\server_scripts\player_dim.js

global.dimChangeEvent = event => {

    let player = event.getEntity();
    let targetDimension = event.dimension.location();
    let server = player.getServer();

    if (targetDimension == 'minecraft:the_nether') {
        if (!player.stages.has('nether_access')) {
            player.statusMessage = Text.of("The portal doesn't seem to work...");
            server.schedule(2*1000, ()=> player.statusMessage = Text.of("You have not unlocked the ability to use this portal, you must kill the warden first!"));
            return event.setCanceled(true);
        } else {
            event.setCanceled(false);
        }
    }
}```
rain knoll
#

what event does this use?

steel furnace
#

hold on

#

hit enter too fast

rain knoll
#

the forge event?

#

ah

steel furnace
#

And:
startup_scripts\player_dim.js

ForgeEvents.onEvent("net.minecraftforge.event.entity.EntityTravelToDimensionEvent", (event) => {
    global.dimChangeEvent(event);
});
#

It works flawlessly in 1.20.1

rain knoll
#

yeah thats probably cleaner than the one i sent over

steel furnace
#

To break it down simply...

Player tries to enter the portal, the target dimension is checked, if it's the nether they aren't allowed to travel and get a message.

#

I use this in my pack to block access to any dimension that isn't unlocked

#

Then just add the gamestage to the player and they can enter

#

So once the warden is killed, give them nether_access

rain knoll
#

also you can just do js event.dimension == 'minecraft:the_nether' i believe

steel furnace
#

You're welcome to try, but rhino makes assumptions and tries to turn it into a resource so instead of fighting it I used the resource key instead

rain knoll
#

sorry what

steel furnace
#

LOL

rain knoll
steel furnace
#

If you use the namespace dimension string like that, you'll get an error in the log about an invalid resource location.

#

Rhino or Kube does something dumb with namespaced dimension strings and turns it into the resource key, so if you do a string compare like that, it fails. I instead used the actual ResourceKey that it was expecting to avoid having to cast to string and compare that way.

#

all I know is what I provided above works perfectly for me. ¯_(ツ)_/¯

#

I don't get paid to write code, no professional training, I'm a Server Admin, this is just a hobby 🙂

rain knoll
#

ah yes its js event.dimension.location()

#

returns the resourcelocation instead of the key

steel furnace
#

Gotcha

#

well, either way works

#

one just looks cleaner I suppose

rain knoll
#

this way you dont need to parse to string and read from it

#

so better performance(marginally) as well

#

but yeah wouldnt make a huge diff

steel furnace
#

Sure, feel free to update what I did with a working example for the OP to use 🙂

#

I haven't tested your proposed change, so I don't want to insist it works.

rain knoll
#

i tested it

#

just now actually

steel furnace
#

I am now too

rain knoll
#

it used to be event.dimension in 1.19 but 1.20 i think added the .location() aspect

#

in normal kubejs events its event.player.dimension with no need for .location()

#

probably why not many people know about it

steel furnace
#

Ok, yeah that's cleaner and works

#

Updated above

#

Thanks @rain knoll I learned something today 🙂

rain knoll
#

same same

shell ferry
#

just wanted to put it in file format. i believe these are the end result to work for 1.20.1 forge. tested and confirmed working in my modpack. 🙂 thanks you guys.

rugged mauveBOT
#

Paste version of player_dim.js, player_dim.js from @shell ferry

steel furnace
#

happy to help

rugged mauveBOT
#

Please close your ticket (with </ticket close:1054771505520717835> or the button atop this thread) once you resolved your issue! This also helps others that would like to help out, as they don't have to look into this thread to check if it has been resolved by now.

Do you have any other questions regarding your issue? Feel free to ask!
Note: You generally should create a new post for unrelated issues.

bright anvilBOT
#

Ticket re-opened!

shell ferry
rugged mauveBOT
#

Paste version of crash-2024-02-26_12.24.08-server.txt from @shell ferry

shell ferry
#

@steel furnace the fun starts. lol im not sure how common this crash wlil happen... and even if it can be avoided using this method?

steel furnace
#

Uhh

#

Did something else go through the portal? Might need to do an entity check I guess

#

Yep, I get that error when a sheep goes through the portal. Hold on, let me get a fix

shell ferry
steel furnace
#

Yep, I can reproduce, hold on a sec

rain knoll
#

why not just return early if the entity isnt a player?

#
if (!player.isPlayer()) return```
steel furnace
#

if (! event.getEntity().player) return;

#

Or that

rain knoll
steel furnace
#
global.dimChangeEvent = event => {
    if (! event.getEntity().player) return;```
#

That works for me

ivory meteor
steel furnace
#

probably not

#

I like to overcomplicate things sometimes

#

over-engineered is a way of life

#

probably also don't need to return event.setCancelled(true) and can just cancel it.

#
global.dimChangeEvent = event => {
    if (!event.getEntity().player) return;

    let player = event.getEntity();
    let targetDimension = event.dimension.location();
    let server = player.getServer();

    if (targetDimension == 'minecraft:the_nether') {
        if (!player.stages.has('nether_access')) {
            player.statusMessage = Text.of("The portal doesn't seem to work...");
            server.schedule(2 * 1000, () => player.statusMessage = Text.of("You have not unlocked the ability to use this portal!"));
            event.setCanceled(true);
        }
    }
}```
A little cleaner 🙂  Works for all dimensions
ivory meteor
#

I'd directly add the event.dimension.location() into the if tbh

#

it's only used once anyway

#

and I might merge the 2 ifs into one using a boolean and

steel furnace
#

Not for me it's not

#

That's just a small snippet for this specific use case

ivory meteor
#

yup, of course if you are using it somewhere else don't do that

steel furnace
#

There's no real performance gain from any of that, it just shrinks the code more. I like to keep things a bit more readable.

#

It's why I use event and not e

ivory meteor
#
global.dimChangeEvent = event => {
    if (!event.getEntity().player) return;

    let player = event.getEntity();
    let server = player.getServer();

    if (event.dimension.location() == 'minecraft:the_nether' && !player.stages.has('nether_access') {
        player.statusMessage = Text.of("The portal doesn't seem to work...");
        server.schedule(2 * 1000, () => player.statusMessage = Text.of("You have not unlocked the ability to use this portal!"));
        event.setCanceled(true);
    }
}
steel furnace
#

I'm not trying to save 8bytes of code to take up less lines and whitespace

ivory meteor
#

I find indents hard to follow

#

so I reduce indents

#

but otherwise I don't try reduce stuff

steel furnace
#

You're not contributing anything useful though, just rewriting my code to your preference.

ivory meteor
#

I was trying to help with code organisation

steel furnace
#

But that's a preference

ivory meteor
#

the removing of the variable wasn't, that was to make it clearer

steel furnace
#

The OP didn't ask for help making the code take up less lines, space or fit a coding style

ivory meteor
#

they can still choose either, I just wanted to offer an alternative solution ¯_(ツ)_/¯

steel furnace
#

They asked for a solution, I provided one and then you come in trying to shrink it and format it differently without changing the functionality.

#

It's not an alternative solution

ivory meteor
#

no need to get heated I'm jsut trying to help make stuff more readable

ivory meteor
#

however you want to phrase it

steel furnace
#

You offer great ideas in other threads, I'm not challenging your skills, but what you are doing here is just annoying

ivory meteor
#

how is helping formatting annoying (even if it's subjective)? Plus if you disagree just state it, no need to attack what I'm doing

#

just going to repost both lots of the code so it can be seen and isnt buried by this tangent

#
global.dimChangeEvent = event => {
    if (!event.getEntity().player) return;

    let player = event.getEntity();
    let targetDimension = event.dimension.location();
    let server = player.getServer();

    if (targetDimension == 'minecraft:the_nether') {
        if (!player.stages.has('nether_access')) {
            player.statusMessage = Text.of("The portal doesn't seem to work...");
            server.schedule(2 * 1000, () => player.statusMessage = Text.of("You have not unlocked the ability to use this portal!"));
            event.setCanceled(true);
        }
    }

or

global.dimChangeEvent = event => {
    if (!event.getEntity().player) return;

    let player = event.getEntity();
    let server = player.getServer();

    if (event.dimension.location() == 'minecraft:the_nether' && !player.stages.has('nether_access') {
        player.statusMessage = Text.of("The portal doesn't seem to work...");
        server.schedule(2 * 1000, () => player.statusMessage = Text.of("You have not unlocked the ability to use this portal!"));
        event.setCanceled(true);
    }
}
#

both should do the same thing, the bottom is just more compact

rain knoll
#
global.dimChangeEvent = event => {
    const {entity:{player,server,statusMessage,stages},dimension} = event
    if (!player) return;
    if (dimension.location() == 'minecraft:the_nether' && !stages.has('nether_access')) {
        statusMessage = Text.of("The portal doesn't seem to work...");
        server.schedule(2 * 1000, () => statusMessage = Text.of("You have not unlocked the ability to use this portal!"));
        event.setCanceled(true);
    }
}``` ![smugphalia](https://cdn.discordapp.com/emojis/756825728804192347.webp?size=128 "smugphalia")
shell ferry
#

much appricated guys hahaha been a thrill to read lol

shell ferry
rain knoll
steel furnace
rain knoll
#

ah good catch

#

sec, editing it

#

but wait

steel furnace
#

see, shortening code and making it less readable for the noobs isn't always the best way

rain knoll
#

player is entity though

#

let player = event.getEntity();

#

and we're going off the entity

steel furnace
#

You aren't using statusMessage though, it's just an unused variable.
My code may have had nested if statements and used more lines to get things done but for someone that is a novice or noob coder, it was easier to read and actually worked 😛

rain knoll
#

should be used

steel furnace
#

but it's not sending to the player

rain knoll
#

this is your code

#

youre doing player.statusMessage right

steel furnace
#

yes

rain knoll
#

thats what this is js const {entity:{statusMessage}} = event

steel furnace
#

I don't think it's working the way you think it should though

rain knoll
#

uh

#

would you say this would be the correct way then? js let statusMessage = event.entity.statusMessage

steel furnace
#

Yeah, I would think so, I'd have to test to see if it works the same way.

rain knoll
#

which means its not the issue here

steel furnace
#

I've found that things don't always work the way you'd expect

#

Mine works for me, so I'm keeping what I've got. I like where your head's at with assigning those variables off the event like that but it's breaking something.

shell ferry
rain knoll
#

ok why does this work js global.dimChangeEvent = event => { const { entity, entity: { player, server, stages }, dimension } = event if (!player) return if (dimension.location() == 'minecraft:the_nether' && !stages.has('nether_access')) { entity.statusMessage = Text.of("The portal doesn't seem to work..."); server.schedule(2 * 1000, () => entity.statusMessage = Text.of("You have not unlocked the ability to use this portal!")); event.setCanceled(true); } }

steel furnace
rain knoll
#

why does statusMessage not work when nested ahujel

#

thats a bug if anything

rain knoll
#

??closeticket

rugged mauveBOT
# rain knoll ??closeticket

Please close your ticket (with </ticket close:1054771505520717835> or the button atop this thread) once you resolved your issue! This also helps others that would like to help out, as they don't have to look into this thread to check if it has been resolved by now.

Do you have any other questions regarding your issue? Feel free to ask!
Note: You generally should create a new post for unrelated issues.