Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[SOLVED]Client-Side packet handler not receiving packet with dedicated server.

Featured Replies

Posted

Hi There

Im currently sending packets from a tile entity to all nearby clients to play sounds(This is necessary over traditional playSound methods due to high control over the sound). On a single player world, everything works as expected and the debug printouts within the code come from the expected sides. When running on a dedicated server, the packet constructor and toBytes() are both called with correct data but nothing happens following that. No code within the Packet Handler is executed and neither is fromBytes().

 

PacketPlaySoundOnClient:

Spoiler

import io.netty.buffer.ByteBuf;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;


public class PacketPlaySoundOnClient implements IMessage{

	public BlockPos pos;
	public int slot;	
	public int instruction;

	public PacketPlaySoundOnClient(BlockPos pos, int slot, int instruction) {
		this.pos = pos;
		this.slot = slot;
		this.instruction = instruction;
		
		System.out.println("Constructing Packet");
		
	}

	public PacketPlaySoundOnClient() {}
	
	@Override
	public void fromBytes(ByteBuf buf) {
		pos = BlockPos.fromLong(buf.readLong());
		slot = buf.readInt();
		instruction = buf.readInt();	
		System.out.println("From Bytes Pos = " + pos + ", Slot = " + slot);
	}

	@Override
	public void toBytes(ByteBuf buf) {
		System.out.println("To Bytes Pos = " + pos + ", Slot = " + slot);
		buf.writeLong(pos.toLong());
		buf.writeInt(slot);
		buf.writeInt(instruction);
	}

	



}

 

 

ClientSoundHandler:

Spoiler

package net.themcjavafre4k.mcdj.networking;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.themcjavafre4k.mcdj.SoundHandler;
import net.themcjavafre4k.mcdj.item.ItemCustomRecord;
import net.themcjavafre4k.mcdj.item.MCDJItems;
import net.themcjavafre4k.mcdj.sound.SoundDJDeck;
import net.themcjavafre4k.mcdj.tileentity.TileEntityDJDeck;

public class ClientSoundHandler implements IMessageHandler<PacketPlaySoundOnClient, IMessage> {

	private List<SoundDJDeck> slot1 = new ArrayList<SoundDJDeck>();

	@Override
	public IMessage onMessage(PacketPlaySoundOnClient msg, MessageContext ctx) {

		if(ctx.side != Side.CLIENT) {

			System.err.println("Recieved on Server Side");
			return null;

		}
		
		System.out.println("Packet recieved");

		Minecraft.getMinecraft().addScheduledTask(() -> {

			TileEntityDJDeck tile = (TileEntityDJDeck) Minecraft.getMinecraft().world.getTileEntity(msg.pos);

			System.out.print("Plays");

			ItemStack records = tile.inventory.getStackInSlot(0);
			ItemCustomRecord slot1Record = (ItemCustomRecord)records.getItem();

				switch(msg.slot) {
				case 0:
					System.out.println("Playing Sound In Slot 1");

					slot1.add(new SoundDJDeck(msg.pos, SoundHandler.getRecordSound(MCDJItems.customRecord.indexOf(tile.inventory.getStackInSlot(0).getItem())), 0));			
					Minecraft.getMinecraft().getSoundHandler().playSound(slot1.get(0));
					break;

				}



		});
		return null;
	}

}

 

 

Registering packet in my ClientProxy(Only packet with id 11):

MCDJMod.network.registerMessage(ClientSoundHandler.class, PacketPlaySoundOnClient.class, 11, Side.CLIENT);
		

 

 

Calling send method:

Spoiler

System.out.print("Play Method Called \n");

		if(!world.isRemote) {

			MCDJMod.network.sendToAllAround(new PacketPlaySoundOnClient(pos, slot, 0), new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 20)); 

		}

 

 

On a side not, If i leave out the instruction integer from my packet, I get the following IndexOutOfBoundException from the client followed by disconnection from the server:

readerIndex(11) + length(2) exceeds writerIndex(12): UnpooledSlicedByteBuf(ridx: 11, widx: 12, cap: 12/12, unwrapped: PooledUnsafeDirectByteBuf(ridx: 0, widx: 13, cap: 13))
	

 

Not sure if this may have something to do with the issue.

Edited by TheMCJavaFre4k
Solved

  • Author
44 minutes ago, diesieben07 said:

registerMessage must be called on both sides.

Thanks heaps for the reply, that would be the issue. Its now working as intended. 

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.