#block interaction -> commands?

1 messages Β· Page 1 of 1 (latest)

wary parcel
#

Ight, I have a script setup that detects player right clicking my custom blocks, anyone got ideas how can I connect that to running commands? I can probably setup a function I think I know enough about it now, and I'm actively reading up on scripting stuff. But as of now I can't figure out how to run a command when this is triggered.

    world,
    system
  } from "@minecraft/server";

world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    if (event.block.typeId === "qol:lesser_ender_core") {
       world.sendMessage("You clicked");
    }
});``` here's the code I have running right now as an example
#

oh yea also I haven't seen mention of this method for right click detection anywhere, seems like everybody is looking at use item or somrthing when i search in the server but for block detection this works great ^

sage sonnet
#

you can use runCommandAsync

wary parcel
sage sonnet
wary parcel
#

darn I was so close to finding that on my own too 😭

sage sonnet
#

youre doing greatt! lmk if it works

wary parcel
#

I was going through the scripting stuff on there and just reading and I was two places away πŸ˜‚

wary parcel
# sage sonnet youre doing greatt! lmk if it works

so what is the syntax of .runCommand? if I want to run a function command do I just do

.runCommand(function blahblahblah)

?

or does it need to be a string: .runCommand("function blah") ?

or is it something else entirely

i don't really understand the example that it gives on the website ;-;

sage sonnet
#

it should be smth like this

    if (event.block.typeId === "qol:lesser_ender_core") {
        world.runCommand(
     "/function blablah");
    }
});```
wary parcel
#

oh so it has to be a string and it has to have the / oki

sage sonnet
#

yes!!

wary parcel
#

uhhhh would it need specifications about command permissions?

#

or should it just run as is

#

oki I got it working

#

uhhhh now I hav a smol question which should actually be more related to the command rather than scripting technically

#

where does it execute the command from

#

cuz it says it's from the "script engine"

#

but I want a function to run at the position of the block I click on

wary parcel
#

ok I just need to figure out how to run the function at the position of the block and then I should be all good

#

hmmm for now I should be able to add a step on event and just have it say please step on the block

wary parcel
#

ignore the face location bit

#

that does nothing

#

i think theres a way to get a vector3 something or other from the block but like idk how you do that and even if i did idk how the heck to get it into the ~~~ there

sage sonnet
#

sorry for the delay, its already midnight here

iirc you need to add these

let location = block.location```
after the first if

and then you can add runCommandAsync("blabla", location);
#

or you can try

x: location.x + 0.5,
y: location.y + 0.25,
z: location.z + 0.5
 };```

and adjust to your needs
wary parcel
#

also ty for all your help thus far

wary parcel
sage sonnet
# wary parcel so, assuming this works, what the heck is this code actually doing? ik const is...

