#Prevents blocks to be set near entity

31 messages · Page 1 of 1 (latest)

exotic vapor
#
let mob = 'minecraft:zombie'
let radius = 40

BlockEvents.placed(event => {
  let {player, level} = event;
  let aabb = player.getBoundingBox().inflate(radius);
  let ents = level.getEntities(player, aabb);

  for (let ent of ents) {
    if (ent.type == mob) {
      let dx = ent.x - player.x;
      let dy = ent.y - player.y;
      let dz = ent.z - player.z;
      if (dx*dx + dy*dy + dz*dz <= radius * radius) {
        player.inventoryMenu.broadcastFullState()
        event.cancel(); 
      }
    }
  }
});

I ve got the code and it works pretty well. but now im trying to optimize it a little bit. the main goal is to stop running script if there is no needed entity in the world.
is there any way to check that entity is alive and it is in the world?

limber vaporBOT
#

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

fleet scaffold
#

there will almost always be other entities inside the world. I recommend instead using the getEntitiesWithin(aabb) method and then filtering by .length > 1 to see if there are more entities inside your bounding box check than required for the script to run

exotic vapor
#

something like that u mean?

let mob = 'minecraft:zombie'
let radius = 40

BlockEvents.placed(event => {
  let {player, level} = event;
  let aabb = player.getBoundingBox().inflate(radius);
  let ents = level.getEntitiesWithin(aabb);
  
  let right_ents = ents.filter(ent => ent.type == mob);
  
  if (right_ents.length == 0) {
      player.tell('no entity nearby')
      return
  }

  for (let ent of ents) {
    if (ent.type == mob) {
      let dx = ent.x - player.x;
      let dy = ent.y - player.y;
      let dz = ent.z - player.z;
      if (dx*dx + dy*dy + dz*dz <= radius * radius) {
        player.inventoryMenu.broadcastFullState()
        event.cancel(); 
      }
    }
  }
});
exotic vapor
fleet scaffold
#

it wouldnt be >1 if you filter it

#

because you would always be counted inside your own bounding box

#

but yeah that looks good to me so

pearl pebbleBOT
exotic vapor
#

yeah. i have already tryed

fleet scaffold
#

also one thing to note is that inflating the aabb by 40 like that does not make it a 40x40 bounding box, its bigger than that

fleet scaffold
exotic vapor
#

do you mean that the box will be bigger because it includes chunks instead of blocks?

fleet scaffold
#

no

#

when you inflate 1 it inflates a 2x1x1 1 in each direction so it'd become 4x2x2, so on and so forth

#

so really youd set it to radius of 20 not 40

#

unless you want 80x80 aabb

exotic vapor
#

ah no. 40 is exactly the radius I wanted. the 80x80 box

fleet scaffold
#

alright

#

if there isnt anything else then

pearl pebbleBOT
#

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 should create a new post for unrelated issues.

exotic vapor
#

thank you

#

or mb you have any other advises?

fleet scaffold
#

advice about optimizing it?

#

hmm

exotic vapor
#

yes. to make it less weight to server

fleet scaffold
#

theres an entity.distanceToEntitySqr(ent) function

#

though it does essentially the same as your distance squared logic, it does reduce the code length, other than that there isnt really much else tbh. you do need the getEntitiesWithin running at all times for it to always work. looks optimized to me

exotic vapor
#

ok. i understand

#

thanks again

fleet scaffold
#

yea, just edited it, its actually distanceToEntitySqr not distanceToSqr