Jump to content

Getting entity from id on client side


Briggybros

Recommended Posts

Hello, I've set up a packet to send entity data from the server to the client. However, I cannot get the instance of the entity on the client side to change its variables. Here is my packet class:

public class MyEntityPacket extends MyModPacket {

int entityID,flag;

public MyEntityPacket(int entityID, int flag) {
	this.entityID = entityID;
	this.flag = flag;
}

public MyEntityPacket() {}

@Override
public void write(ByteArrayDataOutput out) {
	out.writeInt(entityID);
	out.writeInt(flag);
}

@Override
public void read(ByteArrayDataInput in) {
	entityID = in.readInt();
	flag = in.readInt();
}

@Override
public void execute(EntityPlayer player, Side side) {
	if (side.isClient()) {
		if (Minecraft.getMinecraft().theWorld.getEntityByID(entityID) instanceof MyEntity) {
		MyEntity e = (MyEntity) player.worldObj.getEntityByID(entityID);
		e.setFlag(flag == 1 ? true : false);
		} else {
			System.out.println("Something ****** up! - supposed my entity with id: " + entityID + " isn't");
		}
	}
}

}

 

That all works fine. My problem is that the method getEntityByID(entityID) always returns null instead of the entity. I'm using this to create the packet in the constructor of the entity (it is the final thing in the constructor so the entity should be fine).

PacketDispatcher.sendPacketToAllAround(par2, par4, par6, 250, this.dimension, new MyEntityPacket(this.entityId, flag ? 1 : 0).makePacket()); // 250 is view radius

Can anyone enlighten me? Do I need to use the UUID (persistent id) instead? If so, how would I get the entity from it?

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.