its an alternative method!! you can use like this

    const { block } = event;
    if (event.block.typeId === "qol:lesser_ender_core") {
        world.runCommand(
     "execute positioned ${block.location.x + 0.5} ${block.location.y + 0.5} ${block.location.z + 0.5} function blablah");
    }
});```
or this
```world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    const { block } = event;
    if (event.block.typeId === "qol:lesser_ender_core") {
        block.runCommand(
     "/function blablah");
    }
});```
and like this
```world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    const { block } = event;
    let location = block.location;
    if (event.block.typeId === "qol:lesser_ender_core") {
        world.runCommand(
     "/function blablah", location);
    }
});```

im half sleep rn so if any { or whatever is out of place mb 🀠 but lmk if you got it working
wary parcel
#

ty for the much helps

#

but if you are awake enough to answer or you see this when you wake up, what is the $ ?

left swan
wary parcel
#

ohhhh okay

#

none of this works tho

#

unfortunately

#

and idk why

left swan
#

Can I see what you have?

#

And any error logs?

wary parcel
#

the error logs have been extremely unhelpful

#

gimme a sec

left swan
#

Sometimes, it says a lot more then you think

wary parcel
#

here's what I currently have, this does not work, it just throws an expected syntax error because the $ thing doesn't work for some reason

    world,
    system,
    BlockEvent,
    BlockComponentPlayerInteractEvent
  } from "@minecraft/server";
var ML_reacting = false;
let secondsPassed = 0;

function mainTick() {
  if (system.currentTick % 20 === 0) {
    secondsPassed += 1;
  }
  if (system.currentTick % 100 === 0)
  {
    secondsPassed = 0;
  }
  if (ML_reacting === true & system.currentTick % 600 === 0) {
    ML_reacting = false
  }
  system.run(mainTick);
}
system.run(mainTick);

world.beforeEvents.playerInteractWithBlock.subscribe((BlockComponentPlayerInteractEvent) => {
    if (BlockComponentPlayerInteractEvent.block.typeId === "qol:mystical_core") {
      if(ML_reacting === false) {
        world.getDimension("overworld").runCommandAsync("/execute positioned ${BlockComponentPlayerInteractEvent.faceLocation.x} ${BlockComponentPlayerInteractEvent.faceLocation.y} ${BlockComponentPlayerInteractEvent.faceLocation.z} function ML_reactor");
        world.sendMessage("Mystical Core Activated");
        ML_reacting = true
      }
      else if (ML_reacting = true) {
        if (system.currentTick % 100 === 2) {
          world.sendMessage("Mystical Core Reacting");
        }
      }
      else {
       world.sendMessage("Seconds Passed: " + secondsPassed)  
        event.cancel
      }
    }
  }
);```
#

here's the function btw

structure load qol:mystical_spire ~~~ 0_degrees none layer_by_layer 15```
left swan
#

What scripting version are you using?

wary parcel
#

here is the closest version of it working```import {
world,
system,
BlockEvent,
BlockComponentPlayerInteractEvent
} from "@minecraft/server";
var ML_reacting = false;
let secondsPassed = 0;

function mainTick() {
if (system.currentTick % 20 === 0) {
secondsPassed += 1;
}
if (system.currentTick % 100 === 0)
{
secondsPassed = 0;
}
if (ML_reacting === true & system.currentTick % 600 === 0) {
ML_reacting = false
}
system.run(mainTick);
}
system.run(mainTick);

world.beforeEvents.playerInteractWithBlock.subscribe((BlockComponentPlayerInteractEvent) => {
if (BlockComponentPlayerInteractEvent.block.typeId === "qol:mystical_core") {
if(ML_reacting === false) {
world.getDimension("overworld").runCommandAsync("/function ML_reactor");
world.sendMessage("Mystical Core Activated");
ML_reacting = true
}
else if (ML_reacting = true) {
if (system.currentTick % 100 === 2) {
world.sendMessage("Mystical Core Reacting");
}
}
else {
world.sendMessage("Seconds Passed: " + secondsPassed)
event.cancel
}
}
}
);```

wary parcel
#

haven't been able to figure it out

#

and forgor to ask earlier

left swan
#

Do you have something like this in your manifest? This is from one of my packs so yours might look different

wary parcel
#

I don't have a dependencies bit in my manifest... i don't think- nvm I do, huh, welp then it's version 1.5.0

#

that seems veruh old

wary parcel
#

I want to specify that this works, the only issue is that it does it at 0,0,0 which I assume is because it is referencing the position of the dimension and not the block

#

but I can't get it to work without that dimension bit

left swan
#

An old one. Haven't used that version in quiet a long time.

#

Ok

wary parcel
#

I can change that if it's gonna be easier, I have pretty much 0 idea what I'm doing and I can just make backups in case whatever I try doesn't work

left swan
#

So it goes to 0,0,0 because your function is using relative coordinates and running them at world level, so it world be 0,0,0. Using execute can help move where it will run

wary parcel
#

and I assumed that was the problem

#

but idk how to get around it, cuz execute has not been helping

wary parcel
left swan
#

Normally I would not use a function because it isn't flexible. You can run the commands in you script instead which would allow more flexibility with its location

wary parcel
#

you mean just doing .runcommand separately?

#

ok I changed it so it doesn't use the function and just runs the /structure command but it does the same thing

left swan
#

This is what I did for something a 1 year and a half ago. You can see that I using the block's location as x, y, and z and then adding a value to those

#

Instead of ~~~ do ${BlockComponentPlayerInteractEvent.block.location.x} ${BlockComponentPlayerInteractEvent.block.location.y} ${BlockComponentPlayerInteractEvent.block.location.z}

wary parcel
#

what do I need to import for this cuz like, right now, "BlockComponentPlayerInteractEvent" and "event" do the exact same thing

#

and block.location just doesn't exist it seems

left swan
#

hold on, let me do something

wary parcel
left swan
#

So I would have it more like this

world.beforeEvents.playerInteractWithBlock.subscribe((ev) => {
    if (ev.block.typeId === "qol:mystical_core") {
        const {x, y, z} = ev.block.location
        if (ML_reacting === false) {
            ev.block.dimension.runCommand(`structure load qol:mystical_spire ${x} ${y} ${z} 0_degrees none layer_by_layer 15`);
            world.sendMessage(`MYSTICAL CORE ACTIVATED`)
        }
    }
})```
#

