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

    • 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
    • Okay, but does the modpack works with 1.12 or just with 1.12.2, because I need the Forge client specifically for Minecraft 1.12, not 1.12.2
    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
  • Topics

×
×
  • Create New...

Important Information

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