Jump to content

[1.19.2]please a working example of Level.getNearbyEntities() to match the closest LivingEntity thing around the player


Recommended Posts

Posted (edited)

good nights 

 

the thing i currently in is to make the player to rotate and look to some ghost entity under certain non defined yet conditions 

the code is still wip soo for now im just looking to find the nearest living entity and rotate to look diretly towars the target getEyePosition() 

i alredy have the maths  

but have troubles to scan the area and find the nearest entity from LivingEntity mi findSourroundingEntitys() looks like just picking ramdon entities sometimes the close sometimes the farest or none at all 

 

minecraft has alredy a method for that but its asking for an object i dont get how to setup thas why i begin to make my own 

level.getNearbyEntities(Class<T>, TargetConditions, pe, aabb_box);

 

i need a working example of getNearbyEntities soo i can use it as template for mi code 

 

Spoiler

				
			
		package mercblk.item.rod;				
			import java.util.ArrayList;
		import java.util.HashSet;
		import java.util.List;
		import java.util.Optional;
		import java.util.Set;				
			import mercblk.util.Target;				
			import java.util.Map.Entry;
		import java.util.function.Predicate;				
			import net.minecraft.advancements.critereon.EntityPredicate;
		import net.minecraft.core.BlockPos;
		import net.minecraft.core.Direction;
		import net.minecraft.core.particles.ParticleTypes;
		import net.minecraft.network.chat.Component;
		import net.minecraft.resources.ResourceKey;
		import net.minecraft.server.level.ServerLevel;
		import net.minecraft.sounds.SoundEvents;
		import net.minecraft.world.InteractionHand;
		import net.minecraft.world.InteractionResultHolder;
		import net.minecraft.world.entity.Entity;
		import net.minecraft.world.entity.LivingEntity;
		import net.minecraft.world.entity.ai.targeting.TargetingConditions;
		import net.minecraft.world.entity.player.Player;
		import net.minecraft.world.item.Item;
		import net.minecraft.world.item.ItemStack;
		import net.minecraft.world.item.Items;
		import net.minecraft.world.level.Level;
		import net.minecraft.world.level.block.Block;
		import net.minecraft.world.level.block.Blocks;
		import net.minecraft.world.level.block.state.BlockState;
		import net.minecraft.world.phys.AABB;
		import net.minecraft.world.phys.Vec2;
		import net.minecraft.world.phys.Vec3;
		import net.minecraftforge.registries.ForgeRegistries;				
			public class aligner_stick extends Item {				
			    // ########## ########## ########## ##########
		    public aligner_stick(Properties propiedad) {
		        super(propiedad);
		        // TODO Auto-generated constructor stub
		    }				
			    // ########## ########## ########## ##########
		    @Override
		    public InteractionResultHolder<ItemStack> use(Level warudo, Player pe, InteractionHand hand) {				
			        // if ( !warudo.isClientSide )
		        { // && !warudo.isClientSide				
			            Vec3 vo = null;//				
			            ArrayList<LivingEntity> lista = this.findSourroundingEntitys(warudo, pe.getEyePosition());				
			            // list<LivingEntity> lista = warudo.getNearbyEntities(Class<T>, TargetConditions, pe, aabb_box);
		            // in this method i dont get how to set up the TargetConditions thing				
			            System.out.println("\n\n");				
			            int count = 0;
		            for (LivingEntity le : lista) {
		                System.out.println(count + "] " + pe.distanceTo(le) + " => " + le.getName().toString());				
			                count++;
		            }				
			            System.out.println("\n\n");				
			            // the entity number zero is the player soo the closest must be the entity
		            // number one
		            if (lista.size() > 1) {
		                vo = lista.get(1).getEyePosition();				
			                float dx = this.get_XRot(pe.getEyePosition(), vo);
		                float dy = this.get_YRot(pe.getEyePosition(), vo);				
			                System.out.println("[" + lista.get(1).getName().toString() + "]");
		                System.out.println(dx + ", " + dy);				
			                pe.setXRot(dx);
		                pe.setYRot(dy);				
			            }				
			        }				
			        return InteractionResultHolder.pass(pe.getItemInHand(hand));
		    }				
			    // ########## ########## ########## ##########
		    public float get_XRot(Vec3 tmpvi, Vec3 tmpvo) {				
			        Double angle = 0.0D;				
			        Double dy = tmpvo.y - tmpvi.y;// cateto y
		        Double dx = Math.sqrt(Math.pow((tmpvo.x - tmpvi.x), 2) + Math.pow((tmpvo.z - tmpvi.z), 2));// cateto x				
			        // System.out.println("cateto y " + dy);
		        // System.out.println("cateto x " + dx);				
			        Double tan = Math.atan(dy / dx) * -1;// angle in radians
		        angle = this.radianesAgrados(tan);// angle in grads				
			        // System.out.println("tan α " + tan);
		        // System.out.println("angle β " + angle);				
			        return (float) (angle * 1.0F);
		    }				
			    // ########## ########## ########## ##########
		    public float get_YRot(Vec3 tmpvi, Vec3 tmpvo) {				
			        Double angle = 0.0D;				
			        Double dx = tmpvo.x - tmpvi.x;// cateto x
		        Double dz = tmpvo.z - tmpvi.z;// cateto z				
			        // System.out.println("cateto x " + dx);
		        // System.out.println("cateto z " + dz);				
			        Double tan = Math.atan(dx / dz) * -1;// angle in radians
		        angle = this.radianesAgrados(tan);// angle in grads				
			        // System.out.println("tan α " + tan);				
			        // fixing numbers to mach minecraft way of doing things
		        if (dz < 0) {
		            angle = (dx < 0) ? (180D + angle) : (-180D + angle);
		        }				
			        // System.out.println("angle Φ " + angle);
		        return (float) (angle * 1.0F);
		    }				
			    // #########################################################################3
		    public Double radianesAgrados(Double x) {
		        return ((x * 180F) / Math.PI);
		    }				
			    // ########## ########## ########## ##########
		    // list of entities around of pe.getEyePosition() an sort them by distance to
		    // pe.getEyePosition()
		    public ArrayList<LivingEntity> findSourroundingEntitys(Level warudo, Vec3 center) {				
			        float size = 16f;
		        float msize = size * -1;				
			        Vec3 vi = center.add(msize, msize, msize);
		        Vec3 vo = center.add(size, size, size);
		        //				
			        AABB bb = new AABB(vi, vo);				
			        ArrayList<LivingEntity> list = new ArrayList<LivingEntity>();
		        Predicate<? super Entity> cosa = target -> (target instanceof LivingEntity);//				
			        for (Entity entity1 : warudo.getEntities((Player) null, bb, cosa)) {
		            list.add((LivingEntity) entity1);
		        }				
			        // sort the list from close to far
		        list.sort((o1,
		                o2) -> ((int) ((o2.position().distanceTo(vi)) * 1000) - (int) ((o1.position().distanceTo(vi)) * 1000)));				
			        return list;
		    }				
			    // ##########
		    public BlockPos blockpos_from_vec3(Vec3 in) {
		        return new BlockPos((int) in.x, (int) in.y, (int) in.z);
		    }				
			    // ##########
		    public static Vec3 vec3_from_blockpos(BlockPos in) {
		        return new Vec3(in.getX() + 0.5, in.getY() + 0.5, in.getZ() + 0.5);
		    }				
			    public double doublex2(double f) {
		        int i = (int) (f * 100);
		        return ((double) i / 100.0D);
		    }				
			    public static void particle(Level warudo, BlockPos vh) {				
			        particle(warudo, vec3_from_blockpos(vh.above()));
		    }				
			    public static void particle(Level warudo, Vec3 vh) {
		        System.out.println("particle(" + vh);				
			        // util.showthis( String.format(" %1$s, %2$s, %3$s", vh.x, vh.y, vh.z ) , pe);
		        warudo.addAlwaysVisibleParticle(ParticleTypes.FLAME, vh.x, vh.y, vh.z, 0.0D, 0.0D, 0.0D);
		        warudo.addParticle(ParticleTypes.SMOKE, vh.x, vh.y, vh.z, 0.0D, 0.0D, 0.0D);				
			        // pe.playSound(SoundEvents.FIRE_EXTINGUISH, 1.0F, 1.0F);
		    }				
			    // ########## ########## ########## ##########
		    public static void showthis(String mensaje, LivingEntity le) {				
			        if (le instanceof Player) {
		            Player pe = (Player) le;
		            System.out.println(mensaje);
		            pe.displayClientMessage(Component.translatable(mensaje), true);				
			        }
		    }				
			    // ########## ########## ########## ##########
		    // ########## ########## ########## ##########
		    // ########## ########## ########## ##########
		    // ########## ########## ########## ##########				
			}
		 				
			

 

 

 

 

 

 

 

 

 

 

thanks for your time 

 

Edited by perromercenary00
adding more information
  • perromercenary00 changed the title to [1.19.2]please a working example of Level.getNearbyEntities() to match the closest LivingEntity thing around the player

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.