Jump to content

[1.7.10] Small iMessage issue


Frag

Recommended Posts

Hi guys,

 

I was wondering if using EntityPlayer player = FantasticMod.proxy.getPlayerFromMessage(ctx) and Entity _ent = player.worldObj.getEntityByID(message.entityId) is a standard way to get the worldobj in iMessage. Is there any other way "safer"?

 

I am asking because sometimes the Entity returned by my line Entity _ent = player.worldObj.getEntityByID(message.entityId) is null.

 

Any cleaner way to get the entity id? I don't trust that one much...

 

@Override
public IMessage onMessage(AIStateMessage message, MessageContext ctx) 
{
	if (message!=null)
	{
		FantasticDebug.Output("MESSAGE RECEIVED. Entity: "+Integer.toString(message.entityId)+" AI State:"+Integer.toString(message.aiState));
		EntityPlayer player = FantasticMod.proxy.getPlayerFromMessage(ctx);
		if (player!=null)
		{
			Entity _ent = player.worldObj.getEntityByID(message.entityId);
			if (_ent!=null)
			{
				((EntityFantasticFish)_ent).brain.SetCurrentAIState(message.aiState);
				FantasticDebug.Output("New AIState set to: Entity: "+Integer.toString(message.entityId)+" AIState:"+Integer.toString(message.aiState));
				return null;

			}
			else
			{
				FantasticDebug.Output("Entity IS NULL! ",true);
			}


		}
		else
		{
			FantasticDebug.Output("PLAYER IS NULL!");
		}
	}
	else
	{
		FantasticDebug.Output("MESSAGE IS NULL!",true);
	}
	return null;

}

 

Here is the full message code if you want to see it...

 

package fantastic.network;


import io.netty.buffer.ByteBuf; 
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer; 



import cpw.mods.fml.common.network.simpleimpl.IMessage; 
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; 
import cpw.mods.fml.common.network.simpleimpl.MessageContext; 
import cpw.mods.fml.relauncher.Side; 
import fantastic.FantasticDebug;
import fantastic.FantasticMod;
import fantastic.entities.EntityFantasticFish;


public class AIStateMessage implements IMessage, IMessageHandler<AIStateMessage, IMessage> 
{ 
    private int entityId;
    private int aiState;


    public AIStateMessage() 
    { 
    } 


    public AIStateMessage(int anEntityId,int anAIState) 
    { 
        entityId=anEntityId;
        aiState=anAIState;


    } 


    @Override 
    public void fromBytes(ByteBuf buf) 
    { 
        entityId = buf.readInt(); 
        aiState = buf.readInt(); 
    } 


    @Override 
    public void toBytes(ByteBuf buf) 
    { 
        buf.writeInt(entityId); 
        buf.writeInt(aiState); 
    } 


@Override
public IMessage onMessage(AIStateMessage message, MessageContext ctx) 
{
	if (message!=null)
	{
		FantasticDebug.Output("MESSAGE RECEIVED. Entity: "+Integer.toString(message.entityId)+" AI State:"+Integer.toString(message.aiState));
		EntityPlayer player = FantasticMod.proxy.getPlayerFromMessage(ctx);
		if (player!=null)
		{
			Entity _ent = player.worldObj.getEntityByID(message.entityId);
			if (_ent!=null)
			{
				((EntityFantasticFish)_ent).brain.SetCurrentAIState(message.aiState);
				FantasticDebug.Output("New AIState set to: Entity: "+Integer.toString(message.entityId)+" AIState:"+Integer.toString(message.aiState));
				return null;

			}
			else
			{
				FantasticDebug.Output("Entity IS NULL! ",true);
			}


		}
		else
		{
			FantasticDebug.Output("PLAYER IS NULL!");
		}
	}
	else
	{
		FantasticDebug.Output("MESSAGE IS NULL!",true);
	}
	return null;

} 
} 

Link to comment
Share on other sites

EntityPlayer player = FantasticMod.proxy.getPlayerFromMessage(ctx)

I have no idea what this does.

