Jump to content

[1.12.2] Client Side Entity variable not changing with Packet


FuzzyAcornIndustries

Recommended Posts

I am working with Packets to assign a value to an Entity's Client Side version to pass animation information to its Model class. This particular value tells the client side entity that the mob has a current target (aggro). Most all of the code I have checked seems to run/fire correctly but the key value I need assigned/changed doesn't change on the client side entity.

 

With this set up, when I run print statements I see the Server side Entity will say true for isAggroed but false for Client side.

 

Here is what I have right now:

Entity Class (EntityImmortalArcanine):

	public boolean isAggroed = false;

.......

	@Override
	public void onUpdate()
	{
		super.onUpdate();

		.....

  		if(!this.world.isRemote)
		{
			System.out.println( "Server isAggroed " + isAggroed );
			
			checkAggro();
		}
		else
		{
			System.out.println( "Client isAggroed " + isAggroed );
			
			calculateAggroValue();
		}
		....
	}

	public void checkAggro()
	{
		if(this.getAttackTarget() != null)
		{
			if(!this.isAggroed)
			{
				this.isAggroed = true;
				KindredLegacyMain.sendMobAggroPacket(this, true);
			}
		}
		else
		{
			if(this.isAggroed)
			{
				this.isAggroed = false;
				KindredLegacyMain.sendMobAggroPacket(this, false);
			}
		}
	}

	@Override
	public void setAggro(boolean isAggroed)
	{
		this.isAggroed = isAggroed;
	}

 

Main Mod Class (KindredLegacyMain):

	@EventHandler
	public void init(FMLInitializationEvent event)
	{
		network = NetworkRegistry.INSTANCE.newSimpleChannel(ModInfo.CHANNEL);
		
		int packetID = 0;
		network.registerMessage(PacketAnimation.Handler.class, PacketAnimation.class, packetID, Side.CLIENT);
		packetID++;
		network.registerMessage(PacketMobTargeting.Handler.class, PacketMobTargeting.class, packetID, Side.CLIENT);

		.....
	}

.......

	public static boolean isEffectiveClient() 
	{
		return FMLCommonHandler.instance().getEffectiveSide().isClient();
	}

	public static void sendMobAggroPacket(IAdvAnimatedEntity entity, boolean isAggroed) 
	{
		if(isEffectiveClient()) 
			return;

		entity.setAggro(isAggroed);
		network.sendToAll(new PacketMobTargeting((boolean)isAggroed, ((Entity)entity).getEntityId()));
	}

 

Packet Class (PacketMobTargeting):

public class PacketMobTargeting implements IMessage 
{
	private boolean isTargeting;
	private int entityID;

	public PacketMobTargeting() {}

	public PacketMobTargeting(boolean isTargetingBoolean, int entity) 
	{
		isTargeting = isTargetingBoolean;
		entityID = entity;
	}

	@Override
	public void toBytes(ByteBuf buffer) 
	{
		buffer.writeBoolean(isTargeting);
		buffer.writeInt(entityID);
	}

	@Override
	public void fromBytes(ByteBuf buffer) 
	{
		isTargeting = buffer.readBoolean();
		entityID = buffer.readInt();
	}

	public static class Handler implements IMessageHandler<PacketMobTargeting, IMessage> 
	{
		@Override
		public IMessage onMessage(PacketMobTargeting packet, MessageContext ctx) 
		{
			World world = KindredLegacyMain.proxy.getWorldClient();
			IAdvAnimatedEntity entity = (IAdvAnimatedEntity)world.getEntityByID(packet.entityID);

			if(entity != null) 
			{
				entity.setAggro(packet.isTargeting);
			}

			return null;
		}
	}
}

 

Associated Interface (IAdvAnimatedEntity):

public interface IAdvAnimatedEntity 
{
	void setAggro(boolean isAggro);
	float getAggroValue();
}

 

This set up is almost identical to an action animation set up I have and that does work correctly.

Link to comment
Share on other sites

Ah, I did not know EntityDataManager applied to both Client and Server. It works beautifully now, thanks!

 

I may look to see if that can affect my action animation code as that logic was originally written before EntityDataManager was an established thing.

 

Thank you for that little bit about Packets as that helped me better realize what those various functions on SimpleNetworkWrapper are intended to do.

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.