Jump to content

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


Kalman98

Recommended Posts

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.
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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Minecraft Version: 1.8-1.20.X Forge Version: All (Tested on 49.0.27, 11.14.4.1577 and some others) Steps to Reproduce: Setup a server with IPV6 only Setup a docker container with forge Try to connect to the Forge Server Description of issue: Hello, I am reaching out to seek your expertise within this forum to clarify a technical situation I am encountering, suspecting a potential issue related to Forge in a Docker environment, specifically in an IPv6 context. Initial Configuration: Debian 12 server, configured exclusively for IPv6, with Docker installed. Using the Docker image ghcr.io/pterodactyl/yolks:java_17, launching a Minecraft Vanilla server version 1.20.4 proceeds without any issues. Problem: The issue arises when deploying a Minecraft Forge server version 1.20.4 with the same Docker image (ghcr.io/pterodactyl/yolks:java_17), where connecting to the server becomes impossible. Notably, this issue does not occur when launching outside of Docker, where the server functions as expected. Hypothesis: This situation leads me to question the interaction between Forge and Docker, particularly in an IPv6-only configuration, despite several resolution attempts (testing with different versions of Forge, adjusting container network configurations (0.0.0.0, ::/0, and the server's ipv6), trials with various network settings, and modifications of Java options). Further testing was conducted with and without the use of the Pterodactyl game panel, unsuccessfully. The parameter -Djava.net.preferIPv4Stack=false also did not provide a solution. I tried to do the same things on multiple Minecraft server (include vanilla,spigot,fabric,sponge) and this work fine. The problem only happend with Forge. This issue seem to happend on all forge versions.   I appreciate your time and assistance in advance.
    • The game crashed whilst exception ticking world Error: java.lang.NullPointerException: Cannot invoke "net.minecraft.resources.ResourceLocation.equals(Object)" because "this.lootTableId" is null Error given is above. I was already playing for around 15 minutes and wasn't doing anything specific or even breaking anything when the crashed happened. This is update 1.19.2 forge: 43.2.23 Mod list: ESSENTIAL Mod (by SparkUniverse_) Traveler's Titles (Forge) (by YUNGNICKYOUNG) Resourceful Config (by ThatGravyBoat) Dynamic Lights (by atomicstrykergrumpy) TenzinLib (by CommodoreThrawn) Nature's Compass (by Chaosyr) Library Ferret - Forge (by jtl_elisa) Cold Sweat (by Mikul) Simple Voice Chat (by henkelmax) Waystones (by BlayTheNinth) Carry On (by Tschipp) [Let's Do] Meadow (by satisfy) Creeper Overhaul (by joosh_7889) AutoRegLib (by Vazkii) Moonlight Lib (by MehVahdJukaar) AppleSkin (by squeek502) Xaero's World Map (by xaero96) Rotten Creatures (by fusionstudiomc) YUNG's API (Forge) (by YUNGNICKYOUNG) Village Artifacts (by Lothrazar) Right Click, Get Crops (by TeamCoFH) Supplementaries (by MehVahdJukaar) Automatic Tool Swap (by MelanX) Better Third Person (by Socolio) Supplementaries Squared (by plantspookable) Traveler's Backpack (by Tiviacz1337) Caelus API (Forge/NeoForge) (by TheIllusiveC4) Creatures and Beasts (by joosh_7889) Architectury API (Fabric/Forge/NeoForge) (by shedaniel) Quark Oddities (by Vazkii) Origins (Forge) (by EdwinMindcraft) Villager Names (by Serilum) GeckoLib (by Gecko) Realistic Bees (by Serilum) Illuminations Forge 🔥 (by dimadencep) Serene Seasons (by TheAdubbz) Critters and Companions (by joosh_7889) [Let's Do] Bakery (by satisfy) Falling Leaves (Forge) (by Cheaterpaul) Jade 🔍 (by Snownee) Collective (by Serilum) TerraBlender (Forge) (by TheAdubbz) [Let's Do] API (by Cristelknight) Towns and Towers (by Biban_Auriu) More Villagers (by SameDifferent) Biomes O' Plenty (by Forstride) Goblin Traders (by MrCrayfish) Corpse (by henkelmax) Tree Harvester (by Serilum) Balm (Forge Edition) (by BlayTheNinth) Mouse Tweaks (by YaLTeR) Sound Physics Remastered (by henkelmax) Xaero's Minimap (by xaero96) Just Enough Items (JEI) (by mezz) Terralith (by Starmute) Quark (by Vazkii) [Let's Do] Vinery (by satisfy) [Let's Do] Candlelight (by satisfy) Repurposed Structures (Neoforge/Forge) (by telepathicgrunt) Dusty Decorations (by flint_mischiff) Immersive Armors [Fabric/Forge] (by Conczin) Serene Seasons Fix (by Or_OS) Shutup Experimental Settings! (by Corgi_Taco) Skin Layers 3D (Fabric/Forge) (by tr7zw)
    • Make sure Java is running via Nvidia GPU https://windowsreport.com/minecraft-not-using-gpu/  
    • Also make a test with other Custom Launchers like AT Launcher, MultiMC or Technic Launcher
    • Embeddium and valkyrienskies are not working together - remove one of these mods With removing Embeddium, also remove Oculus, TexTrue's Embeddium Options and Embeddium Extras Or use Rubidium instead
  • Topics

×
×
  • Create New...

Important Information

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