#need help with a script

89 messages · Page 1 of 1 (latest)

wanton veldt
#

I need a server side script that checks pre defined area each minute and replaces minecraft:barrel with minecraft:diamond_block
for example area being x1= -1000 z1=-1000 y= 0 x2=1000 z2=1000 y2=256

steady sleetBOT
#

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

wanton veldt
#

as I am new to this I have no clue on how to get this working please help peepoLove

vivid blade
#

??patience

simple tangleBOT
# vivid blade ??patience

Please be patient, we're helping as fast as we can
We volunteer our time and knowledge to help people as we can.
Not everyone here will have your answer the moment you want it.
Time zones, work/life/schedules etc. also change when people are available to help.

TL;DR: Please have patience. IF and WHEN someone knows, they will assist you.

severe vessel
#

this would be really laggy to implement if your idea is to scan entire regions for blocks

pure ruin
#

yes, 853,334 blocks every tick if distributed perfectly

severe vessel
#

i swear there was a library mod for scanning regions for blocks at some point

#

i can't remember what it was though

pure ruin
#

what you could do is track the block place and break

wanton veldt
#

well thing is I have custom ore nodes (blocks that look like wow nodes)
basically I have full and empty node blocks, what i have an issue is that they do not go back to full block type when server has been restarted

#

(i have them working on while server is on) for example player breaks ore node and then after a minute it regenerates back to full node

pure ruin
#

use persistentdata to store which blocks should regenerate

#

instead of variables

severe vessel
#

could make them a block entity

#

if it's a custom block

pure ruin
wanton veldt
#

its for a custom server where those nodes are custom placed

severe vessel
#

ah

wanton veldt
#

basically those are one of the only blocks players can break really

#

full node -> broken -> replaced with empty node-> 1 minute -> replaced with full node

#

did that with mcreator

#

but yeah have been messing around with this for 4 days now and cant get it to do the regen if world/server is shut down and started back up 😄

pure ruin
#

please don't use mcreator

wanton veldt
#

I have even less of an idea how to get it working with kubeJS 😄

#

also wouldnt it cause the same issue either way

#

that if server is restarted when some of the nodes are still in empty state: they would not start regenerating?

pure ruin
#

you could stoore some data in persistentData with kubejs

#

idk if something like that even exists in mcreator

wanton veldt
#

no clue as well... well I guess more research for another 4 days and banging my head against wall till it works kek

pure ruin
#

This code should place the block again after 60 seconds if it was broken. Note that if the block can be obtained it will be a bit op and it wouldn't work in other dimensions correctly

let time = 1200 //60 seconds
ServerEvents.loaded(event => {
    if(!event.server.persistentData.broken_ores){
        event.server.persistentData.broken_ores = {}
        for(let i = 0; i < time; i++){
            event.server.persistentData.broken_ores.put(i.toString(), [])
        }
        event.server.persistentData.broken_ores_time = 0
    }
})
BlockEvents.broken("block_here", event => {
    let pData = event.server.persistentData
    let key = pData.broken_ores_time.toString()
    pData.broken_ores.put(key, [[event.block.x, event.block.y, event.block.z]].concat(pData.broken_ores.get(key)))
    event.block.set("temp_block_here")
    event.cancel()
})
ServerEvents.tick(event => {
    let pData = event.server.persistentData
    pData.broken_ores_time++
    if(pData.broken_ores_time >= time) pData.broken_ores_time = 0
    let key = pData.broken_ores_time.toString()
    pData.broken_ores.get(key).forEach(loc => {
        event.server.getLevel("overworld").getBlock(new BlockPos(loc[0], loc[1], loc[2])).set("block_here")
    })
    pData.broken_ores.put(key, [])
})
wanton veldt
#

for "block_here" the " should stay or no?

pure ruin
#

yes

#

you need to replace both with the block id, so "minecraft:diamond_ore" for example

wanton veldt
#

so first one is the block that is broken? and second one is for what block is placed instead of it?

