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

  Reveal hidden contents

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
  On 2/15/2019 at 5:25 AM, 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

Expand  

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

    • Thank you so much! I didnt see it in the log😭😭  
    • So im completely new to modding in general, i have some experience in Behavior packs in minecraft bedrock and i would like to modify entities stats and attributes like in a behavior pack (health, damage, speed, xp drop...). The problem is that i cant find any information online on how to do that, and I have no clue on what to do and where to start. I am currently modding in 1.20.4 with IntelliJ if that helps. My final objective is to buff mobs health and damage (double it for exemple), but since there is no entity file anywhere i don't know how to change it... 😢
    • Unlock an Instant $100 OFF with the Exclusive Temu Coupon Code ALF401700! Whether you're a first-time shopper or a loyal returning customer, this verified Temu promo code ALF401700 is your gateway to incredible savings on thousands of products. By applying code ALF401700 at checkout, you’ll receive a guaranteed $100 discount on your purchase, plus enjoy additional savings of up to 50% OFF on selected items. This special coupon is designed to maximize your discounts, making your shopping experience at Temu more affordable than ever. Why Choose Temu Coupon Code ALF401700 in 2025? First-Time Shoppers: Score a massive 50% off your first order plus a flat $100 OFF using promo code ALF401700. Returning Customers: Don’t miss out! Use ALF401700 to claim a generous $100 OFF on your next purchase. Massive Clearance Sales: With Temu’s ongoing 2025 clearance events, this code unlocks up to 90% OFF on select deals. Global Reach: Whether you shop from the USA, Canada, Europe, Asia, or beyond, Temu coupon ALF401700 works worldwide. 2025’s Top Temu Discounts Powered by ALF401700: Temu $100 OFF New User Promo — ALF401700 Temu Exclusive Discount for Returning Shoppers — ALF401700 Memorial Day Special: $100 OFF Using ALF401700 Country-Specific Offers: USA, Japan, Mexico, Chile, Colombia, Malaysia, Philippines, South Korea, Saudi Arabia, Qatar, Germany, France, Israel — all accept code ALF401700 Free Gift Unlocks — Apply ALF401700 Without Referrals Stackable Bundles: Combine ALF401700 for $100 OFF with up to 50% sitewide discounts 100% OFF Flash Deals during Temu Events with ALF401700 How to Redeem Your Temu Coupon Code ALF401700: Visit the official Temu website or open the Temu app. Select your favorite products and add them to your cart. Enter the promo code ALF401700 in the discount code field at checkout. Watch your total instantly drop by $100 and enjoy any additional percentage discounts automatically applied. Complete your order and enjoy huge savings! Key Benefits of Using ALF401700 Temu Promo Code: Verified and Tested: This code is active and guaranteed to work for 2025. Applicable for All Users: New or existing customers get to enjoy the perks. Works Across Multiple Countries: Perfect for international shoppers. No Minimum Purchase Required: Use it anytime to save $100. Perfect for Big and Small Orders: Whether buying essentials or splurging, save big with ALF401700. Temu Coupon Code ALF401700 by Region: 🇺🇸 United States — Save $100 OFF 🇨🇦 Canada — Instant $100 Discount 🇬🇧 United Kingdom — Exclusive $100 OFF 🇯🇵 Japan — Hot $100 OFF Deal 🇲🇽 Mexico — Get $100 OFF 🇨🇱 Chile — 2025 Special Discount 🇰🇷 South Korea — Massive Savings 🇵🇭 Philippines — Extra $100 OFF 🇸🇦 Saudi Arabia — Verified Promo 🇶🇦 Qatar — Top Discount 🇩🇪 Germany — Exclusive Savings 🇫🇷 France — Coupon Works 🇮🇱 Israel — Huge Discount Final Words: Make 2025 your year of unbeatable savings with the Temu coupon code ALF401700. Act now to claim your $100 OFF plus unlock additional discounts on thousands of products. Whether shopping for fashion, electronics, home essentials, or gifts, this is the best Temu promo code to maximize your budget. Download the Temu app today, enter ALF401700 at checkout, and watch the savings roll in!
    • Verified users can now unlock a $100 OFF Temu Coupon Code using the verified promo code [ALF401700]. This Temu $100 OFF code works for both new and existing customers and can be used to redeem up to 50% off your next order. Our exclusive Temu coupon code [ALF401700] delivers a flat $100 OFF on top of existing deals. First-time customers using code ALF401700 can save an extra 100% off select items. Returning users also qualify for an automatic $100 OFF discount just by applying this code at checkout. But wait—there’s more. With our Temu coupon codes for 2025, users can score up to 90% OFF on clearance items. Whether you’re shopping in the USA, Canada, UK, or elsewhere, Temu promo code ALF401700 unlocks extra discounts tailored to your account. Some users are saving 100% on items using this 2025 Temu promo code. 🔥 Temu Coupon Highlights Using Code [ALF401700]: Temu new user code – ALF401700: Save 50% off your first order + $100 OFF. Temu promo for existing customers – ALF401700: Enjoy flat $100 OFF instantly. Global availability: Works in the USA, UK, Canada, Germany, France, Japan, Chile, Colombia, Malaysia, Mexico, South Korea, Philippines, Saudi Arabia, Qatar, Pakistan, and more. Top 2025 Coupon Deal: Get $200 OFF plus 100% bonus discounts using code ALF401700. Top-Ranked Temu Deals for 2025 (Coupon Code: ALF401700): ✅ Temu $100 OFF Memorial Day Sale — Use ALF401700 ✅ Temu First Order Coupon — Use ALF401700 for 50% + $100 OFF ✅ Temu USA Coupon Code — Save $100 instantly with ALF401700 ✅ Temu Japan, Germany, Chile Codes — All support ALF401700 ✅ Temu Reddit Discount – $100 OFF: Valid for both new and old users ✅ Temu Coupon Bundle 2025 — $100 OFF + up to 50% slash ✅ 100% OFF Free Gift Code — Use ALF401700, no invite needed ✅ Temu Sign-Up Bonus Promo — Get a welcome $100 OFF instantly ✅ Free Temu Code for New Users — Use ALF401700, no referral required  Temu Clearance Codes 2025 — Use ALF401700 for 85–100% discounts Why ALF401700 is the Best Temu Code in 2025 Using Temu code ALF401700 is your ticket to massive savings, free shipping, first-order discounts, and stackable coupon bundles. Whether you're browsing electronics, fashion, home goods, or beauty products, this verified Temu discount code offers real savings—up to 90% OFF + $100 OFF on qualified orders. 💡 Pro Tip: Apply ALF401700 during checkout in the Temu app or website to activate your instant $100 discount, even if you’re not a new user. Temu $100 OFF Code by Country (All Use ALF401700): 🇺🇸 Temu USA – ALF401700 🇯🇵 Temu Japan – ALF401700 🇲🇽 Temu Mexico – ALF401700 🇨🇱 Temu Chile – ALF401700 🇨🇴 Temu Colombia – ALF401700 🇲🇾 Temu Malaysia – ALF401700 🇵🇭 Temu Philippines – ALF401700 🇰🇷 Temu Korea – ALF401700 🇵🇰 Temu Pakistan – ALF401700 🇫🇮 Temu Finland – ALF401700 🇸🇦 Temu Saudi Arabia – ALF401700 🇶🇦 Temu Qatar – ALF401700 🇫🇷 Temu France – ALF401700 🇩🇪 Temu Germany – ALF401700 🇮🇱 Temu Israel – ALF401700 Temu Coupon Code [ALF401700] Summary for SEO: Temu $100 OFF Code  Temu First Order Discount Code 2025  Temu Verified Promo Code ALF401700  Temu 50% OFF + $100 Bonus  Temu 100% OFF Code 2025  Temu App Promo ALF401700  Temu Working Discount Code 2025 
    • Hey there, nothing to do with the code, I am just suggesting you use Intelij IDEA. Trust me, it is the best.
  • Topics

×
×
  • Create New...

Important Information

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