Jump to content

1.11 - Packets help? Variable not updating client-side.


Eria8

Recommended Posts

I'm trying to send a packet to update a variable that's being rendered on the player's screen.

When a mob dies, a specific amount is supposed to be added to the variable. But it always renders as 0, so it's just not updating.

There are no errors given in the console.

 

EventHandler

	@SubscribeEvent
	public void die(LivingDeathEvent event)
	{
		int echoes = 0;

		if (event.getSource().getEntity() != null && event.getSource().getEntity() instanceof EntityPlayer)
		{
			EntityLivingBase entity = event.getEntityLiving();

			if (entity instanceof EntitySlime)
			{
				System.out.println("Killed mob"); // This does print, so the event is definitely registered.
				echoes = 5;
			}
			if (echoes > 0)
			{
				PacketHandler.net.sendTo(new UpdateEchoes(), (EntityPlayerMP) event.getSource().getSourceOfDamage());
			}
		}
	}

 

PacketHandler

	public static final SimpleNetworkWrapper net = NetworkRegistry.INSTANCE.newSimpleChannel(Main.MODID);

	public static int id = 0;

	// This is called from the main mod class' FMLInitializationEvent
	public static void init()
	{
		net.registerMessage(UpdateEchoes.Message.class, UpdateEchoes.class, id++, Side.SERVER);
	}

 

UpdateEchoes implements IMessage

	private int echoes, insight;

	public UpdateEchoes(){}

	public UpdateEchoes(int echoes, int insight)
	{
		this.echoes = echoes;
		this.insight = insight;
	}

	@Override
	public void toBytes(ByteBuf buf)
	{
		buf.writeInt(echoes);
		buf.writeInt(insight);
	}

	@Override
	public void fromBytes(ByteBuf buf)
	{
		echoes = buf.readInt();
		insight = buf.readInt();
	}

	public static class Message implements IMessageHandler<UpdateEchoes, IMessage>
	{
		@Override
		public IMessage onMessage(final UpdateEchoes message, MessageContext ctx)
		{
			final EntityPlayerMP player = ctx.getServerHandler().playerEntity;

			Minecraft.getMinecraft().addScheduledTask(new Runnable()
			{
				public void run()
				{
					process(player, message.echoes, message.insight);
				}
			});
			return null;
		}

		public void process(EntityPlayerMP player, int echoes, int insight)
		{
			player.getCapability(EchoesManager.ECHOES, null)
					.setEchoes(player.getCapability(EchoesManager.ECHOES, null).getEchoes() + echoes);
			player.getCapability(EchoesManager.ECHOES, null)
					.setInsight(player.getCapability(EchoesManager.ECHOES, null).getInsight() + insight);
		}
	}

 

Link to comment
Share on other sites

Okay I admit that was stupid.

But changing it to 

PacketHandler.net.sendTo(new UpdateEchoes(echoes, 0)
	(EntityPlayerMP) event.getSource().getSourceOfDamage());

doesn't work either.

 

And isn't the fact that I need to send it to the client because that it's already and only on the server?

How would I do that otherwise?

Link to comment
Share on other sites

I don't understand. I tried changing it to what I think you mean, but it's still not working.

 

EventHandler

	if (echoes > 0)
	{
		IEchoesCapability cap = ((EntityPlayer) event.getSource().getSourceOfDamage())
				.getCapability(EchoesManager.ECHOES, null);
		cap.addEchoes(echoes);

		PacketHandler.net.sendTo(new UpdateEchoes(cap), (EntityPlayerMP) event.getSource().getSourceOfDamage());
	}

 

UpdateEchoes

	public UpdateEchoes(IEchoesCapability cap)
	{
		this.echoes = cap.getEchoes();
		this.insight = cap.getInsight();
	}


public static class Message implements IMessageHandler<UpdateEchoes, IMessage>
	{
		@Override
		public IMessage onMessage(final UpdateEchoes message, MessageContext ctx)
		{
			Minecraft.getMinecraft().addScheduledTask(new Runnable()
			{
				public void run()
				{
					IEchoesCapability cap = Minecraft.getMinecraft().thePlayer.getCapability(EchoesManager.ECHOES, null);
					cap.setEchoes(message.echoes);
					cap.setInsight(message.insight);
				}
			});
			return null;
		}
	}

 

Edited by diesieben07
syntax hightlighting
Link to comment
Share on other sites

It's all way more complicated than it needs to be...

 

I understand that everything important exists on the server, and it needs to send packets to update the client. Right? What I do not understand is how exactly that happens, and how exactly I'm supposed to use that for my specific needs. All tutorials/documentation that I've found are either outdated, missing important parts, or just plain vague.

 

The server has been successfully counting my echoes and they're being saved to NBT, I just don't understand how exactly to send the information to the client.

 

Are you saying addEchoes is supposed to be like this?

	@Override
	public void addEchoes(EntityPlayer player, int amount)
	{
		this.echoes = echoes + amount > maxEchoes ? maxEchoes : echoes + amount;
		PacketHandler.net.sendTo(new UpdateEchoes(this), (EntityPlayerMP) player);
	}

 

This is where I'm rendering it.

	@SubscribeEvent
	public void onRenderGui(RenderGameOverlayEvent.Post event)
	{
		if (event.getType() != ElementType.EXPERIENCE)
		{
			return;
		}
		IEchoesCapability echoes = mc.thePlayer.getCapability(EchoesManager.ECHOES, null);

		if (echoes != null)
		{
			drawString(mc.fontRendererObj,
					TextFormatting.RED + "Blood Echoes: " + TextFormatting.WHITE + echoes.getEchoes(), 5, 5, colour);
		}
	}

 

I added lines to print the amount of echoes the player has inside of addEchoes() and the UpdateEchoes() contructor, and both of them fire with the correct amount of echoes gained in total.

 

But none of the printouts in the onMessage() method are ever being called.

 

	public static class Message implements IMessageHandler<UpdateEchoes, IMessage>
	{
		@Override
		public IMessage onMessage(final UpdateEchoes message, MessageContext ctx)
		{
          	// This is never called.
			Logger.getLogger(Main.MODID).log(Level.INFO, "onMessage starting");

			Minecraft.getMinecraft().addScheduledTask(new Runnable()
			{
				public void run()
				{
          			// This is never called.
					Logger.getLogger(Main.MODID).log(Level.INFO, "Processing packet");

					IEchoesCapability cap = Minecraft.getMinecraft().thePlayer.getCapability(EchoesManager.ECHOES,
							null);
					cap.setEchoes(message.echoes);
					cap.setInsight(message.insight);
                  
         		 	// This is never called.
					Logger.getLogger(Main.MODID).log(Level.INFO,
							"Packet Processed... Echoes: " + cap.getEchoes() + ", Insight: " + cap.getInsight());
				}
			});
			return null;
		}
	}

 

Link to comment
Share on other sites

Wonderful, all that trouble for just another dumb mistake. Thanks for pointing that out.

 

Now the only trouble is that when I first log in, it's always at 0.

It's not until I kill something to gain echoes that it updates.

I assume that would be fixed just by sending a packet to the client when the player logs in?

Would I need to do the same thing when the player enters a dimension?

Edited by Eria8
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.