#every 10 seconds instead of every tick

53 messages · Page 1 of 1 (latest)

vapid bone
#
ServerEvents.tick((event) => {
    if(event.level.time % 200 != 0) return
    let array = []
    array = event.server.persistentData.bregen
    let time = Date.now()
    if (!array) return
    
    event.server.persistentData.bregen = array.filter(element => {
        if (element.time <= time) {
            event.server.runCommandSilent(`setblock ${element.posx} ${element.posy} ${element.posz} ${getRandomItem(randomores)} replace`);
            event.server.tell(`setblock ${element.posx} ${element.posy} ${element.posz} ${getRandomItem(randomores)} replace`)
            // Filter out the elements that meet the condition
            return false;
        }
        // Keep the elements that don't meet the condition
        return true;
    });
});```
alpine vortexBOT
#

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

vapid bone
#

as title explains

#

I don't want it to run every tick

#

but if(event.level.time % 200 != 0) return gives error

#

[17:16:17] [ERROR] ! reina_server.js#75: Error in 'ServerEvents.tick': TypeError: Cannot read property "time" from undefined

vapid bone
#

well this code lags the server so much after a while

#

even I ran it with every 10 seconds.

#

well researched a bit and found using for are more optimized

#
ServerEvents.tick((event) => {
    if (event.tickCount % 72000) return;
    let array = event.server.persistentData.bregen;
    if (!array || array.length === 0) return;

    let time = Date.now();
    let newBregen = [];

    for (let i = 0; i < array.length; i++) {
        let element = array[i];

        if (element.time <= time) {
            event.server.runCommandSilent(`setblock ${element.posx} ${element.posy} ${element.posz} ${getRandomItem(randomores)} replace`);
        } else {
            newBregen.push(element);
        }
    }

    event.server.persistentData.bregen = newBregen;
});```
#

and added run every hour (72000 tick)

#

but I have another question

#

my array is too long (likely 400-500+ and increasing) and when tick after 1 hour comes the server likely freeze for a good moment, is there a way to run this for parallel so server wouldn't lag?

vapid bone
#

or optimize this code I dunno

#

when I run the server with this script (I was using every tick) servers tps was 15ish after some time it became 10 and timed out

simple hemlock
#

Are you trying to implement some sort of Ore Regeneration for your server?

vapid bone
cunning peakBOT
#

Paste version of message.txt from @vapid bone

vapid bone
#

432000000 equals to 5 days

#

currently I have this lag issue because of length of the array and I need to find the x,z coortinations of the chunk by dividing the blocks by 16 then forceload the chunk for regenation

#

but force generating so many chunks at the same time sounds awfull so I might be check every chunk load event and check if there are an ore to replace

frosty perch
vapid bone
#

a code example would be cool

frosty perch
#
ServerEvents.tick((event) => {
  let pData = event.server.persistentData
  if(pData.cycle == 0){
    pData.c_bregen = pData.bregen
  }
  for(let i = Math.ceil(len(pData.c_bregen) / 200 * cycle); 
    i < len(pData.c_bregen) / 200 * (cycle + 1); i++
  ){
    // process element by using pData.c_bregen[i] and removing the time and pos object in pData.bregen and NOT in pData.c_bregen
  }

  pData.cycle += 1 
  if(pData.cycle >= 200){
    pData.cycle = 0 // initialize the pData.cycle as 0 in postinit event or something like that if it doesn't exist already
  }
})
#

This code will seperate the execution to 200 ticks

vapid bone
#

to 10 seconds

#

hmm

frosty perch
#

you can also increase this by replacing all 200's with the new tick amount

#

there was a mistake, updated the code now

vapid bone
#

there are so much syntax mistakes but understood thanks!

frosty perch
vapid bone
frosty perch
#

If you have any problems, feel free to ask.

frosty nest
#

fellow python coder

#

what I actually came here to say was

cunning peakBOT
#

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.

frosty nest
#

if it worked

cunning peakBOT
#

Please post how you solved your issue. Others may find this ticket later, but then have to create another because the actual solution wasn't mentioned. thanks <3

frosty nest
#

that too

frosty perch
#

Just also Post your final Code xd

vapid bone
#

yeah I will but still trying to fix the errors

cunning peakBOT
#

You can find your KubeJS server log in /minecraft/logs/kubejs/server.log.
If you are on 1.18 or below it will be called server.txt.
Please send it if asked, as it contains helpful information.

frosty nest
#

might as well post them here so we can help

frosty nest
#

post logs then

vapid bone
#
ServerEvents.tick((event) => {
    //get array of blocks
    let array = event.server.persistentData.tregen3;
    if (!array || array.length === 0) {
        event.server.tell("array")
        return;
    }
    //get the tick counter
    let tickCounter = event.server.persistentData.tcounter2;

    if (tickCounter === undefined) {
        event.server.persistentData.tcounter2 = 0;
        event.server.tell("undefined")
        return;
    }

    //if tick counter is higher or equal to array reset the counter
    if (tickCounter >= array.length) {
        tickCounter = 0;
        event.server.persistentData.tcounter2 = tickCounter;
        return;
    }

    let element = array[tickCounter];
    event.server.tell(tickCounter)
    let time = Date.now();

    if (element.time <= time) {
        event.server.runCommandSilent(`setblock ${element.posx} ${element.posy} ${element.posz} ${getRandomItem(randomores)} replace`);

        array.splice(tickCounter, 1);
        event.server.persistentData.tregen3 = array

        event.server.persistentData.tcounter2 = 0;
    } else {
        tickCounter++;
        event.server.persistentData.tcounter2 = tickCounter;
    }
});
[21:26:47 ERROR] [KubeJS Server]: java.lang.ArrayIndexOutOfBoundsException```
#

so what I tried is

#

Creating a tick counter counts ticks every tick and checks the array[tickCounter] of it, if tickcounter is higher or equal to arrays length reset the counter,

Basicly I created a for loop but using ticks

frosty perch
#

Why did you make two counters, and why couldn't you get my Code to work

#

What did you try for my code

vapid bone
frosty perch
#

It should definetly work. I currently have noticed, that it IS actually easier to inverse the Order of counting to Go from the biggest to smallest Index, and Not how it currently does the other way around