Jump to content

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


TheMCJavaFre4k

Recommended Posts

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



×
×
  • Create New...

Important Information

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