#How do I make it to where if this entity exists, the script ends or returns?

1 messages · Page 1 of 1 (latest)

spark furnace
#

Hi, I have a simple script here, but for whatever reason. I just can't figure out how to return or end the script if an entity exists.

                for (const entity of world.getDimension("overworld").getEntities({type: 'chaotic:multi'})) {
                    const multiType = entity.getDynamicProperty('multiType')
                    if (multiType == 'generic'){
                        const x = entity.location.x
                        const y = entity.location.y + 1
                        const z = entity.location.z
                        const items = world.getDimension("overworld").getEntities({tags:["item"],location:{x:x,y:y,z:z},maxDistance:1})
                        const random = Math.floor(Math.random() * (1 - 1 + 1)) + 1
                        if (random == 1){
                            world.structureManager.place("chaotic:itemPotionBomb",world.getDimension("overworld"),{x:x,y:y,z:z})
                        }
                    }
                }

I'm somewhat new to scripting, so lmk what I'm supposed to do

#

I want to check for if item exists, then return. However I have no idea how to do that

untold pagoda
#

detect item then fire return

spark furnace
#

I'm not really sure what you mean by that

untold pagoda
#

return;
its built-in callback or function

#

are you having issues with detecting item?

#

or returning part?

spark furnace
#

The part on firing return

#

I have the item detected I just don't know how to get after it being detected, if its successful it fires a return

untold pagoda
#

if you want to detect item then stop script from going onwards js do return;

spark furnace
#

I need to have it to where if it succeeds it returns, it wont always have an item.

#

Say it doesen't have an item. I want it to not return

untold pagoda
#

assign found items to variable then check if ur particular item is there

#

if it is then return

#

if its not dont return

#

casual if statement

spark furnace
#

The main issue is that they use a tag, as once they are picked up they lose the tag and act as a normal item. We are only checking for items with a tag. I need to make an if statement with this and I don't know how.

untold pagoda
#

items.forEach(element => {
    let comp = element.getComponent("item")
    if (comp.itemStack.typeId == "<item_identifier>") {
        world.sendMessage("item found!")
    }
    else {
       world.sendMessage("item not found :<")
    }
})```
spark furnace
#

I'm trying to get the item entity tag. As in from the /tag command

untold pagoda
#

with tag:


items.forEach(element => {
    if (element.hasTag("<tag_here>")) {
        world.sendMessage("item found!")
    }
    else {
       world.sendMessage("item not found :<")
    }
})```
spark furnace
#

Ooh okay

spark furnace
# untold pagoda with tag: ```const items = dimension.getEntities({location: position, type:"mine...

It seems none of the code within the object will execute, as no item exists to begin with. As the item spawns from the code we are trying to stop running if an item is spawned.

At least thats from what I'm seeing. As not even console.warn() will go through from the object.

The code I'm using here:

const items = world.getDimension('overworld').getEntities({location: {x:x,y:y,z:z}, type:"minecraft:item"})
items.forEach(element => {
    if (element.hasTag("item")) {
        console.warn('Failed')
    } else {
        console.warn('Sucess')
        const random = Math.floor(Math.random() * (1 - 1 + 1)) + 1
        if (random == 1){
            world.structureManager.place("chaotic:itemPotionBomb",world.getDimension("overworld"),{x:x,y:y,z:z})
        } 
    }}
#

I'm spawning it via a structureblock to have a tag attached to it, as tags get removed once picked up. But are added when spawned w/ it

untold pagoda
#

what is running that script? any event? and failed should be success

spark furnace
#

For now its just on system interval as its normally delayed to work every 1200 ticks, or 1 minute.

#

I have it set faster for working on it though

untold pagoda
#

um try using entitySpawn or load event

#

it would be much more optimized

#

and easier to detect item

spark furnace
#

Which includes the console.warn

#

Probably because we're only running if their is an entity. then running the check.

untold pagoda
#

put world.sendMessage(${element.typeId}) inside forEach

#

for debug

spark furnace
#

Alr

#

"element is not defined" gets returned everywhere. Because the item doesn't exist yet

untold pagoda
#

yeah i got it, what happens if the item does exist

spark furnace
#

Then it works

#

Another thing is I had maxDistance:1 setup. This I don't

#

If I set that up it'd likely not work again either, unless it meets all requirements

untold pagoda
#

yea cause you set maxDistance to 1

untold pagoda
spark furnace
#

The item is supposed to only spawn if the item isn't already spawned

#

And the moment somebody picks it up it can spawn again

#

Hence the maxDistance 1 is being used

#

(It's also spawned with a tag because a special effect is added to it)