Jump to content

Bug in multi-entity spawning [SOLVED]


awger

Recommended Posts

In FMLClientHandler.java, function spawnEntityIntoClientWorld, the entityId of the instantiated entity is set to the entityId passed in the packet.  Further down, the new entityId (not the original) is used to calculate the offset for the child entities, resulting in incorrect entityId's that don't match those on the other end.

 

My quick fix was to calculate the offset before altering the entityId of the parent:

 

int ofs = 0;
...
ofs = packet.entityId - entity.entityId;
...
parts[j].entityId += ofs;

 

Link to comment
Share on other sites

** UPDATE **

 

The problem appears to be slightly more complicated than I originally thought... seems that child entities aren't necessarily spawned starting with id's equal to parent.entityId+1.  In testing I've seen several instances of the child entities starting at +2 or +3, suggesting that some other entity spawned after the parent but before the children.

 

Assuming that another entity can be spawned at any time (ie in between a parent and its children, or in between children) the only complete solution is to pass the entityId's of all the children and sync them directly.

 

Not sure about the best way to do this.  I started looking into IEntityAdditionalSpawnData and it looks like it's only one-way (server to client?).  This presents a conundrum, as I was using the vanilla boat code that spawns client-side, and it doesn't work properly when spawned server-side (no instantiation on the client side).

 

Looks like I'll be using a custom packet to set the child id's... unless someone has a better suggestion?

 

Link to comment
Share on other sites

** WORKAROUND / SOLUTION **

 

I updated to the most recent recommend release (6.4.1.401) and added a data watcher and a custom packet handler.  The basic approach is:

  • spawn parent entity on client side, parent spawns child entities
  • client sends spawn packet to server
  • server spawns parent entity and children
  • server sets parent entityId to match client, screws up child entityId's
  • child entities on server watch for change of entityId, update counter in parent when entityId changes
  • when all children on server have updated entityId, server sets flag in data watcher that syncs to client
  • when flag is set on client, client sends custom packet to update entityId's of children on server
  • server receives packet, updates entityId's of children

 

While testing I was seeing some weird things in my logs.

 

First problem:  None of the data watchers appear to be sync'ing with the other end.  I don't know if they're supposed to be unidirectional or bidirectional, but they don't seem to be working in either respect.  I need to do some more research on this but, for now, I have a work-around (ugly kludge, but it works).

 

Second problem:  When trying to update the client entityId's on the server side, I kept retrieving entity data from the client side.

 

I got utterly confused, and started drinking scotch.  Which seems to have done the trick.  At a minimum it didn't hurt and, in any event, I didn't stumble upon the solution until after I started drinking.

 

I'm using getEntityByID (with the entityId passed in the packet) to find the parent entity and retrieve the child entity list.  For some reason unknown to me, within the confines of my PacketHandler, "WorldServer" returns entityId's from the client side, and "WorldClient" returns entityId's from the server side.

 

Code for PacketHandler follows.  Note usage of getEntityByID, which is easily switched between wc (WorldClient) and ws (WorldServer).

 

 

 

package awger.mods.punt.common;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

import net.minecraft.client.Minecraft;
import net.minecraft.src.Entity;
import net.minecraft.src.EntityClientPlayerMP;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.EntityPlayerMP;
import net.minecraft.src.INetworkManager;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.WorldClient;
import net.minecraft.src.WorldServer;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;

public class PacketHandler implements IPacketHandler
{
public static final byte ptSyncChildren = 1;