Having BlockComponentPlayerInteractEvent is really long and having it shorter can help

wary parcel
#

I don't even know what it is ngl

#

cuz it does the exact same thing if I just use "event"

wary parcel
#

never seen this one before

left swan
#

I forgot async in runCommand
do runCommandAsync

wary parcel
#

oops

#

I should have caught that lol

#

heeeeeeeey it works less goooooo

#

ok

#

what is "ev"

#

can you just put anything in there?

left swan
#

ev is the samething as BlockComponentPlayerInteractEvent, event, bob. It is like a const

wary parcel
#

interesting

left swan
#

Or you can define what you need in an object instead

wary parcel
#

ok so it looks like the issue was just, I only needed to do ${x} cuz this is actually the same format as something I tried earlier and it just didn't work lol

wary parcel
#

just reads it as part of the string

#

but anyways

#

this worked now

left swan
#

If you have any more question, feel free to ask. When you learn more, please update you scripting api version, 1.5.0 is really old like 3 years old

wary parcel
#

yah I kinda figured that out πŸ’€

#

but ye if it works it works and for now that's what matters

left swan
#

One of the versions does remove runCommandAsync so you might have to do

system.run(() => {
                ev.block.dimension.runCommand(`structure load qol:mystical_spire ${x} ${y} ${z} 0_degrees none layer_by_layer 15`);
                world.sendMessage(`MYSTICAL CORE ACTIVATED`)
            })

Which should let it run without the async

#

For future reference

wary parcel
#

ye

#

oh wait system run

#

that's weird

#

interesting

left swan
#

The error that it can't run in restricted execution, it is because beforeEvents run in early execution and runCommand can't use early execute so system.run() helps run it. Doing system.runTimeout() can let you delay the run in ticks

wary parcel
#

gotcha

left swan
#

afterEvents doesn't need system.run()

wary parcel
#

yo what does this "unexpected version for loaded data" mean?

wary parcel
# wary parcel here is the closest version of it working```import { world, system, ...

https://youtu.be/T7sjFq-zlWU boo

mostly everything is working, only weird thing that idk how to fix is during the loop where it says "Mystic Core Reacting" it sends the message 3 times instead of once

so a few questions,

first, how do i fix it so that it only sends the message once

and second, how can i restructure the timer so that it isn't based on the world tick but based on how many ticks after clicking the core

right now all I really have to do is right click the block, you don't actually need the reactor cuz it doesn't check for it yet, but I wanted to show off my ideas for said new reactors

β–Ά Play video
#

I'll do some more investigating later but i gtg to work rn so if anyone has any ideas they're well appreciated ^-^

left swan
wary parcel
#

interesting

wary parcel
wary parcel
#

might have an idea

wary parcel
#

yah it worked, i now have a counter for the reaction

pastel verge
wary parcel
#

