Monday 7 October 2013

Packets: From Client to Server

Packets sent to the server nearly all come from either
  • EntityClientPlayerMP (changes in state of a player –in particular position, flying), or
  • PlayerControllerMP (actions performed by the user – clicking / destroying blocks, attacking entities).
The packets are usually handled by a method in EntityPlayerMP or ItemInWorldManager, which then call other server methods as appropriate.


Typical Client origin
NetServerHandler (Server processing)
Called by Server during processing
Packet3Chat
Chat messages including commands
EntityClientPlayerMP
.handleChat()
If command:
.handleSlashCommand()
If chat message:
Send Packet3Chat to all players


FMLNetworkHandler.handleChatMessage()
ForgeHooks.onServerChatEvent()
ICommandManager.executeCommand()
Packet7UseEntity
The player performs interact (0) or attack (1) on another entity
PlayerControllerMP
attackEntity
interactWithEntity {func_78768_b}
.handleUseEntity()
If 0:
EntityPlayerMP.interactWith()
If 1:
EntityPlayerMP
  .attackTargetEntityWithCurrentItem()

Packet9Respawn
Not used
Not used
.handleRespawn()
Overridden but does nothing

Packet10Flying,
Packet11PlayerPosition,
Packet12PlayerLook,
Packet13PlayerLookMove
Tell the server the player’s flying state, position: x, y(feet) and y(eyes), z, and look (yaw, pitch)
EntityClientPlayerMP:
10 – changes flying state only
11 – changes position only
12 – changes look only
13 – changes position and look
.handleFlying()
If the position is different (player has moved):
move the player (checking for collision etc) then update position and look.
If position is not different:
Once every second, send a Packet13 with the current position & look back to the client.
EntityPlayerMP
.moveEntity()
.onUpdateEntity()
.setPositionAndRotation()
WorldServer
.updateEntity()

Packet14BlockDig
Item action performed by the user:
0 = click block once (called repeatedly while user holds left button down)
1 = stop clicking block (undo any damage)
2 = block destroyed
3 = drop item stack (CTRL held)
4 = drop one item
5 = stop using item (release right button eg eating, drinking, blocking)

PlayerControllerMP
EntityClientPlayerMP
.handleBlockDig()
For dig actions – check reach, immunity etc, then damage block appropriately.
Send Packet53BlockChange to ensure Client stays synchronised.
EntityPlayerMP
.dropOneItem()
.stopUsingItem()
ItemInWorldManager
.onBlockClicked()
.cancelDestroyingBlock()
.uncheckedTryHarvestBlock()
Forge PlayerInteractEvent
Packet15Place
Right click action:
Either on a block, or air (direction = 255)
PlayerControllerMP
.handlePlace()
perform appropriate action and send Packet53BlockChange to ensure Client stays synchronised.
Cleans up after item use (if it has been used up or destroyed), removes from inventory, sends Packet103SetSlot if reqd to update held item on the Client.
ItemInWorldManager
.tryUseItem()
.activateBlockOrUseItem()
Forge PlayerInteractEvent
Packet16BlockItemSwitch
Changes the selected hotbar slot (0..8)
PlayerControllerMP
.handleBlockItemSwitch()
Change inventory.currentitem to the nominated slot.

Packet18Animation
1 = swing item
EntityClientPlayerMP
.handleAnimation()
swing the currently held item
EntityPlayerMP.swingItem()
Packet19EntityAction
1 = start sneaking
2 = stop sneaking
3 = wake up
4 = start sprinting
5 = stop sprinting
6 = horse jump
7 = open horse inventory
GuiSleepMP
EntityClientPlayerMP
.sendMotionUpdates()

.handleEntityAction()
Make the entity perform the appropriate animation.
EntityPlayerMP
EntityHorse
Packet27PlayerInput
Transmit the current state of the player movement keys (forward/back, left/right, jump, sneak).  Currently used for riding only.
EntityClientPlayerMP.
.onUpdate()
handlePlayerInput()
Copy the state of player movement keys to the server player.
EntityPlayerMP.setEntityActionState()

Packet101CloseWindow
Informs the server that the user has closed the currently open container (chest, furnace, etc)
EntityClientPlayerMP
.handleCloseWindow()

EntityPlayerMP
.closeContainer
Packet102WindowClick
PlayerControllerMP
.windowClick()
(from GuiContainer, eg .mouseClicked)

.handleWindowClick()
The type of “click” depends on
.holdingShift.
0 = normal click
1 = shiftclick
2 = hotbar key pressed (i.e. 1, 2, 3 etc)
3 = middle mouse button click
4 = drop key pressed
5, 6 = ??
L click = pick up / drop
Shft Lclick: in invent -> swap to hotbar.  If hotbar -> swap to inv.  No drag.
EntityPlayerMP.openContainer.slotClick
Packet106Transaction
This packet works together with Packet102 to ensure that the container contents remain properly synchronised on client and server.
NetClientHandler
.handleTransaction()
handleTransaction()


Packet107CreativeSetSlot
In creative mode: put an item into the player’s inventory, or drop on the ground.
PlayerControllerMP
.handleCreativeSetSlot()

EntityPlayerMP
.inventoryContainer
.dropPlayerItem
Packet108EnchantItem
Tells the server to enchant the item on the table and deducts XP from player
GUIEnchantment
PlayerControllerMP
.handleEnchantItem()
ContainerEnchantment
. enchantItem
Packet130UpdateSign
Changes the text on a sign
GUIeditSign
.handleUpdateSign()
TileEntitySign
Packet131MapData
Not used by map; appears to be hijacked by Forge for sending other non-map data?
PacketDispatcher
.getTinyPacket
.handleMapData()
     
FMLNetworkHandler
.handlePacket131Packet
--->ItemMap.getMPMapData (vanilla)
Or
NetworkRegistry.handleTinyPacket

Packet202PlayerAbilities
Tell the server that the player is flying.
EntityClientPlayerMP
.handlePlayerAbilities()
only .isFlying is used.  Other fields ignored.
EntityPlayerMP
Packet203AutoComplete
Client asks for possible autocompletions for the given string fragment
GUIchat
.handleAutoComplete()
makes a list of possible completions and sends back to client.
MinecraftServer
.getPossibleCompletions
Packet204ClientInfo
Update info about the client (eg language, show cape, chat colour)
GameSettings
.handleClientInfo()
EntityPlayerMP
Packet205ClientCommand
0 sent to a netLoginHandler starts the server, 1 sent to NetServerHandler forces a respawn
EntityClientPlayerMP
GUIwinGame
NetClientHandler
.handleClientCommand()
ServerConfigurationManager
Packet250CustomPayload
Channel (string)
Length
Rawdata (byte stream)
Vanilla:
NetClientHandler
GUIbeacon
GUIcommandBlock
GUImerchant
GUIrepair
GUIscreenBoook

ForgePacket:
FML.common.network

.handleCustomPayload()
Channels starting with MC| are handled by vanilla code
Channels starting with FML are sent to handleFMLPacket.
All others are sent to NetworkRegistry.handleCustomPacket (mod can register a custom handler)
FMLNetworkHandler.handlePacket250Packet
handleFMLPacket;
handleVanilla250Packet