Skip to main content

KubeJS

If KubeJS is installed, Midnight Thoughts exposes a binding and five events, so you can react to sleep, hand out the Rested effect, and even override how comfortable a bedroom is — all from a script, with no Java and no recompiling.

Optional dependency

KubeJS is entirely optional. The mod detects it at load time; without it nothing changes and no errors are printed.

note

All five events are server events, so scripts belong in kubejs/server_scripts/. They will not fire from startup_scripts or client_scripts.


Bindings

The global object is called MidnightThoughts.

MethodReturnsDescription
grantWellRested(player, level)Applies the Rested effect at level 1–5
grantMvp(player)Applies the MVP variant of the effect
clearWellRested(player)Removes the effect
hasWellRested(player)booleanWhether the effect is currently active
getLevel(player)intCurrent effect level, 0 if inactive
getComfort(player)intComfort level around the player right now
ItemEvents.rightClicked('minecraft:clock', event => {
const player = event.player
player.tell('Comfort here: ' + MidnightThoughts.getComfort(player))
player.tell('Rested level: ' + MidnightThoughts.getLevel(player))
})
note

getComfort returns the same comfort level the rest of the mod acts on — the one compared against nightmareThreshold and sleepBlockThreshold. See the comfort system.


Events

All events are registered under the MidnightThoughtsEvents group.

EventFires when
wellRestedAppliedThe Rested effect is granted
wellRestedExpiredThe Rested effect is removed
nightmareA player falls asleep in nightmare conditions
mvpDeterminedThe night's MVP is chosen
comfortCalculatedComfort is calculated for a player
GetterTypeDescription
getPlayer()ServerPlayerWho received the effect
getLevel()intEffect level, 1–5
isMvp()booleanWhether this is the MVP variant
getDurationTicks()intTotal duration in ticks
MidnightThoughtsEvents.wellRestedApplied(event => {
const minutes = Math.round(event.getDurationTicks() / 20 / 60)
event.getPlayer().tell('Rested ' + event.getLevel() + ' for ' + minutes + ' min')
})

Fires after waking up, from the wellrested grant command, and from the grantWellRested binding.


Overriding comfort

comfortCalculated is the only event with a setter, and it is the most powerful hook the mod offers. The value you set is what the rest of the mod uses — it decides the Rested level, whether nightmares trigger, and whether sleep is blocked at all.

That makes it enough to build entire custom rules on top of the block-scanning system:

// Sleeping outdoors is always miserable, no matter how nice the furniture is
MidnightThoughtsEvents.comfortCalculated(event => {
const player = event.getPlayer()
if (player.level.canSeeSky(player.blockPosition())) {
event.setLevel(Math.min(event.getLevel(), 0))
}
})
tip

Comfort results are cached per player for a short time and per position, so this event does not fire every tick. You can do moderately expensive work in it without hurting server performance.

warning

Because setLevel overrides everything, a script that unconditionally sets a high value effectively disables the comfort system. That is a valid thing to want — just be aware it is what you are doing.


Availability

info

The integration ships with the Forge and NeoForge builds, on the Minecraft versions where a matching KubeJS build exists. There is no KubeJS integration in the Fabric build. If you load the mod where KubeJS is unavailable, the integration simply does not register — everything else works as normal.

Java mod developers who want the same hooks without KubeJS should use the Java API instead.