it's a superflat quality of life hack

#

and the original idea was to bring back the old so that you have a way to actually reach the nether without using cheats to give yourself lava and stuff... could I also have changed the wandering trader to give you lava instead of doing all this?... yes.... yes I could have...

buuuut you still wouldn't be able to get to the end so I figured hmmmm well I can't really let the wandering trader give you end portal frames... that's a bit busted and also you'd have no way to break them if you make a mistake lol so I went with this idea

#

I added 4 different ones so that it's not like "oh it's trivial to get to the end now" so there's still a good bit of progression which is fun

#

and everything works, but now I need to make sure it checks that you placed the reactors correctly

#

which I should be able to do with a bunch of if statements in an execute command lol

pastel verge
#

looks super cool, its a very unique idea tbh. when i saw the first reactor it reminded me of the og minecraft reactor loll

wary parcel
#

ye that's what it's based off of!

#

:3

#

it's not gonna function exactly the same I think

wary parcel
#

lmao the execute command I'm using to check for blocks here is so long that it crashes the game if you try to run it in chat 😭

#

or not the game but the world

wary parcel
#

it works tho :3

left swan
#

works but crashes?

wary parcel
#

no

#

it just works

#

the command is fine when run by a command block or by a script

#

but when you run it in chat it crashes the world

#

luckily

#

you will never need to run it in chat

#

like ever

#

so that's not an issue lol

left swan
#

What are you running that would crash if you run it in chat?

wary parcel
#

/execute positioned ~~~ if block ~~-1~1 respawn_anchor ["respawn_anchor_charge"=4] if block ~~-1~-1 respawn_anchor ["respawn_anchor_charge"=4] if block ~-1~-1~ respawn_anchor ["respawn_anchor_charge"=4] if block ~1~-1~ respawn_anchor ["respawn_anchor_charge"=4] if block ~1~-1~1 obsidian if block ~-1~-1~1 obsidian if block ~-1~-1~-1 obsidian if block ~1~-1~-1 obsidian if block ~~-1~ obsidian if block ~~1~ obsidian if block ~~1~1 obsidian if block ~~1~-1 obsidian if block ~1~1~ obsidian if block ~-1~1~ obsidian if entity @e [type=ender_crystal,x=~-1,z=~-1] if entity @e [type=ender_crystal,x=~1,z=~-1] if entity @e [type=ender_crystal,x=~1,z=~1] if entity @e [type=ender_crystal,x=~-1,z=~1] run structure load qol:empyrean_spire ~-5 ~-2 ~-5 0_degrees none layer_by_layer 15

left swan
#

That would do it

wary parcel
#

:3

#

but yeah it's totally fine and it doesn't even lag or anything either so that's a bonus

#

okay

#

so

#

last thing I need to fix

#

I need to add a condition to the function where this command is run, where it only does the rest of the function if the command succeeds

#

cuz rn it starts the reactor countdown even though it failed

wary parcel
#

😭

left swan
#

People are creative with scripts

wary parcel
#

uhhh I don't think I actually want to use this though

#

cuz I don't want it to activate when you place the core inside I want it to activate upon right clicking the core while it's in the correct position

#

very cool and good to know tho

wary parcel
#

so uhhhh

left swan
#

Probably an if statement or try catch statement. Your options are limited if you are still using the old api

wary parcel
#

I'm tryna think how I would even catch the error or success even with an if or try catch statement

#

cuz it's from a string that isn't actually run in the function it's just running the command, and that function executes correctly regardless of if the command executes correctly

left swan
wary parcel
#

yeah I meant not in this api version

left swan
#

Wasn't a thought in that version

wary parcel
#

cuz ik I could just add a output to the custom command if that was the case lol

wraith solar
#

hey mind i use ur code for my buttons i'm trying to make for my own addons, i'll give u credit for the code, i'm asking as i have zero skill on how to do scripting

left swan
wary parcel
#

most of it isn't really my code alone anyways

#

but what are you tryna do

#

