#How to swap a specific block back and forth with another block in an area after every jump?

5 messages · Page 1 of 1 (latest)

vital reef
#

Title, I'm trying to make every red wool block between two points turn into a custom block when a player jumps, then back into a red wool block when they jump again. I do have a script that works, but it's using minecraft's fill command replace function, and there's a noticeable stutter since the area I'm trying to target is relatively large. I'm wondering if there's a way to do this without runCommandSilent.

Here's my script that works for reference

let $EntityJump = Java.loadClass("net.minecraftforge.event.entity.living.LivingEvent$LivingJumpEvent");
global.switch = 0

ForgeEvents.onEvent($EntityJump, event => {
        if(event.entity.level.clientSide) {
                if (global.switch == 0) {
                        Utils.server.runCommandSilent(`fill 254 -30 -29 225 -20 -57 minecraft:red_wool replace kubejs:red_swap`)
                        global.switch = 1
                        return
                }
                if (global.switch == 1) {
                        Utils.server.runCommandSilent(`fill 254 -30 -29 225 -20 -57 kubejs:red_swap replace minecraft:red_wool`)
                        global.switch = 0
                        return
                }
}})
sturdy hamletBOT
#

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

woeful ivy
#

you could try installing worldedit (or similar) and see if thier commands perform better or
try to go through every block in that arrea and check which it is and change it. If that will perform better than the fill command though idk

level.getBlock(BlockPos(x, y, z))

this should allow you getting a block at xyz given a level

#

you can set it with block.set(blockId) and get the id with block.id iirc

vital reef
#

I dug around some more and got it working using the BlockPos class to check the area. Works without any lag for the area I was targeting. Final scripts for reference:
Startup:

let $EntityJump = Java.loadClass("net.minecraftforge.event.entity.living.LivingEvent$LivingJumpEvent");
global.switch = 0

ForgeEvents.onEvent($EntityJump, event => {
        if(event.entity.level.clientSide) { //Client check to prevent event from triggering an extra time on the server
                if (global.switch == 0) {
                        event.entity.sendData('blockSwitch_RedWoolBlueEmpty')
                        
                        global.switch = 1
                        return
                }
                if (global.switch == 1) {
                        event.entity.sendData('blockSwitch_RedEmptyBlueWool')
                        
                        global.switch = 0
                        return
                }
}})

Server:

let $BlockPos = Java.loadClass('net.minecraft.core.BlockPos')

NetworkEvents.dataReceived('blockSwitch_RedWoolBlueEmpty', (event)=>{
    let Red1 = $BlockPos.betweenClosedStream(225, -30, -57, 254, -20, -29)
                        .map(blockPos => event.entity.level.getBlock(blockPos))
                        .filter(block => block.id == 'kubejs:red_swap')
                        .forEach(block => {
                                block.set('minecraft:red_wool')
                        })
    let Blue1 = $BlockPos.betweenClosedStream(225, -30, -57, 254, -20, -29)
                        .map(blockPos => event.entity.level.getBlock(blockPos))
                        .filter(block => block.id == 'minecraft:blue_wool')
                        .forEach(block => {
                                block.set('kubejs:blue_swap')
                        })
})

NetworkEvents.dataReceived('blockSwitch_RedEmptyBlueWool', (event)=>{
    let Red2 = $BlockPos.betweenClosedStream(225, -30, -57, 254, -20, -29)
                        .map(blockPos => event.entity.level.getBlock(blockPos))
                        .filter(block => block.id == 'minecraft:red_wool')
                        .forEach(block => {
                                block.set('kubejs:red_swap')
                        })
    let Blue2 = $BlockPos.betweenClosedStream(225, -30, -57, 254, -20, -29)
                        .map(blockPos => event.entity.level.getBlock(blockPos))
                        .filter(block => block.id == 'kubejs:blue_swap')
                        .forEach(block => {
                                block.set('minecraft:blue_wool')
                        })
})