#set night to day if block is placed in world

42 messages · Page 1 of 1 (latest)

past berry
#

I want to use this script in a custom block, and executes it if the block is placed in world, how should I do it?

LevelEvents.tick(event => {
    if (event.getLevel().isNight()) {
        event.server.runCommandSilent('time set day')
    }
})
azure portalBOT
#

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

past berry
#

maybe onBlockPlaced, but if I leave the world, what happens? the code should not happen until I place the block again, right?

hushed lake
#

??xy

cloud nebulaBOT
# hushed lake ??xy

The XY problem is when you really want to do X, and you think that Y can achieve X. However, you can't get Y to work, and so ask for help exclusively about Y.

This can lead to a lot of confusion from the people trying to help you, as Y can seem like a very random thing to want to do, and a lot of the time isn't the best way to achieve X anyway.

Its fine to ask about Y, just always include some context about X so you can be put on the right track if Y won't do X well, or there is an easier way to do X.

hushed lake
#

what are you actually trying to do?

#

(like from a user perspective, ie having a bed that always skips night for the player)

past berry
#

just a block that passively turns night to day

#

I'm sorry if it seemed confusing

hushed lake
#

does it do it once, when placed? or all the time? or only when its night time?

#

based on your level tick code its only when night

past berry
#

should do every time is night

hushed lake
#

the best way to do this would be to use the new block entities

dull valley
#

you could just set it to day and stop the time, instead of spamming it per tick

hushed lake
past berry
#

oh didn't knew about that, way more optimized than this

dull valley
#

gamerules

past berry
#

just did a sample code to improve later

dull valley
#

doDaylightCycle false

past berry
dull valley
#

if i remember correctly

past berry
#

bruh for real

#

didn't thought about this gamerule

dull valley
#

made a quick bot macro

cloud nebulaBOT
#

To change gamerules properly, instead of running commands, you can do it like this:

ServerEvents.loaded(e => {
  if (e.server.persistentData.gameRules) return

  e.server.gameRules.set("doMobSpawning", false)
  e.server.gameRules.set("mobGriefing", false)
  e.server.gameRules.set("doDaylightCycle", false)

  e.server.persistentData.gameRules = true
})

Keep in mind this is just an example, your event/context may vary.

dull valley
#

@hushed lake why is the persistent data a thing hmmm

#

yoinked it from your example

hushed lake
#

so that it only happens on first world load, incase someone wants to change it (ie dev world)

dull valley
#

ah

#

should i leave it in the example

hushed lake
#

no

#

unless the example is changed to be 'default gamerules'

#

cause that is what the script is

cloud nebulaBOT
#

@dull valley updated macro __tips_gamerules!

dull valley
#

removed it now

#

its mainly to show that you can change it without commands

naive roost
#

Be careful with pausing time because some mods may depend on that

hushed lake
#

daytime/night time and game time are different things that mods hopefully shouldnt be confusing

naive roost
#

for TFC if you set cycle to false all your foods will never expire, like cheating

past berry
#

after some search, I found the block entity example, working fine
the plan is keep this way and implement energy to clear time/rain

StartupEvents.registry("block", (event) => {
    event.create('cycle_stopper').blockEntity(entityInfo => {
        entityInfo.serverTick(20, 0, entity => {
            const level = entity.getLevel();
            const server = level.server;
            if (level.isNight()) {
                server.runCommandSilent('time set day');
            }
            if (level.isRaining()) {
                server.runCommandSilent('weather clear');
            }
        })
    })
})
#

thanks for the tips, really appreciated!