Jump to content

Server not sending packet to player on multiplayer


GotoLink

Recommended Posts

I have been struggling with this issue for the past few days.

I made an universal packet handler on my network mod, which handles changes to a tileentity induced by a player from a gui.

public class PacketHandler implements IPacketHandler{

@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) 
{
	if(packet.channel == GeneralRef.PACKET_CHANNELS[0])
	{//Sent by AnimatorGUI to server when a button has been activated
		handleGuiChange(manager, packet,(EntityPlayer) player);
	}
	else if(packet.channel == GeneralRef.PACKET_CHANNELS[1])
	{//Sent by server to client FIXME: packet isn't received on multiplayer ??
		handleDescriptionPacket(packet,(EntityPlayer) player);
	}
	//DEBUG:
	Side side = ((EntityPlayer)player).worldObj.isRemote?Side.CLIENT:Side.SERVER;
	FMLLog.getLogger().info(side.toString()+" recieved a "+packet.channel +" packet");
}
/**
* Client method to handle a packet describing the TileEntityAnimator from server
* @param packet
* @param player
*/
private static void handleDescriptionPacket(Packet250CustomPayload packet, EntityPlayer player)
{
	DataInputStream dat = new DataInputStream(new ByteArrayInputStream(packet.data));
	try{
        int x = dat.readInt();
        int y = dat.readInt();
        int z = dat.readInt();
        TileEntity te = player.worldObj.getBlockTileEntity(x, y, z);
        if (te instanceof TileEntityAnimator)
        {
            TileEntityAnimator animator = (TileEntityAnimator) te;
            animator.setEditing(dat.readBoolean());
            if(!animator.isEditing() && animator.getStackInSlot(0)!=null)
                resetRemote(animator.getStackInSlot(0));
            animator.setFrame(dat.readInt());
            animator.setMaxFrame(dat.readInt());
            animator.setCount(dat.readInt());
            animator.resetDelay();
            animator.setDelay(dat.readInt());
            animator.setMode(Mode.values()[dat.readShort()]);
        }
	}catch(IOException i)
	{
		i.printStackTrace();
	}
}
/**
* Server method to handle a client action in AnimatorGUI
* @param manager 
* @param packet
* @param player
*/
private static void handleGuiChange(INetworkManager manager, Packet250CustomPayload packet, EntityPlayer player) 
{
	DataInputStream inStream = new DataInputStream(new ByteArrayInputStream(packet.data));
	int[] data = new int[packet.data.length/4];
	try 
	{
		for(int id = 0; id < data.length; id++)
			data[id] = inStream.readInt();
	} 
	catch (IOException e) 
	{
            e.printStackTrace();
            return;
	}
	TileEntity tile = player.worldObj.getBlockTileEntity(data[1], data[2], data[3]);
	if(tile instanceof TileEntityAnimator)
	{
		handleData((TileEntityAnimator) tile, data);
            if(player.openContainer instanceof ContainerAnimator && ((ContainerAnimator)player.openContainer).getControl() == tile)
    			player.openContainer.detectAndSendChanges();
		//manager.addToSendQueue(getPacket((TileEntityAnimator) tile));
		player.worldObj.markBlockForUpdate(data[1], data[2], data[3]);
	}
}

public static void handleData(TileEntityAnimator animator, int... data)
{
	switch(data[0])
	{
	case 0://"+" button has been pressed
		animator.setDelay(1);
		break;
	case 1://"-" button has been pressed
		if(animator.getDelay()>-1)
		{//Lower delay won't work and might crash
			animator.setDelay(-1);
		}
		break;
	case 2://"Switch button has been pressed, going LOOP->ORDER->REVERSE->RANDOM->LOOP
		int mod = animator.getMode().ordinal();
		if(mod + 1 < Mode.values().length)
			animator.setMode(Mode.values()[mod + 1]);
		else
			animator.setMode(Mode.LOOP);
            break;
        case 3:
        case 4://One of the "Reset" button has been pressed
                animator.setEditing(false);
                animator.setLinker(null);
                if(data[0]==4)//This is a full reset
                {
                	resetAnimator(animator);
                }
                if(data.length > 4)//Get the item and reset it
                	resetRemote(animator.getStackInSlot(0));
                break;
        case 5://Increment Max number of frames that will run
        	animator.setMaxFrame(animator.getMaxFrame() + 1);
		break;
        case 6://Increment first frame to display
    		animator.setFrame(animator.getFrame() + 1);
        	break;
	}

}

public static void resetAnimator(TileEntityAnimator animator) 
{
	animator.setFrame(0);                
        animator.setMode(Mode.ORDER);
        animator.resetDelay();
        animator.setMaxFrame(-1);
        animator.setCount(0);
}

public static void resetRemote(ItemStack stack)
{
        ItemBase remote = (ItemBase)stack.getItem();
        remote.resetLinker();
        stack.getTagCompound().removeTag(ItemBase.KEYTAG);
}