    @Override
    public void onPacketData(INetworkManager manager,
            Packet250CustomPayload packet, Player p)
    {
    	DataInputStream dis = new DataInputStream(new ByteArrayInputStream(packet.data));

    	byte type = 0;
    	
    	int count;
    	int pid;
    	int cid;
    	int i;
    	
    	EntityPlayer player = (EntityPlayer)p;
    	EntityPlayerMP playerServer = (EntityPlayerMP) player;

    	Entity parent;
        Entity parts[];
    	Entity child;
    	
    	FMLLog.info("common.onPacketData()");

        if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
        	FMLLog.info("Side == CLIENT");
        else if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
        	FMLLog.info("Side == SERVER");
        else
        	FMLLog.info("Side == UNKNOWN");
        
        WorldServer ws = playerServer.getServerForPlayer();
        
        Minecraft mc = FMLClientHandler.instance().getClient();
        WorldClient wc = mc.theWorld;
        
        FMLLog.info("WorldServer.isRemote == %b", ws.isRemote);
        
    	try
    	{
    		type = dis.readByte();
    		
    		switch(type)
    		{
    		case ptSyncChildren:
    			pid = dis.readInt();
    			parent = wc.getEntityByID(pid);
    			
    			if (parent == null)
    				FMLLog.info("!! parent object %d not found", pid);
    			else
    			{
    				parts = parent.getParts();
    				
    				if (parts == null)
    					FMLLog.info("!! parent object %d has no children", pid);
    				else
    				{
    	    			count = dis.readInt();

    	    			if (parts.length != count)
    						FMLLog.info("!! parts count for object %d doesn't match", pid);
    					else
    					{
    						for (i=0; i<count; i++)
    						{
    							cid = dis.readInt();
    							FMLLog.info("\tupdating %s(%d) to %d", ((EntityBoatPart)parts[i]).Name, parts[i].entityId, cid);
    							parts[i].entityId = cid;
    						}
    					}
    				}
    			}
    			break;
    		}
    	} 
    	catch (Exception e)
    	{
    		e.printStackTrace();
    		return;
    	}
    }
}

 

 

 

Here's log from a run calling getEntityId from WorldServer:

 

 

 

2012-11-30 21:30:35 [iNFO] [ForgeModLoader] client: setPosition(0.000000, 0.000000, 0.000000)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] client.entityInit()
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] client: instantiating EntityPunt(3681)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] client: instantiating client.Bow(3682)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] client: instantiating client.Midship(3683)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] client: instantiating client.Stern(3684)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] client: instantiating client.Mast(3685)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] client: setPosition(-204.500000, 63.475000, 251.500000)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] client.addChildrenToWorld()
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] FMLClientHandler: spawnEntityIntoClientWorld()
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] server: setPosition(0.000000, 0.000000, 0.000000)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] server.entityInit()
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] server: instantiating EntityPunt(3687)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] server: instantiating server.Bow(3688)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] server: instantiating server.Midship(3689)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] server: instantiating server.Stern(3690)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] server: instantiating server.Mast(3691)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] 	renumbering parent(3687) to 3681
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] server: setPosition(-204.500000, 63.943750, 251.500000)
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] 	renumbering server.Bow(3688) to 3688
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] 	renumbering server.Midship(3689) to 3689
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] 	renumbering server.Stern(3690) to 3690
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] 	renumbering server.Mast(3691) to 3691
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] !! server.EntityPunt.entityId changed to 3681
2012-11-30 21:30:35 [iNFO] [ForgeModLoader] server.childUpdateCount = 1
2012-11-30 21:30:36 [iNFO] [ForgeModLoader] client.sendChildEntityUpdate()
2012-11-30 21:30:36 [iNFO] [ForgeModLoader] common.onPacketData()
2012-11-30 21:30:36 [iNFO] [ForgeModLoader] Side == SERVER
2012-11-30 21:30:36 [iNFO] [ForgeModLoader] WorldServer.isRemote == false
2012-11-30 21:30:36 [iNFO] [ForgeModLoader] WorldClient.isRemote == true
2012-11-30 21:30:36 [iNFO] [ForgeModLoader] 	updating client.Bow(3682) to 3682
2012-11-30 21:30:36 [iNFO] [ForgeModLoader] 	updating client.Midship(3683) to 3683
2012-11-30 21:30:36 [iNFO] [ForgeModLoader] 	updating client.Stern(3684) to 3684
2012-11-30 21:30:36 [iNFO] [ForgeModLoader] 	updating client.Mast(3685) to 3685
2012-11-30 21:30:37 [iNFO] [ForgeModLoader] server.Bow(3688).attackEntityFrom()
2012-11-30 21:30:37 [iNFO] [ForgeModLoader] server(3681).attackEntityFromPart()

 

 

