#Run code after skiping the night

81 messages · Page 1 of 1 (latest)

nocturne coyote
#

Is there a way to run code when players sleep? (preferably after the skiping the night)

feral lightBOT
#

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

boreal lake
#

are you looking to do something do something to the sleeping players? or just want to trigger something on a forced new day?

nocturne coyote
#

I have a thirst mod installed and i want to set the thirst level when players wake up

jade plinth
#

??kjsaddons

gleaming ventureBOT
# jade plinth ??kjsaddons

KubeJS Additions adds more events and methods to make it easier for pack creators to do things such as toggling force-loaded chunks or getting ticking chunks, add additional checks for blocks and entities, add player stats, toggle a player's elytra, mess with JEI and more!

jade plinth
#

actually

#

are these fabric only? BigEyes

boreal lake
#

yeah uh lio..... there's nothing there for sleep on forge

jade plinth
#

yeah we'll have to go off the forge sleep event

#

sec

boreal lake
#

there is a forge event you want.... probably SleepFinishedTimeEvent

#

in startup:

ForgeEvents.onEvent("net.minecraftforge.event.level.SleepFinishedTimeEvent", event=>{
  // the rest of the owl
})

this triggers right before the time change

jade plinth
#
ForgeEvents.onEvent('net.minecraftforge.event.entity.player.PlayerSleepInBedEvent', event => {
    global.onSleep(event)
})
global.onSleep = event => {
    const { entity } = event
}
ForgeEvents.onEvent('net.minecraftforge.event.entity.player.PlayerWakeUpEvent', event => {
    global.onWakeUp(event)
})
global.onWakeUp = event => {
    const {entity} = event
}```
#

you'll wanna use global events so you can run /kubejs reload startup_scripts without having to reload the entire game every time you want to change something

nocturne coyote
#

perfect

#

thank you guys

boreal lake
#

are you sure sleep in bed isn't triggered just when they right click on the bed, whether the time changes or not?

jade plinth
#

i provided both

#

the player sleep in bed event fires as you right click the bed yes

boreal lake
#

which is not what they want

jade plinth
#

they might want it pepeshrug

#

i had both on hand so why not, i dont see the issue here hmmm

boreal lake
#

preferably after the skiping the night

nocturne coyote
#

there is no problem, if i want the code to be executed after the skip, i just need to put it on the global.onWakeUp right?

jade plinth
#

yup

#

and if you dont want the other event just dont use it raha

boreal lake
#

well playersleepinbed wouldn't trigger the new day until enough players are sleeping

jade plinth
#

i never said it would confused_dog_lex

#

i think you're confused about all this

boreal lake
#

I'm not

jade plinth
#

seems like it

#

either way i think this tickets a close

boreal lake
#

I'm saying the event you gave is not what they were asking for, how am I the one confused?

jade plinth
#

??closeticket

gleaming ventureBOT
# jade plinth ??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 should generally create a new post for unrelated issues.

nocturne coyote
#

ahh just one last check

jade plinth
#

sure

nocturne coyote
#

if player leave bed does the code run?

#

i mean

#

without skiping the night

jade plinth
#

wakeup event fires whenever they wake up and get kicked from the bed

#

not when they leave the bed

nocturne coyote
#

ahh nice

#

thank you again

jade plinth
#

yup heh

feral lightBOT
#

Ticket closed!

boreal lake
#

sure about that?

     public void stopSleepInBed(boolean p_36226_, boolean p_36227_) {
         net.minecraftforge.event.ForgeEventFactory.onPlayerWakeup(this, p_36226_, p_36227_);
         super.stopSleeping();
         if (this.level() instanceof ServerLevel && p_36227_) {
             ((ServerLevel)this.level()).updateSleepingPlayerList();
jade plinth
boreal lake
#
ForgeEvents.onEvent('net.minecraftforge.event.entity.player.PlayerWakeUpEvent', event => {
    if (event.wakeImmediately()) return;
    // got out of bed, not slept through night
    global.onWakeUp(event)
})
global.onWakeUp = event => {
    const {entity} = event
}
jade plinth
#

yeah you're right, do this one "net.minecraftforge.event.level.SleepFinishedTimeEvent"

#

mb

#

either way works though sleepfinishedtimeevent would be cleaner

boreal lake
#

if you did the sleepfinishedtimeevent, you get the level instead of a player who slept, so you'd want to get the player list to iterate through by doing event.level.players

jade plinth
#

this is also true

boreal lake
#

otherwise the wakeupevent will trigger for each player individually

#

I wonder if that triggers for players who didn't sleep

#

when you have the gamerule at less than 100%

jade plinth
#

so i guess we were both half right then heh

#

2 halves make a whole

boreal lake
#

depends on if all players need to have their thirst updated or only the sleepy ones 😉

jade plinth
#

should work for what you need

#

also might have to re-close the ticket heh

nocturne coyote
#

np 👍

feral lightBOT
#

Ticket re-opened!

nocturne coyote
#

i put it on startup scripts

#
ForgeEvents.onEvent('net.minecraftforge.event.entity.player.PlayerWakeUpEvent', event => {
  if (event.wakeImmediately()) return;
  // got out of bed, not slept through night
  global.onWakeUp(event)
})
global.onWakeUp = event => {
  event.server.runCommandSilent('thirst set ' + event.player.username + ' 8 2')
}
boreal lake
#

forgeevents don't have player, use event.entity.username there and those events don't expose server so you need to do event.entity.server.runCommandSilent

nocturne coyote
#

thank you, will test it rn

#

also, is there a place besides the wiki were i can learn more about developing with kubejs?

boreal lake
#

my documentation is the code itself lol..... just kinda learned how to read java and generally understand it.... can't write it for shit 😉

jade plinth
#

??tutorial

gleaming ventureBOT
jade plinth
#

also as i always say the best wiki is right here 😉

nocturne coyote
#

thank you again 😁