The entity will be null if no entity with that ID exists on the client. Show where you send the packet.

 

Actually I just want to get the worldobj object. Where can I get it from when in an iMessage?

Link to comment
Share on other sites

EntityPlayer player = FantasticMod.proxy.getPlayerFromMessage(ctx)

I have no idea what this does.

The entity will be null if no entity with that ID exists on the client. Show where you send the packet.

 

Actually I just want to get the worldobj object. Where can I get it from when in an iMessage?

 

That depends. Are you calling that from the server side, or the client side?

Link to comment
Share on other sites

EntityPlayer player = FantasticMod.proxy.getPlayerFromMessage(ctx)

I have no idea what this does.

The entity will be null if no entity with that ID exists on the client. Show where you send the packet.

 

Actually I just want to get the worldobj object. Where can I get it from when in an iMessage?

 

That depends. Are you calling that from the server side, or the client side?

 

Thanks for the help TrashCaster. On the client side.

 

I have a simple need, I am trying to get the worldobj from the clientside when the message is received so I can find the entity from its ID. Look at my 2 lines in bold. The first one get me the player, in the second one, I use the player to get the world object to find the entity with it. But I hate that method. I am pretty sure there a more effective way to get the entity from its ID. Tons of people use iMessage, just did not find an example where the Entity is found clientside from its ID.

 

@Override

public IMessage onMessage(AIStateMessage message, MessageContext ctx)

{

if (message!=null)

{

FantasticDebug.Output("MESSAGE RECEIVED. Entity: "+Integer.toString(message.entityId)+" AI State:"+Integer.toString(message.aiState));

EntityPlayer player = FantasticMod.proxy.getPlayerFromMessage(ctx);

if (player!=null)

{

Entity _ent = player.worldObj.getEntityByID(message.entityId);

if (_ent!=null)

{

((EntityFantasticFish)_ent).brain.SetCurrentAIState(message.aiState);

FantasticDebug.Output("New AIState set to: Entity: "+Integer.toString(message.entityId)+" AIState:"+Integer.toString(message.aiState));

return null;

 

}

else

{

FantasticDebug.Output("Entity IS NULL! ",true);

}

 

 

}

else

{

FantasticDebug.Output("PLAYER IS NULL!");

}

}

else

{

FantasticDebug.Output("MESSAGE IS NULL!",true);

}

return null;

 

}

Link to comment
Share on other sites

Why do you "hate" that method? It is the correct way to do this.

 

Checking player and message for null is pointless, they won't be.

 

I just realized, while talking about it, that in a multiplayer environment, it will happen a lot that an entity will exist and will have some value set server side BEFORE a new client connects.

 

Question. When a client connect, does ALL variables set server side will be fully synch with the brand new client side when it will connect?

Link to comment
Share on other sites

Why do you "hate" that method? It is the correct way to do this.

 

Checking player and message for null is pointless, they won't be.

 

I just realized, while talking about it, that in a multiplayer environment, it will happen a lot that an entity will exist and will have some value set server side BEFORE a new client connects.

 

Question. When a client connect, does ALL variables set server side will be fully synch with the brand new client side when it will connect?

 

Was about to comment you before-edit post...

 

As to current one: NO

Setting stuff and sending it, will not magically tell server to auto-send it whenever it is needed.

 

There are few things to look at here:

 

PlayerEvent.StartTracking - fired on server whenever Player starts tracking some event.target entity.

You can use this to instantly send data you need t obe synced. Situations: Some player logs i world where there are a lot of some fishes or whatever. Since this event fires for each entity being tracked you can send update about every singe fish that appeared in players area.

 

PlayerLoggedInEvent - previous event fires when you start tracking other entities, not yourself. This event is precisely for that. There are also: RespawnEvent and ChangedDimensionEvent. Or you can go "fk this" and use EntityJoinWorld event to sent packets needed.

 

EntityTracker - you can utilize it to send packet about some entity to all players currently tracking given entity. Think of it as a very sophisticated "sendToAllAround" which actually sends data to players who need it.

1.7.10 is no longer supported by forge, you are on your own.

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.