cuz alot of it might be unnecessary for you depending on your goals for that addon

wraith solar
#

so what i'm trying to do is make a button like the ones in the dugeons content addon, i know how the code and what works, i just don't know how to make my own, (what i mean by the buttons in dca are when the 'off' button is pressed, it switchts to the 'on' block and places a redstone block a block below the block the block the button is on so it can power redstone stuff)

wary parcel
#

in order to update it

left swan
#

You updating to version 1.15.0? That should be it

wary parcel
#

no I was on 1.15.0 already

#

I tried typing in 1.19.0 and it didn't seem to do anything?

left swan
wary parcel
#

no like... hmm lemme test just to be sure but idt anything changed

#

yah it all functions the same

left swan
#

I do a lot with the V2 so somethings are different between the 2
V1 highest is 1.19.0, which is a year old

wary parcel
#

ok but

#

all I have to do to update is to just type a different number there?

left swan
#

From my list, these are the versions that can be used

left swan
wary parcel
#

damn okay

#

well lemme just try 2.6 ig

left swan
#

V1 to V2, lots of changes

wary parcel
#

oh right runcommandasync doesn't work

wary parcel
#

unless this isn't an actual button

#

and it's a block that functions like a button

left swan
wary parcel
#

oh

left swan
#

You would need a button to trigger that. Doesn't work with a custom button

#

Custom button is a different rabbit hole

wary parcel
#

hmmm alr

#

well anyways

