コード例 #1
0
func (this *requestNotifyPlayerCodec) Encode(writer io.Writer, request Request) (err error) {
	requestNotifyPlayer := request.(*RequestNotifyPlayer)
	err = packet.WriteBool(writer, requestNotifyPlayer.Add)
	if err != nil {
		return
	}
	err = packet.WriteString(writer, requestNotifyPlayer.Player)
	if err != nil {
		return
	}
	err = packet.WriteUUID(writer, requestNotifyPlayer.Uuid)
	return
}
コード例 #2
0
func (this *packetClientPlayerListCodec) Encode(writer io.Writer, encode packet.Packet) (err error) {
	packetClientPlayerList := encode.(*PacketClientPlayerList)
	err = packet.WriteVarInt(writer, packetClientPlayerList.Action)
	if err != nil {
		return
	}
	err = packet.WriteVarInt(writer, len(packetClientPlayerList.Items))
	if err != nil {
		return
	}
	for _, item := range packetClientPlayerList.Items {
		err = packet.WriteUUID(writer, item.UUID)
		if err != nil {
			return
		}
		switch packetClientPlayerList.Action {
		case PACKET_CLIENT_PLAYER_LIST_ACTION_ADD:
			addPlayer := item.Info.(PacketClientPlayerListAddPlayer)
			err = packet.WriteString(writer, addPlayer.Name)
			if err != nil {
				return
			}
			err = packet.WriteVarInt(writer, len(addPlayer.Properties))
			if err != nil {
				return
			}
			for _, property := range addPlayer.Properties {
				err = packet.WriteString(writer, property.Name)
				if err != nil {
					return
				}
				err = packet.WriteString(writer, property.Value)
				if err != nil {
					return
				}
				if property.Signature == "" {
					err = packet.WriteBool(writer, false)
					if err != nil {
						return
					}
				} else {
					err = packet.WriteBool(writer, true)
					if err != nil {
						return
					}
					err = packet.WriteString(writer, property.Signature)
					if err != nil {
						return
					}
				}
			}
			err = packet.WriteVarInt(writer, addPlayer.Gamemode)
			if err != nil {
				return
			}
			err = packet.WriteVarInt(writer, addPlayer.Latency)
			if err != nil {
				return
			}
			if addPlayer.DisplayName == "" {
				err = packet.WriteBool(writer, false)
				if err != nil {
					return
				}
			} else {
				err = packet.WriteBool(writer, true)
				if err != nil {
					return
				}
				err = packet.WriteString(writer, addPlayer.DisplayName)
				if err != nil {
					return
				}
			}
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_GAMEMODE:
			updateGamemode := item.Info.(PacketClientPlayerListUpdateGamemode)
			err = packet.WriteVarInt(writer, updateGamemode.Gamemode)
			if err != nil {
				return
			}
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_LATENCY:
			updateLatency := item.Info.(PacketClientPlayerListUpdateLatency)
			err = packet.WriteVarInt(writer, updateLatency.Latency)
			if err != nil {
				return
			}
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_DISPLAY_NAME:
			updateDisplayName := item.Info.(PacketClientPlayerListUpdateDisplayName)
			if updateDisplayName.DisplayName == "" {
				err = packet.WriteBool(writer, false)
				if err != nil {
					return
				}
			} else {
				err = packet.WriteBool(writer, true)
				if err != nil {
					return
				}
				err = packet.WriteString(writer, updateDisplayName.DisplayName)
				if err != nil {
					return
				}
			}
		case PACKET_CLIENT_PLAYER_LIST_ACTION_REMOVE:
			// no payload
		default:
			err = errors.New(fmt.Sprintf("Encode, PacketClientPlayerList action is not valid: %d", packetClientPlayerList.Action))
		}
	}
	return
}