Jump to content

[1.9.4] [UNSOLVED] Get nearby entities problem


Bektor

Recommended Posts

Hi,

 

I've got some code to get the nearby entity but for some reason it never adds the player to the list and only

adds stuff to the list when I am in survival mode. When I'm in creative mode it can't find any animals nearby or does

not add them to the list.

 

What should the code do?

1. Find all nearby entities

2. Add all nearby entities except for spectator and creative players

3. Should ignore the searching entity

 

The code:

        ArrayList<Entity> output = new ArrayList<Entity>();
        List<Entity> foundOnes = worldIn.getEntitiesWithinAABB(clazz, new AxisAlignedBB(pos).expand(range, range, range));
        
        if(foundOnes.size() > 0) {
            for(Object e : foundOnes) {
                Entity result = (Entity) e;
                
                if(result != null && result.getEntityId() != entity.getEntityId()) {
                    if(result instanceof EntityPlayer) {
                        EntityPlayer player = (EntityPlayer) result;
                        
                        if(!player.isSpectator() && !player.isCreative())
                            output.add(result);
                    } else
                        output.add(result);
                }
            }
        }
        
        return output;

 

Method header:

    public static ArrayList<Entity> getEntitiesInRange(World worldIn, Entity entity, Class<? extends Entity> clazz, BlockPos pos, int range) {

 

Get's called by this:

getEntitiesInRange(this.worldObj, this, Entity.class, this.getPosition(), 20);

 

So what am I doing wrong and is so much code required or is there something else I could use to do the same?

 

Thx in advance.

Bektor

Developer of Primeval Forest.

Link to comment
Share on other sites

public static <T extends Entity> List<T> getEntitiesInRange(World world, Entity exclude, Class<T> clazz, BlockPos pos, int range) {
    return world.getEntitiesWithinAABB(
        clazz,
        new AxisAlignedBB(pos).expandXyz(range),
        Predicates.and(e -> e != exclude, EntitySelectors.CAN_AI_TARGET)
    );
}

Ok, but the exclude thing is not working and could you explain the code?

Developer of Primeval Forest.

Link to comment
Share on other sites

Define "not working".

As for the code:

 

expandXyz(a)

is the same as

expand(a, a, a)

.

Predicates.and

combines two predicates into one that only returns true if both return true. The first predicate I pass in is a Lambda Expression from Java 8, you could use an anonymous class here or

Predicates.not(Predicates.equalTo(exclude))

, but these are both pretty clunky imho.

The 2nd predicate is

CAN_AI_TARGET

, which as you can check, checks if the entity is either not a player of it they are a player checks if they are neither in spectator nor survival mode.

Ok, so what is the first predicate for? And what is an predicate?

The problem is: exclude cannot be resolved to a variable

This happens even when I'm copying our code.

 

Ok, nevermind, figured it out myself and thanks for the help. ;)

Developer of Primeval Forest.

Link to comment
Share on other sites

Ok, the thing with exclude is working... just got a typo in the parameter list.

But for some reason, the code which calls the method does not affect the player, but it does effect all other entities. (I'm in survival)

The list tells me also that there was one entity found (the player), but it does not move the player.

 

        Iterator<Entity> i = this.nearby.iterator();
        
        while(i.hasNext()) {
            Entity entity = (Entity) i.next();
            
            if(entity == null || entity.isDead)
                return;
            
            double moveX = (this.posX + .5d) - (entity.posX + .5d);
            double moveY = (this.posY + .5d) - (entity.posY + .5d);
            double moveZ = (this.posZ + .5d) - (entity.posZ + .5d);
            
            double distanceSqrt = Math.sqrt(moveX * moveX + moveY * moveY + moveZ * moveZ);
            entity.motionX += moveX / distanceSqrt * .5f;
            entity.motionY += moveY / distanceSqrt * .5f;
            entity.motionZ += moveZ / distanceSqrt * .5f;
        }

So what is wrong there? nearby is the list which calls getEntitiesInRange.

Developer of Primeval Forest.

Link to comment
Share on other sites

Ok, I've got now a packet which should be send to the client:

 

public class PacketPortal implements IMessage, IMessageHandler<PacketPortal , IMessage>{
    
    private double motionX;
    private double motionY;
    private double motionZ;
    
    public PacketPortal (double motionX, double motionY, double motionZ) {
        this.motionX = motionX;
        this.motionY = motionY;
        this.motionZ = motionZ;
    }
    
    @Override
    public IMessage onMessage(PacketPortal message, MessageContext ctx) {
        return message;
    }
    
    @Override
    public void fromBytes(ByteBuf buf) {
        this.motionX = buf.readDouble();
        this.motionY = buf.readDouble();
        this.motionZ = buf.readDouble();
    }
    
    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeDouble(this.motionX);
        buf.writeDouble(this.motionY);
        buf.writeDouble(this.motionZ);
    }
}

 

So what do I need to put into onMessage to let the player move into that direction and also to check if the player does the right thing.

 

And what is better? Do it that way or use the start and end positions? And if using them, how do I have to modify my packet and my other code which calles the packet (which is in the entity update).

Developer of Primeval Forest.

Link to comment
Share on other sites

Well, what I want is to move the player to my entity. So my entity is basically like a black hole. It gets everything nearby and pulls them to my entity.

 

So I'm now wondering what the most performant way is. (oh and with some mods with add more motion then my entity it should be possible to get away from my entity).

 

And how do I get the player?

Developer of Primeval Forest.

Link to comment
Share on other sites

1. You had better provide the position information of the 'black hole' in the packet, and move the player to the direction.

2. The player we control is stored in Minecraft#thePlayer. As far as I know, it is the only entity which is not fully controlled on the server.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

Something like Proxy#getDefaultPlayer() with implementation on clientproxy only?

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

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.