#How do I run a command once daily?

14 messages · Page 1 of 1 (latest)

rigid lichen
#

Hello all! I'm trying to figure out how to write a script that will run a specific command when a player has been on the server for one full in-game day, and only up to one time per IRL day. I think this should be possible with KubeJS, but am not sure where to start... could anyone help me out, please?

molten rivetBOT
#

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

lethal rampart
#

This is doable with command blocks and by extension with kubejs
time is measured in ticks. 1 second has 20 ticks

/scoreboard objectives add timer minecraft.custom:minecraft.play_time

This triggers a /summon after someone has spent 9999 ticks on the server
It resets and repeats itself
/execute as @a[scores={timer=9999..}] run summon minecraft:lightning_bolt
/execute as @a[scores={timer=9999..}] run scoreboard players set @s timer 0

rigid lichen
#

Okay, but that would only allow me to run the command based on in-game time, and would include time that the player is offline too, wouldn't it?

lethal rampart
#

the timer ticks when the player is online

#

if u use command blocks you'd have to forceload them or put them in spawnchunks

rigid lichen
#

I'd prefer not to use command blocks, if possible. But how could I make it so the resulting command can only run once per IRL day?

lethal rampart
#

my knowledge of scoreboard commands is kinda outdated (13 major versions) but what u could do is scoreboard players add dummy timer 1 in a repeating cmd block which adds 1 score per tick and implement a check to look at 'dummy's score and execute the command

#

im sure its not too hard to implement in kube

#

u could literally just use the command block logic with kubejs using server.runCommandSilent

rigid lichen
#

Okay, but that still doesn't solve the problem of limiting the command to only running once per IRL day?

lethal rampart
#

/scoreboard objectives add dayCheck dummy
/scoreboard players add dummy dayCheck 9999
/execute if score dummy timer >= dummy timer2 run summon minecraft:lightning_bolt
/execute if score dummy timer >= dummy timer2 run scoreboard players set dummy timer 0

#

that runs it as long as the server is running

rigid lichen
#

Update: After considerable testing and tweaking, I think I've gotten everything working correctly in my script. Here's the code I came up with (this goes in server_scripts):

// Defines 'date' to be used for both PlayerEvents
const date = 'BLANK'

PlayerEvents.loggedIn(event => {
  // Set player's time online back to 0 (necessary to set to 0 for new players)
  event.player.persistentData.timeOnline = 0
  
  // Set the current date as string
  const today = new Date()
  const day = today.getDate()
  const month = today.getMonth() + 1
  const year = today.getFullYear()
  date = `${day}-${month}-${year}`
})

PlayerEvents.tick(event => {
  // Cancel further checks if reward has already been claimed today (to minimize lag)
  if (event.player.persistentData.lastReward === date) return

  // If no reward was claimed by the player yet today, add 1 tick to time online
  event.player.persistentData.timeOnline++

  // Give reward after 20 consecutive minutes online
  if (event.player.persistentData.timeOnline >= 24000) {
    event.server.runCommand(`numismatics pay ${event.player.username} 1 COG`)
    event.player.tell([
      { text: 'Thanks for playing, ', color: 'light_purple' },
      { text: event.player.username, color: 'aqua' },
      { text: '! You\'ve received ', color: 'light_purple' },
      { text: '1 cog', color: 'aqua' },
      { text: ' as your daily reward.', color: 'light_purple' }
    ])

    // Mark reward as claimed for today's date, and reset time online value
    event.player.persistentData.lastReward = date
    event.player.persistentData.timeOnline = 0
    console.log(`${event.player.username} received daily reward for ${event.player.persistentData.lastReward}.`)
  }
})```