Jump to content

[SOLVED][1.12.2] Send custom CPacketChatMessage to server?


Recommended Posts

Posted (edited)

Hello again everyone. Thanks for stopping by!

 

For reasons which I have explained over in this thread, I am creating a mod which extends the maximum amount of characters a player can type and send in chat for the client side only. That thread got closed for being about Forge for 1.8.9, so I have now ported my code to 1.12.2. Which means yes, this code is completely useless in 1.12, but hopefully the knowledge I gain from it will help me fix my 1.8.9 mod, as the code I am working with has not changed much between the versions.

 

With that said, here's what I've done with Forge 1.12.2-14.23.5.2814:

 

1) Created a custom class, LongGuiChat, extending GuiChat, which is displayed to the player whenever I catch (and cancel) a GuiOpenEvent associated with a GuiChat.

 

2) Overridden LongGuiChat's sendChatMessage(String, boolean) method, as by default it converts the text to a CPacketChatMessage packet and then sends it off to be sent to the server. In CPacketChatMessage's constructor it truncates the String given to it down to 256 characters, and there is no public method to modify the String outside of this, so I had to override sendChatMessage to get around using that class. I would like to note that I could also subscribe to ClientChatEvent for the same effect here.

 

3) Created another custom class, LongCPacketChatMessage, which extends CPacketChatMessage and changes nothing. No, seriously, I didn't change anything, I just super call the constructor. In the final code this will have the maximum character limit raised, but that is not relevant right now.

 

So what is the effect of this? Well, in singleplayer, the game works like normal. The game skips over the problematic code and there are no issues. When connected to a multiplayer server, however, things don't go so well.

 

I can connect to a server, open chat, and type in it just fine. When I press Enter, though, I am kicked from the server, given this error message in-game:

Internal Exception: io.netty.handler.codec.EncoderException: java.lang.RuntimeException: ConnectionProtocol unknown: net.kalman98.longerchat.LongCPacketChatMessage@3bd493fc

 

...and a stack trace in the logs. The trace leads to net.minecraft.network.NettyPacketEncoder.encode:

protected void encode(ChannelHandlerContext p_encode_1_, Packet<?> p_encode_2_, ByteBuf p_encode_3_) throws IOException, Exception
{
    EnumConnectionState enumconnectionstate = (EnumConnectionState)p_encode_1_.channel().attr(NetworkManager.PROTOCOL_ATTRIBUTE_KEY).get();

    if (enumconnectionstate == null)
    {
        throw new RuntimeException("ConnectionProtocol unknown: " + p_encode_2_.toString());
    }

 

...with that if statement resolving to true and the RuntimeException being thrown. The trace up to this point is just several classes passing the packet back and forth over and over again. For reference, the EncoderException is thrown in io.netty.handler.codec.MessageToByteEncoder.write(ChannelHandlerContext, Object, ChannelPromise), which is what calls the encode method shown above.

 

I get these errors when I send my own LongCPacketChatMessage, which, as I said, extends CPacketChatMessage and has no code changes. The errors do not occur when I'm using my custom code but the original CPacketChatMessage instead of my LongCPacketChatMessage. I'm not sure how to write my own version of NettyPacketEncoder.encode to accomplish my purposes, and really, I don't think that would be a great idea. By the way, the ByteBuf argument comes from io.netty.handler.codec.MessageToByteEncoder.write(ChannelHandlerContext, Object, ChannelPromise), and the ChannelHandlerContext comes from io.netty.channel.DefaultChannelPipeline.writeAndflush(Object).

 

Any insight, suggestions, opinions, and advice you can give would be appreciated. I'm not even sure what I'm trying to do is actually possible at this point. I make no claims to being incredibly knowledgeable about Java, and I'm sure I've made some mistakes in the process of creating this mod. I would be grateful if you could point out anything I've done incorrectly. And again, sorry about all the text.

 

Thanks in advance for your time!

Edited by Kalman98
Marking as solved.
Posted (edited)

I am marking this thread as solved. A friend of mine saw the thread and told me to use reflection. I have heard of this magical "reflection" in many places on the Internet, and I have always veered away from its mysterious workings. However, he gave me a sample of code, and it works perfectly! I am now researching how reflection works for the future.

 

Thanks to anyone who took the time to read my silly, uneducated threads! ?

 

Edit: Oh, fine, I can mention how it was fixed. The solution was to not use my custom packet class, and instead to use the original CPacketChatMessage class. Reflection allowed us to access the private String variable in the class, and from there we were able to set it to any length of String we wanted to! The following is code for the useless 1.12.2 version of the mod. I simply changed the class name to adapt this to 1.8.

 

// code courtesy of @ParkerMc! Thanks a ton!
try {
    // Needs to only be done once
    Field f = CPacketChatMessage.class.getDeclaredFields()[0]; // Get all the fields of the class including the private one(s). As it is the only field you need index 0
    f.setAccessible(true); // Set the field to accessible so you can change it

    // Create the packet
    CPacketChatMessage packet = new CPacketChatMessage();
    // Set it
    f.set(packet, "some text");
} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
    e.printStackTrace();
}

 

Edited by Kalman98
Added solution code.
Posted

Forge has always had inbuilt Reflection util code. Look at ReflectionHelper and ObfuscationReflectionHelper (only in the latest forge version) as you are in 1.8.9. You may have to copy some classes/methods to your own util classes. Just please, change the Reflection code you have right now - it’s unreadable. Also don’t just catch a mod-breaking exception and do nothing with it

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted
14 minutes ago, Cadiboo said:

Forge has always had inbuilt Reflection util code. Look at ReflectionHelper and ObfuscationReflectionHelper (only in the latest forge version) as you are in 1.8.9. You may have to copy some classes/methods to your own util classes. Just please, change the Reflection code you have right now - it’s unreadable. Also don’t just catch a mod-breaking exception and do nothing with it

Thank you very much! I am a bit busy at the moment, but I will look into the ReflectionHelper as soon as I am able to. Thank you again for pointing out the issue with the exceptions... I've never really heard of those particular exceptions before now and kind of glazed over them. I'll be sure to pay more attention to exception handling in the future. I'll post my updated solution code on this thread once it's finished.

 

Thanks again, I appreciate the tips!

Posted (edited)

The code has now been ported to use ReflectionHelper. Thanks @Cardiboo! Here is the new solution (note that I wrote the code for 1.8.9, I believe it is the same or very similar on current versions):

		Field c01MessageField = ReflectionHelper.findField(C01PacketChatMessage.class, "message", "field_149440_a");
    	c01MessageField.setAccessible(true);
		
		C01PacketChatMessage packet = new C01PacketChatMessage();
        
        try {
        	c01MessageField.set(packet, msg);
        } catch (IllegalAccessException e) {
        	MyModClass.logger.error("Error setting message length, sticking with 100.", e);
        	packet = new C01PacketChatMessage(msg);
        }

 

 

If more examples are needed for any future help-seekers, my full (1.8.9) source code is here: https://gitlab.com/Kalman98/256chat

 

I am sorry to have devolved this thread back to 1.8. The code really is very close to the modern stuff, at least. ?

Edited by Kalman98
Removed things I accidentally left in the code sample.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.