Jump to content

Syncing entities without datawatcher


shucke

Recommended Posts

I am wondering if there is a way to sync my entities without using the datawatcher.

I dont want to use the datawatcher since it is a lot of data and packets are easier in my opinion.

So i need to sync my entities with all the players when the entity is instatiated.

so i made this simple packet:

public class PacketEntityPetData extends PacketBase{

private EntityPet pet;

private int entityID;
private int petID;

public PacketEntityPetData(){}

public PacketEntityPetData(EntityPet entityPet){
	this.pet = entityPet;
}

@Override
public void write(ByteArrayDataOutput out) {
	if(FMLCommonHandler.instance().getEffectiveSide().isClient()){
		out.writeInt(pet.entityId);
	}else{
		out.writeInt(pet.entityId);
		out.writeInt(pet.getPet().getPetID());
	}
}

@Override
public void read(ByteArrayDataInput in) throws ProtocolException {
	if(FMLCommonHandler.instance().getEffectiveSide().isClient()){
		this.entityID = in.readInt();
		this.petID = in.readInt();
	}else{
		this.entityID = in.readInt();
	}
}

@Override
public void execute(EntityPlayer player, Side side) throws ProtocolException {
	if(side.isClient()){
		this.pet = (EntityPet) player.worldObj.getEntityByID(entityID);
		this.pet.setPetBase(petID);
	}else{
		System.out.println(this.entityID);
		this.pet = (EntityPet) player.worldObj.getEntityByID(entityID);
		PacketDispatcher.sendPacketToPlayer(new PacketEntityPetData(this.pet).makePacket(), (Player) player);
	}
}
}

and the packet is send in the entityInit method:

protected void entityInit(){
	super.entityInit();
	if(this.worldObj.isRemote){
		PacketDispatcher.sendPacketToServer(new PacketEntityPetData(this).makePacket());
	}
	//this.dataWatcher.addObject(21, Integer.valueOf(1));
}

 

but i noticed that the entity ids arent the same when i send the packet.

it prints 214 on the first tick on client opposing to the server which prints 103.

so im getting a NullPointerException.

 

anyone knows how to sync properly between client and server?

 

Link to comment
Share on other sites

If it's a TileEntity you can pass XYZ coordinates, then use world.getBlockTileEntity(...)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

A:  You cannot rely upon EntityID on the client side until AFTER that entity has been fully setup.  The entity spawn packet/data will force-set that client-side entity to use the same entityID as the server-side copy of that entity -- but only AFTER it has received all of its spawn data.  E.g.  you cannot rely upon client-side entityID in the constructor for that entity.

 

(as entityInit gets called in the entity constructor, it is not yet fully setup at that point).

 

Also, as others point out, you should be sending information from server -> client in most cases.  Only rarely would you need to send client->server data from an entity -- it is usually better to handle the player interaction logic server side and use the normal server-> client update channels.

 

The proper method to setup a client-side entity is to use the IEntityAdditionalSpawnData interface to load the initial client-side entity with data.  After that, a data-watcher is probably the best built-in method to keep things synched.  It has less overhead than you seem to think -- the data-watcher is already being synched to client-- adding in a few more bits (ints) of data is small compared to the overhead of a complete packet for only sending 1/2 ints -- data watchers also only update/send data when their data has changed.

Link to comment
Share on other sites

I thought that since the entity init was also called on the client side i just created a package to request the data from the server and send a new package back to the client.

 

and i didnt know about the IEntityAdditionalSpawnData interface :/

but that is exactly what i needed, thx!

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.



×
×
  • Create New...

Important Information

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