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;
});
});```
#every 10 seconds instead of every tick
53 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
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
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?
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
Are you trying to implement some sort of Ore Regeneration for your server?
yes
this is the whole ore regenation script
Paste version of message.txt from @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
you can definetly run this in parallel, just devide the current index by how many ticks you want to span it over and than only loop over those indexes of the array (that are returned by the devision) and store the removed indexes (those blocks that were placed), to then clear them after finishing a full cicle
a code example would be cool
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
you can also increase this by replacing all 200's with the new tick amount
there was a mistake, updated the code now
there are so much syntax mistakes but understood thanks!
Incan Tell you why, because I was Just before coding Python and Always have a Problem when switching programming languages
oh no problem with that fixed the typos and new issues have been arrived but I believe I can fix it
If you have any problems, feel free to ask.
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.
if it worked
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
that too
Just also Post your final Code xd
yeah I will but still trying to fix the errors
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.
might as well post them here so we can help
couldn't get this working
post logs then
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
Why did you make two counters, and why couldn't you get my Code to work
What did you try for my code
fixed the counters and it didn't run so I gave up
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