Jump to content

[Event] Packet Send and Read Events


yesdood

Recommended Posts

This is something that can be used for a TON of stuff - Packet events.

 

Packet Read Event:

Where it would be called: channelRead0 in NetworkManager.java

Purpose: Packets are incredibly powerful and are used for nearly everything.  Most existing events could arguably be replaced with Packet events.  Health change?  Packet for that.  Chat message? Packet for that.

PacketReadEvent would be used to read packets sent to the client.

 

Packet Send Event:

Where it would be called: addToSendQueue in NetHandlerPlayClient.java

Purpose: Again, packets are incredibly powerful.  These would be the packets sent from the client to the server.  Things like clicking blocks, attacking entities, placing blocks, tab completing, sending chat messages, etc. 

PacketSendEvent would be cancellable, as well as be able to change the sent packet before it is sent.

 

Example Usages:

PacketReadEvent:

For reading messages sent in chat:

protected void onPacketRead(PacketReadEvent event) {
	if (event.getPacket() instanceof S02PacketChat) {
		S02PacketChat packetChat = (S02PacketChat)event.getPacket();
		System.out.println(packetChat.func_148915_c().getUnformattedText());
	}
}

For getting health changed:

protected void onPacketRead(PacketReadEvent event) {
	if (event.getPacket() instanceof S06PacketUpdateHealth) {
		S06PacketUpdateHealth packetHealth = (S06PacketUpdateHealth)event.getPacket();
		System.out.println(packetHealth.getHealth());
	}
}

 

PacketSendEvent:

Block breaking:

	protected void onPacketSend(PacketSendEvent event) {
	if (event.getPacket() instanceof C07PacketPlayerDigging) {
		C07PacketPlayerDigging digging = (C07PacketPlayerDigging)event.getPacket();
		BlockPos blockPos = digging.func_179715_a();
		Block block = mc.theWorld.getBlockState(blockPos).getBlock();
		if (digging.getStatus() == C07PacketPlayerDigging.Action.START_DESTROY_BLOCK) {
			mc.thePlayer.addChatMessage(new ChatComponentText(String.format("Started digging block %s at %s, %s, %s", 
					block.getLocalizedName(), blockPos.getX(), blockPos.getY(), blockPos.getZ())));
		} else if (digging.getStatus() == C07PacketPlayerDigging.Action.STOP_DESTROY_BLOCK) {
			mc.thePlayer.addChatMessage(new ChatComponentText(String.format("Stopped digging block %s at %s, %s, %s",
					block.getLocalizedName(), blockPos.getX(), blockPos.getY(), blockPos.getZ())));
		}
	}
}

Attacking entities:

protected void onPacketSend(PacketSendEvent event) {
	if (event.getPacket() instanceof C02PacketUseEntity)  {
		C02PacketUseEntity packetUseEntity = (C02PacketUseEntity)event.getPacket();
		if (packetUseEntity.getAction() == C02PacketUseEntity.Action.ATTACK) {
			mc.thePlayer.addChatMessage(new ChatComponentText(String.format("Started attacking entity: %s", 
					packetUseEntity.getEntityFromWorld(mc.thePlayer.getEntityWorld()).getName())));
		}
	}
}

 

These are all basic examples, though there are TONS of other examples that could benefit from this.  While there are existing events for the examples I provided, I'm sure you get the point. 

Link to comment
Share on other sites

Yes I get the point, but as with the last 50 times this was suggested, it was decide that having that low level of access to the packets themselves is undesirable from a stability/high-level design point of view. Modders should not care, nore touch any of the networking code. As is pretty much everything you need/want to do with the packets directly you can do in another way.

 

So, I see no real point to add this broad of a system, that gives modders way to much opportunity to mess things up.

The costs outweigh the benefits.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

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.