pure ruin
#

the first is the one to be broken and the second is the one that should respawn

#

if you want to replace it with a different block in the meantime

#

I have edited the script to also have that

wanton veldt
#

oh damn

#

again thank you peepoLove

#

testing it now

#

this script goes in the server_scripts folder right?

pure ruin
#

yes

wanton veldt
#

[18:49:13] [Server thread/ERROR] [KubeJS Server/]: example.js#14: Error in 'BlockEvents.broken': TypeError: Cannot read property "block" from undefined

pure ruin
#

accidently had a dot instead of a comma

wanton veldt
#

after key?

pure ruin
#

no in this line pData.broken_ores.put(key, [[event.block.x, event.block.y. event.block.z]].concat(pData.broken_ores.get(key))) (this is the wrong version)

wanton veldt
#

so after event.block.x

#

so it would be event.block.x. instead of event.block.x,

pure ruin
#

no it would be event.block.y, instead of event.block.y. as it should be added to the array and not continue the method chain (idk the actual name for this)

#

the error is after event.block.y

wanton veldt
#

oh... sorry for being quite of a PepeDent

#

uhh [19:04:08] [Server thread/ERROR] [KubeJS Server/]: example.js#23: Error in 'ServerEvents.tick': Java constructor for "net.minecraft.core.BlockPos" with arguments "dev.latvian.mods.rhino.Undefined,dev.latvian.mods.rhino.Undefined,dev.latvian.mods.rhino.Undefined" not found.