wary parcel
# left swan Use this to replace that
    const {x, y, z} = ev.block.location
    if (ev.block.typeId === "qol:mystical_core") {
      if (reacting === false) {
            ev.block.dimension.runCommand(`/execute positioned ${x} ${y} ${z} if block ~~-1~1 obsidian if block ~~-1~-1 obsidian if block ~-1~-1~ obsidian if block ~1~-1~ obsidian if block ~1~~1 obsidian if block ~-1~~1 obsidian if block ~-1~~-1 obsidian if block ~1~~-1 obsidian if block ~~-1~ obsidian if block ~~1~ obsidian if block ~~1~1 obsidian if block ~~1~-1 obsidian if block ~1~1~ obsidian if block ~-1~1~ obsidian run structure load qol:mystical_spire ~-5 ~-2 ~-5 0_degrees none layer_by_layer 15`);
            world.sendMessage(`MYSTICAL CORE ACTIVATED`);
            starttime += system.currentTick;
            reactortype = "Mystical Core";
          reacting = true
      }
      else {
      ev.cancel
      }
    }```
#

so where would I put the system.run bit of code in here

left swan
#

The command needs to be wrapped in it to fire

#

And send message too

wary parcel
#

so the rest of the world.sendmessage n stuff doesn't need to be in it, just the ev.block.dimension.runComm-

#

ok hmm

#

ok got it

#

ok so

#

with that

#

that means I'm just up to date now

#

wahoo

wary parcel
left swan
#

With all the new features. Well, you can make it check the blocks each then summon the structure. That will let you stop it if it doesn't have a block there

wary parcel
#

wdym by check them each and then summon it

#

cuz that sounds like what already happens

left swan
#

you can getBlock of a block in the world. This gives you a better chance to cancel it if it doesn't detect a block. You said that it still runs when the command fails, this is will make it so it doesn't need the command. A little complex, but not that bad

wary parcel
#

oh

#

no

#

the structure doesn't run

#

the timer runs

#

here let me get a video

left swan
#

Because the command runs, it doesn't matter if it passes or not

#

-# I hate execute commands

wary parcel
#

ye the structure is unrelated

wary parcel
wary parcel
left swan
#

I am getting tired so I am going to bed. Good luck

wary parcel
#

oki tyty :3

wary parcel
#

if it's a custom block you'd want to use this I think world.beforeEvents.playerInteractWithBlock.subscribe((ev) => { const {x, y, z} = ev.block.location if (ev.block.typeId === "custom_namespace:custom_block_identifier") { system.run(() => { ev.block.dimension.runCommand(`/setblock ${x} ${y-1} ${z} redstone_block`)}) }

#

if it's a normal button like acacia_button then it's the same thing but replace custom_namespace:custom_block_identifier with minecraft:acacia_button

#

i think

wraith solar
wary parcel
wary parcel
# left swan I am getting tired so I am going to bed. Good luck

if you're awake now do u know why the blocktest is returning true even though there obsidian isn't anywhere near the block? ```world.beforeEvents.playerInteractWithBlock.subscribe((ev) => {
const {x, y, z} = ev.block.location
if (ev.block.typeId === "qol:mystical_core") {
if (reacting === false) {

    let filter = {blockFilter:["minecraft:obsidian"]};
    let from = {x:ev.block.location.x-1,y:ev.block.location.y-1,z:ev.block.location.z}
    let to = {x:ev.block.location.x+1,y:ev.block.location.y-1,z:ev.block.location.z}
    let volume = new BlockVolume(from,to)
    let blocktest = ev.block.dimension.containsBlock(volume,filter)
  }```
#

ik the blockvolume is set up correctly cuz i did a fillblocks function and that worked correctly

left swan
#

So in the 3 blocks that it is detecting, it returns true even if there are no obsidian?

wary parcel
#

yea

wary parcel
#

at least I think it's correct,

I set up world.sendMessage("" + blocktest) in the system.run which sends it as a string and it sends true so that should be whats happening unless for some reason the sendMessage is just saying that blocktest exists or something but that doesn't make sense

left swan
#

I won't be able to help much for the next hour to an hour and a half.

I don't think I have used containsBlock so I need to look at how it works when I get home

wary parcel
#

its pretty simple you just need a blockvolume and a filter (the filter checks that the type of block you want ((in this case obsidian)) is in the position you define)

wary parcel
left swan
#

It returns the block

wary parcel
#

is it just the location, is it the type id or just the whole thing

#

cuz whatever i tried with it just didnt work at all lol

left swan
#

Just needs the location I believe

wary parcel
#

cuz block is more than just the position right? you're using the position you input in order to get the- nvm holdon i might have figured it out

#

yeeee kinda

#

figured out how to check one block at least

#

now i just want to optimize a bit instead of running that same thing a bajillion tomes

wary parcel
wary parcel
#

everything is working yaaaaaaay

#

or at least it should

#

i havent finished setting it up but im pretty sure my smol knowledge of actionscript should help me finish up the rest of this since its very similar

left swan
wary parcel
#

currently I'm making arrays to check each layer of the build

wary parcel
#

nvm you assign it to a variable i thinks

left swan
wary parcel
#

if i understand correctly, it's not defining the items from that object, specifically it's defining properties for the new object, which it does by getting properties of that Vector3 object

#

or in other words, its defining its own properties, by copying properties from the Vector3 object

left swan
#

const {x,y,z} = block.location
works the same as
block.location.x, block.location.y, block.location.z

wary parcel
#

ye

left swan
#

Just makes it smaller

wary parcel
#

the .x tells it to grab the "x" property of block.location

#

doing the {x,y,z} creates a new object (as {} is the new object operator) and then setting it = to block.location gets the properties of that block.location and sets it to the properties of the new object

#

anyways

left swan
#

Been home for 3-4 hours

wary parcel
#

lol

wary parcel
#

i was gonna ask you if you had an idea but i ended up asking in other servers and also in script api general

wary parcel
#

ok

#

now I've got my nested while loop function

#

gonna test and try to get it to output stuff

wary parcel
#

so I think my plan is to use the loops to create an array of objects with the typeIds of each block

#

then in the rightclick definition for each of the reactors, I will add a if statement to check the array against a predetermined array to make sure everything lines up, that way I don't have to check that every single block matches

#

I can just check the whole thing at once

left swan
#

You still checking that each block matches, just in a different way. But that should work fine

wary parcel
#

it works!

wary parcel
#

tho ig technically I could maybe do that with a loop?

#

idk

#

still more efficient to check it once

wary parcel