...and here's the excerpt from calling getEntityId from WorldClient:

 

 

 

2012-11-30 21:29:32 [iNFO] [ForgeModLoader] client: setPosition(0.000000, 0.000000, 0.000000)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] client.entityInit()
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] client: instantiating EntityPunt(3827)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] client: instantiating client.Bow(3828)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] client: instantiating client.Midship(3829)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] client: instantiating client.Stern(3830)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] client: instantiating client.Mast(3831)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] client: setPosition(-204.500000, 63.475000, 252.500000)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] client.addChildrenToWorld()
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] FMLClientHandler: spawnEntityIntoClientWorld()
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] server: setPosition(0.000000, 0.000000, 0.000000)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] server.entityInit()
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] server: instantiating EntityPunt(3832)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] server: instantiating server.Bow(3833)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] server: instantiating server.Midship(3834)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] server: instantiating server.Stern(3835)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] server: instantiating server.Mast(3836)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] 	renumbering parent(3832) to 3827
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] server: setPosition(-204.500000, 63.943750, 252.500000)
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] 	renumbering server.Bow(3833) to 3833
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] 	renumbering server.Midship(3834) to 3834
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] 	renumbering server.Stern(3835) to 3835
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] 	renumbering server.Mast(3836) to 3836
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] !! server.EntityPunt.entityId changed to 3827
2012-11-30 21:29:32 [iNFO] [ForgeModLoader] server.childUpdateCount = 1
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] client.sendChildEntityUpdate()
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] common.onPacketData()
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] Side == SERVER
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] WorldServer.isRemote == false
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] WorldClient.isRemote == true
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] 	updating server.Bow(3833) to 3828
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] 	updating server.Midship(3834) to 3829
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] 	updating server.Stern(3835) to 3830
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] 	updating server.Mast(3836) to 3831
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] !! server.Bow.entityId changed to 3828
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] server.childUpdateCount = 2
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] !! server.Midship.entityId changed to 3829
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] server.childUpdateCount = 3
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] !! server.Stern.entityId changed to 3830
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] server.childUpdateCount = 4
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] !! server.Mast.entityId changed to 3831
2012-11-30 21:29:33 [iNFO] [ForgeModLoader] server.childUpdateCount = 5
2012-11-30 21:29:35 [iNFO] [ForgeModLoader] server.Bow(3828).attackEntityFrom()
2012-11-30 21:29:35 [iNFO] [ForgeModLoader] server(3827).attackEntityFromPart()
2012-11-30 21:29:35 [iNFO] [ForgeModLoader] client.Bow(3828).attackEntityFrom()
2012-11-30 21:29:35 [iNFO] [ForgeModLoader] client(3827).attackEntityFromPart()
2012-11-30 21:29:35 [iNFO] [ForgeModLoader] client.kill()

 

 

A couple items to note:

  • the server/client portion of the child entity name is set at instantiation, so there's no doubt that they're associated with one side or the other
  • log messages are comingled from multiple threads, illustrating the base problem and underscoring the need for a "real" fix (although I'm not sure there is one, and this approach may be the best option)
  • when SIDE == SERVER, isRemote is reversed from the client perspective -- ie, when you're on the server, "remote" is the client side
  • WorldClient and WorldServer appear to be backwards, at least from the perspective of getEntityByID

 

Verbosity for posterity, in the hopes that it may be useful for some other poor schmuck.

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.