[19:04:08] [Server thread/ERROR] [KubeJS Server/]: …rhino.EvaluatorException: Java constructor for "net.minecraft.core.BlockPos" with arguments "…rhino.Undefined,…rhino.Undefined,…rhino.Undefined" not found. (server_scripts:example.js#23)

pure ruin
#

fixed

#

so sorry for the issues

wanton veldt
#

all good you are currently helping me by a ton and half 😄

#

[19:09:40] [Server thread/ERROR] [KubeJS Server/]: example.js#23: Error in 'ServerEvents.tick': Can't find method net.minecraft.world.level.Level.m_6933_(net.minecraft.core.BlockPos,string).

[19:09:40] [Server thread/ERROR] [KubeJS Server/]: …rhino.EvaluatorException: Can't find method net.minecraft.world.level.Level.m_6933_(net.minecraft.core.BlockPos,string). (server_scripts:example.js#23)

#

do I need to change the overworld as well?

#

or the world name does not matter really as its more of a dimension name?

pure ruin
#

try it now

pure ruin
wanton veldt
#

oh no need for other dims in my case really as everything is in same dim

#

ok seems like its ignoring the temporary block for some reason but does place the block back tho

pure ruin
#

you've replaced "temp_block_here" with the correct block, right?

wanton veldt
#

yep

let time = 1200 //60 seconds
ServerEvents.loaded(event => {
    if(!event.server.persistentData.broken_ores){
        event.server.persistentData.broken_ores = {}
        for(let i = 0; i < 1200; i++){
            event.server.persistentData.broken_ores.put(i.toString(), [])
        }
        event.server.persistentData.broken_ores_time = 0
    }
})
BlockEvents.broken("minecraft:diamond_ore", event => {
    let pData = event.server.persistentData
    let key = pData.broken_ores_time.toString()
    pData.broken_ores.put(key, [[event.block.x, event.block.y, event.block.z]].concat(pData.broken_ores.get(key)))
    event.block.set("minecraft:stone")
})
ServerEvents.tick(event => {
    let pData = event.server.persistentData
    pData.broken_ores_time++
    if(pData.broken_ores_time >= 1200) pData.broken_ores_time = 0
    let key = pData.broken_ores_time.toString()
    pData.broken_ores.get(key).forEach(loc => {
        event.server.getLevel("overworld").getBlock(new BlockPos(loc[0], loc[1], loc[2])).set("minecraft:diamond_ore")
    })
})```
#

also it seems that it ignores the time tho

#

sometimes regens right after and sometimes takes half minute

pure ruin
#

reason was that the locations were never removed

#

can you try now for the temp block

wanton veldt
#

hmm the timer now works but temp block is still not placed

wanton veldt
#

just noticed you changed it and it works! thank you!

#

testing on vanilla diamond or it does not drop the diamond tho WHYY

#

aka no drops

#

while the loot drop part i will figure out myself, it seems like when I changed the time to 12000 (10 minutes) it just spams the console with: 20:11:52] [Server thread/ERROR] [KubeJS Server/]: example.js#23: Error in 'ServerEvents.tick': TypeError: Cannot call method "forEach" of null

pure ruin
wanton veldt
# pure ruin Can you send the code
let time = 12000 //60 seconds
ServerEvents.loaded(event => {
    if(!event.server.persistentData.broken_ores){
        event.server.persistentData.broken_ores = {}
        for(let i = 0; i < time; i++){
            event.server.persistentData.broken_ores.put(i.toString(), [])
        }
        event.server.persistentData.broken_ores_time = 0
    }
})
BlockEvents.broken("minecraft:diamond_ore", event => {
    let pData = event.server.persistentData
    let key = pData.broken_ores_time.toString()
    pData.broken_ores.put(key, [[event.block.x, event.block.y, event.block.z]].concat(pData.broken_ores.get(key)))
    event.block.set("minecraft:stone")
    event.cancel()
})
ServerEvents.tick(event => {
    let pData = event.server.persistentData
    pData.broken_ores_time++
    if(pData.broken_ores_time >= time) pData.broken_ores_time = 0
    let key = pData.broken_ores_time.toString()
    pData.broken_ores.get(key).forEach(loc => {
        event.server.getLevel("overworld").getBlock(new BlockPos(loc[0], loc[1], loc[2])).set("minecraft:diamond_ore")
    })
    pData.broken_ores.put(key, [])
})```
pure ruin
#

Add event.server.persistentData = {} as the First Line in the .loaded event. relog into the world and remove the Line again

#

Idk how much memry this Takes Up though

#

I can't find Like a simple answer on how much that would be online

#

As for each extra Tick we create a new object key

wanton veldt
#

not sure if i did it corectly but seems like i didnt

wanton veldt
#

after having unrest I managed to get it working tho I removed the temp_block as not sure how to get that working. tho I did all that after giving up and using forbidden tool:

let time = 12000; // 60 seconds

ServerEvents.loaded(event => {
    if (!event.server.persistentData.broken_ores) {
        event.server.persistentData.broken_ores = {};
        for (let i = 0; i < time; i++) {
            event.server.persistentData.broken_ores.put(i.toString(), []);
        }
        event.server.persistentData.broken_ores_time = 0;
    }
});

BlockEvents.broken("minecraft:diamond_ore", event => {
    let pData = event.server.persistentData;
    let key = pData.broken_ores_time.toString();
    let locations = pData.broken_ores.get(key) || [];
    
    locations.unshift([event.block.x, event.block.y, event.block.z]);  // More efficient insertion
    pData.broken_ores.put(key, locations);
});

ServerEvents.tick(event => {
    let pData = event.server.persistentData;
    pData.broken_ores_time++;

    if (pData.broken_ores_time >= time) pData.broken_ores_time = 0;

    let key = pData.broken_ores_time.toString();
    let locations = pData.broken_ores.get(key);

    if (Array.isArray(locations)) {
        locations.forEach(loc => {
            let blockPos = new BlockPos(loc[0], loc[1], loc[2]);
            event.server.getLevel("overworld").getBlock(blockPos).set("minecraft:diamond_ore");
        });
    }

    pData.broken_ores.put(key, []);  // Clear the list after processing
});
pure ruin
wanton veldt
#

well tbh it is fine in the latest script i posted, as it does what it needs and also allows the timer to be as long as needed

#

plus im thinking of scrapping the need of empty nodes anyways