public static Packet getPacket(TileEntityAnimator animator) 
{
	ByteArrayOutputStream bos = new ByteArrayOutputStream(31);
        DataOutputStream dos = new DataOutputStream(bos);     
        try
        {
            dos.writeInt(animator.xCoord);
            dos.writeInt(animator.yCoord);
            dos.writeInt(animator.zCoord);
            dos.writeBoolean(animator.isEditing());
            dos.writeInt(animator.getFrame());
            dos.writeInt(animator.getMaxFrame());
            dos.writeInt(animator.getCount());
            dos.writeInt(animator.getDelay());
            dos.writeShort(animator.getMode().ordinal());
        }
        catch (IOException e)
        {
        	e.printStackTrace();
        }
        Packet250CustomPayload pkt = new Packet250CustomPayload();
        pkt.channel = GeneralRef.PACKET_CHANNELS[1];
        pkt.data = bos.toByteArray();
        pkt.length = bos.size();
        pkt.isChunkDataPacket = true;
        return pkt;
}
}

While it works on single player, with values being changed and gui following it

("SERVER received a Gui packet","CLIENT received description packet")

it doesn't on multiplayer with mcp test server

("SERVER received a Gui packet",*nothing follows*)

 

Hopefully someone will found where I derped :)

 

Edit: This is on 1.5.2 and Forge 7.8.1.737 by the way.

Link to comment
Share on other sites

player.openContainer.detectAndSendChanges();

//manager.addToSendQueue(getPacket((TileEntityAnimator) tile));

player.worldObj.markBlockForUpdate(data[1], data[2], data[3]);

 

this is commented ?

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

I tried to fix with the commented part, but result is the same.

In my tile entity, i have

@Override
    public Packet getDescriptionPacket()
    {
        return PacketHandler.getPacket(this);
    }

So with

player.worldObj.markBlockForUpdate(data[1], data[2], data[3]);

the description packet is sent. But only on single player... :-\

Link to comment
Share on other sites

btw im looking at your code atm i just wanted to tell you i made a tutorial about tile entity synchronizing on the wiki (its kindaof shitty though)

 

its called: "synchronizing tile entities" or something similar

 

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

oh, im sorry its so simple, you're not sending the packet with the right channel

 

server will send a packet using GeneralRef.PACKET_CHANNELS[1] but client only accepts [0]

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

Nope, the description packet given by getPacket(TileEntityAnimator) has channel 1 and the receiving part handleDescriptionPacket is also waiting for channel 1. It wouldn't work on single player if I made that mistake.

Link to comment
Share on other sites

@NetworkMod(clientSideRequired = true, serverSideRequired = false , channels={"Gui","Animator"}, packetHandler = PacketHandler.class)

public static final String[] PACKET_CHANNELS = {"Gui","Animator"};

While reading your tutorial, I came across PacketDispatcher class. Trying the methods in it right now.

 

Link to comment
Share on other sites

While reading your tutorial, I came across PacketDispatcher class. Trying the methods in it right now.

 

is not really different then

 

manager.addToSendQueue(getPacket((TileEntityAnimator) tile));

 

 

its just that its the same everywhere :P

 

so thats probably not goign to help

 

can you at least println BEFORE the packet from server is sent ? (making sure that it WAS send )

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

ok but when the gui is changed, can you println right before you send the packet so you can tell for sure that the server is sending the packet, because if it IS sending it then you know your client  packet handler is not registered correctly

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

Debug says "SERVER received gui packet", even on multiplayer.

But I only have "CLIENT received animator packet" on single player.

That is the whole issue, as my title says, server don't send the packet to the player on multiplayer.

Link to comment
Share on other sites

yes link i know that but just because the message gets to the server doesnt meant that it is sent FROM the server. i jsut want to make sure the server is actually SENDING the packet, if its not sending it the client is obviously not receiving it

 

if(player.openContainer instanceof ContainerAnimator && ((ContainerAnimator)player.openContainer).getControl() == tile)

    player.openContainer.detectAndSendChanges();

//manager.addToSendQueue(getPacket((TileEntityAnimator) tile));

System.out.println("yes i have send the packet :)");

player.worldObj.markBlockForUpdate(data[1], data[2], data[3]);

}

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

Single player:

[sTDOUT] yes i have send the packet :)

[iNFO] [ForgeModLoader] SERVER recieved a Gui packet

[iNFO] [ForgeModLoader] CLIENT recieved a Animator packet

Multi:

[iNFO] [ForgeModLoader] SERVER recieved a Gui packet

 

Server not sending packet, as I said.

Oh, and there isn't any exception thrown due to bad data.

Link to comment
Share on other sites

(sorry i got to go)

but try println before and after every if, method call, while, for

this way youll know exactly at which point this is stopping

 

 

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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